eFinancialPlanner  V1.0 (proof of concept)
Personal Financial Planning based on Maslowian Portfolio Theory
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
simulation.class.cpp
Go to the documentation of this file.
1 
10 #include <stdlib.h>
11 
13 {
14 public:
15  void show_simulation ();
16  simulation(int investor);
17  void simulate();
19  void plot_market_evol();
20 
21 protected:
22  int nbrMonths;
23 // vector< vector<float> > market_return; ///< market_return [asset_class] [month]
24 // float* market_return= new float[NBR_ASSET_CLASSES * 1];
25  std::map <int, std::map <int, float> > market_return;
26 
27 private:
29  void simulate_market();
30  void simulate_portfolios();
31 
32 };
33 
38  : investment_problem(investor)
39 {
41 }
42 
43 
50 {
51  solve();
54 }
55 
56 
63 {
64  int ac, m;
65  float mu, sigma;
66 /* float* new_hist = NULL;
67  new_hist = (float*) realloc(market_return, (NBR_ASSET_CLASSES + 1) * (nbrMonths + 1) * sizeof(float));
68  if (new_hist != NULL)
69  {
70  market_return = new_hist;
71  }
72  else
73  {
74  free(market_return);
75 #ifdef DEBUG
76  debug_info = debug_info + "<br/>realloc() failed in simulate_market()";
77 #endif
78  }
79 */
80  srand (time(NULL)); //seed the random number generator
81 
82  for (ac = 1; ac <= NBR_ASSET_CLASSES; ac++)
83  {
84 // market_return[INDEX(ac,0)] = 0;
85 // market_return.push_back(vector<float>());
86  mu = pow(1 + assetClass_mu[ac], 0.0833333) - 1;
87  sigma = sqrt(assetClass_covar[INDEX(ac,ac)]);
88  for (m = 1; m <= nbrMonths; m++)
89  {
90  //market_return[ac].push_back((float)norminv((float)(rand() / RAND_MAX)) * sigma + mu);
91  //mm market_return[INDEX(ac, m)] = (float)(norminv(((float)rand() / (float)RAND_MAX)) * sigma + mu);
92  market_return[ac][m] = (float)(norminv(((float)rand() / (float)RAND_MAX)) * sigma + mu);
93  //cout << "<br> market_return[INDEX("<<ac<<","<<m<<")]=" << market_return[ac][m] ;
94  }
95  }
96 }
97 
104 {
105  int g, m, ac;
106  float V, x, curr_age = age(0);
107  string comma;
108  for (g = 1; g <= get_nbr_goals(); g++)
109  {
110  V = goalZ[g].saving_plan[0] - goalZ[g].means_goal[0];
111  goalZ[g].simulation_string = "[" + to_string(curr_age) + "," + to_string(V) + "]";
112 
113 
114  for (m = 1; m <= min(((goal_type_i2s[goalZ[g].goal_type] == "rainy day savings") ? months2simulate() : goalZ[g].realization_monthNbr), nbrMonths); m++)
115  {
116  //R = w . mu
117  for (ac = 1; ac <= NBR_ASSET_CLASSES; ac++)
118  {
119  //mm V *= exp(market_return[INDEX(ac,m)] * goalZ[g].benchmark[ac]);
120  V *= exp(market_return[ac][m] * goalZ[g].benchmark[ac]);
121  //cout << "<br>g=" <<g<<", m="<<m<<", ac="<<ac<<":V="<<V <<" | | w="<< goalZ[g].benchmark[ac] << " | market_return[INDEX(ac,m)]=" << market_return[ac][m];
122  }
123  V += goalZ[g].saving_plan[m] - goalZ[g].means_goal[m];
124 // cout << "<br>saving_plan[goal" << g << "][" << m << "]=" << to_string(goalZ[g].saving_plan[m])<<"-->V="<<V;
125  x = curr_age + (float)m / 12;
126  goalZ[g].simulation_string = goalZ[g].simulation_string + ", [" + to_string(x) + "," + to_string(V) + "]";
127  goalZ[g].simulation_endvalue = V;
128  }
129  }
130 }
131 
132 
139 {
140  int m, ac;
141  float x;
142  float curr_age = age(0);
143  std::map<int, std::string> evol_str;
144  vector < float > evol_value (NBR_ASSET_CLASSES+1);
145  string comma;
146 
148  for (ac = 1; ac <= NBR_ASSET_CLASSES; ac++)
149  {
150  evol_str[ac] = "";
151  evol_value.at(ac) = 100;
152  }
153 
154 
157  {
158  cout << "<table border=\"1\"><tr><th>Asset Class</th><th>Expected Return</th><th>Estimated Volatility</th></tr>";
159  for (ac = 1; ac <= NBR_ASSET_CLASSES; ac++)
160  {
161  cout << "<tr><td>";
162  cout << assetClass_name[ac] << "</td><td>" << round(assetClass_mu[ac] * 100) << "%</td><td>" << round(sqrt(12 * assetClass_covar[INDEX(ac,ac)]) * 100) << "%</td>";
163  cout << "</tr>";
164  }
165  cout << "</table>" << endl;
166  }
168  for (m = 1; m <= nbrMonths + 1; m++)
169  {
170  comma = (m == 1)? "" : ", ";
171  x = curr_age + (float)m / 12;
172  for (ac= 1; ac <= NBR_ASSET_CLASSES; ac++)
173  {
174 // cout << " market_return[INDEX("<<ac<<","<<m<<")]=" << market_return[INDEX(ac,m)] << " | ev=" << evol_value[ac] << "<BR>";
175  evol_str[ac] = evol_str[ac] + comma + "[" + to_string(x) + ", " + to_string(evol_value[ac]) + "]";
176 //mm evol_value[ac] = evol_value[ac] * exp(market_return[INDEX(ac, m)]);
177  evol_value[ac] = evol_value[ac] * exp(market_return[ac][m]);
178  }
179  }
181  cout << "<script class=\"code\" type=\"text/javascript\">$(document).ready(function(){ ";
182  for (ac= 1; ac <= NBR_ASSET_CLASSES; ac++)
183  {
184  cout << "var l" << ac << " = [" + evol_str[ac] + "]; ";
185  }
186  cout << "var plotME = $.jqplot('chartMEvol', [";
187  for (ac = 1; ac <= NBR_ASSET_CLASSES; ac++)
188  {
189  comma = (ac == 1)? "" : ", ";
190  cout << comma << " l" << ac;
191  }
192  cout << "], {";
193  cout << "seriesColors:" << hh.assetClassColors << ",";
194  cout << "seriesDefaults: {rendererOptions: { smooth: false }},";
195  cout << "legend: {show: true, location: 'e', placement: 'outside', marginRight: \"600px\",";
196  cout << "labels:[";
197  for (ac= 1; ac <= NBR_ASSET_CLASSES; ac++)
198  {
199  comma = (ac == 1)? "" : ", ";
200  cout << comma << "'" << assetClass_name[ac] << "'";
201  }
202  cout << "]},";
203  cout << "series:[";
204  for (ac= 1; ac <= NBR_ASSET_CLASSES; ac++)
205  {
206  comma = (ac == 1)? "" : ", ";
207  cout << comma << "{showMarker: false}" ;
208  }
209  cout << "],";
210 
211  cout << "axesDefaults: {labelRenderer: $.jqplot.CanvasAxisLabelRenderer},";
212  cout << "axes: {xaxis: {label: \"age\" }, yaxis: {label: \"market evolution\"}}"; // , pad: 0
213  cout << " } ); }); </script>";
214 }
215 
216 
224 {
225  int n;
226  int defaultValue = 12;
228  vector<FormEntry>::iterator nYears = cgi.getElement("nYears");
229  if(nYears != cgi.getElements().end())
230  {
231  n = atoi(cgi("nYears").c_str()) * 12;
232  }
233  else
234  {
235  n = 0;
236  }
237  vector<FormEntry>::iterator nMonths = cgi.getElement("nMonths");
238  if(nMonths != cgi.getElements().end())
239  {
240  n = n + atoi(cgi("nMonths").c_str());
241  }
242  if ((n >= MIN_MNTHS_2_SIMULATE) && (n <= MAX_MNTHS_2_SIMULATE))
243  {
244  nbrMonths = n;
245  }
246  else
247  {
248  nbrMonths = defaultValue;
249  error_message = error_message + "<li>Please enter a number of months between " + to_string(MIN_MNTHS_2_SIMULATE) + " and " + to_string(MAX_MNTHS_2_SIMULATE) + " (= " + to_string((int)(MAX_MNTHS_2_SIMULATE/12)) + " years)</li>";
250  }
251 }
252 
253 
260 {
261  return nbrMonths;
262 }
void plot_market_evol()
const bool simul_show_table
true if we show the table of E[R] and vol in the simulation-overview
simulation(int investor)
type norminv(type p)
Definition: normdist.h:73
int get_nbrMonths2simulate()
returns the number of months to simulate as in "37"
int nbrMonths
the number of months to simulate, per goal we simulate max(realization_monthNbr, nbrMonths) ...
#define MIN_MNTHS_2_SIMULATE
the maximum number of months that we allow to visualize a simulation
Definition: efp.cpp:93
float age(int Mnbr=0)
unordered_map< int, string > goal_type_i2s
Definition: efp.cpp:168
#define MAX_MNTHS_2_SIMULATE
the maximum number of months that we allow to visualize a simulation
Definition: efp.cpp:92
#define NBR_ASSET_CLASSES
the nunmber of asset classes (not dynamically updated!)
Definition: efp.cpp:97
string error_message
global message variables
Definition: efp.cpp:132
float * assetClass_covar
void simulate_market()
sets nbrMonths
void solve()
allocates means (with a benchmark) to goals
html_helper hh
own c++ classes
Definition: efp.cpp:180
void simulate_portfolios()
float assetClass_mu[(NBR_ASSET_CLASSES+1)]
note: index 0 not used
Cgicc cgi
Definition: efp.cpp:129
std::map< int, std::map< int, float > > market_return
void set_nbrMonths2simulate_from_env()
std::map< int, string > assetClass_name
note: index 0 not used
const string assetClassColors
void show_simulation()
#define INDEX(x, y)
this macro allows to address int cell = array[INDEX(2,3)]; in stead of array[2+NBR_ASSET_CLASSES*3]; ...
Definition: efp.cpp:100
config oConfig
mysql connection mysql connector from dev.mysql.com
Definition: efp.cpp:149
std::map< int, aGOAL > goalZ
map containing all goals
int months2simulate()