-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvestmentExperiment.java
More file actions
351 lines (279 loc) · 15.3 KB
/
InvestmentExperiment.java
File metadata and controls
351 lines (279 loc) · 15.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package symposium;
import java.io.FileWriter;
import java.io.IOException;
import java.time.YearMonth;
import java.time.temporal.ChronoUnit;
public class InvestmentExperiment {
private static final double INITIAL_BALANCE = 0;
private static final double MONTHLY_CONTRIBUTION = 1000;
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 = {100, 80, 60, 40, 20, 0};
private static final double[] INITIAL_AMOUNTS = {0, 10_000, 20_000, 50_000, 100_000};
private static final double[] YIELDS = {0, 2, 4, 6, 8, 10};
private static final double[] MONTHLY_CONTRIBUTIONS = {500, 1000, 1500, 2000, 2500, 3000};
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("investment_results.txt")) {
// Write the headers for each section
//writer.write("Variable\tFinal Balance\tTotal Contribution\tAverage Monthly Contribution\n");
// Allocation Experiment
//runAllocationExperiment(writer);
// Time Experiment
//runTimeExperiment(writer);
// Contribution Experiment
//runContributionExperiment(writer);
// Increase Experiment
//runIncreaseExperiment(writer);
// Initial Balance Experiment
//runInitialBalanceExperiment(writer);
// Yield Experiment
//runYieldExperiment(writer);
// Glide Experiment
//runGlideExperiment(writer);
// Special Time Ranges Experiment
runOptimalAllocationByTime(writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void runOptimalAllocationByTime(FileWriter writer) throws IOException {
writer.write("\nOptimal Allocation by Time Horizon:\n");
writer.write("Years\tBest Stock %\tBest Final Balance\n");
// Loop over time spans: 1 to 40 years
for (int years = 1; years <= 40; years++) {
double bestAvgBalance = 0;
double bestStockPercent = 0;
// Try allocations from 100% stocks to 0% stocks in 10% steps
for (int stockPct = 0; stockPct <= 100; stockPct += 10) {
double avgBalance = 0;
int runs = 0;
// Loop over all valid start years so (start + years <= 2024)
for (int startYear = 1985; startYear + years <= 2024; startYear++) {
YearMonth start = YearMonth.of(startYear, 5);
YearMonth end = YearMonth.of(startYear + years, 5);
// Run simulation
InvestSimulator sim = new InvestSimulator(
INITIAL_BALANCE,
MONTHLY_CONTRIBUTION,
stockPct / 100.0,
stockPct / 100.0, // flat allocation
FIXED_YIELD,
ANNUAL_INCREASE,
start,
end
);
sim.runSimulation();
avgBalance += sim.getFinalAmount();
runs++;
}
avgBalance /= runs;
// Check if this allocation is better than the current best
if (avgBalance > bestAvgBalance) {
bestAvgBalance = avgBalance;
bestStockPercent = stockPct;
}
}
writer.write(String.format("%d\t%.0f\t%.2f%n", years, bestStockPercent, bestAvgBalance));
}
}
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
InvestSimulator sim = new InvestSimulator(INITIAL_BALANCE, MONTHLY_CONTRIBUTION, 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.getTotalContribution();
}
// 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
InvestSimulator sim = new InvestSimulator(INITIAL_BALANCE, MONTHLY_CONTRIBUTION, 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.getTotalContribution();
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 InvestSimulator(INITIAL_BALANCE, MONTHLY_CONTRIBUTION, STOCK_ALLOCATION, STOCK_ALLOCATION, FIXED_YIELD, ANNUAL_INCREASE, startDate, endDate);
sim.runSimulation();
// Retrieve the results from the simulation
finalBalance = sim.getFinalAmount();
totalContribution = sim.getTotalContribution();
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 runContributionExperiment(FileWriter writer) throws IOException {
writer.write("\nTesting Monthly Contribution:\n");
for (double monthly : MONTHLY_CONTRIBUTIONS) {
double finalBalance = 0;
double totalContribution = 0;
double avgMonthlyContribution = 0;
for (int i = 0; i < RUNS; i++) {
// Run simulator for iteration
InvestSimulator sim = new InvestSimulator(INITIAL_BALANCE, monthly, 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.getTotalContribution();
}
// 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", monthly, 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
InvestSimulator sim = new InvestSimulator(INITIAL_BALANCE, MONTHLY_CONTRIBUTION, 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.getTotalContribution();
}
// 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
InvestSimulator sim = new InvestSimulator(initial, MONTHLY_CONTRIBUTION, 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.getTotalContribution();
}
// 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
InvestSimulator sim = new InvestSimulator(INITIAL_BALANCE, MONTHLY_CONTRIBUTION, 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.getTotalContribution();
}
// 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
InvestSimulator sim = new InvestSimulator(INITIAL_BALANCE, MONTHLY_CONTRIBUTION, 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.getTotalContribution();
}
// 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();
// Simulate investment for the current special period
InvestSimulator sim = new InvestSimulator(INITIAL_BALANCE, MONTHLY_CONTRIBUTION, STOCK_ALLOCATION, STOCK_ALLOCATION, FIXED_YIELD, ANNUAL_INCREASE, startDates[i], endDates[i]);
sim.runSimulation();
// Retrieve the results from the simulation
double finalBalance = sim.getFinalAmount();
double totalContribution = sim.getTotalContribution();
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));
}
}
}