Changeset 1367 for applications/robust
- Timestamp:
- 05/11/11 18:48:05 (14 years ago)
- Location:
- applications/robust
- Files:
-
- 3 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 -
applications/robust/robustlib.cpp
r1366 r1367 192 192 } 193 193 194 double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition_sum[0];194 double a_m = -(*vert_ref)->get_coordinates()*cur_condition+as_toprow->condition_sum[0]; 195 195 if(a_m<current_emlig->min_ll) 196 196 { -
applications/robust/robustlib.h
r1366 r1367 1112 1112 void step_me(int marker) 1113 1113 { 1114 1114 set<int> orders; 1115 1115 1116 for(int i = 0;i<statistic.size();i++) 1116 1117 { … … 1125 1126 if(i==statistic.size()-1) 1126 1127 { 1128 orders.insert(((toprow*)horiz_ref)->condition_order); 1129 1130 /* 1127 1131 cout << ((toprow*)horiz_ref)->condition_sum << " " << ((toprow*)horiz_ref)->probability << endl; 1128 1132 cout << "Condition: " << ((toprow*)horiz_ref)->condition_sum << endl; 1129 cout << "Order:" << ((toprow*)horiz_ref)->condition_order << endl; 1133 cout << "Order:" << ((toprow*)horiz_ref)->condition_order << endl;*/ 1130 1134 } 1131 1135 … … 1142 1146 } 1143 1147 1148 /* 1144 1149 for(set<simplex*>::iterator sim_ref = (*horiz_ref).triangulation.begin();sim_ref!=(*horiz_ref).triangulation.end();sim_ref++) 1145 1150 { … … 1149 1154 } 1150 1155 } 1156 */ 1151 1157 1152 1158 /* … … 1437 1443 1438 1444 for(vector<list<polyhedron*>>::iterator vert_ref = for_merging.begin();vert_ref<for_merging.end();vert_ref++) 1439 { 1440 for(list<polyhedron*>:: reverse_iterator merge_ref = vert_ref->rbegin();merge_ref!=vert_ref->rend();merge_ref++)1445 { 1446 for(list<polyhedron*>::iterator merge_ref = (*vert_ref).begin();merge_ref!=(*vert_ref).end();merge_ref++) 1441 1447 { 1442 1448 if((*merge_ref)->get_multiplicity()>1) … … 1649 1655 } 1650 1656 1651 statistic.delete_polyhedron(k-1,*merge_ref); 1652 1653 1657 statistic.delete_polyhedron(k-1,*merge_ref); 1658 1659 for_splitting[k-1].remove(*merge_ref); 1660 //for_merging[k].remove(*loc_merge_ref); 1654 1661 1655 1662 if(k==1) 1656 1663 { 1657 vertices_to_be_reduced.insert((vertex*)(*merge_ref)); 1658 for_splitting[k-1].remove(*merge_ref); 1664 vertices_to_be_reduced.insert((vertex*)(*merge_ref)); 1659 1665 } 1666 /* 1660 1667 else 1661 { 1662 delete (*merge_ref); 1663 for_splitting[k-1].remove(*merge_ref); 1668 { 1669 delete (*loc_merge_ref); 1664 1670 } 1671 */ 1665 1672 } 1666 1673 } … … 1881 1888 1882 1889 log_nc = log(normalization_factor) + logfact(condition_order-number_of_parameters-2)-(condition_order-number_of_parameters-2)*log(2.0); 1890 1891 if(condition_order == 20) 1892 step_me(88); 1893 1894 //cout << "Factorial factor: " << condition_order-number_of_parameters-2 << endl; 1883 1895 1884 1896 /* … … 2046 2058 for(multimap<double,double>::iterator pg_ref = (*v_ref).begin();pg_ref!=(*v_ref).end();pg_ref++) 2047 2059 { 2048 pg_sum += exp((sampled_simplex->min_beta-(*pg_ref).second)/sigma)*pow((*pg_ref).second/sigma,(int)conditions.size() -number_of_parameters-1)*(*pg_ref).second/fact(conditions.size()-number_of_parameters-1)*(*pg_ref).first;2060 pg_sum += exp((sampled_simplex->min_beta-(*pg_ref).second)/sigma)*pow((*pg_ref).second/sigma,(int)conditions.size())*(*pg_ref).second/fact(conditions.size())*(*pg_ref).first; 2049 2061 } 2050 2062 } … … 2055 2067 for(multimap<double,double>::iterator ng_ref = (*v_ref).begin();ng_ref!=(*v_ref).end();ng_ref++) 2056 2068 { 2057 ng_sum += exp((sampled_simplex->min_beta-(*ng_ref).second)/sigma)*pow((*ng_ref).second/sigma,(int)conditions.size() -number_of_parameters-1)*(*ng_ref).second/fact(conditions.size()-number_of_parameters-1)*(*ng_ref).first;2069 ng_sum += exp((sampled_simplex->min_beta-(*ng_ref).second)/sigma)*pow((*ng_ref).second/sigma,(int)conditions.size())*(*ng_ref).second/fact(conditions.size())*(*ng_ref).first; 2058 2070 } 2059 2071 } … … 2788 2800 this->has_constant = has_constant; 2789 2801 2790 posterior = new emlig(number_of_parameters,0.0 1);2802 posterior = new emlig(number_of_parameters,0.001); 2791 2803 2792 2804 this->window_size = window_size;