1 | |
---|
2 | /*! |
---|
3 | \file |
---|
4 | \brief Robust |
---|
5 | \author Vasek Smidl |
---|
6 | |
---|
7 | */ |
---|
8 | |
---|
9 | #include "estim/arx.h" |
---|
10 | #include "robustlib.h" |
---|
11 | #include <vector> |
---|
12 | #include <iostream> |
---|
13 | #include <fstream> |
---|
14 | #include <itpp/itsignal.h> |
---|
15 | #include "windows.h" |
---|
16 | #include "ddeml.h" |
---|
17 | #include "stdio.h" |
---|
18 | |
---|
19 | //#include "DDEClient.h" |
---|
20 | //#include <conio.h> |
---|
21 | |
---|
22 | |
---|
23 | using namespace itpp; |
---|
24 | using namespace bdm; |
---|
25 | |
---|
26 | //const int emlig_size = 2; |
---|
27 | //const int utility_constant = 5; |
---|
28 | |
---|
29 | const int max_model_order = 2; |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | HDDEDATA CALLBACK DdeCallback( |
---|
34 | UINT uType, // Transaction type. |
---|
35 | UINT uFmt, // Clipboard data format. |
---|
36 | HCONV hconv, // Handle to the conversation. |
---|
37 | HSZ hsz1, // Handle to a string. |
---|
38 | HSZ hsz2, // Handle to a string. |
---|
39 | HDDEDATA hdata, // Handle to a global memory object. |
---|
40 | DWORD dwData1, // Transaction-specific data. |
---|
41 | DWORD dwData2) // Transaction-specific data. |
---|
42 | { |
---|
43 | return 0; |
---|
44 | } |
---|
45 | |
---|
46 | void DDERequest(DWORD idInst, HCONV hConv, char* szItem) |
---|
47 | { |
---|
48 | HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0); |
---|
49 | HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT, |
---|
50 | XTYP_REQUEST,TIMEOUT_ASYNC , NULL); |
---|
51 | if (hData==NULL) |
---|
52 | { |
---|
53 | printf("Request failed: %s\n", szItem); |
---|
54 | } |
---|
55 | |
---|
56 | if (hData==0) |
---|
57 | { |
---|
58 | printf("Request failed: %s\n", szItem); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | class model |
---|
63 | { |
---|
64 | |
---|
65 | |
---|
66 | public: |
---|
67 | list<pair<int,int>> ar_components; |
---|
68 | |
---|
69 | // Best thing would be to inherit the two models from a single souce, this is planned, but now structurally |
---|
70 | // problematic. |
---|
71 | RARX* my_rarx; |
---|
72 | ARXwin* my_arx; |
---|
73 | |
---|
74 | bool has_constant; |
---|
75 | int window_size; |
---|
76 | int predicted_channel; |
---|
77 | mat* data_matrix; |
---|
78 | |
---|
79 | model(list<pair<int,int>> ar_components, |
---|
80 | bool robust, |
---|
81 | bool has_constant, |
---|
82 | int window_size, |
---|
83 | int predicted_channel, |
---|
84 | mat* data_matrix) |
---|
85 | { |
---|
86 | this->ar_components.insert(this->ar_components.begin(),ar_components.begin(),ar_components.end()); |
---|
87 | this->has_constant = has_constant; |
---|
88 | this->window_size = window_size; |
---|
89 | this->predicted_channel = predicted_channel; |
---|
90 | this->data_matrix = data_matrix; |
---|
91 | |
---|
92 | if(robust) |
---|
93 | { |
---|
94 | if(has_constant) |
---|
95 | { |
---|
96 | my_rarx = new RARX(ar_components.size()+1,window_size,true); |
---|
97 | my_arx = NULL; |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | my_rarx = new RARX(ar_components.size(),window_size,false); |
---|
102 | my_arx = NULL; |
---|
103 | } |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | my_rarx = NULL; |
---|
108 | my_arx = new ARXwin(); |
---|
109 | mat V0; |
---|
110 | |
---|
111 | if(has_constant) |
---|
112 | { |
---|
113 | V0 = 0.01 * eye(ar_components.size()+2); |
---|
114 | V0(0,0) = 1; |
---|
115 | my_arx->set_constant(true); |
---|
116 | |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | |
---|
121 | V0 = 0.01 * eye(ar_components.size()+1); |
---|
122 | V0(0,0) = 1; |
---|
123 | my_arx->set_constant(false); |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | my_arx->set_statistics(1, V0); |
---|
128 | my_arx->set_parameters(window_size); |
---|
129 | my_arx->validate(); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | void data_update(int time) |
---|
134 | { |
---|
135 | vec data_vector; |
---|
136 | for(list<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++) |
---|
137 | { |
---|
138 | data_vector.ins(data_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second)); |
---|
139 | } |
---|
140 | |
---|
141 | if(my_rarx!=NULL) |
---|
142 | { |
---|
143 | data_vector.ins(0,(*data_matrix).get(predicted_channel,time)); |
---|
144 | my_rarx->bayes(data_vector); |
---|
145 | } |
---|
146 | else |
---|
147 | { |
---|
148 | vec pred_vec; |
---|
149 | pred_vec.ins(0,(*data_matrix).get(predicted_channel,time)); |
---|
150 | my_arx->bayes(pred_vec,data_vector); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | static list<list<pair<int,int>>> possible_models_recurse(int max_order,int number_of_channels) |
---|
155 | { |
---|
156 | list<list<pair<int,int>>> created_model_types; |
---|
157 | |
---|
158 | if(max_order == 1) |
---|
159 | { |
---|
160 | for(int channel = 0;channel<number_of_channels;channel++) |
---|
161 | { |
---|
162 | list<pair<int,int>> returned_type; |
---|
163 | returned_type.push_back(pair<int,int>(channel,1)); |
---|
164 | created_model_types.push_back(returned_type); |
---|
165 | } |
---|
166 | |
---|
167 | return created_model_types; |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | created_model_types = possible_models_recurse(max_order-1,number_of_channels); |
---|
172 | list<list<pair<int,int>>> returned_types; |
---|
173 | |
---|
174 | for(list<list<pair<int,int>>>::iterator model_ref = created_model_types.begin();model_ref!=created_model_types.end();model_ref++) |
---|
175 | { |
---|
176 | |
---|
177 | for(int order = 1; order<=max_order; order++) |
---|
178 | { |
---|
179 | for(int channel = 0;channel<number_of_channels;channel++) |
---|
180 | { |
---|
181 | list<pair<int,int>> returned_type; |
---|
182 | pair<int,int> new_pair = pair<int,int>(channel,order); |
---|
183 | if(find((*model_ref).begin(),(*model_ref).end(),new_pair)==(*model_ref).end()) |
---|
184 | { |
---|
185 | returned_type.insert(returned_type.begin(),(*model_ref).begin(),(*model_ref).end()); |
---|
186 | returned_type.push_back(new_pair); |
---|
187 | returned_types.push_back(returned_type); |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | created_model_types.insert(created_model_types.end(),returned_types.begin(),returned_types.end()); |
---|
194 | |
---|
195 | return created_model_types; |
---|
196 | } |
---|
197 | } |
---|
198 | }; |
---|
199 | |
---|
200 | |
---|
201 | |
---|
202 | |
---|
203 | int main ( int argc, char* argv[] ) { |
---|
204 | |
---|
205 | itpp::Laplace_RNG LapRNG = Laplace_RNG(); |
---|
206 | |
---|
207 | /* |
---|
208 | char szApp[] = "MT4"; |
---|
209 | char szTopic[] = "ASK"; |
---|
210 | char szItem1[] = "EURUSD"; |
---|
211 | |
---|
212 | //DDE Initialization |
---|
213 | DWORD idInst=0; |
---|
214 | UINT iReturn; |
---|
215 | iReturn = DdeInitialize(&idInst, (PFNCALLBACK)DdeCallback, |
---|
216 | APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0 ); |
---|
217 | if (iReturn!=DMLERR_NO_ERROR) |
---|
218 | { |
---|
219 | printf("DDE Initialization Failed: 0x%04x\n", iReturn); |
---|
220 | Sleep(1500); |
---|
221 | return 0; |
---|
222 | } |
---|
223 | |
---|
224 | /* |
---|
225 | //Start DDE Server and wait for it to become idle. |
---|
226 | HINSTANCE hRet = ShellExecute(0, "open", szTopic, 0, 0, SW_SHOWNORMAL); |
---|
227 | if ((int)hRet < 33) |
---|
228 | { |
---|
229 | printf("Unable to Start DDE Server: 0x%04x\n", hRet); |
---|
230 | Sleep(1500); DdeUninitialize(idInst); |
---|
231 | return 0; |
---|
232 | } |
---|
233 | Sleep(1000); |
---|
234 | */ |
---|
235 | |
---|
236 | /* |
---|
237 | //DDE Connect to Server using given AppName and topic. |
---|
238 | HSZ hszApp, hszTopic; |
---|
239 | HCONV hConv; |
---|
240 | hszApp = DdeCreateStringHandle(idInst, szApp, 0); |
---|
241 | hszTopic = DdeCreateStringHandle(idInst, szTopic, 0); |
---|
242 | hConv = DdeConnect(idInst, hszApp, hszTopic, NULL); |
---|
243 | DdeFreeStringHandle(idInst, hszApp); |
---|
244 | DdeFreeStringHandle(idInst, hszTopic); |
---|
245 | if (hConv == NULL) |
---|
246 | { |
---|
247 | printf("DDE Connection Failed.\n"); |
---|
248 | Sleep(1500); DdeUninitialize(idInst); |
---|
249 | return 0; |
---|
250 | } |
---|
251 | |
---|
252 | //Execute commands/requests specific to the DDE Server. |
---|
253 | |
---|
254 | DDERequest(idInst, hConv, szItem1); |
---|
255 | |
---|
256 | //DDE Disconnect and Uninitialize. |
---|
257 | //DdeDisconnect(hConv); |
---|
258 | //DdeUninitialize(idInst); |
---|
259 | |
---|
260 | Sleep(300000); |
---|
261 | Sleep(3000); |
---|
262 | */ |
---|
263 | |
---|
264 | /* |
---|
265 | // EXPERIMENT: 100 AR model generated time series of length of 30 from y_t=0.95*y_(t-1)+0.05*y_(t-2)+0.2*e_t, |
---|
266 | // where e_t is normally, student(4) and cauchy distributed are tested using robust AR model, to obtain the |
---|
267 | // variance of location parameter estimators and compare it to the classical setup. |
---|
268 | vector<vector<vector<string>>> string_lists; |
---|
269 | string_lists.push_back(vector<vector<string>>()); |
---|
270 | string_lists.push_back(vector<vector<string>>()); |
---|
271 | string_lists.push_back(vector<vector<string>>()); |
---|
272 | |
---|
273 | char* file_strings[3] = {"c:\\ar_normal.txt", "c:\\ar_student.txt", "c:\\ar_cauchy.txt"}; |
---|
274 | |
---|
275 | |
---|
276 | for(int i = 0;i<3;i++) |
---|
277 | { |
---|
278 | ifstream myfile(file_strings[i]); |
---|
279 | if (myfile.is_open()) |
---|
280 | { |
---|
281 | while ( myfile.good() ) |
---|
282 | { |
---|
283 | string line; |
---|
284 | getline(myfile,line); |
---|
285 | |
---|
286 | vector<string> parsed_line; |
---|
287 | while(line.find(',') != string::npos) |
---|
288 | { |
---|
289 | int loc = line.find(','); |
---|
290 | parsed_line.push_back(line.substr(0,loc)); |
---|
291 | line.erase(0,loc+1); |
---|
292 | } |
---|
293 | |
---|
294 | string_lists[i].push_back(parsed_line); |
---|
295 | } |
---|
296 | myfile.close(); |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | for(int j = 0;j<string_lists.size();j++) |
---|
301 | { |
---|
302 | |
---|
303 | for(int i = 0;i<string_lists[j].size()-1;i++) |
---|
304 | { |
---|
305 | vector<vec> conditions; |
---|
306 | //emlig* emliga = new emlig(2); |
---|
307 | RARX* my_rarx = new RARX(2,30); |
---|
308 | |
---|
309 | for(int k = 1;k<string_lists[j][i].size();k++) |
---|
310 | { |
---|
311 | vec condition; |
---|
312 | //condition.ins(0,1); |
---|
313 | condition.ins(0,string_lists[j][i][k]); |
---|
314 | conditions.push_back(condition); |
---|
315 | |
---|
316 | //cout << "orig:" << condition << endl; |
---|
317 | |
---|
318 | if(conditions.size()>1) |
---|
319 | { |
---|
320 | conditions[k-2].ins(0,string_lists[j][i][k]); |
---|
321 | |
---|
322 | } |
---|
323 | |
---|
324 | if(conditions.size()>2) |
---|
325 | { |
---|
326 | conditions[k-3].ins(0,string_lists[j][i][k]); |
---|
327 | |
---|
328 | //cout << "modi:" << conditions[k-3] << endl; |
---|
329 | |
---|
330 | my_rarx->bayes(conditions[k-3]); |
---|
331 | |
---|
332 | |
---|
333 | //if(k>5) |
---|
334 | //{ |
---|
335 | // cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
---|
336 | //} |
---|
337 | |
---|
338 | } |
---|
339 | |
---|
340 | } |
---|
341 | |
---|
342 | //emliga->step_me(0); |
---|
343 | /* |
---|
344 | ofstream myfile; |
---|
345 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
346 | myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";"; |
---|
347 | myfile.close(); |
---|
348 | |
---|
349 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
350 | myfile << emliga->minimal_vertex->get_coordinates()[1] << ";"; |
---|
351 | myfile.close(); |
---|
352 | |
---|
353 | |
---|
354 | cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
---|
355 | cout << "Step: " << i << endl; |
---|
356 | } |
---|
357 | |
---|
358 | cout << "One experiment finished." << endl; |
---|
359 | |
---|
360 | ofstream myfile; |
---|
361 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
362 | myfile << endl; |
---|
363 | myfile.close(); |
---|
364 | |
---|
365 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
366 | myfile << endl; |
---|
367 | myfile.close(); |
---|
368 | }*/ |
---|
369 | |
---|
370 | |
---|
371 | |
---|
372 | // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from |
---|
373 | // y_t=0.95*y_(t-1)+0.05*y_(t-2)+0.2*e_t, where e_t is normally, student(4) and cauchy distributed. It |
---|
374 | // can be compared to the classical setup. |
---|
375 | |
---|
376 | |
---|
377 | vector<vector<string>> strings; |
---|
378 | |
---|
379 | char* file_string = "c:\\data"; |
---|
380 | |
---|
381 | char dfstring[80]; |
---|
382 | strcpy(dfstring,file_string); |
---|
383 | strcat(dfstring,".txt"); |
---|
384 | |
---|
385 | |
---|
386 | mat data_matrix; |
---|
387 | ifstream myfile(dfstring); |
---|
388 | if (myfile.is_open()) |
---|
389 | { |
---|
390 | string line; |
---|
391 | while(getline(myfile,line)) |
---|
392 | { |
---|
393 | vec data_vector; |
---|
394 | while(line.find(',') != string::npos) |
---|
395 | { |
---|
396 | int loc2 = line.find('\n'); |
---|
397 | int loc = line.find(','); |
---|
398 | data_vector.ins(data_vector.size(),atof(line.substr(0,loc).c_str())); |
---|
399 | line.erase(0,loc+1); |
---|
400 | } |
---|
401 | |
---|
402 | data_matrix.ins_row(data_matrix.rows(),data_vector); |
---|
403 | } |
---|
404 | |
---|
405 | myfile.close(); |
---|
406 | } |
---|
407 | else |
---|
408 | { |
---|
409 | cout << "Can't open data file!" << endl; |
---|
410 | } |
---|
411 | |
---|
412 | list<list<pair<int,int>>> model_types = model::possible_models_recurse(max_model_order,data_matrix.rows()); |
---|
413 | |
---|
414 | list<model*> models; |
---|
415 | for(list<list<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++) |
---|
416 | { |
---|
417 | models.push_back(new model((*model_type),true,false,30,0,&data_matrix)); |
---|
418 | models.push_back(new model((*model_type),false,false,30,0,&data_matrix)); |
---|
419 | } |
---|
420 | |
---|
421 | mat result_lognc; |
---|
422 | |
---|
423 | for(int time = max_model_order;20;time++) //time<data_matrix.cols() |
---|
424 | { |
---|
425 | vec cur_res_lognc; |
---|
426 | |
---|
427 | for(list<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) |
---|
428 | { |
---|
429 | (*model_ref)->data_update(time); |
---|
430 | if((*model_ref)->my_rarx!=NULL) |
---|
431 | { |
---|
432 | cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_rarx->posterior->log_nc); |
---|
433 | } |
---|
434 | else |
---|
435 | { |
---|
436 | cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_arx->posterior().lognc()); |
---|
437 | } |
---|
438 | } |
---|
439 | |
---|
440 | result_lognc.ins_col(result_lognc.cols(),cur_res_lognc); |
---|
441 | |
---|
442 | cout << "Updated." << endl; |
---|
443 | |
---|
444 | /* |
---|
445 | vector<vec> conditions; |
---|
446 | //emlig* emliga = new emlig(2); |
---|
447 | RARX* my_rarx = new RARX(2,10,false); |
---|
448 | |
---|
449 | |
---|
450 | mat V0 = 0.0001 * eye ( 3 ); |
---|
451 | ARX* my_arx = new ARX(0.85); |
---|
452 | my_arx->set_statistics ( 1, V0 ); //nu is default (set to have finite moments) |
---|
453 | my_arx->set_constant ( false ); |
---|
454 | my_arx->validate(); |
---|
455 | |
---|
456 | |
---|
457 | for(int k = 1;k<strings[j].size();k++) |
---|
458 | { |
---|
459 | vec condition; |
---|
460 | //condition.ins(0,1); |
---|
461 | condition.ins(0,strings[j][k]); |
---|
462 | conditions.push_back(condition); |
---|
463 | |
---|
464 | //cout << "orig:" << condition << endl; |
---|
465 | |
---|
466 | if(conditions.size()>1) |
---|
467 | { |
---|
468 | conditions[k-2].ins(0,strings[j][k]); |
---|
469 | |
---|
470 | } |
---|
471 | |
---|
472 | if(conditions.size()>2) |
---|
473 | { |
---|
474 | conditions[k-3].ins(0,strings[j][k]); |
---|
475 | |
---|
476 | // cout << "Condition:" << conditions[k-3] << endl; |
---|
477 | |
---|
478 | my_rarx->bayes(conditions[k-3]); |
---|
479 | //my_rarx->posterior->step_me(1); |
---|
480 | |
---|
481 | vec cond_vec; |
---|
482 | cond_vec.ins(0,conditions[k-3][0]); |
---|
483 | |
---|
484 | my_arx->bayes(cond_vec,conditions[k-3].right(2)); |
---|
485 | |
---|
486 | /* |
---|
487 | if(k>8) |
---|
488 | { |
---|
489 | //my_rarx->posterior->step_me(0); |
---|
490 | |
---|
491 | //mat samples = my_rarx->posterior->sample_mat(10); |
---|
492 | |
---|
493 | pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(1000); |
---|
494 | |
---|
495 | //cout << imp_samples.first << endl; |
---|
496 | |
---|
497 | vec sample_prediction; |
---|
498 | vec averaged_params = zeros(imp_samples.second.rows()); |
---|
499 | for(int t = 0;t<imp_samples.first.size();t++) |
---|
500 | { |
---|
501 | vec lap_sample = conditions[k-3].left(2); |
---|
502 | //lap_sample.ins(lap_sample.size(),1.0); |
---|
503 | |
---|
504 | lap_sample.ins(0,LapRNG()); |
---|
505 | |
---|
506 | sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t)); |
---|
507 | |
---|
508 | averaged_params += imp_samples.first[t]*imp_samples.second.get_col(t); |
---|
509 | } |
---|
510 | |
---|
511 | averaged_params = averaged_params*(1/(imp_samples.first*ones(imp_samples.first.size()))); |
---|
512 | |
---|
513 | // cout << "Averaged estimated parameters: " << averaged_params << endl; |
---|
514 | |
---|
515 | vec sample_pow = sample_prediction; |
---|
516 | |
---|
517 | // cout << sample_prediction << endl; |
---|
518 | vec poly_coefs; |
---|
519 | double prediction; |
---|
520 | bool stop_iteration = false; |
---|
521 | int en = 1; |
---|
522 | do |
---|
523 | { |
---|
524 | double poly_coef = imp_samples.first*sample_pow/(imp_samples.first*ones(imp_samples.first.size())); |
---|
525 | |
---|
526 | if(en==1) |
---|
527 | { |
---|
528 | prediction = poly_coef; |
---|
529 | } |
---|
530 | |
---|
531 | poly_coef = poly_coef*en*fact(utility_constant-2+en)/fact(utility_constant-2); |
---|
532 | |
---|
533 | if(abs(poly_coef)>numeric_limits<double>::epsilon()) |
---|
534 | { |
---|
535 | sample_pow = elem_mult(sample_pow,sample_prediction); |
---|
536 | poly_coefs.ins(0,pow(-1.0,en+1)*poly_coef); |
---|
537 | } |
---|
538 | else |
---|
539 | { |
---|
540 | stop_iteration = true; |
---|
541 | } |
---|
542 | |
---|
543 | en++; |
---|
544 | |
---|
545 | if(en>20) |
---|
546 | { |
---|
547 | stop_iteration = true; |
---|
548 | } |
---|
549 | } |
---|
550 | while(!stop_iteration); |
---|
551 | |
---|
552 | /* |
---|
553 | ofstream myfile_coef; |
---|
554 | |
---|
555 | myfile_coef.open("c:\\coefs.txt",ios::app); |
---|
556 | |
---|
557 | for(int t = 0;t<poly_coefs.size();t++) |
---|
558 | { |
---|
559 | myfile_coef << poly_coefs[t] << ","; |
---|
560 | } |
---|
561 | |
---|
562 | myfile_coef << endl; |
---|
563 | myfile_coef.close(); |
---|
564 | */ |
---|
565 | |
---|
566 | //cout << "Coefficients: " << poly_coefs << endl; |
---|
567 | |
---|
568 | /* |
---|
569 | vec bas_coef = vec("1.0 2.0 -8.0"); |
---|
570 | cout << "Coefs: " << bas_coef << endl; |
---|
571 | cvec actions2 = roots(bas_coef); |
---|
572 | cout << "Roots: " << actions2 << endl; |
---|
573 | */ |
---|
574 | |
---|
575 | /* |
---|
576 | |
---|
577 | cvec actions = roots(poly_coefs); |
---|
578 | |
---|
579 | |
---|
580 | bool is_max = false; |
---|
581 | for(int t = 0;t<actions.size();t++) |
---|
582 | { |
---|
583 | if(actions[t].imag() == 0) |
---|
584 | { |
---|
585 | double second_derivative = 0; |
---|
586 | for(int q = 1;q<poly_coefs.size();q++) |
---|
587 | { |
---|
588 | second_derivative+=poly_coefs[q]*pow(actions[t].real(),q-1)*q; |
---|
589 | } |
---|
590 | |
---|
591 | if(second_derivative<0) |
---|
592 | { |
---|
593 | cout << "Action:" << actions[t].real() << endl; |
---|
594 | |
---|
595 | is_max = true; |
---|
596 | } |
---|
597 | } |
---|
598 | } |
---|
599 | |
---|
600 | if(!is_max) |
---|
601 | { |
---|
602 | cout << "No maximum." << endl; |
---|
603 | } |
---|
604 | |
---|
605 | // cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl; |
---|
606 | |
---|
607 | /* |
---|
608 | double prediction = 0; |
---|
609 | for(int s = 1;s<samples.rows();s++) |
---|
610 | { |
---|
611 | |
---|
612 | double avg_parameter = imp_samples.get_row(s)*ones(samples.cols())/samples.cols(); |
---|
613 | |
---|
614 | prediction += avg_parameter*conditions[k-3][s-1]; |
---|
615 | |
---|
616 | |
---|
617 | |
---|
618 | /* |
---|
619 | ofstream myfile; |
---|
620 | char fstring[80]; |
---|
621 | strcpy(fstring,file_strings[j]); |
---|
622 | |
---|
623 | char es[5]; |
---|
624 | strcat(fstring,itoa(s,es,10)); |
---|
625 | |
---|
626 | strcat(fstring,"_res.txt"); |
---|
627 | |
---|
628 | |
---|
629 | myfile.open(fstring,ios::app); |
---|
630 | |
---|
631 | //myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; |
---|
632 | myfile << avg_parameter; |
---|
633 | |
---|
634 | if(k!=strings[j].size()-1) |
---|
635 | { |
---|
636 | myfile << ","; |
---|
637 | } |
---|
638 | else |
---|
639 | { |
---|
640 | myfile << endl; |
---|
641 | } |
---|
642 | myfile.close(); |
---|
643 | */ |
---|
644 | |
---|
645 | |
---|
646 | //} |
---|
647 | |
---|
648 | // cout << "Prediction: "<< prediction << endl; |
---|
649 | /* |
---|
650 | enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2)); |
---|
651 | double prediction2 = pred_mat->mean()[0]; |
---|
652 | */ |
---|
653 | |
---|
654 | ofstream myfile; |
---|
655 | char fstring[80]; |
---|
656 | strcpy(fstring,file_string); |
---|
657 | |
---|
658 | strcat(fstring,"lognc.txt"); |
---|
659 | |
---|
660 | myfile.open(fstring,ios::app); |
---|
661 | |
---|
662 | // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; |
---|
663 | |
---|
664 | for(int i = 0;i<cur_res_lognc.size();i++) |
---|
665 | { |
---|
666 | myfile << cur_res_lognc[i] << ','; |
---|
667 | } |
---|
668 | |
---|
669 | myfile << endl; |
---|
670 | |
---|
671 | myfile.close(); |
---|
672 | |
---|
673 | /* |
---|
674 | myfile.open(f2string,ios::app); |
---|
675 | myfile << prediction2; |
---|
676 | |
---|
677 | if(k!=strings[j].size()-1) |
---|
678 | { |
---|
679 | myfile << ","; |
---|
680 | } |
---|
681 | else |
---|
682 | { |
---|
683 | myfile << endl; |
---|
684 | } |
---|
685 | myfile.close(); |
---|
686 | //*//* |
---|
687 | |
---|
688 | } |
---|
689 | } */ |
---|
690 | |
---|
691 | //emliga->step_me(0); |
---|
692 | /* |
---|
693 | ofstream myfile; |
---|
694 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
695 | myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";"; |
---|
696 | myfile.close(); |
---|
697 | |
---|
698 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
699 | myfile << emliga->minimal_vertex->get_coordinates()[1] << ";"; |
---|
700 | myfile.close(); |
---|
701 | |
---|
702 | |
---|
703 | cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
---|
704 | cout << "Step: " << i << endl;*/ |
---|
705 | //} |
---|
706 | |
---|
707 | |
---|
708 | } |
---|
709 | |
---|
710 | |
---|
711 | // EXPERIMENT: One step ahead price prediction. Comparison of classical and robust model using optimal trading |
---|
712 | // with maximization of logarithm of one-step ahead wealth. |
---|
713 | |
---|
714 | |
---|
715 | |
---|
716 | /* |
---|
717 | cout << "One experiment finished." << endl; |
---|
718 | |
---|
719 | ofstream myfile; |
---|
720 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
721 | myfile << endl; |
---|
722 | myfile.close(); |
---|
723 | |
---|
724 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
725 | myfile << endl; |
---|
726 | myfile.close();*/ |
---|
727 | |
---|
728 | |
---|
729 | //emlig* emlig1 = new emlig(emlig_size); |
---|
730 | |
---|
731 | //emlig1->step_me(0); |
---|
732 | //emlig* emlig2 = new emlig(emlig_size); |
---|
733 | |
---|
734 | /* |
---|
735 | emlig1->set_correction_factors(4); |
---|
736 | |
---|
737 | for(int j = 0;j<emlig1->correction_factors.size();j++) |
---|
738 | { |
---|
739 | for(set<my_ivec>::iterator vec_ref = emlig1->correction_factors[j].begin();vec_ref!=emlig1->correction_factors[j].end();vec_ref++) |
---|
740 | { |
---|
741 | cout << j << " "; |
---|
742 | |
---|
743 | for(int i=0;i<(*vec_ref).size();i++) |
---|
744 | { |
---|
745 | cout << (*vec_ref)[i]; |
---|
746 | } |
---|
747 | |
---|
748 | cout << endl; |
---|
749 | } |
---|
750 | }*/ |
---|
751 | |
---|
752 | /* |
---|
753 | vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5"; |
---|
754 | |
---|
755 | emlig1->add_condition(condition5); |
---|
756 | //emlig1->step_me(0); |
---|
757 | |
---|
758 | |
---|
759 | vec condition1a = "-1.0 1.02 0.5"; |
---|
760 | //vec condition1b = "1.0 1.0 1.01"; |
---|
761 | emlig1->add_condition(condition1a); |
---|
762 | //emlig2->add_condition(condition1b); |
---|
763 | |
---|
764 | vec condition2a = "-0.3 1.7 1.5"; |
---|
765 | //vec condition2b = "-1.0 1.0 1.0"; |
---|
766 | emlig1->add_condition(condition2a); |
---|
767 | //emlig2->add_condition(condition2b); |
---|
768 | |
---|
769 | vec condition3a = "0.5 -1.01 1.0"; |
---|
770 | //vec condition3b = "0.5 -1.01 1.0"; |
---|
771 | |
---|
772 | emlig1->add_condition(condition3a); |
---|
773 | //emlig2->add_condition(condition3b); |
---|
774 | |
---|
775 | vec condition4a = "-0.5 -1.0 1.0"; |
---|
776 | //vec condition4b = "-0.5 -1.0 1.0"; |
---|
777 | |
---|
778 | emlig1->add_condition(condition4a); |
---|
779 | //cout << "************************************************" << endl; |
---|
780 | //emlig2->add_condition(condition4b); |
---|
781 | //cout << "************************************************" << endl; |
---|
782 | |
---|
783 | //cout << emlig1->minimal_vertex->get_coordinates(); |
---|
784 | |
---|
785 | //emlig1->remove_condition(condition3a); |
---|
786 | //emlig1->step_me(0); |
---|
787 | //emlig1->remove_condition(condition2a); |
---|
788 | //emlig1->remove_condition(condition1a); |
---|
789 | //emlig1->remove_condition(condition5); |
---|
790 | |
---|
791 | |
---|
792 | //emlig1->step_me(0); |
---|
793 | //emlig2->step_me(0); |
---|
794 | |
---|
795 | |
---|
796 | // DA SE POUZIT PRO VYPIS DO SOUBORU |
---|
797 | // emlig1->step_me(0); |
---|
798 | |
---|
799 | //emlig1->remove_condition(condition1); |
---|
800 | |
---|
801 | |
---|
802 | |
---|
803 | |
---|
804 | |
---|
805 | /* |
---|
806 | for(int i = 0;i<100;i++) |
---|
807 | { |
---|
808 | cout << endl << "Step:" << i << endl; |
---|
809 | |
---|
810 | double condition[emlig_size+1]; |
---|
811 | |
---|
812 | for(int k = 0;k<=emlig_size;k++) |
---|
813 | { |
---|
814 | condition[k] = (rand()-RAND_MAX/2)/1000.0; |
---|
815 | } |
---|
816 | |
---|
817 | |
---|
818 | vec* condition_vec = new vec(condition,emlig_size+1); |
---|
819 | emlig1->add_condition(*condition_vec); |
---|
820 | |
---|
821 | /* |
---|
822 | for(polyhedron* toprow_ref = emlig1->statistic.rows[emlig_size]; toprow_ref != emlig1->statistic.end_poly; toprow_ref = toprow_ref->next_poly) |
---|
823 | { |
---|
824 | cout << ((toprow*)toprow_ref)->probability << endl; |
---|
825 | } |
---|
826 | */ |
---|
827 | /* |
---|
828 | cout << emlig1->statistic_rowsize(emlig_size) << endl << endl; |
---|
829 | |
---|
830 | /* |
---|
831 | if(i-emlig1->number_of_parameters >= 0) |
---|
832 | { |
---|
833 | pause(30); |
---|
834 | } |
---|
835 | */ |
---|
836 | |
---|
837 | // emlig1->step_me(i); |
---|
838 | |
---|
839 | /* |
---|
840 | vector<int> sizevector; |
---|
841 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
---|
842 | { |
---|
843 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
---|
844 | } |
---|
845 | */ |
---|
846 | //} |
---|
847 | |
---|
848 | |
---|
849 | |
---|
850 | |
---|
851 | /* |
---|
852 | emlig1->step_me(1); |
---|
853 | |
---|
854 | vec condition = "2.0 0.0 1.0"; |
---|
855 | |
---|
856 | emlig1->add_condition(condition); |
---|
857 | |
---|
858 | vector<int> sizevector; |
---|
859 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
---|
860 | { |
---|
861 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
---|
862 | } |
---|
863 | |
---|
864 | emlig1->step_me(2); |
---|
865 | |
---|
866 | condition = "2.0 1.0 0.0"; |
---|
867 | |
---|
868 | emlig1->add_condition(condition); |
---|
869 | |
---|
870 | sizevector.clear(); |
---|
871 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
---|
872 | { |
---|
873 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
---|
874 | } |
---|
875 | */ |
---|
876 | |
---|
877 | return 0; |
---|
878 | } |
---|
879 | |
---|
880 | |
---|