-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithdrawExperiment.java
More file actions
300 lines (239 loc) · 13.3 KB
/
WithdrawExperiment.java
File metadata and controls
300 lines (239 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package symposium;
import java.io.FileWriter;
import java.io.IOException;
import java.time.YearMonth;
import java.time.temporal.ChronoUnit;
public class WithdrawExperiment {
private static final double INITIAL_BALANCE = 1_000_000;
private static final double MONTHLY_WITHDRAW = 0.04;
private static final double STOCK_ALLOCATION = 0.80;
private static final double FIXED_ALLOCATION = 0.20;
private static final double FIXED_YIELD = 2.0;
private static final double ANNUAL_INCREASE = 0;
private static final int[] YEARS = {10, 15, 20, 25, 30, 35, 40};
private static final double[] ALLOCATION_PERCENTS = {20, 40, 60, 80, 100};
private static final double[] INITIAL_AMOUNTS = {1_000_000, 2_000_000, 3_000_000, 4_000_000, 5_000_000};
private static final double[] YIELDS = {0, 2, 4, 6, 8, 10};
private static final double[] MONTHLY_WITHDRAWS = {0, 2, 4, 6, 8, 10};
private static final double[] INCREASES = {0, 2, 4, 6, 8, 10};
private static final int RUNS = 10;
private static final int YEAR_SPAN = 30;
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("withdraw_results.txt")) {
// Write the headers for each section
writer.write("Variable\tFinal Balance\tTotal Withdraw\tAverage Monthly Withdrawal\n");
// Allocation Experiment
runAllocationExperiment(writer);
// Time Experiment
runTimeExperiment(writer);
// Withdrawal Experiment
runWithdrawalExperiment(writer);
// Increase Experiment
runIncreaseExperiment(writer);
// Initial Balance Experiment
runInitialBalanceExperiment(writer);
// Yield Experiment
runYieldExperiment(writer);
// Glide Experiment
runGlideExperiment(writer);
// Special Time Ranges Experiment
runSpecialExperiment(writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void runAllocationExperiment(FileWriter writer) throws IOException {
writer.write("\nTesting Allocation:\n");
for (double stockPercent : ALLOCATION_PERCENTS) {
double finalBalance = 0;
double totalContribution = 0;
double avgMonthlyContribution = 0;
for (int i = 0; i < RUNS; i++) {
// Run simulator for iteration
WithdrawSimulator sim = new WithdrawSimulator(INITIAL_BALANCE, MONTHLY_WITHDRAW, stockPercent / 100, stockPercent / 100, FIXED_YIELD, ANNUAL_INCREASE, YearMonth.of(1985 + i, 5), YearMonth.of(2015 + i, 5));
sim.runSimulation();
// Tally up sums
finalBalance += sim.getFinalAmount();
totalContribution += sim.getTotalWithdrawal();
}
// Averaging values
finalBalance /= RUNS;
totalContribution /= RUNS;
avgMonthlyContribution = totalContribution / (12 * YEAR_SPAN); // (MONTH * YEAR SPAN)
// Write the variable along with results
writer.write(String.format("%.2f\t%.2f\t%.2f\t%.2f%n", stockPercent, finalBalance, totalContribution, avgMonthlyContribution));
}
}
private static void runTimeExperiment(FileWriter writer) throws IOException {
writer.write("\nTesting Time:\n");
for (int years : YEARS) {
double finalBalance = 0;
double totalContribution = 0;
double avgMonthlyContribution = 0;
int validRuns = 0;
if (years < 40) {
for (int startYear = 1985; startYear + years <= 2024; startYear++) {
// Run simulator for iteration
WithdrawSimulator sim = new WithdrawSimulator(INITIAL_BALANCE, MONTHLY_WITHDRAW, STOCK_ALLOCATION, STOCK_ALLOCATION, FIXED_YIELD, ANNUAL_INCREASE, YearMonth.of(startYear, 5), YearMonth.of(startYear + years, 5));
sim.runSimulation();
// Tally up sums
finalBalance += sim.getFinalAmount();
totalContribution += sim.getTotalWithdrawal();
validRuns++;
}
// Averaging values
finalBalance /= validRuns;
totalContribution /= validRuns;
avgMonthlyContribution = totalContribution / (12 * years); // (MONTH * YEAR SPAN)
} else {
// Define simulation start and end dates
YearMonth startDate = YearMonth.of(1985, 5);
YearMonth endDate = YearMonth.of(2024, 12);
// Calculate the number of months between the start and end dates
int totalMonths = (int) ChronoUnit.MONTHS.between(startDate, endDate);
// Run simulator for iteration
var sim = new WithdrawSimulator(INITIAL_BALANCE, MONTHLY_WITHDRAW, STOCK_ALLOCATION, STOCK_ALLOCATION, FIXED_YIELD, ANNUAL_INCREASE, startDate, endDate);
sim.runSimulation();
// Retrieve the results from the simulation
finalBalance = sim.getFinalAmount();
totalContribution = sim.getTotalWithdrawal();
avgMonthlyContribution = totalContribution / totalMonths; // Use dynamic totalMonths
}
// Write the variable along with results
writer.write(String.format("%d\t%.2f\t%.2f\t%.2f%n", years, finalBalance, totalContribution, avgMonthlyContribution));
}
}
private static void runWithdrawalExperiment(FileWriter writer) throws IOException {
writer.write("\nTesting Withdraw Amount:\n");
for (double withdraws : MONTHLY_WITHDRAWS) {
double finalBalance = 0;
double totalContribution = 0;
double avgMonthlyContribution = 0;
for (int i = 0; i < RUNS; i++) {
// Run simulator for iteration
WithdrawSimulator sim = new WithdrawSimulator(INITIAL_BALANCE, withdraws / 100, STOCK_ALLOCATION, STOCK_ALLOCATION, FIXED_YIELD, ANNUAL_INCREASE, YearMonth.of(1985 + i, 5), YearMonth.of(2015 + i, 5));
sim.runSimulation();
// Tally up sums
finalBalance += sim.getFinalAmount();
totalContribution += sim.getTotalWithdrawal();
}
// Averaging values
finalBalance /= RUNS;
totalContribution /= RUNS;
avgMonthlyContribution = totalContribution / (12 * YEAR_SPAN); // (MONTH * YEAR SPAN)
// Write the variable along with results
writer.write(String.format("%.2f\t%.2f\t%.2f\t%.2f%n", withdraws, finalBalance, totalContribution, avgMonthlyContribution));
}
}
private static void runIncreaseExperiment(FileWriter writer) throws IOException {
writer.write("\nTesting Annual Increase:\n");
for (double increase : INCREASES) {
double finalBalance = 0;
double totalContribution = 0;
double avgMonthlyContribution = 0;
for (int i = 0; i < RUNS; i++) {
// Run simulator for iteration
WithdrawSimulator sim = new WithdrawSimulator(INITIAL_BALANCE, MONTHLY_WITHDRAW, STOCK_ALLOCATION, STOCK_ALLOCATION, FIXED_YIELD, increase, YearMonth.of(1985 + i, 5), YearMonth.of(2015 + i, 5));
sim.runSimulation();
// Tally up sums
finalBalance += sim.getFinalAmount();
totalContribution += sim.getTotalWithdrawal();
}
// Averaging values
finalBalance /= RUNS;
totalContribution /= RUNS;
avgMonthlyContribution = totalContribution / (12 * YEAR_SPAN); // (MONTH * YEAR SPAN)
// Write the variable along with results
writer.write(String.format("%.2f\t%.2f\t%.2f\t%.2f%n", increase, finalBalance, totalContribution, avgMonthlyContribution));
}
}
private static void runInitialBalanceExperiment(FileWriter writer) throws IOException {
writer.write("\nTesting Initial Balance:\n");
for (double initial : INITIAL_AMOUNTS) {
double finalBalance = 0;
double totalContribution = 0;
double avgMonthlyContribution = 0;
for (int i = 0; i < RUNS; i++) {
// Run simulator for iteration
WithdrawSimulator sim = new WithdrawSimulator(initial, MONTHLY_WITHDRAW, STOCK_ALLOCATION, STOCK_ALLOCATION, FIXED_YIELD, ANNUAL_INCREASE, YearMonth.of(1985 + i, 5), YearMonth.of(2015 + i, 5));
sim.runSimulation();
// Tally up sums
finalBalance += sim.getFinalAmount();
totalContribution += sim.getTotalWithdrawal();
}
// Averaging values
finalBalance /= RUNS;
totalContribution /= RUNS;
avgMonthlyContribution = totalContribution / (12 * YEAR_SPAN); // (MONTH * YEAR SPAN)
// Write the variable along with results
writer.write(String.format("%.2f\t%.2f\t%.2f\t%.2f%n", initial, finalBalance, totalContribution, avgMonthlyContribution));
}
}
private static void runYieldExperiment(FileWriter writer) throws IOException {
writer.write("\nTesting Yield:\n");
for (double yield : YIELDS) {
double finalBalance = 0;
double totalContribution = 0;
double avgMonthlyContribution = 0;
for (int i = 0; i < RUNS; i++) {
// Run simulator for iteration
WithdrawSimulator sim = new WithdrawSimulator(INITIAL_BALANCE, MONTHLY_WITHDRAW, STOCK_ALLOCATION, STOCK_ALLOCATION, yield, ANNUAL_INCREASE, YearMonth.of(1985 + i, 5), YearMonth.of(2015 + i, 5));
sim.runSimulation();
// Tally up sums
finalBalance += sim.getFinalAmount();
totalContribution += sim.getTotalWithdrawal();
}
// Averaging values
finalBalance /= RUNS;
totalContribution /= RUNS;
avgMonthlyContribution = totalContribution / (12 * YEAR_SPAN); // (MONTH * YEAR SPAN)
// Write the variable along with results
writer.write(String.format("%.2f\t%.2f\t%.2f\t%.2f%n", yield, finalBalance, totalContribution, avgMonthlyContribution));
}
}
private static void runGlideExperiment(FileWriter writer) throws IOException {
writer.write("\nTesting Glide Path:\n");
for (double eStock : ALLOCATION_PERCENTS) {
double finalBalance = 0;
double totalContribution = 0;
double avgMonthlyContribution = 0;
for (int i = 0; i < RUNS; i++) {
// Run simulator for iteration
WithdrawSimulator sim = new WithdrawSimulator(INITIAL_BALANCE, MONTHLY_WITHDRAW, 0.60, eStock / 100, FIXED_YIELD, ANNUAL_INCREASE, YearMonth.of(1985 + i, 5), YearMonth.of(2015 + i, 5));
sim.runSimulation();
// Tally up sums
finalBalance += sim.getFinalAmount();
totalContribution += sim.getTotalWithdrawal();
}
// Averaging values
finalBalance /= RUNS;
totalContribution /= RUNS;
avgMonthlyContribution = totalContribution / (12 * YEAR_SPAN); // (MONTH * YEAR SPAN)
// Write the variable along with results
writer.write(String.format("%.2f\t%.2f\t%.2f\t%.2f%n", eStock, finalBalance, totalContribution, avgMonthlyContribution));
}
}
private static void runSpecialExperiment(FileWriter writer) throws IOException {
writer.write("\nTesting Special Periods:\n");
// Define start and end dates for special periods
YearMonth[] startDates = {YearMonth.of(2000, 5), YearMonth.of(2000, 5),
YearMonth.of(2007, 5), YearMonth.of(2009, 5)};
YearMonth[] endDates = {YearMonth.of(2012, 5), YearMonth.of(2009, 5),
YearMonth.of(2017, 5), YearMonth.of(2019, 5)};
// Loop over each special period
for (int i = 0; i < startDates.length; i++) {
// Calculate YEAR_SPAN dynamically
int yearSpan = endDates[i].getYear() - startDates[i].getYear();
// Run simulator for iteration
WithdrawSimulator sim = new WithdrawSimulator(INITIAL_BALANCE, MONTHLY_WITHDRAW, STOCK_ALLOCATION, STOCK_ALLOCATION, FIXED_YIELD, ANNUAL_INCREASE, YearMonth.of(1985 + i, 5), YearMonth.of(2015 + i, 5));
sim.runSimulation();
// Retrieve the results from the simulation
double finalBalance = sim.getFinalAmount();
double totalContribution = sim.getTotalWithdrawal();
double avgMonthlyContribution = totalContribution / (12 * yearSpan); // (MONTH * YEAR SPAN)
// Write the results to the file, formatted to two decimal places
writer.write(String.format("%s - %s\t%.2f\t%.2f\t%.2f%n", startDates[i], endDates[i], finalBalance, totalContribution, avgMonthlyContribution));
}
}
}