00001
00002
00003
00004
00005
00006 #include <OMATH.H>
00007
00008
00009 #include <OINFO.H>
00010 #include <OPSCHOOL.H>
00011 #include <OFINANCE.H>
00012 #include <ODEVELOP.H>
00013 #include <OCHANCE.H>
00014
00015 #include "OINVEST.H"
00016
00017
00018
00019
00020
00021
00022
00023
00024 static float endowment_lambda[INPUT_OPTION_COUNT] = { 0, 0.398f, 0.541f };
00025
00026 static float tmp_max = 1;
00027
00028
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 asset_array[AS_LC_STOCK].allocation_percent = 50;
00053 asset_array[AS_SC_STOCK].allocation_percent = 20;
00054 asset_array[AS_BONDS].allocation_percent = 30;
00055
00056 asset_array[AS_LC_STOCK].return_mean = 0.06f;
00057 asset_array[AS_SC_STOCK].return_mean = 0.095f;
00058 asset_array[AS_BONDS].return_mean = 0.025f;
00059
00060 asset_array[AS_LC_STOCK].return_deviation = 0.22f;
00061 asset_array[AS_SC_STOCK].return_deviation = 0.26f;
00062 asset_array[AS_BONDS].return_deviation = 0.18f;
00063
00064
00065
00066 int i, j;
00067
00068 for (i=0; i < ASSET_TYPE_COUNT; i++)
00069 for (j=0; j < ASSET_TYPE_COUNT; j++) {
00070
00071 float multiplier;
00072
00073 if ( i == j)
00074 multiplier = 1;
00075 else if ( ( i == AS_SC_STOCK && j == AS_LC_STOCK ) || ( j == AS_SC_STOCK && i == AS_LC_STOCK ) )
00076 multiplier = 0.9f;
00077 else if ( ( i == AS_SC_STOCK && j == AS_BONDS ) || ( j == AS_SC_STOCK && i == AS_BONDS ) )
00078 multiplier = 0.3f;
00079 else if ( ( i == AS_LC_STOCK && j == AS_BONDS ) || ( j == AS_LC_STOCK && i == AS_BONDS ) )
00080 multiplier = 0.4f;
00081
00082 asset_covariance_array[i][j] = multiplier * asset_array[j].return_deviation * asset_array[i].return_deviation;
00083 }
00084
00085 s_n_p_history[THIS_MONTH] = 100;
00086 endowment_performance_history[THIS_MONTH] = 100;
00087
00088
00089
00090
00091
00092
00093 degree_smoothing_for_endowment_payout = 0;
00094
00095
00096
00097
00098 endowment_ratio = float(100*development_office.last_year.endowment / finance.last_year.asset_array[AC_ENDOWMENT]);
00099
00100 endowment_change_year_to_date = 0;
00101
00102
00103
00104
00105
00106 float realReturn = 0;
00107
00108 for (i=0; i < ASSET_TYPE_COUNT; i++)
00109 realReturn += asset_array[i].allocation_percent * ( asset_array[i].return_mean );
00110
00111 expected_real_return.init(FA_HISTORY_YEAR, realReturn / 12.0f );
00112
00113 update_history(UPDATE_MONTH);
00114
00115
00116 tmp_max = max(1.0f, max(s_n_p_history[THIS_MONTH], endowment_performance_history[THIS_MONTH]));
00117 s_n_p_history[THIS_MONTH] /= tmp_max;
00118 endowment_performance_history[THIS_MONTH] /= tmp_max;
00119
00120 int m = THIS_MONTH;
00121 s_n_p_history[m-1] = s_n_p_history[m-2] = s_n_p_history[THIS_MONTH];
00122 endowment_performance_history[m-1] = endowment_performance_history[m-2] = endowment_performance_history[THIS_MONTH];
00123
00124
00125
00126
00127 calc_equilibrium_payout_rate();
00128
00129
00130
00131 err_when(! finance.last_year.asset_array[AC_ENDOWMENT]);
00132 }
00133
00134
00135
00136
00141
00142 int i,j;
00143
00144
00145 float monthlyEndowmentGrowthRate = finance.inflation_rate_hist.get_cur();
00146 float realReturn = 0;
00147
00148 for (i=0; i < ASSET_TYPE_COUNT; i++)
00149 realReturn += asset_array[i].allocation_percent * ( asset_array[i].return_mean );
00150
00151 monthlyEndowmentGrowthRate += realReturn / 12.0f / 100.0f;
00152
00153
00154 expected_real_return.shift_hist();
00155 expected_real_return.set_cur(realReturn / 12.0f);
00156 expected_real_annual_return_rate = expected_real_return.get_sum();
00157
00158
00159 endowment_growthrate_deviation = 0;
00160 for (i=0; i < ASSET_TYPE_COUNT; i++)
00161 for (j=0; j < ASSET_TYPE_COUNT; j++)
00162 endowment_growthrate_deviation += (asset_array[i].allocation_percent / 100.0f) * (asset_array[j].allocation_percent / 100.0f) * asset_covariance_array[i][j];
00163
00164 endowment_growthrate_deviation = (float) sqrt(endowment_growthrate_deviation);
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 float endowmentGrowthrateDeviationMonthly = 0;
00186 endowmentGrowthrateDeviationMonthly = math.get_random_snd(monthlyEndowmentGrowthRate, endowment_growthrate_deviation/12);
00187
00188
00189 int dir = m.random(2) ? 1:0 ;
00190 if(dir == 1)
00191 endowmentGrowthrateDeviationMonthly = -(endowmentGrowthrateDeviationMonthly);
00192
00193
00194
00195 float endRand = math.get_random_snd(monthlyEndowmentGrowthRate, endowmentGrowthrateDeviationMonthly);
00196
00197
00198
00199 if(chance_event.stock_dir == -1) {
00200 endRand = -float(100+m.random(400)) / 1000.f;
00201 chance_event.stock_dir = 0;
00202 }
00203 else if(chance_event.stock_dir == 1) {
00204 endRand = float(100+m.random(400)) / 1000.f;
00205 chance_event.stock_dir = 0;
00206 }
00207
00208
00209
00210 float oldEndowment = (float) finance.this_year.asset_array[AC_ENDOWMENT];
00211
00212 endowment_change_year_to_date += oldEndowment * endRand;
00213 endowment_performance_history[THIS_MONTH] = float( (1+endRand) * endowment_performance_history[THIS_MONTH-1]);
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 }
00232
00233
00234
00235
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 endowment_spending_history[PROJECTED_VALUE][P_THIS_YEAR] = (int)finance.projected_revenue_array[AC_ENDOWMENT_SPENDING].this_year.total;
00281 endowment_spending_history[PROJECTED_MAX_VALUE][P_THIS_YEAR] = (int)finance.projected_revenue_array[AC_ENDOWMENT_SPENDING].this_year.total;
00282 endowment_spending_history[PROJECTED_MIN_VALUE][P_THIS_YEAR] = (int)finance.projected_revenue_array[AC_ENDOWMENT_SPENDING].this_year.total;
00283
00284
00285
00286 const float annualReturnRate = expected_real_annual_return_rate;
00287 const float annualReturnDeviation = endowment_growthrate_deviation;
00288 const float payoutRate = float(finance.revenue_policy_array[PL_ENDOWMENT_SPENDING_RATE].result_value) / 100;
00289 #if(GAME_VERSION>=200)
00290 float curEndowmentValue = (float) finance.this_year.asset_array[AC_ENDOWMENT]+(float) finance.this_year.asset_array[AC_QUASI];
00291 #else
00292 float curEndowmentValue = (float) finance.this_year.asset_array[AC_ENDOWMENT];
00293 #endif
00294
00295
00296 float lam = endowment_lambda[degree_smoothing_for_endowment_payout];
00297 #if(GAME_VERSION>=200)
00298 lam = 1.0f - lam;
00299 #endif
00300 err_when(degree_smoothing_for_endowment_payout>INPUT_HIGH);
00301
00302 curEndowmentValue *= (1+annualReturnRate/100.0f);
00303 float smoothedEndowmentValue = math.latency_func(lam, curEndowmentValue, smoothed_endowment_value);
00304
00305 endowment_spending_history[PROJECTED_VALUE][P_NEXT_YEAR] = int( smoothedEndowmentValue * payoutRate);
00306 endowment_spending_history[PROJECTED_MAX_VALUE][P_NEXT_YEAR] = int( math.latency_func(lam, curEndowmentValue * (1+annualReturnDeviation), smoothed_endowment_value) * payoutRate );
00307 endowment_spending_history[PROJECTED_MIN_VALUE][P_NEXT_YEAR] = int( math.latency_func(lam, curEndowmentValue * (1-annualReturnDeviation), smoothed_endowment_value) * payoutRate );
00308
00309
00310 int temp1 = int( smoothedEndowmentValue * payoutRate);
00311 int temp2 = (int)finance.revenue_array[AC_ENDOWMENT_SPENDING].total;
00312
00313
00314
00315
00316
00317
00318 curEndowmentValue *= (1+annualReturnRate/100.0f);
00319 smoothedEndowmentValue = math.latency_func(lam, curEndowmentValue, smoothed_endowment_value);
00320
00321 endowment_spending_history[PROJECTED_VALUE][P_2ND_NEXT_YEAR] = int( smoothedEndowmentValue * payoutRate);
00322 endowment_spending_history[PROJECTED_MAX_VALUE][P_2ND_NEXT_YEAR] = int( math.latency_func(lam, curEndowmentValue * (1+annualReturnDeviation), smoothed_endowment_value) * payoutRate );
00323 endowment_spending_history[PROJECTED_MIN_VALUE][P_2ND_NEXT_YEAR] = int( math.latency_func(lam, curEndowmentValue * (1-annualReturnDeviation), smoothed_endowment_value) * payoutRate );
00324
00325
00326
00327 int temp3 = int( smoothedEndowmentValue * payoutRate);
00328 int temp4 = (int)finance.projected_revenue_array[AC_ENDOWMENT_SPENDING].this_year.total;
00329 finance.projected_revenue_array[AC_ENDOWMENT_SPENDING].next_year.total = temp3;
00330 finance.projected_revenue_array[AC_ENDOWMENT_SPENDING].change_next_year.total = finance.calc_change(temp3 , temp4);
00331
00332 }
00333
00334
00335
00336
00339
00340
00341 if(info.game_day == 29 && info.game_month == 8) {
00342 int asdfasdfasd=7;
00343 }
00344
00345 if ( info.game_day == 1 ) {
00346
00347 if ( info.game_month == finance.fiscal_year_start_month )
00348 update_history(UPDATE_YEAR);
00349
00350 update_history(UPDATE_MONTH);
00351 }
00352
00353
00354 if ( info.game_day == player_school.trimester_array[player_school.cur_trimester].start_day
00355 && info.game_month == player_school.trimester_array[player_school.cur_trimester].start_month ) {
00356 update_history(UPDATE_TRIMESTER);
00357 }
00358 }
00359
00360
00361
00362
00367
00368
00369 int i;
00370
00371 switch (updateFlag) {
00372 case UPDATE_MONTH:
00373 shift_history(s_n_p_history, HISTORY_MONTH_COUNT);
00374 shift_history(endowment_performance_history, HISTORY_MONTH_COUNT);
00375 calc_endowment_growth();
00376 update_history_sub();
00377 break;
00378
00379 case UPDATE_TRIMESTER:
00380 break;
00381
00382 case UPDATE_YEAR:
00383 for (i=0; i<PROJECTION_TYPE_COUNT; i++) {
00384 shift_history(endowment_spending_history[i], P_HISTORY_YEAR_COUNT);
00385 }
00386
00387 calc_equilibrium_payout_rate();
00388 endowment_change_year_to_date = 0;
00389 break;
00390
00391 case UPDATE_ALL:
00392
00393
00394
00395
00396
00397 default:
00398 err_here();
00399 break;
00400 }
00401 }
00402
00403
00404
00405
00409
00410 float road, r, i, g, E, B, I;
00411
00412
00413
00414
00415
00416 road = (float) expected_real_annual_return_rate;
00417 r = float(Finance::calc_change(finance.projected_total_expense.this_year.total, finance.total_expense.total)
00418 - finance.inflation_rate);
00419
00420 float thisRev = float(finance.projected_total_revenue.this_year.total - finance.projected_revenue_array[AC_ENDOWMENT_SPENDING].this_year.total);
00421 float lastRev = float(finance.total_revenue.total - finance.revenue_array[AC_ENDOWMENT_SPENDING].total);
00422
00423 i = float(Finance::calc_change(thisRev, lastRev) - finance.inflation_rate);
00424 g = endowment_ratio = math.latency_func(0.6f, endowment_ratio,
00425 float(100*development_office.this_year.endowment / finance.this_year.asset_array[AC_ENDOWMENT]));
00426 E = float(finance.this_year.asset_array[AC_ENDOWMENT]);
00427 B = float(finance.projected_total_expense.this_year.total);
00428 I = thisRev;
00429
00430
00431
00432
00433 equilibrium_payout_rate = (float) sqrt(max(0.0f, math.safe_pow(road+g, 2) - 4 * (r*B-i*I) /E));
00434
00435 equilibrium_payout_rate = (float) min(100.0f, max(0.04f, 0.5f * (road+g + equilibrium_payout_rate)));
00436
00437
00438 }
00439
00440
00441
00442
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 float lam = endowment_lambda[degree_smoothing_for_endowment_payout];
00480
00481 #if(GAME_VERSION>=200)
00482 smoothed_endowment_value = (float)((1.0f-lam) * finance.this_year.asset_array[AC_ENDOWMENT] + lam * smoothed_endowment_value);
00483 smoothed_quasi_endowment_value = (float)((1.0f-lam) * finance.this_year.asset_array[AC_QUASI] + lam * smoothed_quasi_endowment_value);
00484 #else
00485 smoothed_endowment_value = (float)(lam * finance.this_year.asset_array[AC_ENDOWMENT] + (1-lam) * smoothed_endowment_value);
00486 #endif
00487
00488
00489
00490 }
00491
00492
00493
00494
00495 #if(GAME_VERSION>=200)
00496
00497 void Investment::save_initial_data() {
00498
00499
00500 initial_asset_array0_allocation_percent = asset_array[0].allocation_percent;
00501 initial_asset_array1_allocation_percent = asset_array[1].allocation_percent;
00502 initial_asset_array2_allocation_percent = asset_array[2].allocation_percent;
00503 initial_endowment_performance = endowment_performance_history[THIS_MONTH];
00504 }
00505
00506
00507 #endif
00508