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 | |
---|
16 | using namespace itpp; |
---|
17 | using namespace bdm; |
---|
18 | |
---|
19 | const int emlig_size = 2; |
---|
20 | const int utility_constant = 10; |
---|
21 | |
---|
22 | |
---|
23 | int main ( int argc, char* argv[] ) { |
---|
24 | |
---|
25 | itpp::Laplace_RNG LapRNG = Laplace_RNG(); |
---|
26 | |
---|
27 | /* |
---|
28 | // 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, |
---|
29 | // where e_t is normally, student(4) and cauchy distributed are tested using robust AR model, to obtain the |
---|
30 | // variance of location parameter estimators and compare it to the classical setup. |
---|
31 | vector<vector<vector<string>>> string_lists; |
---|
32 | string_lists.push_back(vector<vector<string>>()); |
---|
33 | string_lists.push_back(vector<vector<string>>()); |
---|
34 | string_lists.push_back(vector<vector<string>>()); |
---|
35 | |
---|
36 | char* file_strings[3] = {"c:\\ar_normal.txt", "c:\\ar_student.txt", "c:\\ar_cauchy.txt"}; |
---|
37 | |
---|
38 | |
---|
39 | for(int i = 0;i<3;i++) |
---|
40 | { |
---|
41 | ifstream myfile(file_strings[i]); |
---|
42 | if (myfile.is_open()) |
---|
43 | { |
---|
44 | while ( myfile.good() ) |
---|
45 | { |
---|
46 | string line; |
---|
47 | getline(myfile,line); |
---|
48 | |
---|
49 | vector<string> parsed_line; |
---|
50 | while(line.find(',') != string::npos) |
---|
51 | { |
---|
52 | int loc = line.find(','); |
---|
53 | parsed_line.push_back(line.substr(0,loc)); |
---|
54 | line.erase(0,loc+1); |
---|
55 | } |
---|
56 | |
---|
57 | string_lists[i].push_back(parsed_line); |
---|
58 | } |
---|
59 | myfile.close(); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | for(int j = 0;j<string_lists.size();j++) |
---|
64 | { |
---|
65 | |
---|
66 | for(int i = 0;i<string_lists[j].size()-1;i++) |
---|
67 | { |
---|
68 | vector<vec> conditions; |
---|
69 | //emlig* emliga = new emlig(2); |
---|
70 | RARX* my_rarx = new RARX(2,30); |
---|
71 | |
---|
72 | for(int k = 1;k<string_lists[j][i].size();k++) |
---|
73 | { |
---|
74 | vec condition; |
---|
75 | //condition.ins(0,1); |
---|
76 | condition.ins(0,string_lists[j][i][k]); |
---|
77 | conditions.push_back(condition); |
---|
78 | |
---|
79 | //cout << "orig:" << condition << endl; |
---|
80 | |
---|
81 | if(conditions.size()>1) |
---|
82 | { |
---|
83 | conditions[k-2].ins(0,string_lists[j][i][k]); |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | if(conditions.size()>2) |
---|
88 | { |
---|
89 | conditions[k-3].ins(0,string_lists[j][i][k]); |
---|
90 | |
---|
91 | //cout << "modi:" << conditions[k-3] << endl; |
---|
92 | |
---|
93 | my_rarx->bayes(conditions[k-3]); |
---|
94 | |
---|
95 | |
---|
96 | //if(k>5) |
---|
97 | //{ |
---|
98 | // cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
---|
99 | //} |
---|
100 | |
---|
101 | } |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | //emliga->step_me(0); |
---|
106 | /* |
---|
107 | ofstream myfile; |
---|
108 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
109 | myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";"; |
---|
110 | myfile.close(); |
---|
111 | |
---|
112 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
113 | myfile << emliga->minimal_vertex->get_coordinates()[1] << ";"; |
---|
114 | myfile.close(); |
---|
115 | |
---|
116 | |
---|
117 | cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
---|
118 | cout << "Step: " << i << endl; |
---|
119 | } |
---|
120 | |
---|
121 | cout << "One experiment finished." << endl; |
---|
122 | |
---|
123 | ofstream myfile; |
---|
124 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
125 | myfile << endl; |
---|
126 | myfile.close(); |
---|
127 | |
---|
128 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
129 | myfile << endl; |
---|
130 | myfile.close(); |
---|
131 | }*/ |
---|
132 | |
---|
133 | |
---|
134 | // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from |
---|
135 | // 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 |
---|
136 | // can be compared to the classical setup. |
---|
137 | |
---|
138 | |
---|
139 | vector<vector<string>> strings; |
---|
140 | |
---|
141 | char* file_strings[3] = {"c:\\dataCDClosePercDiff","c:\\ar_student_single","c:\\ar_cauchy_single"}; |
---|
142 | |
---|
143 | for(int i = 0;i<3;i++) |
---|
144 | { |
---|
145 | char dfstring[80]; |
---|
146 | strcpy(dfstring,file_strings[i]); |
---|
147 | strcat(dfstring,".txt"); |
---|
148 | |
---|
149 | ifstream myfile(dfstring); |
---|
150 | if (myfile.is_open()) |
---|
151 | { |
---|
152 | string line; |
---|
153 | getline(myfile,line); |
---|
154 | |
---|
155 | vector<string> parsed_line; |
---|
156 | while(line.find(',') != string::npos) |
---|
157 | { |
---|
158 | int loc = line.find(','); |
---|
159 | parsed_line.push_back(line.substr(0,loc)); |
---|
160 | line.erase(0,loc+1); |
---|
161 | } |
---|
162 | |
---|
163 | strings.push_back(parsed_line); |
---|
164 | |
---|
165 | myfile.close(); |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | for(int j = 0;j<strings.size();j++) |
---|
170 | { |
---|
171 | vector<vec> conditions; |
---|
172 | //emlig* emliga = new emlig(2); |
---|
173 | RARX* my_rarx = new RARX(2,10,false); |
---|
174 | |
---|
175 | |
---|
176 | mat V0 = 0.0001 * eye ( 3 ); |
---|
177 | ARX* my_arx = new ARX(0.85); |
---|
178 | my_arx->set_statistics ( 1, V0 ); //nu is default (set to have finite moments) |
---|
179 | my_arx->set_constant ( false ); |
---|
180 | my_arx->validate(); |
---|
181 | |
---|
182 | |
---|
183 | for(int k = 1;k<strings[j].size();k++) |
---|
184 | { |
---|
185 | vec condition; |
---|
186 | //condition.ins(0,1); |
---|
187 | condition.ins(0,strings[j][k]); |
---|
188 | conditions.push_back(condition); |
---|
189 | |
---|
190 | //cout << "orig:" << condition << endl; |
---|
191 | |
---|
192 | if(conditions.size()>1) |
---|
193 | { |
---|
194 | conditions[k-2].ins(0,strings[j][k]); |
---|
195 | |
---|
196 | } |
---|
197 | |
---|
198 | if(conditions.size()>2) |
---|
199 | { |
---|
200 | conditions[k-3].ins(0,strings[j][k]); |
---|
201 | |
---|
202 | // cout << "Condition:" << conditions[k-3] << endl; |
---|
203 | |
---|
204 | my_rarx->bayes(conditions[k-3]); |
---|
205 | //my_rarx->posterior->step_me(1); |
---|
206 | |
---|
207 | vec cond_vec; |
---|
208 | cond_vec.ins(0,conditions[k-3][0]); |
---|
209 | |
---|
210 | my_arx->bayes(cond_vec,conditions[k-3].right(2)); |
---|
211 | |
---|
212 | |
---|
213 | if(k>8) |
---|
214 | { |
---|
215 | //my_rarx->posterior->step_me(0); |
---|
216 | |
---|
217 | //mat samples = my_rarx->posterior->sample_mat(10); |
---|
218 | |
---|
219 | pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(1000); |
---|
220 | |
---|
221 | //cout << imp_samples.first << endl; |
---|
222 | |
---|
223 | vec sample_prediction; |
---|
224 | for(int t = 0;t<imp_samples.first.size();t++) |
---|
225 | { |
---|
226 | vec lap_sample = conditions[k-3].left(2); |
---|
227 | //lap_sample.ins(lap_sample.size(),1.0); |
---|
228 | |
---|
229 | lap_sample.ins(0,LapRNG()); |
---|
230 | |
---|
231 | sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t)); |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | vec sample_pow = sample_prediction; |
---|
236 | |
---|
237 | // cout << sample_prediction << endl; |
---|
238 | vec poly_coefs; |
---|
239 | double prediction; |
---|
240 | bool stop_iteration = false; |
---|
241 | int en = 1; |
---|
242 | do |
---|
243 | { |
---|
244 | double poly_coef = imp_samples.first*sample_pow/(imp_samples.first*ones(imp_samples.first.size())); |
---|
245 | |
---|
246 | if(en==1) |
---|
247 | { |
---|
248 | prediction = poly_coef; |
---|
249 | } |
---|
250 | |
---|
251 | poly_coef = poly_coef*en*fact(utility_constant-2+en)/fact(utility_constant-2); |
---|
252 | |
---|
253 | if(abs(poly_coef)>numeric_limits<double>::epsilon()) |
---|
254 | { |
---|
255 | sample_pow = elem_mult(sample_pow,sample_prediction); |
---|
256 | poly_coefs.ins(0,pow(-1.0,en+1)*poly_coef); |
---|
257 | } |
---|
258 | else |
---|
259 | { |
---|
260 | stop_iteration = true; |
---|
261 | } |
---|
262 | |
---|
263 | en++; |
---|
264 | |
---|
265 | if(en>20) |
---|
266 | { |
---|
267 | stop_iteration = true; |
---|
268 | } |
---|
269 | } |
---|
270 | while(!stop_iteration); |
---|
271 | |
---|
272 | /* |
---|
273 | ofstream myfile_coef; |
---|
274 | |
---|
275 | myfile_coef.open("c:\\coefs.txt",ios::app); |
---|
276 | |
---|
277 | for(int t = 0;t<poly_coefs.size();t++) |
---|
278 | { |
---|
279 | myfile_coef << poly_coefs[t] << ","; |
---|
280 | } |
---|
281 | |
---|
282 | myfile_coef << endl; |
---|
283 | myfile_coef.close(); |
---|
284 | */ |
---|
285 | |
---|
286 | //cout << "Coefficients: " << poly_coefs << endl; |
---|
287 | |
---|
288 | /* |
---|
289 | vec bas_coef = vec("1.0 2.0 -8.0"); |
---|
290 | cout << "Coefs: " << bas_coef << endl; |
---|
291 | cvec actions2 = roots(bas_coef); |
---|
292 | cout << "Roots: " << actions2 << endl; |
---|
293 | */ |
---|
294 | |
---|
295 | |
---|
296 | |
---|
297 | cvec actions = roots(poly_coefs); |
---|
298 | |
---|
299 | |
---|
300 | bool is_max = false; |
---|
301 | for(int t = 0;t<actions.size();t++) |
---|
302 | { |
---|
303 | if(actions[t].imag() == 0) |
---|
304 | { |
---|
305 | double second_derivative = 0; |
---|
306 | for(int q = 1;q<poly_coefs.size();q++) |
---|
307 | { |
---|
308 | second_derivative+=poly_coefs[q]*pow(actions[t].real(),q-1)*q; |
---|
309 | } |
---|
310 | |
---|
311 | if(second_derivative<0) |
---|
312 | { |
---|
313 | cout << "Action:" << actions[t].real() << endl; |
---|
314 | |
---|
315 | is_max = true; |
---|
316 | } |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | if(!is_max) |
---|
321 | { |
---|
322 | cout << "No maximum." << endl; |
---|
323 | } |
---|
324 | |
---|
325 | // cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl; |
---|
326 | |
---|
327 | /* |
---|
328 | double prediction = 0; |
---|
329 | for(int s = 1;s<samples.rows();s++) |
---|
330 | { |
---|
331 | |
---|
332 | double avg_parameter = imp_samples.get_row(s)*ones(samples.cols())/samples.cols(); |
---|
333 | |
---|
334 | prediction += avg_parameter*conditions[k-3][s-1]; |
---|
335 | |
---|
336 | |
---|
337 | |
---|
338 | /* |
---|
339 | ofstream myfile; |
---|
340 | char fstring[80]; |
---|
341 | strcpy(fstring,file_strings[j]); |
---|
342 | |
---|
343 | char es[5]; |
---|
344 | strcat(fstring,itoa(s,es,10)); |
---|
345 | |
---|
346 | strcat(fstring,"_res.txt"); |
---|
347 | |
---|
348 | |
---|
349 | myfile.open(fstring,ios::app); |
---|
350 | |
---|
351 | //myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; |
---|
352 | myfile << avg_parameter; |
---|
353 | |
---|
354 | if(k!=strings[j].size()-1) |
---|
355 | { |
---|
356 | myfile << ","; |
---|
357 | } |
---|
358 | else |
---|
359 | { |
---|
360 | myfile << endl; |
---|
361 | } |
---|
362 | myfile.close(); |
---|
363 | */ |
---|
364 | |
---|
365 | |
---|
366 | //} |
---|
367 | |
---|
368 | // cout << "Prediction: "<< prediction << endl; |
---|
369 | |
---|
370 | enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2)); |
---|
371 | double prediction2 = pred_mat->mean()[0]; |
---|
372 | |
---|
373 | |
---|
374 | ofstream myfile; |
---|
375 | char fstring[80]; |
---|
376 | char f2string[80]; |
---|
377 | strcpy(fstring,file_strings[j]); |
---|
378 | strcpy(f2string,fstring); |
---|
379 | |
---|
380 | strcat(fstring,"pred.txt"); |
---|
381 | strcat(f2string,"2pred.txt"); |
---|
382 | |
---|
383 | |
---|
384 | myfile.open(fstring,ios::app); |
---|
385 | |
---|
386 | // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; |
---|
387 | myfile << prediction; |
---|
388 | |
---|
389 | if(k!=strings[j].size()-1) |
---|
390 | { |
---|
391 | myfile << ","; |
---|
392 | } |
---|
393 | else |
---|
394 | { |
---|
395 | myfile << endl; |
---|
396 | } |
---|
397 | myfile.close(); |
---|
398 | |
---|
399 | |
---|
400 | myfile.open(f2string,ios::app); |
---|
401 | myfile << prediction2; |
---|
402 | |
---|
403 | if(k!=strings[j].size()-1) |
---|
404 | { |
---|
405 | myfile << ","; |
---|
406 | } |
---|
407 | else |
---|
408 | { |
---|
409 | myfile << endl; |
---|
410 | } |
---|
411 | myfile.close(); |
---|
412 | //*/ |
---|
413 | |
---|
414 | } |
---|
415 | } |
---|
416 | |
---|
417 | //emliga->step_me(0); |
---|
418 | /* |
---|
419 | ofstream myfile; |
---|
420 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
421 | myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";"; |
---|
422 | myfile.close(); |
---|
423 | |
---|
424 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
425 | myfile << emliga->minimal_vertex->get_coordinates()[1] << ";"; |
---|
426 | myfile.close(); |
---|
427 | |
---|
428 | |
---|
429 | cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
---|
430 | cout << "Step: " << i << endl;*/ |
---|
431 | } |
---|
432 | |
---|
433 | |
---|
434 | } |
---|
435 | |
---|
436 | |
---|
437 | // EXPERIMENT: One step ahead price prediction. Comparison of classical and robust model using optimal trading |
---|
438 | // with maximization of logarithm of one-step ahead wealth. |
---|
439 | |
---|
440 | |
---|
441 | |
---|
442 | /* |
---|
443 | cout << "One experiment finished." << endl; |
---|
444 | |
---|
445 | ofstream myfile; |
---|
446 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
447 | myfile << endl; |
---|
448 | myfile.close(); |
---|
449 | |
---|
450 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
451 | myfile << endl; |
---|
452 | myfile.close();*/ |
---|
453 | |
---|
454 | |
---|
455 | //emlig* emlig1 = new emlig(emlig_size); |
---|
456 | |
---|
457 | //emlig1->step_me(0); |
---|
458 | //emlig* emlig2 = new emlig(emlig_size); |
---|
459 | |
---|
460 | /* |
---|
461 | emlig1->set_correction_factors(4); |
---|
462 | |
---|
463 | for(int j = 0;j<emlig1->correction_factors.size();j++) |
---|
464 | { |
---|
465 | for(set<my_ivec>::iterator vec_ref = emlig1->correction_factors[j].begin();vec_ref!=emlig1->correction_factors[j].end();vec_ref++) |
---|
466 | { |
---|
467 | cout << j << " "; |
---|
468 | |
---|
469 | for(int i=0;i<(*vec_ref).size();i++) |
---|
470 | { |
---|
471 | cout << (*vec_ref)[i]; |
---|
472 | } |
---|
473 | |
---|
474 | cout << endl; |
---|
475 | } |
---|
476 | }*/ |
---|
477 | |
---|
478 | /* |
---|
479 | vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5"; |
---|
480 | |
---|
481 | emlig1->add_condition(condition5); |
---|
482 | //emlig1->step_me(0); |
---|
483 | |
---|
484 | |
---|
485 | vec condition1a = "-1.0 1.02 0.5"; |
---|
486 | //vec condition1b = "1.0 1.0 1.01"; |
---|
487 | emlig1->add_condition(condition1a); |
---|
488 | //emlig2->add_condition(condition1b); |
---|
489 | |
---|
490 | vec condition2a = "-0.3 1.7 1.5"; |
---|
491 | //vec condition2b = "-1.0 1.0 1.0"; |
---|
492 | emlig1->add_condition(condition2a); |
---|
493 | //emlig2->add_condition(condition2b); |
---|
494 | |
---|
495 | vec condition3a = "0.5 -1.01 1.0"; |
---|
496 | //vec condition3b = "0.5 -1.01 1.0"; |
---|
497 | |
---|
498 | emlig1->add_condition(condition3a); |
---|
499 | //emlig2->add_condition(condition3b); |
---|
500 | |
---|
501 | vec condition4a = "-0.5 -1.0 1.0"; |
---|
502 | //vec condition4b = "-0.5 -1.0 1.0"; |
---|
503 | |
---|
504 | emlig1->add_condition(condition4a); |
---|
505 | //cout << "************************************************" << endl; |
---|
506 | //emlig2->add_condition(condition4b); |
---|
507 | //cout << "************************************************" << endl; |
---|
508 | |
---|
509 | //cout << emlig1->minimal_vertex->get_coordinates(); |
---|
510 | |
---|
511 | //emlig1->remove_condition(condition3a); |
---|
512 | //emlig1->step_me(0); |
---|
513 | //emlig1->remove_condition(condition2a); |
---|
514 | //emlig1->remove_condition(condition1a); |
---|
515 | //emlig1->remove_condition(condition5); |
---|
516 | |
---|
517 | |
---|
518 | //emlig1->step_me(0); |
---|
519 | //emlig2->step_me(0); |
---|
520 | |
---|
521 | |
---|
522 | // DA SE POUZIT PRO VYPIS DO SOUBORU |
---|
523 | // emlig1->step_me(0); |
---|
524 | |
---|
525 | //emlig1->remove_condition(condition1); |
---|
526 | |
---|
527 | |
---|
528 | |
---|
529 | |
---|
530 | |
---|
531 | /* |
---|
532 | for(int i = 0;i<100;i++) |
---|
533 | { |
---|
534 | cout << endl << "Step:" << i << endl; |
---|
535 | |
---|
536 | double condition[emlig_size+1]; |
---|
537 | |
---|
538 | for(int k = 0;k<=emlig_size;k++) |
---|
539 | { |
---|
540 | condition[k] = (rand()-RAND_MAX/2)/1000.0; |
---|
541 | } |
---|
542 | |
---|
543 | |
---|
544 | vec* condition_vec = new vec(condition,emlig_size+1); |
---|
545 | emlig1->add_condition(*condition_vec); |
---|
546 | |
---|
547 | /* |
---|
548 | for(polyhedron* toprow_ref = emlig1->statistic.rows[emlig_size]; toprow_ref != emlig1->statistic.end_poly; toprow_ref = toprow_ref->next_poly) |
---|
549 | { |
---|
550 | cout << ((toprow*)toprow_ref)->probability << endl; |
---|
551 | } |
---|
552 | */ |
---|
553 | /* |
---|
554 | cout << emlig1->statistic_rowsize(emlig_size) << endl << endl; |
---|
555 | |
---|
556 | /* |
---|
557 | if(i-emlig1->number_of_parameters >= 0) |
---|
558 | { |
---|
559 | pause(30); |
---|
560 | } |
---|
561 | */ |
---|
562 | |
---|
563 | // emlig1->step_me(i); |
---|
564 | |
---|
565 | /* |
---|
566 | vector<int> sizevector; |
---|
567 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
---|
568 | { |
---|
569 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
---|
570 | } |
---|
571 | */ |
---|
572 | //} |
---|
573 | |
---|
574 | |
---|
575 | |
---|
576 | |
---|
577 | /* |
---|
578 | emlig1->step_me(1); |
---|
579 | |
---|
580 | vec condition = "2.0 0.0 1.0"; |
---|
581 | |
---|
582 | emlig1->add_condition(condition); |
---|
583 | |
---|
584 | vector<int> sizevector; |
---|
585 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
---|
586 | { |
---|
587 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
---|
588 | } |
---|
589 | |
---|
590 | emlig1->step_me(2); |
---|
591 | |
---|
592 | condition = "2.0 1.0 0.0"; |
---|
593 | |
---|
594 | emlig1->add_condition(condition); |
---|
595 | |
---|
596 | sizevector.clear(); |
---|
597 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
---|
598 | { |
---|
599 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
---|
600 | } |
---|
601 | */ |
---|
602 | |
---|
603 | return 0; |
---|
604 | } |
---|
605 | |
---|
606 | |
---|