Changeset 1361

Show
Ignore:
Timestamp:
05/09/11 00:09:35 (13 years ago)
Author:
sindj
Message:

Do main.cpp pridelano automaticke odhadovani struktury modelu a automaticke generovani pripustnych modelu. JS

Location:
applications/robust
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/CMakeLists.txt

    r1358 r1361  
    1515 
    1616add_library(robustlib robustlib.cpp robustlib.h) 
    17 add_library(DDEClient ddeclient.cpp ddeclient.h dcerrors.h) 
     17add_library(DDEClient SHARED ddeclient.cpp ddeclient.h dcerrors.h) 
    1818 
    1919EXEC(main robustlib DDEClient) 
  • applications/robust/DDEClient.h

    r1358 r1361  
    55// Date:    04.10.2006 
    66// 
     7#include "windows.h" 
    78 
    89// DDE data structure for access data received by DDE transaction 
  • applications/robust/main.cpp

    r1358 r1361  
    1313#include <fstream> 
    1414#include <itpp/itsignal.h> 
    15 #include <windows.h> 
    16 #include "DDEClient.h" 
    17 #include <conio.h> 
     15#include "windows.h" 
     16#include "ddeml.h" 
     17#include "stdio.h" 
     18 
     19//#include "DDEClient.h" 
     20//#include <conio.h> 
    1821 
    1922 
     
    2124using namespace bdm; 
    2225 
    23 const int emlig_size = 2; 
    24 const int utility_constant = 5; 
     26//const int emlig_size = 2; 
     27//const int utility_constant = 5; 
     28 
     29const int max_model_order = 2; 
     30 
     31 
     32 
     33HDDEDATA 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 
     46void 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 
     62class model 
     63{ 
     64         
     65 
     66public: 
     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.0001 * eye(ar_components.size()+2); 
     114                                //V0(0,0) = 0.1; 
     115                                my_arx->set_constant(true);      
     116                                 
     117                        } 
     118                        else 
     119                        { 
     120                                 
     121                                V0  = 0.0001 * eye(ar_components.size()+1); 
     122                                //V0(0,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 
    25201 
    26202 
    27203int main ( int argc, char* argv[] ) { 
    28204         
    29         itpp::Laplace_RNG LapRNG = Laplace_RNG(); 
    30  
    31         WORD wConvNo; 
    32         char szType[] = "request"; 
    33         char szData[21];  
    34         char szItem[] = "EURUSD"; 
    35         char szService[] = "MT4"; 
    36         char szTopic[] = "BID"; 
    37         char szFormat[] = "CF_TEXT"; 
    38         DWORD dwTimeout = 0; 
    39         //char szAccess[] = "string"; 
    40  
    41         if(!DCInit())  
    42         { 
    43                 cout << "DDE doesn't work." << endl; 
    44         } 
    45         else 
    46         { 
    47                 // The following if-block shows a complete conversation with a 
    48                 // single transaction. You therefore do not need to free any memory 
    49                 // explicitly. 
    50  
    51                 // connect to server 
    52                 if (!DCConnect(&wConvNo,szService, szTopic))  
    53                 { 
    54                         cout << "Couldn't connect DDE." << endl; 
    55                 } 
    56                 else 
    57                 { 
    58                         // do synchronous request transaction, wait max. 1000 ms,  
    59                         // return data as string 
    60                         if(!DCRequestString(wConvNo,szItem,100000)) 
    61                         { 
    62                                 cout << "No data available." << endl; 
    63                         } 
    64                         else 
    65                         { 
    66                                 if(!DCAsynchTransactionCompleted(wConvNo,DCDA[wConvNo]->dwTransID,true)) 
    67                                 { 
    68                                         cout << "Asynchronous transaction error." << endl; 
    69                                 } 
    70                                 else 
    71                                 { 
    72                                 // output data to console if transaction complete 
    73                                 // DCDA[wConvNo]->pszData is the pointer to the data string 
    74                                         cprintf(DCDA[wConvNo]->pszData); 
    75                                         DCFreeDdeMem(wConvNo); 
    76                                 } 
    77                         } 
    78                         // end conversation 
    79                         if(!DCDisconnect(wConvNo)) 
    80                         { 
    81                                 cout << "Couldn't disconnect DDE." << endl; 
    82                         }  
    83                 } 
    84         } 
    85          
     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 
    86264        /* 
    87265        // 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,  
     
    190368        }*/ 
    191369     
     370 
    192371         
    193372        // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from  
     
    198377        vector<vector<string>> strings; 
    199378 
    200         char* file_strings[3] = {"c:\\dataADClosePercDiff","c:\\ar_student_single","c:\\ar_cauchy_single"}; 
    201  
    202         for(int i = 0;i<3;i++) 
    203         {                        
    204                 char dfstring[80]; 
    205                 strcpy(dfstring,file_strings[i]); 
    206                 strcat(dfstring,".txt"); 
     379        char* file_string = "c:\\data";  
     380 
     381        char dfstring[80]; 
     382        strcpy(dfstring,file_string); 
     383        strcat(dfstring,".txt"); 
    207384                 
    208                 ifstream myfile(dfstring); 
    209                 if (myfile.is_open()) 
     385         
     386        mat data_matrix; 
     387        ifstream myfile(dfstring); 
     388        if (myfile.is_open()) 
     389        {                
     390                string line; 
     391                while(getline(myfile,line)) 
    210392                {                        
    211                         string line; 
    212                         getline(myfile,line); 
    213                                  
    214                         vector<string> parsed_line; 
     393                        vec data_vector; 
    215394                        while(line.find(',') != string::npos) 
    216395                        { 
    217                                 int loc = line.find(','); 
    218                                 parsed_line.push_back(line.substr(0,loc)); 
     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()));                            
    219399                                line.erase(0,loc+1);                                     
    220                         }                                
    221  
    222                         strings.push_back(parsed_line); 
    223                          
    224                         myfile.close(); 
    225                 } 
    226         } 
    227  
    228          
    229          
    230         for(int j = 0;j<strings.size();j++) 
    231         {                
     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                /* 
    232445                vector<vec> conditions; 
    233446                //emlig* emliga = new emlig(2); 
     
    271484                                my_arx->bayes(cond_vec,conditions[k-3].right(2)); 
    272485                                         
    273                                  
     486                                /* 
    274487                                if(k>8) 
    275488                                { 
     
    360573                                        */ 
    361574                                         
    362  
     575                                    /* 
    363576 
    364577                                        cvec actions = roots(poly_coefs); 
     
    434647 
    435648                                        // cout << "Prediction: "<< prediction << endl; 
    436                                          
     649                                        /* 
    437650                                        enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2)); 
    438651                                        double prediction2 = pred_mat->mean()[0]; 
    439                                          
     652                                        */ 
    440653 
    441654                                        ofstream myfile; 
    442                                         char fstring[80]; 
    443                                         char f2string[80]; 
    444                                         strcpy(fstring,file_strings[j]); 
    445                                         strcpy(f2string,fstring); 
    446  
    447                                         strcat(fstring,"pred.txt"); 
    448                                         strcat(f2string,"2pred.txt"); 
    449                                          
     655                                        char fstring[80];                                        
     656                                        strcpy(fstring,file_string); 
     657                                         
     658                                        strcat(fstring,"lognc.txt");                                     
    450659 
    451660                                        myfile.open(fstring,ios::app); 
    452661                                         
    453662                                        // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; 
    454                                         myfile << prediction; 
     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; 
    455676                                         
    456677                                        if(k!=strings[j].size()-1) 
     
    463684                                        } 
    464685                                        myfile.close(); 
    465  
    466                                          
    467                                         myfile.open(f2string,ios::app); 
    468                                         myfile << prediction2; 
    469                                          
    470                                         if(k!=strings[j].size()-1) 
    471                                         { 
    472                                                 myfile << ","; 
    473                                         } 
    474                                         else 
    475                                         { 
    476                                                 myfile << endl; 
    477                                         } 
    478                                         myfile.close(); 
    479                                         //*/ 
     686                                        //*//* 
    480687 
    481688                                }                                        
    482                         }        
     689                        }       */ 
    483690                         
    484691                        //emliga->step_me(0); 
     
    496703                        cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; 
    497704                        cout << "Step: " << i << endl;*/ 
    498                 } 
     705                //} 
    499706 
    500707 
  • applications/robust/robustlib.h

    r1357 r1361  
    2222using namespace itpp; 
    2323 
    24 const double max_range = 5;//numeric_limits<double>::max()/10e-10; 
     24const double max_range = 20;//numeric_limits<double>::max()/10e-10; 
    2525 
    2626/// An enumeration of possible actions performed on the polyhedrons. We can merge them or split them.