Changeset 1367 for applications/robust/main.cpp
- Timestamp:
- 05/11/11 18:48:05 (13 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/robust/main.cpp
r1366 r1367 30 30 31 31 32 32 /* 33 33 HDDEDATA CALLBACK DdeCallback( 34 34 UINT uType, // Transaction type. … … 60 60 } 61 61 62 DWORD WINAPI ThrdFunc( LPVOID n ) 63 { 64 return 0; 65 } 66 */ 67 62 68 class model 63 69 { 64 65 66 70 public: 67 71 list<pair<int,int>> ar_components; … … 111 115 if(has_constant) 112 116 { 113 V0 = 0.0 1 * eye(ar_components.size()+2);114 V0(0,0) = 1;117 V0 = 0.001 * eye(ar_components.size()+2); 118 V0(0,0) = 0; 115 119 my_arx->set_constant(true); 116 120 … … 119 123 { 120 124 121 V0 = 0.0 1 * eye(ar_components.size()+1);122 V0(0,0) = 1;125 V0 = 0.001 * eye(ar_components.size()+1); 126 V0(0,0) = 0; 123 127 my_arx->set_constant(false); 124 128 … … 151 155 } 152 156 } 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 153 213 154 214 static list<list<pair<int,int>>> possible_models_recurse(int max_order,int number_of_channels) … … 201 261 202 262 203 int main ( int argc, char* argv[] ) { 204 205 itpp::Laplace_RNG LapRNG = Laplace_RNG(); 263 int main ( int argc, char* argv[] ) { 206 264 207 265 /* 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 208 277 char szApp[] = "MT4"; 209 char szTopic[] = " BID";210 char szItem1[] = "EUR JPY";278 char szTopic[] = "QUOTE"; 279 char szItem1[] = "EURUSD"; 211 280 212 281 //DDE Initialization … … 222 291 } 223 292 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 293 //DDE Connect to Server using given AppName and topic. 238 294 HSZ hszApp, hszTopic; … … 241 297 hszTopic = DdeCreateStringHandle(idInst, szTopic, 0); 242 298 hConv = DdeConnect(idInst, hszApp, hszTopic, NULL); 243 DdeFreeStringHandle(idInst, hszApp);244 DdeFreeStringHandle(idInst, hszTopic);299 //DdeFreeStringHandle(idInst, hszApp); 300 //DdeFreeStringHandle(idInst, hszTopic); 245 301 if (hConv == NULL) 246 302 { … … 252 308 //Execute commands/requests specific to the DDE Server. 253 309 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 256 324 //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 262 329 263 330 … … 366 433 myfile << endl; 367 434 myfile.close(); 368 }*/ 369 370 435 }*/ 371 436 372 437 // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from … … 374 439 // can be compared to the classical setup. 375 440 376 377 441 itpp::Laplace_RNG LapRNG = Laplace_RNG(); 442 378 443 vector<vector<string>> strings; 379 444 380 char* file_string = "c:\\data CDClosePercDiff";445 char* file_string = "c:\\dataADClosePercDiff"; 381 446 382 447 char dfstring[80]; … … 417 482 for(list<list<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++) 418 483 { 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 } 423 491 } 424 492 425 493 mat result_lognc; 494 mat result_preds; 426 495 427 496 for(int time = max_model_order;time<data_matrix.cols();time++) //time<data_matrix.cols() 428 497 { 429 498 vec cur_res_lognc; 499 vec preds; 430 500 431 501 for(list<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) 432 502 { 433 (*model_ref)->data_update(time); 503 (*model_ref)->data_update(time); 504 434 505 if((*model_ref)->my_rarx!=NULL) 435 506 { … … 440 511 cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_arx->posterior().lognc()); 441 512 } 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)); 443 520 444 521 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; 447 525 448 526 /* … … 661 739 strcpy(fstring,file_string); 662 740 663 strcat(fstring," lognc.txt");741 strcat(fstring,"preds.txt"); 664 742 665 743 myfile.open(fstring,ios::app); … … 667 745 // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; 668 746 669 for(int i = 0;i< cur_res_lognc.size();i++)747 for(int i = 0;i<preds.size();i++) 670 748 { 671 myfile << cur_res_lognc[i] << ',';749 myfile << preds[i] << ','; 672 750 } 673 751