Show
Ignore:
Timestamp:
05/11/11 18:48:05 (13 years ago)
Author:
sindj
Message:

Nova verze main s predikcemi i lognorm faktory. Pristup na DDE data. JS

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/main.cpp

    r1366 r1367  
    3030 
    3131 
    32  
     32/* 
    3333HDDEDATA CALLBACK DdeCallback( 
    3434        UINT uType,     // Transaction type. 
     
    6060} 
    6161 
     62DWORD WINAPI ThrdFunc( LPVOID n ) 
     63{     
     64    return 0; 
     65} 
     66*/ 
     67 
    6268class model 
    6369{ 
    64          
    65  
    6670public: 
    6771        list<pair<int,int>> ar_components; 
     
    111115                        if(has_constant) 
    112116                        {                                
    113                                 V0  = 0.01 * eye(ar_components.size()+2); 
    114                                 V0(0,0) = 1; 
     117                                V0  = 0.001 * eye(ar_components.size()+2); 
     118                                V0(0,0) = 0; 
    115119                                my_arx->set_constant(true);      
    116120                                 
     
    119123                        { 
    120124                                 
    121                                 V0  = 0.01 * eye(ar_components.size()+1); 
    122                                 V0(0,0) = 1; 
     125                                V0  = 0.001 * eye(ar_components.size()+1); 
     126                                V0(0,0) = 0; 
    123127                                my_arx->set_constant(false);                             
    124128                                 
     
    151155                } 
    152156        } 
     157 
     158        pair<vec,vec> predict(int sample_size, int time, itpp::Laplace_RNG* LapRNG) 
     159        { 
     160                vec condition_vector; 
     161                for(list<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++) 
     162                { 
     163                        condition_vector.ins(condition_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second+1)); 
     164                } 
     165 
     166                if(my_rarx!=NULL) 
     167                { 
     168                        pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(sample_size); 
     169 
     170                        //cout << imp_samples.first << endl; 
     171                         
     172                        vec sample_prediction;                   
     173                        for(int t = 0;t<sample_size;t++) 
     174                        { 
     175                                vec lap_sample = condition_vector; 
     176                                 
     177                                if(has_constant) 
     178                                { 
     179                                        lap_sample.ins(lap_sample.size(),1.0); 
     180                                } 
     181                                 
     182                                lap_sample.ins(0,(*LapRNG)()); 
     183 
     184                                sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t));                               
     185                        } 
     186 
     187                        return pair<vec,vec>(imp_samples.first,sample_prediction); 
     188                } 
     189                else 
     190                { 
     191                        mat samples = my_arx->posterior().sample_mat(sample_size); 
     192                         
     193                        vec sample_prediction; 
     194                        for(int t = 0;t<sample_size;t++) 
     195                        { 
     196                                vec gau_sample = condition_vector; 
     197                                 
     198                                if(has_constant) 
     199                                { 
     200                                        gau_sample.ins(gau_sample.size(),1.0); 
     201                                } 
     202                                 
     203                                gau_sample.ins(0,randn()); 
     204 
     205                                sample_prediction.ins(0,gau_sample*samples.get_col(t));                          
     206                        } 
     207 
     208                        return pair<vec,vec>(ones(sample_prediction.size()),sample_prediction); 
     209                } 
     210         
     211        } 
     212 
    153213 
    154214        static list<list<pair<int,int>>> possible_models_recurse(int max_order,int number_of_channels) 
     
    201261 
    202262 
    203 int main ( int argc, char* argv[] ) { 
    204          
    205         itpp::Laplace_RNG LapRNG = Laplace_RNG();        
     263int main ( int argc, char* argv[] ) {    
    206264         
    207265        /* 
     266        DWORD Id; 
     267        HANDLE hThrd = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ThrdFunc, (LPVOID)1, 0, &Id); 
     268   
     269        if ( !hThrd ) 
     270    { 
     271        cout<<"Error Creating Threads,,,,.exiting"<<endl; 
     272        return -1; 
     273    } 
     274    Sleep ( 100 ); 
     275 
     276         
    208277        char szApp[] = "MT4"; 
    209         char szTopic[] = "BID";  
    210         char szItem1[] = "EURJPY";       
     278        char szTopic[] = "QUOTE";        
     279        char szItem1[] = "EURUSD";       
    211280 
    212281        //DDE Initialization 
     
    222291        } 
    223292 
    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         /* 
    237293        //DDE Connect to Server using given AppName and topic. 
    238294        HSZ hszApp, hszTopic; 
     
    241297        hszTopic = DdeCreateStringHandle(idInst, szTopic, 0); 
    242298        hConv = DdeConnect(idInst, hszApp, hszTopic, NULL); 
    243         DdeFreeStringHandle(idInst, hszApp); 
    244         DdeFreeStringHandle(idInst, hszTopic); 
     299        //DdeFreeStringHandle(idInst, hszApp); 
     300        //DdeFreeStringHandle(idInst, hszTopic); 
    245301        if (hConv == NULL) 
    246302        { 
     
    252308        //Execute commands/requests specific to the DDE Server. 
    253309         
    254         DDERequest(idInst, hConv, szItem1);      
    255          
     310        DDERequest(idInst, hConv, szItem1);              
     311         
     312        while(1) 
     313        { 
     314                MSG    msg; 
     315                BOOL   MsgReturn = GetMessage ( &msg , NULL , 0 , 0 ); 
     316             
     317                if(MsgReturn) 
     318                { 
     319                        TranslateMessage(&msg); 
     320                        DispatchMessage(&msg);                   
     321                } 
     322        } 
     323 
    256324        //DDE Disconnect and Uninitialize. 
    257         //DdeDisconnect(hConv); 
    258         //DdeUninitialize(idInst); 
    259  
    260         Sleep(300000); 
    261         Sleep(3000); 
     325        DdeDisconnect(hConv); 
     326        DdeUninitialize(idInst); 
     327        */ 
     328 
    262329         
    263330 
     
    366433                myfile << endl; 
    367434                myfile.close(); 
    368         }*/ 
    369      
    370  
     435        }*/     
    371436         
    372437        // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from  
     
    374439    // can be compared to the classical setup. 
    375440         
    376  
    377          
     441        itpp::Laplace_RNG LapRNG = Laplace_RNG();        
     442 
    378443        vector<vector<string>> strings; 
    379444 
    380         char* file_string = "c:\\dataCDClosePercDiff";  
     445        char* file_string = "c:\\dataADClosePercDiff";  
    381446 
    382447        char dfstring[80]; 
     
    417482        for(list<list<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++) 
    418483        { 
    419                 models.push_back(new model((*model_type),true,true,100,0,&data_matrix)); 
    420                 models.push_back(new model((*model_type),false,true,100,0,&data_matrix)); 
    421                 models.push_back(new model((*model_type),true,false,100,0,&data_matrix)); 
    422                 models.push_back(new model((*model_type),false,false,100,0,&data_matrix)); 
     484                for(int window_size = 30;window_size < 31;window_size++) 
     485                { 
     486                        models.push_back(new model((*model_type),true,true,window_size,0,&data_matrix)); 
     487                        models.push_back(new model((*model_type),false,true,window_size,0,&data_matrix)); 
     488                        models.push_back(new model((*model_type),true,false,window_size,0,&data_matrix)); 
     489                        models.push_back(new model((*model_type),false,false,window_size,0,&data_matrix)); 
     490                } 
    423491        } 
    424492         
    425493        mat result_lognc; 
     494        mat result_preds; 
    426495 
    427496        for(int time = max_model_order;time<data_matrix.cols();time++) //time<data_matrix.cols() 
    428497        {        
    429498                vec cur_res_lognc; 
     499                vec preds; 
    430500 
    431501                for(list<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) 
    432502                { 
    433                         (*model_ref)->data_update(time); 
     503                        (*model_ref)->data_update(time);                         
     504 
    434505                        if((*model_ref)->my_rarx!=NULL) 
    435506                        { 
     
    440511                                cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_arx->posterior().lognc()); 
    441512                        } 
    442                 } 
     513 
     514                        pair<vec,vec> predictions = (*model_ref)->predict(500,time,&LapRNG); 
     515 
     516                        preds.ins(preds.size(),(predictions.first*predictions.second)/(predictions.first*ones(predictions.first.size()))); 
     517                } 
     518 
     519                preds.ins(0,data_matrix.get(0,time+1)); 
    443520 
    444521                result_lognc.ins_col(result_lognc.cols(),cur_res_lognc); 
    445  
    446                 cout << "Updated." << endl; 
     522                result_preds.ins_col(result_preds.cols(),preds); 
     523 
     524                // cout << "Updated." << endl; 
    447525         
    448526                /* 
     
    661739                                        strcpy(fstring,file_string); 
    662740                                         
    663                                         strcat(fstring,"lognc.txt");                                     
     741                                        strcat(fstring,"preds.txt");                                     
    664742 
    665743                                        myfile.open(fstring,ios::app); 
     
    667745                                        // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; 
    668746                                         
    669                                         for(int i = 0;i<cur_res_lognc.size();i++) 
     747                                        for(int i = 0;i<preds.size();i++) 
    670748                                        { 
    671                                                 myfile << cur_res_lognc[i] << ','; 
     749                                                myfile << preds[i] << ','; 
    672750                                        } 
    673751