| 1 | |
|---|
| 2 | /*! |
|---|
| 3 | \file |
|---|
| 4 | \brief Robust |
|---|
| 5 | \author Vasek Smidl |
|---|
| 6 | |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | #include "estim/arx.h" |
|---|
| 10 | #include "robustlib.h" |
|---|
| 11 | #include <vector> |
|---|
| 12 | #include <iostream> |
|---|
| 13 | #include <fstream> |
|---|
| 14 | //#include <itpp/itsignal.h> |
|---|
| 15 | #include "windows.h" |
|---|
| 16 | #include "ddeml.h" |
|---|
| 17 | #include "stdio.h" |
|---|
| 18 | #include <itpp/itoptim.h> |
|---|
| 19 | |
|---|
| 20 | //#include "DDEClient.h" |
|---|
| 21 | //#include <conio.h> |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | using namespace itpp; |
|---|
| 25 | using namespace bdm; |
|---|
| 26 | |
|---|
| 27 | //const int emlig_size = 2; |
|---|
| 28 | //const int utility_constant = 5; |
|---|
| 29 | |
|---|
| 30 | const int max_model_order = 2; |
|---|
| 31 | const double apriorno = 0.01; |
|---|
| 32 | const int max_window_size = 40; |
|---|
| 33 | const int utility_order = 19; |
|---|
| 34 | const int prediction_time = 30; |
|---|
| 35 | |
|---|
| 36 | /* |
|---|
| 37 | HDDEDATA CALLBACK DdeCallback( |
|---|
| 38 | UINT uType, // Transaction type. |
|---|
| 39 | UINT uFmt, // Clipboard data format. |
|---|
| 40 | HCONV hconv, // Handle to the conversation. |
|---|
| 41 | HSZ hsz1, // Handle to a string. |
|---|
| 42 | HSZ hsz2, // Handle to a string. |
|---|
| 43 | HDDEDATA hdata, // Handle to a global memory object. |
|---|
| 44 | DWORD dwData1, // Transaction-specific data. |
|---|
| 45 | DWORD dwData2) // Transaction-specific data. |
|---|
| 46 | { |
|---|
| 47 | return 0; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | void DDERequest(DWORD idInst, HCONV hConv, char* szItem) |
|---|
| 51 | { |
|---|
| 52 | HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0); |
|---|
| 53 | HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT, |
|---|
| 54 | XTYP_ADVSTART,TIMEOUT_ASYNC , NULL); //TIMEOUT_ASYNC |
|---|
| 55 | if (hData==NULL) |
|---|
| 56 | { |
|---|
| 57 | printf("Request failed: %s\n", szItem); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | if (hData==0) |
|---|
| 61 | { |
|---|
| 62 | printf("Request failed: %s\n", szItem); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | DWORD WINAPI ThrdFunc( LPVOID n ) |
|---|
| 67 | { |
|---|
| 68 | return 0; |
|---|
| 69 | } |
|---|
| 70 | */ |
|---|
| 71 | |
|---|
| 72 | double valueCRRAUtility(const double &position, const vec &samples, const int order) |
|---|
| 73 | { |
|---|
| 74 | double value = 0; |
|---|
| 75 | |
|---|
| 76 | for(int i=0;i<samples.length();i++) |
|---|
| 77 | { |
|---|
| 78 | double sample = samples.get(i); |
|---|
| 79 | value += sample/pow(position*sample+1,order+1); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | return value; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | double gradientCRRAUtility(const double &position, const vec &samples, const int order) |
|---|
| 86 | { |
|---|
| 87 | double value = 0; |
|---|
| 88 | |
|---|
| 89 | for(int i=0;i<samples.length();i++) |
|---|
| 90 | { |
|---|
| 91 | double sample = samples.get(i); |
|---|
| 92 | value += (-(order+1)*pow(sample,2))/pow(position*sample+1,order+2); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | return value; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | double newtonRaphson(double startingPoint, double epsilon, vec samples, int order) |
|---|
| 99 | { |
|---|
| 100 | if(samples.length()>800) |
|---|
| 101 | { |
|---|
| 102 | samples.del(801,samples.size()-1); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | int count = 0; |
|---|
| 106 | |
|---|
| 107 | bool epsilon_reached = false; |
|---|
| 108 | |
|---|
| 109 | while(count<1000&&!epsilon_reached) |
|---|
| 110 | { |
|---|
| 111 | double cur_value = valueCRRAUtility(startingPoint,samples,order); |
|---|
| 112 | double cur_gradient = gradientCRRAUtility(startingPoint,samples,order); |
|---|
| 113 | |
|---|
| 114 | startingPoint = startingPoint - cur_value/cur_gradient; |
|---|
| 115 | |
|---|
| 116 | if(cur_value<epsilon) |
|---|
| 117 | { |
|---|
| 118 | epsilon_reached = true; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | if(count==100) |
|---|
| 123 | { |
|---|
| 124 | return startingPoint; // can be different! |
|---|
| 125 | } |
|---|
| 126 | else |
|---|
| 127 | { |
|---|
| 128 | return startingPoint; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | class model |
|---|
| 139 | { |
|---|
| 140 | public: |
|---|
| 141 | set<pair<int,int>> ar_components; |
|---|
| 142 | |
|---|
| 143 | // Best thing would be to inherit the two models from a single souce, this is planned, but now structurally |
|---|
| 144 | // problematic. |
|---|
| 145 | RARX* my_rarx; //vzmenovane parametre pre triedu model |
|---|
| 146 | ARXwin* my_arx; |
|---|
| 147 | |
|---|
| 148 | bool has_constant; |
|---|
| 149 | int window_size; //musi byt vacsia ako pocet krokov ak to nema ovplyvnit |
|---|
| 150 | int predicted_channel; |
|---|
| 151 | mat* data_matrix; |
|---|
| 152 | vec predictions; |
|---|
| 153 | char name[80]; |
|---|
| 154 | |
|---|
| 155 | double previous_lognc; |
|---|
| 156 | |
|---|
| 157 | model(set<pair<int,int>> ar_components, //funkcie treidz model-konstruktor |
|---|
| 158 | bool robust, |
|---|
| 159 | bool has_constant, |
|---|
| 160 | int window_size, |
|---|
| 161 | int predicted_channel, |
|---|
| 162 | mat* data_matrix) |
|---|
| 163 | { |
|---|
| 164 | this->ar_components.insert(ar_components.begin(),ar_components.end()); |
|---|
| 165 | |
|---|
| 166 | strcpy(name,"M"); |
|---|
| 167 | |
|---|
| 168 | for(set<pair<int,int>>::iterator ar_ref = ar_components.begin();ar_ref!=ar_components.end();ar_ref++) |
|---|
| 169 | { |
|---|
| 170 | char buffer1[2]; |
|---|
| 171 | char buffer2[2]; |
|---|
| 172 | itoa((*ar_ref).first,buffer1,10); |
|---|
| 173 | itoa((*ar_ref).second,buffer2,10); |
|---|
| 174 | |
|---|
| 175 | strcat(name,buffer1); |
|---|
| 176 | strcat(name,buffer2); |
|---|
| 177 | strcat(name,"_"); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | this->has_constant = has_constant; |
|---|
| 181 | |
|---|
| 182 | if(has_constant) |
|---|
| 183 | { |
|---|
| 184 | strcat(name,"C"); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | this->window_size = window_size; |
|---|
| 188 | this->predicted_channel = predicted_channel; |
|---|
| 189 | this->data_matrix = data_matrix; |
|---|
| 190 | |
|---|
| 191 | if(robust) |
|---|
| 192 | { |
|---|
| 193 | previous_lognc = 0; |
|---|
| 194 | strcat(name,"R"); |
|---|
| 195 | |
|---|
| 196 | if(has_constant) |
|---|
| 197 | { |
|---|
| 198 | my_rarx = new RARX(ar_components.size()+1,window_size,true,sqrt(2*apriorno),sqrt(2*apriorno),ar_components.size()+3); |
|---|
| 199 | my_arx = NULL; |
|---|
| 200 | } |
|---|
| 201 | else |
|---|
| 202 | { |
|---|
| 203 | my_rarx = new RARX(ar_components.size(),window_size,false,sqrt(2*apriorno),sqrt(2*apriorno),ar_components.size()+2); |
|---|
| 204 | my_arx = NULL; |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | else |
|---|
| 208 | { |
|---|
| 209 | my_rarx = NULL; |
|---|
| 210 | my_arx = new ARXwin(); |
|---|
| 211 | mat V0; |
|---|
| 212 | |
|---|
| 213 | if(has_constant) |
|---|
| 214 | { |
|---|
| 215 | V0 = apriorno * eye(ar_components.size()+2); //aj tu konst |
|---|
| 216 | V0(0,0) = 0; |
|---|
| 217 | my_arx->set_constant(true); |
|---|
| 218 | my_arx->set_statistics(1, V0, V0.rows()+1); |
|---|
| 219 | } |
|---|
| 220 | else |
|---|
| 221 | { |
|---|
| 222 | |
|---|
| 223 | V0 = apriorno * eye(ar_components.size()+1);//menit konstantu |
|---|
| 224 | V0(0,0) = 0; |
|---|
| 225 | //V0(0,1) = -0.01; |
|---|
| 226 | //V0(1,0) = -0.01; |
|---|
| 227 | my_arx->set_constant(false); |
|---|
| 228 | my_arx->set_statistics(1, V0, V0.rows()+1); |
|---|
| 229 | |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | my_arx->set_parameters(window_size); |
|---|
| 233 | my_arx->validate(); |
|---|
| 234 | |
|---|
| 235 | previous_lognc = my_arx->posterior().lognc(); |
|---|
| 236 | |
|---|
| 237 | /* |
|---|
| 238 | vec mean = my_arx->posterior().mean(); |
|---|
| 239 | cout << mean << endl; |
|---|
| 240 | */ |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | void data_update(int time) |
|---|
| 245 | { |
|---|
| 246 | vec data_vector; |
|---|
| 247 | for(set<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++) |
|---|
| 248 | { |
|---|
| 249 | data_vector.ins(data_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second)); |
|---|
| 250 | } |
|---|
| 251 | if(my_rarx!=NULL) |
|---|
| 252 | { |
|---|
| 253 | data_vector.ins(0,(*data_matrix).get(predicted_channel,time)); |
|---|
| 254 | my_rarx->bayes(data_vector); |
|---|
| 255 | } |
|---|
| 256 | else |
|---|
| 257 | { |
|---|
| 258 | vec pred_vec; |
|---|
| 259 | pred_vec.ins(0,(*data_matrix).get(predicted_channel,time)); |
|---|
| 260 | my_arx->bayes(pred_vec,data_vector); |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | pair<vec,vec> predict(int sample_size, int time, itpp::Laplace_RNG* LapRNG) //nerozumiem, ale vraj to netreba, nepouziva to |
|---|
| 265 | { |
|---|
| 266 | vec condition_vector; |
|---|
| 267 | for(set<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++) |
|---|
| 268 | { |
|---|
| 269 | condition_vector.ins(condition_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second+1)); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | if(my_rarx!=NULL) |
|---|
| 273 | { |
|---|
| 274 | pair<vec,mat> imp_samples = my_rarx->posterior->sample(sample_size,true); |
|---|
| 275 | |
|---|
| 276 | //cout << imp_samples.first << endl; |
|---|
| 277 | |
|---|
| 278 | vec sample_prediction; |
|---|
| 279 | for(int t = 0;t<imp_samples.second.cols();t++) |
|---|
| 280 | { |
|---|
| 281 | vec lap_sample = condition_vector; |
|---|
| 282 | |
|---|
| 283 | if(has_constant) |
|---|
| 284 | { |
|---|
| 285 | lap_sample.ins(lap_sample.size(),1.0); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | lap_sample.ins(lap_sample.size(),(*LapRNG)()); |
|---|
| 289 | |
|---|
| 290 | sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t)); |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | return pair<vec,vec>(imp_samples.first,sample_prediction); |
|---|
| 294 | } |
|---|
| 295 | else |
|---|
| 296 | { |
|---|
| 297 | mat samples = my_arx->posterior().sample_mat(sample_size); |
|---|
| 298 | |
|---|
| 299 | vec sample_prediction; |
|---|
| 300 | for(int t = 0;t<sample_size;t++) |
|---|
| 301 | { |
|---|
| 302 | vec gau_sample = condition_vector; |
|---|
| 303 | |
|---|
| 304 | if(has_constant) |
|---|
| 305 | { |
|---|
| 306 | gau_sample.ins(gau_sample.size(),1.0); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | gau_sample.ins(gau_sample.size(),randn()); |
|---|
| 310 | |
|---|
| 311 | sample_prediction.ins(0,gau_sample*samples.get_col(t)); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | return pair<vec,vec>(ones(sample_prediction.size()),sample_prediction); |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | static set<set<pair<int,int>>> possible_models_recurse(int max_order,int number_of_channels) |
|---|
| 321 | { |
|---|
| 322 | set<set<pair<int,int>>> created_model_types; |
|---|
| 323 | |
|---|
| 324 | if(max_order == 1)//ukoncovacia vetva |
|---|
| 325 | { |
|---|
| 326 | for(int channel = 0;channel<number_of_channels;channel++)//pre AR 1 model vytvori kombinace kanalov v prvom kroku poyadu |
|---|
| 327 | { |
|---|
| 328 | set<pair<int,int>> returned_type; |
|---|
| 329 | returned_type.insert(pair<int,int>(channel,1)); //?? |
|---|
| 330 | created_model_types.insert(returned_type); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | return created_model_types; |
|---|
| 334 | } |
|---|
| 335 | else |
|---|
| 336 | { |
|---|
| 337 | created_model_types = possible_models_recurse(max_order-1,number_of_channels);//tu uz mame ulozene kombinace o jeden krok dozadu //rekuryivne volanie |
|---|
| 338 | set<set<pair<int,int>>> returned_types; |
|---|
| 339 | |
|---|
| 340 | for(set<set<pair<int,int>>>::iterator model_ref = created_model_types.begin();model_ref!=created_model_types.end();model_ref++) |
|---|
| 341 | { |
|---|
| 342 | |
|---|
| 343 | for(int order = 1; order<=max_order; order++) |
|---|
| 344 | { |
|---|
| 345 | for(int channel = 0;channel<number_of_channels;channel++) |
|---|
| 346 | { |
|---|
| 347 | set<pair<int,int>> returned_type; |
|---|
| 348 | pair<int,int> new_pair = pair<int,int>(channel,order);//?? |
|---|
| 349 | if(find((*model_ref).begin(),(*model_ref).end(),new_pair)==(*model_ref).end()) //?? |
|---|
| 350 | { |
|---|
| 351 | returned_type.insert((*model_ref).begin(),(*model_ref).end()); //co vlozi na zaciatok retuned_type? |
|---|
| 352 | returned_type.insert(new_pair); |
|---|
| 353 | |
|---|
| 354 | returned_types.insert(returned_type); |
|---|
| 355 | } |
|---|
| 356 | } |
|---|
| 357 | } |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | created_model_types.insert(returned_types.begin(),returned_types.end()); |
|---|
| 361 | |
|---|
| 362 | return created_model_types; |
|---|
| 363 | } |
|---|
| 364 | } |
|---|
| 365 | }; |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | int main ( int argc, char* argv[] ) |
|---|
| 371 | { |
|---|
| 372 | |
|---|
| 373 | // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from |
|---|
| 374 | // 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 |
|---|
| 375 | // can be compared to the classical setup. |
|---|
| 376 | |
|---|
| 377 | itpp::Laplace_RNG LapRNG = Laplace_RNG(); |
|---|
| 378 | |
|---|
| 379 | vector<vector<string>> strings; |
|---|
| 380 | |
|---|
| 381 | char* file_string = "C:\\CD2"; // "C:\\dataADClosePercDiff"; // |
|---|
| 382 | |
|---|
| 383 | char dfstring[80]; |
|---|
| 384 | strcpy(dfstring,file_string); |
|---|
| 385 | strcat(dfstring,".txt"); |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | mat data_matrix; |
|---|
| 389 | ifstream myfile(dfstring); |
|---|
| 390 | if (myfile.is_open()) |
|---|
| 391 | { |
|---|
| 392 | string line; |
|---|
| 393 | while(getline(myfile,line)) |
|---|
| 394 | { |
|---|
| 395 | vec data_vector; |
|---|
| 396 | while(line.find(',') != string::npos) //zmenil som ciarku za medzeru |
|---|
| 397 | { |
|---|
| 398 | //line.erase(0,1); // toto som sem pridal |
|---|
| 399 | int loc2 = line.find('\n'); |
|---|
| 400 | int loc = line.find(','); |
|---|
| 401 | data_vector.ins(data_vector.size(),atof(line.substr(0,loc).c_str())); |
|---|
| 402 | line.erase(0,loc+1); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | data_matrix.ins_row(data_matrix.rows(),data_vector); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | myfile.close(); |
|---|
| 409 | } |
|---|
| 410 | else |
|---|
| 411 | { |
|---|
| 412 | cout << "Can't open data file!" << endl; |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | //konec nacitavania dat |
|---|
| 416 | set<set<pair<int,int>>> model_types = model::possible_models_recurse(max_model_order,data_matrix.rows()); //volanie funkce kde robi kombinace modelov |
|---|
| 417 | //to priradime do model_types, data_matrix.row urcuje pocet kanalov dat |
|---|
| 418 | vector<model*> models; |
|---|
| 419 | for(set<set<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++) |
|---|
| 420 | {// prechadza rozne typy kanalov, a poctu regresorov |
|---|
| 421 | for(int window_size = max_window_size-1;window_size < max_window_size;window_size++) |
|---|
| 422 | { |
|---|
| 423 | models.push_back(new model((*model_type),true,true,window_size,0,&data_matrix)); // to su len konstruktory, len inicializujeme rozne typy |
|---|
| 424 | models.push_back(new model((*model_type),false,true,window_size,0,&data_matrix)); |
|---|
| 425 | models.push_back(new model((*model_type),true,false,window_size,0,&data_matrix)); |
|---|
| 426 | models.push_back(new model((*model_type),false,false,window_size,0,&data_matrix)); |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | //set<pair<int,int>> empty_list; |
|---|
| 430 | //models.push_back(new model(empty_list,false,true,100,0,&data_matrix)); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | mat result_lognc; |
|---|
| 434 | // mat result_preds; |
|---|
| 435 | |
|---|
| 436 | ofstream myfilew; |
|---|
| 437 | char fstring[80]; |
|---|
| 438 | strcpy(fstring,file_string); |
|---|
| 439 | //strcat(fstring,"lognc.txt"); |
|---|
| 440 | strcat(fstring,"preds.txt"); |
|---|
| 441 | |
|---|
| 442 | for(int time = max_model_order;time<data_matrix.cols();time++) //time<data_matrix.cols() |
|---|
| 443 | { |
|---|
| 444 | cout << "Steps: " << time << endl; |
|---|
| 445 | |
|---|
| 446 | /* |
|---|
| 447 | if(time==max_window_size) |
|---|
| 448 | { |
|---|
| 449 | exit(1); |
|---|
| 450 | }*/ |
|---|
| 451 | |
|---|
| 452 | //pocet stlpcov data_matrix je pocet casovych krokov |
|---|
| 453 | vec cur_res_lognc; |
|---|
| 454 | // vec preds; |
|---|
| 455 | vector<string> nazvy; |
|---|
| 456 | for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) |
|---|
| 457 | {//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 |
|---|
| 458 | (*model_ref)->data_update(time); //pozret sa preco je toto tu nutne |
|---|
| 459 | |
|---|
| 460 | //cout << "Updated." << endl; |
|---|
| 461 | //if (time = max_model_order) nazvy.push_back(models.model_ref]);// ako by som mohol dostat nazov modelu? |
|---|
| 462 | |
|---|
| 463 | if((*model_ref)->my_rarx!=NULL) //vklada normalizacni faktor do cur_res_lognc |
|---|
| 464 | { |
|---|
| 465 | //cout << "Maxlik vertex:" << (*model_ref)->my_rarx->posterior->minimal_vertex->get_coordinates() << endl; |
|---|
| 466 | cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_rarx->posterior->_ll()); |
|---|
| 467 | } |
|---|
| 468 | else |
|---|
| 469 | { |
|---|
| 470 | double cur_lognc = (*model_ref)->my_arx->posterior().lognc(); |
|---|
| 471 | double cur_ll = cur_lognc-(*model_ref)->previous_lognc; |
|---|
| 472 | |
|---|
| 473 | /* |
|---|
| 474 | if(time<(*model_ref)->window_size) |
|---|
| 475 | { |
|---|
| 476 | cur_ll-=1.83787706640935; |
|---|
| 477 | }*/ |
|---|
| 478 | |
|---|
| 479 | (*model_ref)->my_arx->_ll(); |
|---|
| 480 | |
|---|
| 481 | cur_res_lognc.ins(cur_res_lognc.size(),cur_ll); |
|---|
| 482 | |
|---|
| 483 | (*model_ref)->previous_lognc = cur_lognc; |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | /* |
|---|
| 487 | if(time == max_window_size-1) |
|---|
| 488 | { |
|---|
| 489 | //*********************** |
|---|
| 490 | int sample_size = 100000; |
|---|
| 491 | //*********************** |
|---|
| 492 | |
|---|
| 493 | pair<vec,mat> samples; |
|---|
| 494 | if((*model_ref)->my_arx!=NULL) |
|---|
| 495 | { |
|---|
| 496 | mat samp_mat = (*model_ref)->my_arx->posterior().sample_mat(sample_size); |
|---|
| 497 | samples = pair<vec,mat>(ones(samp_mat.cols()),samp_mat); |
|---|
| 498 | } |
|---|
| 499 | else |
|---|
| 500 | { |
|---|
| 501 | samples = (*model_ref)->my_rarx->posterior->sample(sample_size,true); |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | char fstring[80]; |
|---|
| 505 | strcpy(fstring,file_string); |
|---|
| 506 | strcat(fstring,(*model_ref)->name); |
|---|
| 507 | strcat(fstring,".txt"); |
|---|
| 508 | |
|---|
| 509 | //cout << samples.first << endl; |
|---|
| 510 | |
|---|
| 511 | myfilew.open(fstring,ios::app); |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | //for(int i = 0;i<samples.first.size();i++) |
|---|
| 515 | //{ |
|---|
| 516 | // myfilew << samples.first.get(i) << ","; |
|---|
| 517 | //} |
|---|
| 518 | //myfilew << endl; |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | for(int j = 0;j<samples.second.rows()+1;j++) |
|---|
| 522 | { |
|---|
| 523 | for(int i = 0;i<samples.second.cols();i++) |
|---|
| 524 | { |
|---|
| 525 | if(j!=samples.second.rows()) |
|---|
| 526 | { |
|---|
| 527 | myfilew << samples.second.get(j,i) << ","; |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | //else |
|---|
| 531 | //{ |
|---|
| 532 | // myfilew << "0,"; |
|---|
| 533 | //} |
|---|
| 534 | |
|---|
| 535 | } |
|---|
| 536 | myfilew << endl; |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | cout << "*************************************" << endl; |
|---|
| 540 | |
|---|
| 541 | myfilew.close(); |
|---|
| 542 | } |
|---|
| 543 | */ |
|---|
| 544 | |
|---|
| 545 | if(time>prediction_time) |
|---|
| 546 | { |
|---|
| 547 | // PREDICTIONS |
|---|
| 548 | pair<vec,vec> predictions = (*model_ref)->predict(5000,time,&LapRNG); |
|---|
| 549 | |
|---|
| 550 | /* |
|---|
| 551 | cout << predictions.first << endl << endl << predictions.second << endl << "*************************************" ; |
|---|
| 552 | pause(5); |
|---|
| 553 | */ |
|---|
| 554 | |
|---|
| 555 | double optimalInvestment = newtonRaphson(0,0.00001,predictions.second,utility_order); |
|---|
| 556 | |
|---|
| 557 | /* |
|---|
| 558 | vec utilityValues; |
|---|
| 559 | for(int j=0;j<1000;j++) |
|---|
| 560 | { |
|---|
| 561 | utilityValues.ins(utilityValues.length(),valueCRRAUtility(-0.5+0.001*j, predictions.second, utility_order)); |
|---|
| 562 | }*/ |
|---|
| 563 | |
|---|
| 564 | double avg_prediction = (predictions.first*predictions.second)/(predictions.first*ones(predictions.first.size())); |
|---|
| 565 | |
|---|
| 566 | (*model_ref)->predictions.ins((*model_ref)->predictions.size(),avg_prediction); |
|---|
| 567 | |
|---|
| 568 | myfilew.open(fstring,ios::app); |
|---|
| 569 | |
|---|
| 570 | /* |
|---|
| 571 | for(int j=0;j<utilityValues.length();j++) |
|---|
| 572 | { |
|---|
| 573 | myfilew << utilityValues.get(j) << ","; |
|---|
| 574 | } |
|---|
| 575 | myfilew << endl; |
|---|
| 576 | */ |
|---|
| 577 | |
|---|
| 578 | myfilew << avg_prediction << "," << optimalInvestment << ","; |
|---|
| 579 | myfilew.close(); |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | |
|---|
| 583 | //preds.ins(0,data_matrix.get(0,time+1)); |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | if(time>prediction_time) |
|---|
| 587 | { |
|---|
| 588 | // REAL PRICE |
|---|
| 589 | myfilew.open(fstring,ios::app); |
|---|
| 590 | myfilew << data_matrix.get(0,time+1) << endl; |
|---|
| 591 | myfilew.close(); |
|---|
| 592 | } |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | result_lognc.ins_col(result_lognc.cols(),cur_res_lognc); |
|---|
| 597 | //result_preds.ins_col(result_preds.cols(),preds); |
|---|
| 598 | |
|---|
| 599 | // cout << "Updated." << endl; |
|---|
| 600 | |
|---|
| 601 | /* |
|---|
| 602 | myfilew.open(fstring,ios::app); |
|---|
| 603 | |
|---|
| 604 | // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; |
|---|
| 605 | |
|---|
| 606 | if(time == max_model_order) |
|---|
| 607 | { |
|---|
| 608 | for(int i = 0;i<cur_res_lognc.size();i++) |
|---|
| 609 | { |
|---|
| 610 | for(set<pair<int,int>>::iterator ar_ref = models[i]->ar_components.begin();ar_ref != models[i]->ar_components.end();ar_ref++) |
|---|
| 611 | { |
|---|
| 612 | myfilew << (*ar_ref).second << (*ar_ref).first; |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | myfilew << "."; |
|---|
| 616 | |
|---|
| 617 | if(models[i]->my_arx == NULL) |
|---|
| 618 | { |
|---|
| 619 | myfilew << "1"; |
|---|
| 620 | } |
|---|
| 621 | else |
|---|
| 622 | { |
|---|
| 623 | myfilew << "0"; |
|---|
| 624 | } |
|---|
| 625 | |
|---|
| 626 | if(models[i]->has_constant) |
|---|
| 627 | { |
|---|
| 628 | myfilew << "1"; |
|---|
| 629 | } |
|---|
| 630 | else |
|---|
| 631 | { |
|---|
| 632 | myfilew << "0"; |
|---|
| 633 | } |
|---|
| 634 | |
|---|
| 635 | myfilew << ","; |
|---|
| 636 | } |
|---|
| 637 | |
|---|
| 638 | myfilew << endl; |
|---|
| 639 | } |
|---|
| 640 | |
|---|
| 641 | |
|---|
| 642 | for(int i = 0;i<cur_res_lognc.size();i++) |
|---|
| 643 | { |
|---|
| 644 | myfilew << cur_res_lognc[i] << ' ';//zmenil som ciarku ze medzeru |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | myfilew << endl; |
|---|
| 648 | myfilew.close(); |
|---|
| 649 | */ |
|---|
| 650 | } |
|---|
| 651 | |
|---|
| 652 | // EXPERIMENT: One step ahead price prediction. Comparison of classical and robust model using optimal trading |
|---|
| 653 | // with maximization of logarithm of one-step ahead wealth. |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | /* |
|---|
| 658 | cout << "One experiment finished." << endl; |
|---|
| 659 | |
|---|
| 660 | ofstream myfile; |
|---|
| 661 | myfile.open("c:\\robust_ar1.txt",ios::app); |
|---|
| 662 | myfile << endl; |
|---|
| 663 | myfile.close(); |
|---|
| 664 | |
|---|
| 665 | myfile.open("c:\\robust_ar2.txt",ios::app); |
|---|
| 666 | myfile << endl; |
|---|
| 667 | myfile.close();*/ |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | //emlig* emlig1 = new emlig(emlig_size); |
|---|
| 671 | |
|---|
| 672 | //emlig1->step_me(0); |
|---|
| 673 | //emlig* emlig2 = new emlig(emlig_size); |
|---|
| 674 | |
|---|
| 675 | /* |
|---|
| 676 | emlig1->set_correction_factors(4); |
|---|
| 677 | |
|---|
| 678 | for(int j = 0;j<emlig1->correction_factors.size();j++) |
|---|
| 679 | { |
|---|
| 680 | for(set<my_ivec>::iterator vec_ref = emlig1->correction_factors[j].begin();vec_ref!=emlig1->correction_factors[j].end();vec_ref++) |
|---|
| 681 | { |
|---|
| 682 | cout << j << " "; |
|---|
| 683 | |
|---|
| 684 | for(int i=0;i<(*vec_ref).size();i++) |
|---|
| 685 | { |
|---|
| 686 | cout << (*vec_ref)[i]; |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | cout << endl; |
|---|
| 690 | } |
|---|
| 691 | }*/ |
|---|
| 692 | |
|---|
| 693 | /* |
|---|
| 694 | vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5"; |
|---|
| 695 | |
|---|
| 696 | emlig1->add_condition(condition5); |
|---|
| 697 | //emlig1->step_me(0); |
|---|
| 698 | |
|---|
| 699 | |
|---|
| 700 | vec condition1a = "-1.0 1.02 0.5"; |
|---|
| 701 | //vec condition1b = "1.0 1.0 1.01"; |
|---|
| 702 | emlig1->add_condition(condition1a); |
|---|
| 703 | //emlig2->add_condition(condition1b); |
|---|
| 704 | |
|---|
| 705 | vec condition2a = "-0.3 1.7 1.5"; |
|---|
| 706 | //vec condition2b = "-1.0 1.0 1.0"; |
|---|
| 707 | emlig1->add_condition(condition2a); |
|---|
| 708 | //emlig2->add_condition(condition2b); |
|---|
| 709 | |
|---|
| 710 | vec condition3a = "0.5 -1.01 1.0"; |
|---|
| 711 | //vec condition3b = "0.5 -1.01 1.0"; |
|---|
| 712 | |
|---|
| 713 | emlig1->add_condition(condition3a); |
|---|
| 714 | //emlig2->add_condition(condition3b); |
|---|
| 715 | |
|---|
| 716 | vec condition4a = "-0.5 -1.0 1.0"; |
|---|
| 717 | //vec condition4b = "-0.5 -1.0 1.0"; |
|---|
| 718 | |
|---|
| 719 | emlig1->add_condition(condition4a); |
|---|
| 720 | //cout << "************************************************" << endl; |
|---|
| 721 | //emlig2->add_condition(condition4b); |
|---|
| 722 | //cout << "************************************************" << endl; |
|---|
| 723 | |
|---|
| 724 | //cout << emlig1->minimal_vertex->get_coordinates(); |
|---|
| 725 | |
|---|
| 726 | //emlig1->remove_condition(condition3a); |
|---|
| 727 | //emlig1->step_me(0); |
|---|
| 728 | //emlig1->remove_condition(condition2a); |
|---|
| 729 | //emlig1->remove_condition(condition1a); |
|---|
| 730 | //emlig1->remove_condition(condition5); |
|---|
| 731 | |
|---|
| 732 | |
|---|
| 733 | //emlig1->step_me(0); |
|---|
| 734 | //emlig2->step_me(0); |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | // DA SE POUZIT PRO VYPIS DO SOUBORU |
|---|
| 738 | // emlig1->step_me(0); |
|---|
| 739 | |
|---|
| 740 | //emlig1->remove_condition(condition1); |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | /* |
|---|
| 747 | for(int i = 0;i<100;i++) |
|---|
| 748 | { |
|---|
| 749 | cout << endl << "Step:" << i << endl; |
|---|
| 750 | |
|---|
| 751 | double condition[emlig_size+1]; |
|---|
| 752 | |
|---|
| 753 | for(int k = 0;k<=emlig_size;k++) |
|---|
| 754 | { |
|---|
| 755 | condition[k] = (rand()-RAND_MAX/2)/1000.0; |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | |
|---|
| 759 | vec* condition_vec = new vec(condition,emlig_size+1); |
|---|
| 760 | emlig1->add_condition(*condition_vec); |
|---|
| 761 | |
|---|
| 762 | /* |
|---|
| 763 | for(polyhedron* toprow_ref = emlig1->statistic.rows[emlig_size]; toprow_ref != emlig1->statistic.end_poly; toprow_ref = toprow_ref->next_poly) |
|---|
| 764 | { |
|---|
| 765 | cout << ((toprow*)toprow_ref)->probability << endl; |
|---|
| 766 | } |
|---|
| 767 | */ |
|---|
| 768 | /* |
|---|
| 769 | cout << emlig1->statistic_rowsize(emlig_size) << endl << endl; |
|---|
| 770 | |
|---|
| 771 | /* |
|---|
| 772 | if(i-emlig1->number_of_parameters >= 0) |
|---|
| 773 | { |
|---|
| 774 | pause(30); |
|---|
| 775 | } |
|---|
| 776 | */ |
|---|
| 777 | |
|---|
| 778 | // emlig1->step_me(i); |
|---|
| 779 | |
|---|
| 780 | /* |
|---|
| 781 | vector<int> sizevector; |
|---|
| 782 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
|---|
| 783 | { |
|---|
| 784 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
|---|
| 785 | } |
|---|
| 786 | */ |
|---|
| 787 | //} |
|---|
| 788 | |
|---|
| 789 | |
|---|
| 790 | |
|---|
| 791 | |
|---|
| 792 | /* |
|---|
| 793 | emlig1->step_me(1); |
|---|
| 794 | |
|---|
| 795 | vec condition = "2.0 0.0 1.0"; |
|---|
| 796 | |
|---|
| 797 | emlig1->add_condition(condition); |
|---|
| 798 | |
|---|
| 799 | vector<int> sizevector; |
|---|
| 800 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
|---|
| 801 | { |
|---|
| 802 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
|---|
| 803 | } |
|---|
| 804 | |
|---|
| 805 | emlig1->step_me(2); |
|---|
| 806 | |
|---|
| 807 | condition = "2.0 1.0 0.0"; |
|---|
| 808 | |
|---|
| 809 | emlig1->add_condition(condition); |
|---|
| 810 | |
|---|
| 811 | sizevector.clear(); |
|---|
| 812 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
|---|
| 813 | { |
|---|
| 814 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
|---|
| 815 | } |
|---|
| 816 | */ |
|---|
| 817 | |
|---|
| 818 | return 0; |
|---|
| 819 | } |
|---|
| 820 | |
|---|
| 821 | |
|---|