377 | | itpp::Laplace_RNG LapRNG = Laplace_RNG(); |
378 | | |
379 | | vector<vector<string>> strings; |
380 | | |
381 | | char* file_string = "C:\\CD2"; // "C:\\dataADClosePercDiff"; // |
382 | | |
383 | | char dfstring[80]; |
384 | | strcpy(dfstring,file_string); |
385 | | strcat(dfstring,".txt"); |
386 | | |
387 | | |
388 | | mat data_matrix; |
389 | | ifstream myfile(dfstring); |
390 | | if (myfile.is_open()) |
391 | | { |
392 | | string line; |
393 | | while(getline(myfile,line)) |
394 | | { |
395 | | vec data_vector; |
396 | | while(line.find(',') != string::npos) //zmenil som ciarku za medzeru |
397 | | { |
398 | | //line.erase(0,1); // toto som sem pridal |
399 | | int loc2 = line.find('\n'); |
400 | | int loc = line.find(','); |
401 | | data_vector.ins(data_vector.size(),atof(line.substr(0,loc).c_str())); |
402 | | line.erase(0,loc+1); |
403 | | } |
404 | | |
405 | | data_matrix.ins_row(data_matrix.rows(),data_vector); |
406 | | } |
407 | | |
408 | | myfile.close(); |
409 | | } |
410 | | else |
411 | | { |
412 | | cout << "Can't open data file!" << endl; |
413 | | } |
414 | | |
415 | | //konec nacitavania dat |
416 | | set<set<pair<int,int>>> model_types = model::possible_models_recurse(max_model_order,data_matrix.rows()); //volanie funkce kde robi kombinace modelov |
417 | | //to priradime do model_types, data_matrix.row urcuje pocet kanalov dat |
418 | | vector<model*> models; |
419 | | for(set<set<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++) |
420 | | {// prechadza rozne typy kanalov, a poctu regresorov |
421 | | for(int window_size = max_window_size-1;window_size < max_window_size;window_size++) |
422 | | { |
423 | | models.push_back(new model((*model_type),true,true,window_size,0,&data_matrix)); // to su len konstruktory, len inicializujeme rozne typy |
424 | | models.push_back(new model((*model_type),false,true,window_size,0,&data_matrix)); |
425 | | models.push_back(new model((*model_type),true,false,window_size,0,&data_matrix)); |
426 | | models.push_back(new model((*model_type),false,false,window_size,0,&data_matrix)); |
427 | | } |
428 | | |
429 | | //set<pair<int,int>> empty_list; |
430 | | //models.push_back(new model(empty_list,false,true,100,0,&data_matrix)); |
431 | | } |
432 | | |
433 | | mat result_lognc; |
434 | | // mat result_preds; |
435 | | |
436 | | ofstream myfilew; |
437 | | char fstring[80]; |
438 | | strcpy(fstring,file_string); |
439 | | //strcat(fstring,"lognc.txt"); |
440 | | strcat(fstring,"preds.txt"); |
441 | | |
442 | | for(int time = max_model_order;time<data_matrix.cols();time++) //time<data_matrix.cols() |
443 | | { |
444 | | cout << "Steps: " << time << endl; |
| 416 | itpp::Laplace_RNG LapRNG = Laplace_RNG(); |
| 417 | |
| 418 | char* folder_string = "C:\\RobustExperiments\\"; // "C:\\dataADClosePercDiff"; // |
| 419 | char* data_folder = "data\\"; |
| 420 | char* results_folder = "results\\"; |
| 421 | |
| 422 | char dfstring[150]; |
| 423 | strcpy(dfstring,folder_string); |
| 424 | strcat(dfstring,data_folder); |
| 425 | strcat(dfstring,commodity); |
| 426 | vector<char*> files = listFiles(dfstring); |
| 427 | |
| 428 | for(int contract=0;contract<files.size();contract++) |
| 429 | { |
| 430 | char *cdf_str = new char[strlen(dfstring) + 1]; |
| 431 | strcpy(cdf_str, dfstring); |
| 432 | strcat(cdf_str,files[contract]); |
| 433 | |
| 434 | mat data_matrix; |
| 435 | ifstream myfile(cdf_str); |
| 436 | if (myfile.is_open()) |
| 437 | { |
| 438 | string line; |
| 439 | while(getline(myfile,line)) |
| 440 | { |
| 441 | vec data_vector; |
| 442 | while(line.find(',') != string::npos) //zmenil som ciarku za medzeru |
| 443 | { |
| 444 | //line.erase(0,1); // toto som sem pridal |
| 445 | int loc2 = line.find('\n'); |
| 446 | int loc = line.find(','); |
| 447 | data_vector.ins(data_vector.size(),atof(line.substr(0,loc).c_str())); |
| 448 | line.erase(0,loc+1); |
| 449 | } |
| 450 | |
| 451 | data_matrix.ins_row(data_matrix.rows(),data_vector); |
| 452 | } |
| 453 | |
| 454 | myfile.close(); |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | cout << "Can't open data file!" << endl; |
| 459 | } |
| 460 | |
| 461 | //konec nacitavania dat |
| 462 | set<set<pair<int,int>>> model_types = model::possible_models_recurse(max_model_order,data_matrix.rows()); //volanie funkce kde robi kombinace modelov |
| 463 | //to priradime do model_types, data_matrix.row urcuje pocet kanalov dat |
| 464 | vector<model*> models; |
| 465 | for(set<set<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++) |
| 466 | {// prechadza rozne typy kanalov, a poctu regresorov |
| 467 | for(int window_size = max_window_size-1;window_size < max_window_size;window_size++) |
| 468 | { |
| 469 | models.push_back(new model((*model_type),true,true,window_size,0,&data_matrix)); // to su len konstruktory, len inicializujeme rozne typy |
| 470 | models.push_back(new model((*model_type),false,true,window_size,0,&data_matrix)); |
| 471 | models.push_back(new model((*model_type),true,false,window_size,0,&data_matrix)); |
| 472 | models.push_back(new model((*model_type),false,false,window_size,0,&data_matrix)); |
| 473 | } |
| 474 | |
| 475 | //set<pair<int,int>> empty_list; |
| 476 | //models.push_back(new model(empty_list,false,true,100,0,&data_matrix)); |
| 477 | } |
| 478 | |
| 479 | mat result_lognc; |
| 480 | // mat result_preds; |
| 481 | |
| 482 | ofstream myfilew; |
| 483 | char rfstring[150]; |
| 484 | strcpy(rfstring,folder_string); |
| 485 | strcat(rfstring,results_folder); |
| 486 | strcat(rfstring,commodity); |
| 487 | strcat(rfstring,files[contract]); |
| 488 | |
| 489 | /* |
| 490 | char predstring[150]; |
| 491 | strcpy(predstring,folder_string); |
| 492 | strcat(predstring,results_folder); |
| 493 | strcat(predstring,commodity); |
| 494 | strcat(predstring,"PRED"); |
| 495 | strcat(predstring,files[contract]); |
| 496 | */ |
| 497 | |
| 498 | for(int time = max_model_order;time<data_matrix.cols();time++) //time<data_matrix.cols() |
| 499 | { |
| 500 | cout << "Steps: " << time << endl; |
452 | | //pocet stlpcov data_matrix je pocet casovych krokov |
453 | | vec cur_res_lognc; |
454 | | // vec preds; |
455 | | vector<string> nazvy; |
456 | | for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) |
457 | | {//posuvam s apo models, co je pole modelov urobene o cyklus vyssie. Teda som v case time a robim to tam pre vsetky typy modelov, kombinace regresorov |
458 | | (*model_ref)->data_update(time); //pozret sa preco je toto tu nutne |
459 | | |
460 | | //cout << "Updated." << endl; |
461 | | //if (time = max_model_order) nazvy.push_back(models.model_ref]);// ako by som mohol dostat nazov modelu? |
462 | | |
463 | | if((*model_ref)->my_rarx!=NULL) //vklada normalizacni faktor do cur_res_lognc |
464 | | { |
465 | | //cout << "Maxlik vertex:" << (*model_ref)->my_rarx->posterior->minimal_vertex->get_coordinates() << endl; |
466 | | cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_rarx->posterior->_ll()); |
467 | | } |
468 | | else |
469 | | { |
470 | | double cur_lognc = (*model_ref)->my_arx->posterior().lognc(); |
471 | | double cur_ll = cur_lognc-(*model_ref)->previous_lognc; |
| 540 | //pocet stlpcov data_matrix je pocet casovych krokov |
| 541 | double cur_loglikelihood; |
| 542 | // vec preds; |
| 543 | int prev_samples_nr; |
| 544 | bool previous_switch = true; |
| 545 | for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) |
| 546 | {//posuvam s apo models, co je pole modelov urobene o cyklus vyssie. Teda som v case time a robim to tam pre vsetky typy modelov, kombinace regresorov |
| 547 | (*model_ref)->data_update(time); //pozret sa preco je toto tu nutne |
| 548 | |
| 549 | //cout << "Updated." << endl; |
| 550 | |
| 551 | if((*model_ref)->my_rarx!=NULL) //vklada normalizacni faktor do cur_res_lognc |
| 552 | { |
| 553 | //cout << "Maxlik vertex(robust):" << (*model_ref)->my_rarx->posterior->minimal_vertex->get_coordinates() << endl; |
| 554 | cur_loglikelihood = (*model_ref)->my_rarx->posterior->_ll(); |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | //cout << "Mean(classical):" << (*model_ref)->my_arx->posterior().mean() << endl; |
| 559 | double cur_lognc = (*model_ref)->my_arx->posterior().lognc(); |
| 560 | cur_loglikelihood = cur_lognc-(*model_ref)->previous_lognc; |
| 561 | |
| 562 | (*model_ref)->previous_lognc = cur_lognc; |
| 563 | } |
543 | | */ |
544 | | |
545 | | if(time>prediction_time) |
546 | | { |
547 | | // PREDICTIONS |
548 | | pair<vec,vec> predictions = (*model_ref)->predict(5000,time,&LapRNG); |
549 | | |
550 | | /* |
551 | | cout << predictions.first << endl << endl << predictions.second << endl << "*************************************" ; |
552 | | pause(5); |
553 | | */ |
554 | | |
555 | | double optimalInvestment = newtonRaphson(0,0.00001,predictions.second,utility_order); |
556 | | |
557 | | /* |
558 | | vec utilityValues; |
559 | | for(int j=0;j<1000;j++) |
560 | | { |
561 | | utilityValues.ins(utilityValues.length(),valueCRRAUtility(-0.5+0.001*j, predictions.second, utility_order)); |
562 | | }*/ |
563 | | |
564 | | double avg_prediction = (predictions.first*predictions.second)/(predictions.first*ones(predictions.first.size())); |
565 | | |
566 | | (*model_ref)->predictions.ins((*model_ref)->predictions.size(),avg_prediction); |
| 622 | */ |
| 623 | |
| 624 | if(time>prediction_time) |
| 625 | { |
| 626 | int samples_nr; |
| 627 | if(previous_switch) |
| 628 | { |
| 629 | samples_nr = 10000; |
| 630 | } |
| 631 | else |
| 632 | { |
| 633 | samples_nr = prev_samples_nr; |
| 634 | } |
| 635 | |
| 636 | |
| 637 | // PREDICTIONS |
| 638 | pair<vec,vec> predictions = (*model_ref)->predict(samples_nr,time,&LapRNG); |
| 639 | |
| 640 | /* |
| 641 | myfilew.open(predstring,ios::app); |
| 642 | for(int i=0;i<10000;i++) |
| 643 | { |
| 644 | if(i<predictions.second.size()) |
| 645 | { |
| 646 | myfilew << predictions.second.get(i) << ","; |
| 647 | } |
| 648 | else |
| 649 | { |
| 650 | myfilew << ","; |
| 651 | } |
| 652 | } |
| 653 | myfilew << endl; |
| 654 | myfilew.close(); |
| 655 | */ |
| 656 | |
| 657 | if(previous_switch) |
| 658 | { |
| 659 | prev_samples_nr = predictions.second.size(); |
| 660 | samples_nr = prev_samples_nr; |
| 661 | } |
| 662 | |
| 663 | previous_switch = !previous_switch; |
| 664 | |
| 665 | double optimalInvestment = newtonRaphson(0,0.00001,predictions.second,utility_order); |
| 666 | |
| 667 | if(abs(optimalInvestment)>max_investment) |
| 668 | { |
| 669 | optimalInvestment = max_investment*sign(optimalInvestment); |
| 670 | } |
| 671 | |
| 672 | |
| 673 | /* |
| 674 | vec utilityValues; |
| 675 | for(int j=0;j<1000;j++) |
| 676 | { |
| 677 | utilityValues.ins(utilityValues.length(),valueCRRAUtility(-0.5+0.001*j, predictions.second, utility_order)); |
| 678 | }*/ |
| 679 | |
| 680 | double avg_prediction = (predictions.first*predictions.second)/(predictions.first*ones(predictions.first.size())); |
| 681 | |
| 682 | (*model_ref)->predictions.ins((*model_ref)->predictions.size(),avg_prediction); |
580 | | } |
581 | | |
582 | | |
583 | | //preds.ins(0,data_matrix.get(0,time+1)); |
584 | | } |
585 | | |
586 | | if(time>prediction_time) |
587 | | { |
588 | | // REAL PRICE |
589 | | myfilew.open(fstring,ios::app); |
590 | | myfilew << data_matrix.get(0,time+1) << endl; |
591 | | myfilew.close(); |
592 | | } |
593 | | |
594 | | |
595 | | |
596 | | result_lognc.ins_col(result_lognc.cols(),cur_res_lognc); |
597 | | //result_preds.ins_col(result_preds.cols(),preds); |
598 | | |
599 | | // cout << "Updated." << endl; |
600 | | |
601 | | /* |
602 | | myfilew.open(fstring,ios::app); |
603 | | |
604 | | // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; |
605 | | |
606 | | if(time == max_model_order) |
607 | | { |
608 | | for(int i = 0;i<cur_res_lognc.size();i++) |
609 | | { |
610 | | for(set<pair<int,int>>::iterator ar_ref = models[i]->ar_components.begin();ar_ref != models[i]->ar_components.end();ar_ref++) |
611 | | { |
612 | | myfilew << (*ar_ref).second << (*ar_ref).first; |
613 | | } |
614 | | |
615 | | myfilew << "."; |
616 | | |
617 | | if(models[i]->my_arx == NULL) |
618 | | { |
619 | | myfilew << "1"; |
620 | | } |
621 | | else |
622 | | { |
623 | | myfilew << "0"; |
624 | | } |
625 | | |
626 | | if(models[i]->has_constant) |
627 | | { |
628 | | myfilew << "1"; |
629 | | } |
630 | | else |
631 | | { |
632 | | myfilew << "0"; |
633 | | } |
634 | | |
635 | | myfilew << ","; |
636 | | } |
637 | | |
638 | | myfilew << endl; |
639 | | } |
640 | | |
641 | | |
642 | | for(int i = 0;i<cur_res_lognc.size();i++) |
643 | | { |
644 | | myfilew << cur_res_lognc[i] << ' ';//zmenil som ciarku ze medzeru |
645 | | } |
646 | | |
647 | | myfilew << endl; |
648 | | myfilew.close(); |
649 | | */ |
650 | | } |
651 | | |
652 | | // EXPERIMENT: One step ahead price prediction. Comparison of classical and robust model using optimal trading |
653 | | // with maximization of logarithm of one-step ahead wealth. |
654 | | |
655 | | |
656 | | |
657 | | /* |
658 | | cout << "One experiment finished." << endl; |
659 | | |
660 | | ofstream myfile; |
661 | | myfile.open("c:\\robust_ar1.txt",ios::app); |
662 | | myfile << endl; |
663 | | myfile.close(); |
664 | | |
665 | | myfile.open("c:\\robust_ar2.txt",ios::app); |
666 | | myfile << endl; |
667 | | myfile.close();*/ |
668 | | |
669 | | |
670 | | //emlig* emlig1 = new emlig(emlig_size); |
671 | | |
672 | | //emlig1->step_me(0); |
673 | | //emlig* emlig2 = new emlig(emlig_size); |
674 | | |
675 | | /* |
676 | | emlig1->set_correction_factors(4); |
677 | | |
678 | | for(int j = 0;j<emlig1->correction_factors.size();j++) |
679 | | { |
680 | | for(set<my_ivec>::iterator vec_ref = emlig1->correction_factors[j].begin();vec_ref!=emlig1->correction_factors[j].end();vec_ref++) |
681 | | { |
682 | | cout << j << " "; |
683 | | |
684 | | for(int i=0;i<(*vec_ref).size();i++) |
685 | | { |
686 | | cout << (*vec_ref)[i]; |
687 | | } |
688 | | |
689 | | cout << endl; |
690 | | } |
691 | | }*/ |
692 | | |
693 | | /* |
694 | | vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5"; |
695 | | |
696 | | emlig1->add_condition(condition5); |
697 | | //emlig1->step_me(0); |
698 | | |
699 | | |
700 | | vec condition1a = "-1.0 1.02 0.5"; |
701 | | //vec condition1b = "1.0 1.0 1.01"; |
702 | | emlig1->add_condition(condition1a); |
703 | | //emlig2->add_condition(condition1b); |
704 | | |
705 | | vec condition2a = "-0.3 1.7 1.5"; |
706 | | //vec condition2b = "-1.0 1.0 1.0"; |
707 | | emlig1->add_condition(condition2a); |
708 | | //emlig2->add_condition(condition2b); |
709 | | |
710 | | vec condition3a = "0.5 -1.01 1.0"; |
711 | | //vec condition3b = "0.5 -1.01 1.0"; |
712 | | |
713 | | emlig1->add_condition(condition3a); |
714 | | //emlig2->add_condition(condition3b); |
715 | | |
716 | | vec condition4a = "-0.5 -1.0 1.0"; |
717 | | //vec condition4b = "-0.5 -1.0 1.0"; |
718 | | |
719 | | emlig1->add_condition(condition4a); |
720 | | //cout << "************************************************" << endl; |
721 | | //emlig2->add_condition(condition4b); |
722 | | //cout << "************************************************" << endl; |
723 | | |
724 | | //cout << emlig1->minimal_vertex->get_coordinates(); |
725 | | |
726 | | //emlig1->remove_condition(condition3a); |
727 | | //emlig1->step_me(0); |
728 | | //emlig1->remove_condition(condition2a); |
729 | | //emlig1->remove_condition(condition1a); |
730 | | //emlig1->remove_condition(condition5); |
731 | | |
732 | | |
733 | | //emlig1->step_me(0); |
734 | | //emlig2->step_me(0); |
735 | | |
736 | | |
737 | | // DA SE POUZIT PRO VYPIS DO SOUBORU |
738 | | // emlig1->step_me(0); |
739 | | |
740 | | //emlig1->remove_condition(condition1); |
741 | | |
742 | | |
743 | | |
744 | | |
745 | | |
746 | | /* |
747 | | for(int i = 0;i<100;i++) |
748 | | { |
749 | | cout << endl << "Step:" << i << endl; |
750 | | |
751 | | double condition[emlig_size+1]; |
752 | | |
753 | | for(int k = 0;k<=emlig_size;k++) |
754 | | { |
755 | | condition[k] = (rand()-RAND_MAX/2)/1000.0; |
756 | | } |
757 | | |
758 | | |
759 | | vec* condition_vec = new vec(condition,emlig_size+1); |
760 | | emlig1->add_condition(*condition_vec); |
761 | | |
762 | | /* |
763 | | for(polyhedron* toprow_ref = emlig1->statistic.rows[emlig_size]; toprow_ref != emlig1->statistic.end_poly; toprow_ref = toprow_ref->next_poly) |
764 | | { |
765 | | cout << ((toprow*)toprow_ref)->probability << endl; |
766 | | } |
767 | | */ |
768 | | /* |
769 | | cout << emlig1->statistic_rowsize(emlig_size) << endl << endl; |
770 | | |
771 | | /* |
772 | | if(i-emlig1->number_of_parameters >= 0) |
773 | | { |
774 | | pause(30); |
775 | | } |
776 | | */ |
777 | | |
778 | | // emlig1->step_me(i); |
779 | | |
780 | | /* |
781 | | vector<int> sizevector; |
782 | | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
783 | | { |
784 | | sizevector.push_back(emlig1->statistic_rowsize(s)); |
785 | | } |
786 | | */ |
787 | | //} |
788 | | |
789 | | |
790 | | |
791 | | |
792 | | /* |
793 | | emlig1->step_me(1); |
794 | | |
795 | | vec condition = "2.0 0.0 1.0"; |
796 | | |
797 | | emlig1->add_condition(condition); |
798 | | |
799 | | vector<int> sizevector; |
800 | | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
801 | | { |
802 | | sizevector.push_back(emlig1->statistic_rowsize(s)); |
803 | | } |
804 | | |
805 | | emlig1->step_me(2); |
806 | | |
807 | | condition = "2.0 1.0 0.0"; |
808 | | |
809 | | emlig1->add_condition(condition); |
810 | | |
811 | | sizevector.clear(); |
812 | | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
813 | | { |
814 | | sizevector.push_back(emlig1->statistic_rowsize(s)); |
815 | | } |
816 | | */ |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | for(vector<model*>::reverse_iterator model_ref = models.rbegin();model_ref!=models.rend();model_ref++) |
| 709 | { |
| 710 | delete *model_ref; |
| 711 | } |
| 712 | } |