Changeset 1401 for applications

Show
Ignore:
Timestamp:
10/04/11 17:51:21 (13 years ago)
Author:
sindj
Message:

Nevim, co se zmenilo, dodelani experimentu s maxlik odhady asi. JS

Location:
applications/robust
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/main.cpp

    r1396 r1401  
    2727//const int utility_constant = 5; 
    2828 
    29 const int max_model_order = 1; 
     29const int max_model_order = 2; 
    3030const double apriorno     = 0.01; 
    31 const int max_window_size = 121; 
    32  
    33 /* 
    34 HDDEDATA CALLBACK DdeCallback( 
    35         UINT uType,     // Transaction type. 
    36         UINT uFmt,      // Clipboard data format. 
    37         HCONV hconv,    // Handle to the conversation. 
    38         HSZ hsz1,       // Handle to a string. 
    39         HSZ hsz2,       // Handle to a string. 
    40         HDDEDATA hdata, // Handle to a global memory object. 
    41         DWORD dwData1,  // Transaction-specific data. 
    42         DWORD dwData2)  // Transaction-specific data. 
    43 { 
    44         return 0; 
    45 } 
    46  
    47 void DDERequest(DWORD idInst, HCONV hConv, char* szItem) 
    48 { 
    49         HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0); 
    50         HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT,  
    51                                                                         XTYP_ADVSTART,TIMEOUT_ASYNC , NULL); //TIMEOUT_ASYNC 
    52         if (hData==NULL) 
    53         { 
    54                 printf("Request failed: %s\n", szItem); 
    55         } 
    56  
    57         if (hData==0) 
    58         { 
    59                 printf("Request failed: %s\n", szItem); 
    60         } 
    61 } 
    62  
    63 DWORD WINAPI ThrdFunc( LPVOID n ) 
    64 {     
    65     return 0; 
    66 } 
    67 */ 
     31const int max_window_size = 30; 
    6832 
    6933class model 
     
    168132                vec data_vector; 
    169133                for(set<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++) 
    170                 {  //ar?iterator ide len od 1 pod 2, alebo niekedy len 1 
     134                {   
    171135                        data_vector.ins(data_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second)); 
    172 // do data vector vlozi pre dany typ regresoru prislusne cisla z data_matrix. Ale ako? preco time-ar_iterator->second? 
    173136                } 
    174137                if(my_rarx!=NULL) 
    175                 {       //pre robust priradi az tu do data_vector aj predikciu 
     138                {        
    176139                        data_vector.ins(0,(*data_matrix).get(predicted_channel,time)); 
    177140                        my_rarx->bayes(data_vector); 
     
    179142                else 
    180143                { 
    181                         vec pred_vec;//tu sa predikcia zadava zvlast 
     144                        vec pred_vec; 
    182145                        pred_vec.ins(0,(*data_matrix).get(predicted_channel,time)); 
    183146                        my_arx->bayes(pred_vec,data_vector); 
     
    245208                set<set<pair<int,int>>> created_model_types;             
    246209 
    247                 if(max_order == 1)//ukoncovacia vetva 
     210                if(max_order == 1) 
    248211                {                        
    249                         for(int channel = 0;channel<number_of_channels;channel++)//pre AR 1 model vytvori kombinace kanalov v prvom kroku poyadu 
     212                        for(int channel = 0;channel<number_of_channels;channel++) 
    250213                        { 
    251214                                set<pair<int,int>> returned_type; 
    252                                 returned_type.insert(pair<int,int>(channel,1));  //?? 
     215                                returned_type.insert(pair<int,int>(channel,1));   
    253216                                created_model_types.insert(returned_type); 
    254217                        } 
     
    258221                else 
    259222                { 
    260                         created_model_types = possible_models_recurse(max_order-1,number_of_channels);//tu uz mame ulozene kombinace o jeden krok dozadu  //rekuryivne volanie 
     223                        created_model_types = possible_models_recurse(max_order-1,number_of_channels); 
    261224                        set<set<pair<int,int>>> returned_types; 
    262225                         
     
    269232                                        { 
    270233                                                set<pair<int,int>> returned_type; 
    271                                                 pair<int,int> new_pair = pair<int,int>(channel,order);//?? 
    272                                                 if(find((*model_ref).begin(),(*model_ref).end(),new_pair)==(*model_ref).end()) //?? 
     234                                                pair<int,int> new_pair = pair<int,int>(channel,order); 
     235                                                if(find((*model_ref).begin(),(*model_ref).end(),new_pair)==(*model_ref).end())  
    273236                                                { 
    274                                                         returned_type.insert((*model_ref).begin(),(*model_ref).end()); //co vlozi na zaciatok retuned_type? 
     237                                                        returned_type.insert((*model_ref).begin(),(*model_ref).end());  
    275238                                                        returned_type.insert(new_pair); 
    276239                                                         
     
    294257int main ( int argc, char* argv[] )  
    295258{ 
    296          
    297         // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from  
    298     // 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 
    299     // can be compared to the classical setup. 
    300          
    301         itpp::Laplace_RNG LapRNG = Laplace_RNG();        
    302  
    303259        vector<vector<string>> strings; 
    304260 
    305         char* file_string =  "C:\\results\\cauchy"; // "C:\\dataADClosePercDiff"; //   
     261        char* file_string =  "C:\\results\\normalM"; // "C:\\dataADClosePercDiff"; //   
    306262 
    307263        char dfstring[80]; 
     
    318274                {                        
    319275                        vec data_vector; 
    320                         while(line.find(',') != string::npos) //zmenil som ciarku za medzeru 
    321                         { 
    322                                 //line.erase(0,1); // toto som sem pridal 
     276                        while(line.find(',') != string::npos)  
     277                        {                                
    323278                                int loc2 = line.find('\n'); 
    324279                                int loc  = line.find(','); 
     
    336291                cout << "Can't open data file!" << endl; 
    337292        } 
    338          
    339         //konec nacitavania dat 
    340         set<set<pair<int,int>>> model_types = model::possible_models_recurse(max_model_order,data_matrix.rows()); //volanie funkce kde robi kombinace modelov 
    341                                                                                                 //to priradime do model_types, data_matrix.row urcuje pocet kanalov dat 
     293 
     294        set<pair<int,int>> model_type; 
     295        model_type.insert(pair<int,int>(0,1)); 
     296        model_type.insert(pair<int,int>(0,2)); 
     297 
    342298        vector<model*> models; 
    343         for(set<set<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++) 
    344         {// prechadza rozne typy kanalov, a poctu regresorov 
    345                 for(int window_size = max_window_size-1;window_size < max_window_size;window_size++) 
    346                 { 
    347                         //models.push_back(new model((*model_type),true,true,window_size,0,&data_matrix));   // to su len konstruktory, len inicializujeme rozne typy 
    348                         //models.push_back(new model((*model_type),false,true,window_size,0,&data_matrix)); 
    349                         models.push_back(new model((*model_type),true,false,window_size,0,&data_matrix)); 
    350                         models.push_back(new model((*model_type),false,false,window_size,0,&data_matrix));               
    351                 } 
    352  
    353                 //set<pair<int,int>> empty_list; 
    354                 //models.push_back(new model(empty_list,false,true,100,0,&data_matrix)); 
    355         } 
    356          
    357         mat result_lognc; 
    358         // mat result_preds; 
    359299 
    360300        ofstream myfilew; 
    361         //char fstring[80];                                      
    362         //strcpy(fstring,file_string); 
    363         //strcat(fstring,"lognc.txt"); 
    364         //strcat(fstring,"preds.txt");   
    365  
    366         for(int time = max_model_order;time<data_matrix.cols();time++) //time<data_matrix.cols()  
    367         {        
    368                 if(time==max_window_size) 
    369                 { 
    370                         exit(1); 
    371                 } 
    372301                 
    373                 //pocet stlpcov data_matrix je pocet casovych krokov 
    374                 vec cur_res_lognc; 
    375                 // vec preds; 
    376                 vector<string> nazvy; 
    377                 for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) 
    378                 {//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 
    379                         (*model_ref)->data_update(time); //pozret sa preco je toto tu nutne 
    380  
    381                         cout << "Updated." << endl; 
    382                         //if (time = max_model_order) nazvy.push_back(models.model_ref]);// ako by som mohol dostat nazov modelu? 
    383  
    384                         if((*model_ref)->my_rarx!=NULL) //vklada normalizacni faktor do cur_res_lognc 
    385                         { 
    386                                 cout << "Maxlik vertex:" << (*model_ref)->my_rarx->posterior->minimal_vertex->get_coordinates() << endl; 
    387                                 cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_rarx->posterior->_ll());                                 
    388                         } 
    389                         else 
    390                         { 
    391                                 cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_arx->_ll()); 
    392                         } 
    393                          
    394                         if(time == max_window_size-1) 
    395                         { 
    396                                 //*********************** 
    397                                 int sample_size = 100000; 
    398                                 //*********************** 
    399                                  
    400                                 pair<vec,mat> samples; 
    401                                 if((*model_ref)->my_arx!=NULL) 
     302 
     303        while(data_matrix.rows()!=0) 
     304        { 
     305                for(int i=0;i<models.size();i++) 
     306                { 
     307                        delete models[i]; 
     308                } 
     309                 
     310                models.clear(); 
     311                models.push_back(new model(model_type,true,false,max_window_size,0,&data_matrix)); 
     312                models.push_back(new model(model_type,false,false,max_window_size,0,&data_matrix)); 
     313 
     314                for(int time = max_model_order;time<max_window_size;time++) //time<data_matrix.cols()  
     315                {                
     316                        vec cur_res_lognc; 
     317                 
     318                        vector<string> nazvy; 
     319                        for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) 
     320                        { 
     321                                (*model_ref)->data_update(time); 
     322 
     323                                cout << "Updated:" << time << endl;      
     324 
     325                                if(time == max_window_size-1) 
    402326                                { 
    403                                         mat samp_mat = (*model_ref)->my_arx->posterior().sample_mat(sample_size); 
    404                                         samples = pair<vec,mat>(ones(samp_mat.cols()),samp_mat); 
     327                                        char fstring[80];                                        
     328                                        strcpy(fstring,file_string); 
     329                                        strcat(fstring,"ml");                                    
     330                                        strcat(fstring,(*model_ref)->name); 
     331                                        strcat(fstring,".txt"); 
     332 
     333                                        vec coords; 
     334                                        if((*model_ref)->my_arx!=NULL) 
     335                                        { 
     336                                                coords = (*model_ref)->my_arx->posterior().est_theta(); 
     337                                        } 
     338                                        else 
     339                                        { 
     340                                                coords = (*model_ref)->my_rarx->posterior->minimal_vertex->get_coordinates();                                            
     341                                        } 
     342 
     343                                        myfilew.open(fstring,ios::app); 
     344 
     345                                        for(int i=0;i<coords.size();i++) 
     346                                        { 
     347                                                myfilew << coords.get(i) << ","; 
     348                                        } 
     349                                        myfilew << endl; 
     350 
     351                                        myfilew.close();                                 
    405352                                } 
    406                                 else 
    407                                 { 
    408                                         samples = (*model_ref)->my_rarx->posterior->sample(sample_size,true);                                    
    409                                 } 
    410  
    411                                 char fstring[80];                                        
    412                                 strcpy(fstring,file_string); 
    413                                 strcat(fstring,(*model_ref)->name); 
    414                                 strcat(fstring,".txt"); 
    415                                  
    416                                 //cout << samples.first << endl; 
    417                                  
    418                                 myfilew.open(fstring,ios::app); 
    419                                  
    420                                 /* 
    421                                 for(int i = 0;i<samples.first.size();i++) 
    422                                 { 
    423                                         myfilew << samples.first.get(i) << ","; 
    424                                 } 
    425                                 myfilew << endl; 
    426                                 */ 
    427  
    428                                 for(int j = 0;j<samples.second.rows()+1;j++) 
    429                                 {                        
    430                                         for(int i = 0;i<samples.second.cols();i++) 
    431                                         { 
    432                                                 if(j!=samples.second.rows()) 
    433                                                 { 
    434                                                         myfilew << samples.second.get(j,i) << ","; 
    435                                                 } 
    436                                                 /* 
    437                                                 else 
    438                                                 { 
    439                                                         myfilew << "0,"; 
    440                                                 } 
    441                                                 */ 
    442                                         }                                
    443                                         myfilew << endl; 
    444                                 } 
    445  
    446                                 cout << "*************************************" << endl; 
    447  
    448                                 myfilew.close();                                 
    449                         } 
    450                          
    451                         /* // PREDICTIONS 
    452                         pair<vec,vec> predictions = (*model_ref)->predict(500,time,&LapRNG); 
    453  
    454                         cout << predictions.first << endl << predictions.second << endl; 
    455  
    456                         double avg_prediction = (predictions.first*predictions.second)/(predictions.first*ones(predictions.first.size())); 
    457  
    458                         (*model_ref)->predictions.ins((*model_ref)->predictions.size(),avg_prediction); 
    459                                          
    460                         myfilew.open(fstring,ios::app); 
    461                         myfilew << avg_prediction << ",";  
    462                         myfilew.close(); 
    463                         */ 
    464  
    465                         //preds.ins(0,data_matrix.get(0,time+1)); 
    466                 } 
    467  
    468                  
    469                 /* // REAL PRICE 
    470                 myfilew.open(fstring,ios::app); 
    471                 myfilew << data_matrix.get(0,time+1) << endl; 
    472                 myfilew.close(); 
    473                 */ 
    474  
    475                 result_lognc.ins_col(result_lognc.cols(),cur_res_lognc); 
    476                 // result_preds.ins_col(result_preds.cols(),preds); 
    477  
    478                 // cout << "Updated." << endl;   
    479                                                          
    480                 /* 
    481                 myfilew.open(fstring,ios::app); 
    482                  
    483                 // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; 
    484                  
    485                 if(time == max_model_order) 
    486                 {  
    487                         for(int i = 0;i<cur_res_lognc.size();i++) 
    488                         { 
    489                                 for(set<pair<int,int>>::iterator ar_ref = models[i]->ar_components.begin();ar_ref != models[i]->ar_components.end();ar_ref++) 
    490                                 { 
    491                                         myfilew << (*ar_ref).second << (*ar_ref).first;                                                  
    492                                 } 
    493  
    494                                 myfilew << "."; 
    495  
    496                                 if(models[i]->my_arx == NULL) 
    497                                 { 
    498                                         myfilew << "1"; 
    499                                 } 
    500                                 else 
    501                                 { 
    502                                         myfilew << "0"; 
    503                                 } 
    504                                          
    505                                 if(models[i]->has_constant) 
    506                                 { 
    507                                         myfilew << "1"; 
    508                                 } 
    509                                 else 
    510                                 { 
    511                                         myfilew << "0"; 
    512                                 } 
    513  
    514                                 myfilew << ","; 
    515                         } 
    516  
    517                         myfilew << endl; 
    518                 } 
    519                  
    520                  
    521                 // for(int i = 0;i<cur_res_lognc.size();i++) 
    522                 // { 
    523                 //         myfilew << cur_res_lognc[i] << ' ';//zmenil som ciarku ze medzeru 
    524                 // } 
    525                  
    526  
    527                 myfilew << endl;                                 
    528                  
    529                 myfilew.close(); 
    530                 */ 
    531                  
    532 } 
    533          
    534  
    535  
    536         // EXPERIMENT: One step ahead price prediction. Comparison of classical and robust model using optimal trading 
    537     //             with maximization of logarithm of one-step ahead wealth. 
    538  
    539          
    540                  
    541                 /* 
    542                 cout << "One experiment finished." << endl; 
    543  
    544                 ofstream myfile; 
    545                 myfile.open("c:\\robust_ar1.txt",ios::app); 
    546                 myfile << endl; 
    547                 myfile.close(); 
    548  
    549                 myfile.open("c:\\robust_ar2.txt",ios::app); 
    550                 myfile << endl; 
    551                 myfile.close();*/ 
    552          
    553  
    554         //emlig* emlig1 = new emlig(emlig_size); 
    555  
    556         //emlig1->step_me(0); 
    557         //emlig* emlig2 = new emlig(emlig_size); 
    558          
    559         /* 
    560         emlig1->set_correction_factors(4); 
    561  
    562         for(int j = 0;j<emlig1->correction_factors.size();j++) 
    563         { 
    564                 for(set<my_ivec>::iterator vec_ref = emlig1->correction_factors[j].begin();vec_ref!=emlig1->correction_factors[j].end();vec_ref++) 
    565                 { 
    566                         cout << j << "    "; 
    567                          
    568                         for(int i=0;i<(*vec_ref).size();i++) 
    569                         { 
    570                                 cout << (*vec_ref)[i]; 
    571                         } 
    572  
    573                         cout << endl; 
    574                 } 
    575         }*/ 
    576          
    577         /* 
    578     vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5"; 
    579  
    580         emlig1->add_condition(condition5); 
    581         //emlig1->step_me(0); 
    582  
    583  
    584         vec condition1a = "-1.0 1.02 0.5"; 
    585         //vec condition1b = "1.0 1.0 1.01"; 
    586         emlig1->add_condition(condition1a); 
    587         //emlig2->add_condition(condition1b); 
    588  
    589         vec condition2a = "-0.3 1.7 1.5"; 
    590         //vec condition2b = "-1.0 1.0 1.0"; 
    591         emlig1->add_condition(condition2a); 
    592         //emlig2->add_condition(condition2b); 
    593  
    594         vec condition3a = "0.5 -1.01 1.0"; 
    595         //vec condition3b = "0.5 -1.01 1.0"; 
    596  
    597         emlig1->add_condition(condition3a); 
    598         //emlig2->add_condition(condition3b);    
    599  
    600         vec condition4a = "-0.5 -1.0 1.0"; 
    601         //vec condition4b = "-0.5 -1.0 1.0";     
    602  
    603         emlig1->add_condition(condition4a); 
    604         //cout << "************************************************" << endl; 
    605         //emlig2->add_condition(condition4b); 
    606         //cout << "************************************************" << endl; 
    607          
    608         //cout << emlig1->minimal_vertex->get_coordinates(); 
    609          
    610         //emlig1->remove_condition(condition3a); 
    611         //emlig1->step_me(0); 
    612         //emlig1->remove_condition(condition2a); 
    613         //emlig1->remove_condition(condition1a); 
    614         //emlig1->remove_condition(condition5); 
    615          
    616  
    617         //emlig1->step_me(0); 
    618         //emlig2->step_me(0); 
    619          
    620  
    621         // DA SE POUZIT PRO VYPIS DO SOUBORU 
    622         // emlig1->step_me(0); 
    623  
    624         //emlig1->remove_condition(condition1); 
    625          
    626          
    627  
    628          
    629          
    630         /* 
    631         for(int i = 0;i<100;i++) 
    632         { 
    633                 cout << endl << "Step:" << i << endl;            
    634  
    635                 double condition[emlig_size+1];          
    636  
    637                 for(int k = 0;k<=emlig_size;k++) 
    638                 { 
    639                         condition[k] = (rand()-RAND_MAX/2)/1000.0;               
    640                 } 
    641                          
    642  
    643                 vec* condition_vec = new vec(condition,emlig_size+1); 
    644                 emlig1->add_condition(*condition_vec); 
    645  
    646                 /* 
    647                 for(polyhedron* toprow_ref = emlig1->statistic.rows[emlig_size]; toprow_ref != emlig1->statistic.end_poly; toprow_ref = toprow_ref->next_poly) 
    648                 { 
    649                         cout << ((toprow*)toprow_ref)->probability << endl; 
    650                 } 
    651                 */ 
    652                 /* 
    653                 cout << emlig1->statistic_rowsize(emlig_size) << endl << endl; 
    654          
    655                 /* 
    656                 if(i-emlig1->number_of_parameters >= 0) 
    657                 { 
    658                         pause(30); 
    659                 } 
    660                 */ 
    661  
    662                 // emlig1->step_me(i); 
    663                  
    664                 /* 
    665                 vector<int> sizevector; 
    666                 for(int s = 0;s<=emlig1->number_of_parameters;s++) 
    667                 { 
    668                         sizevector.push_back(emlig1->statistic_rowsize(s)); 
    669                 } 
    670                 */ 
    671         //} 
    672      
    673  
    674  
    675          
    676         /* 
    677         emlig1->step_me(1); 
    678  
    679         vec condition = "2.0 0.0 1.0";   
    680  
    681         emlig1->add_condition(condition); 
    682  
    683         vector<int> sizevector; 
    684         for(int s = 0;s<=emlig1->number_of_parameters;s++) 
    685         { 
    686                 sizevector.push_back(emlig1->statistic_rowsize(s)); 
    687         } 
    688  
    689         emlig1->step_me(2); 
    690  
    691         condition = "2.0 1.0 0.0"; 
    692  
    693         emlig1->add_condition(condition); 
    694  
    695         sizevector.clear(); 
    696         for(int s = 0;s<=emlig1->number_of_parameters;s++) 
    697         { 
    698                 sizevector.push_back(emlig1->statistic_rowsize(s)); 
    699         } 
    700         */ 
     353                        } 
     354                } 
     355 
     356                data_matrix.del_row(0); 
     357        } 
     358         
     359 
     360 
     361         
    701362 
    702363        return 0; 
  • applications/robust/robustlib.cpp

    r1396 r1401  
    258258                        simplex->probability = int_value; 
    259259                        simplex->volume = volume; 
    260                         cout << "Probability:" << int_value << endl << endl; 
     260                        //cout << "Probability:" << int_value << endl << endl; 
    261261                        //pause(0.1); 
    262262                        return int_value;