31 | | const int max_window_size = 30; |
| 31 | const int max_window_size = 40; |
| 32 | |
| 33 | /* |
| 34 | HDDEDATA CALLBACK DdeCallback( |
| 35 | UINT uType, // Transaction type. |
| 36 | UINT uFmt, // Clipboard data format. |
| 37 | HCONV hconv, // Handle to the conversation. |
| 38 | HSZ hsz1, // Handle to a string. |
| 39 | HSZ hsz2, // Handle to a string. |
| 40 | HDDEDATA hdata, // Handle to a global memory object. |
| 41 | DWORD dwData1, // Transaction-specific data. |
| 42 | DWORD dwData2) // Transaction-specific data. |
| 43 | { |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | void DDERequest(DWORD idInst, HCONV hConv, char* szItem) |
| 48 | { |
| 49 | HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0); |
| 50 | HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT, |
| 51 | XTYP_ADVSTART,TIMEOUT_ASYNC , NULL); //TIMEOUT_ASYNC |
| 52 | if (hData==NULL) |
| 53 | { |
| 54 | printf("Request failed: %s\n", szItem); |
| 55 | } |
| 56 | |
| 57 | if (hData==0) |
| 58 | { |
| 59 | printf("Request failed: %s\n", szItem); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | DWORD WINAPI ThrdFunc( LPVOID n ) |
| 64 | { |
| 65 | return 0; |
| 66 | } |
| 67 | */ |
| 350 | for(set<set<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++) |
| 351 | {// prechadza rozne typy kanalov, a poctu regresorov |
| 352 | for(int window_size = max_window_size-1;window_size < max_window_size;window_size++) |
| 353 | { |
| 354 | models.push_back(new model((*model_type),true,true,window_size,0,&data_matrix)); // to su len konstruktory, len inicializujeme rozne typy |
| 355 | models.push_back(new model((*model_type),false,true,window_size,0,&data_matrix)); |
| 356 | models.push_back(new model((*model_type),true,false,window_size,0,&data_matrix)); |
| 357 | models.push_back(new model((*model_type),false,false,window_size,0,&data_matrix)); |
| 358 | } |
| 359 | |
| 360 | //set<pair<int,int>> empty_list; |
| 361 | //models.push_back(new model(empty_list,false,true,100,0,&data_matrix)); |
| 362 | } |
| 363 | |
| 364 | mat result_lognc; |
| 365 | // mat result_preds; |
301 | | |
302 | | |
303 | | while(data_matrix.rows()!=0) |
304 | | { |
305 | | for(int i=0;i<models.size();i++) |
306 | | { |
307 | | delete models[i]; |
308 | | } |
309 | | |
310 | | models.clear(); |
311 | | models.push_back(new model(model_type,true,false,max_window_size,0,&data_matrix)); |
312 | | models.push_back(new model(model_type,false,false,max_window_size,0,&data_matrix)); |
313 | | |
314 | | for(int time = max_model_order;time<max_window_size;time++) //time<data_matrix.cols() |
315 | | { |
316 | | vec cur_res_lognc; |
317 | | |
318 | | vector<string> nazvy; |
319 | | for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) |
320 | | { |
321 | | (*model_ref)->data_update(time); |
322 | | |
323 | | cout << "Updated:" << time << endl; |
324 | | |
325 | | if(time == max_window_size-1) |
326 | | { |
327 | | char fstring[80]; |
328 | | strcpy(fstring,file_string); |
329 | | strcat(fstring,"ml"); |
330 | | strcat(fstring,(*model_ref)->name); |
331 | | strcat(fstring,".txt"); |
332 | | |
333 | | vec coords; |
334 | | if((*model_ref)->my_arx!=NULL) |
| 368 | char fstring[80]; |
| 369 | strcpy(fstring,file_string); |
| 370 | //strcat(fstring,"lognc.txt"); |
| 371 | strcat(fstring,"preds.txt"); |
| 372 | |
| 373 | for(int time = max_model_order;time<data_matrix.cols();time++) //time<data_matrix.cols() |
| 374 | { |
| 375 | cout << "Steps: " << time << endl; |
| 376 | |
| 377 | /* |
| 378 | if(time==max_window_size) |
| 379 | { |
| 380 | exit(1); |
| 381 | }*/ |
| 382 | |
| 383 | //pocet stlpcov data_matrix je pocet casovych krokov |
| 384 | vec cur_res_lognc; |
| 385 | // vec preds; |
| 386 | vector<string> nazvy; |
| 387 | for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) |
| 388 | {//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 |
| 389 | (*model_ref)->data_update(time); //pozret sa preco je toto tu nutne |
| 390 | |
| 391 | //cout << "Updated." << endl; |
| 392 | //if (time = max_model_order) nazvy.push_back(models.model_ref]);// ako by som mohol dostat nazov modelu? |
| 393 | |
| 394 | if((*model_ref)->my_rarx!=NULL) //vklada normalizacni faktor do cur_res_lognc |
| 395 | { |
| 396 | //cout << "Maxlik vertex:" << (*model_ref)->my_rarx->posterior->minimal_vertex->get_coordinates() << endl; |
| 397 | cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_rarx->posterior->_ll()); |
| 398 | } |
| 399 | else |
| 400 | { |
| 401 | double cur_lognc = (*model_ref)->my_arx->posterior().lognc(); |
| 402 | double cur_ll = cur_lognc-(*model_ref)->previous_lognc; |
| 403 | |
| 404 | /* |
| 405 | if(time<(*model_ref)->window_size) |
| 406 | { |
| 407 | cur_ll-=1.83787706640935; |
| 408 | }*/ |
| 409 | |
| 410 | (*model_ref)->my_arx->_ll(); |
| 411 | |
| 412 | cur_res_lognc.ins(cur_res_lognc.size(),cur_ll); |
| 413 | |
| 414 | (*model_ref)->previous_lognc = cur_lognc; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | if(time == max_window_size-1) |
| 419 | { |
| 420 | //*********************** |
| 421 | int sample_size = 100000; |
| 422 | //*********************** |
| 423 | |
| 424 | pair<vec,mat> samples; |
| 425 | if((*model_ref)->my_arx!=NULL) |
| 426 | { |
| 427 | mat samp_mat = (*model_ref)->my_arx->posterior().sample_mat(sample_size); |
| 428 | samples = pair<vec,mat>(ones(samp_mat.cols()),samp_mat); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | samples = (*model_ref)->my_rarx->posterior->sample(sample_size,true); |
| 433 | } |
| 434 | |
| 435 | char fstring[80]; |
| 436 | strcpy(fstring,file_string); |
| 437 | strcat(fstring,(*model_ref)->name); |
| 438 | strcat(fstring,".txt"); |
| 439 | |
| 440 | //cout << samples.first << endl; |
| 441 | |
| 442 | myfilew.open(fstring,ios::app); |
| 443 | |
| 444 | |
| 445 | //for(int i = 0;i<samples.first.size();i++) |
| 446 | //{ |
| 447 | // myfilew << samples.first.get(i) << ","; |
| 448 | //} |
| 449 | //myfilew << endl; |
| 450 | |
| 451 | |
| 452 | for(int j = 0;j<samples.second.rows()+1;j++) |
| 453 | { |
| 454 | for(int i = 0;i<samples.second.cols();i++) |
350 | | |
351 | | myfilew.close(); |
352 | | } |
353 | | } |
354 | | } |
355 | | |
356 | | data_matrix.del_row(0); |
357 | | } |
358 | | |
359 | | |
360 | | |
361 | | |
| 468 | } |
| 469 | |
| 470 | cout << "*************************************" << endl; |
| 471 | |
| 472 | myfilew.close(); |
| 473 | } |
| 474 | */ |
| 475 | |
| 476 | // PREDICTIONS |
| 477 | pair<vec,vec> predictions = (*model_ref)->predict(3000,time,&LapRNG); |
| 478 | |
| 479 | /* |
| 480 | cout << predictions.first << endl << endl << predictions.second << endl << "*************************************" ; |
| 481 | pause(5); |
| 482 | */ |
| 483 | |
| 484 | double avg_prediction = (predictions.first*predictions.second)/(predictions.first*ones(predictions.first.size())); |
| 485 | |
| 486 | (*model_ref)->predictions.ins((*model_ref)->predictions.size(),avg_prediction); |
| 487 | |
| 488 | myfilew.open(fstring,ios::app); |
| 489 | myfilew << avg_prediction << ","; |
| 490 | myfilew.close(); |
| 491 | |
| 492 | |
| 493 | //preds.ins(0,data_matrix.get(0,time+1)); |
| 494 | } |
| 495 | |
| 496 | |
| 497 | // REAL PRICE |
| 498 | myfilew.open(fstring,ios::app); |
| 499 | myfilew << data_matrix.get(0,time+1) << endl; |
| 500 | myfilew.close(); |
| 501 | |
| 502 | |
| 503 | result_lognc.ins_col(result_lognc.cols(),cur_res_lognc); |
| 504 | //result_preds.ins_col(result_preds.cols(),preds); |
| 505 | |
| 506 | // cout << "Updated." << endl; |
| 507 | |
| 508 | /* |
| 509 | myfilew.open(fstring,ios::app); |
| 510 | |
| 511 | // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; |
| 512 | |
| 513 | if(time == max_model_order) |
| 514 | { |
| 515 | for(int i = 0;i<cur_res_lognc.size();i++) |
| 516 | { |
| 517 | for(set<pair<int,int>>::iterator ar_ref = models[i]->ar_components.begin();ar_ref != models[i]->ar_components.end();ar_ref++) |
| 518 | { |
| 519 | myfilew << (*ar_ref).second << (*ar_ref).first; |
| 520 | } |
| 521 | |
| 522 | myfilew << "."; |
| 523 | |
| 524 | if(models[i]->my_arx == NULL) |
| 525 | { |
| 526 | myfilew << "1"; |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | myfilew << "0"; |
| 531 | } |
| 532 | |
| 533 | if(models[i]->has_constant) |
| 534 | { |
| 535 | myfilew << "1"; |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | myfilew << "0"; |
| 540 | } |
| 541 | |
| 542 | myfilew << ","; |
| 543 | } |
| 544 | |
| 545 | myfilew << endl; |
| 546 | } |
| 547 | |
| 548 | |
| 549 | for(int i = 0;i<cur_res_lognc.size();i++) |
| 550 | { |
| 551 | myfilew << cur_res_lognc[i] << ' ';//zmenil som ciarku ze medzeru |
| 552 | } |
| 553 | |
| 554 | myfilew << endl; |
| 555 | myfilew.close(); |
| 556 | */ |
| 557 | } |
| 558 | |
| 559 | // EXPERIMENT: One step ahead price prediction. Comparison of classical and robust model using optimal trading |
| 560 | // with maximization of logarithm of one-step ahead wealth. |
| 561 | |
| 562 | |
| 563 | |
| 564 | /* |
| 565 | cout << "One experiment finished." << endl; |
| 566 | |
| 567 | ofstream myfile; |
| 568 | myfile.open("c:\\robust_ar1.txt",ios::app); |
| 569 | myfile << endl; |
| 570 | myfile.close(); |
| 571 | |
| 572 | myfile.open("c:\\robust_ar2.txt",ios::app); |
| 573 | myfile << endl; |
| 574 | myfile.close();*/ |
| 575 | |
| 576 | |
| 577 | //emlig* emlig1 = new emlig(emlig_size); |
| 578 | |
| 579 | //emlig1->step_me(0); |
| 580 | //emlig* emlig2 = new emlig(emlig_size); |
| 581 | |
| 582 | /* |
| 583 | emlig1->set_correction_factors(4); |
| 584 | |
| 585 | for(int j = 0;j<emlig1->correction_factors.size();j++) |
| 586 | { |
| 587 | for(set<my_ivec>::iterator vec_ref = emlig1->correction_factors[j].begin();vec_ref!=emlig1->correction_factors[j].end();vec_ref++) |
| 588 | { |
| 589 | cout << j << " "; |
| 590 | |
| 591 | for(int i=0;i<(*vec_ref).size();i++) |
| 592 | { |
| 593 | cout << (*vec_ref)[i]; |
| 594 | } |
| 595 | |
| 596 | cout << endl; |
| 597 | } |
| 598 | }*/ |
| 599 | |
| 600 | /* |
| 601 | vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5"; |
| 602 | |
| 603 | emlig1->add_condition(condition5); |
| 604 | //emlig1->step_me(0); |
| 605 | |
| 606 | |
| 607 | vec condition1a = "-1.0 1.02 0.5"; |
| 608 | //vec condition1b = "1.0 1.0 1.01"; |
| 609 | emlig1->add_condition(condition1a); |
| 610 | //emlig2->add_condition(condition1b); |
| 611 | |
| 612 | vec condition2a = "-0.3 1.7 1.5"; |
| 613 | //vec condition2b = "-1.0 1.0 1.0"; |
| 614 | emlig1->add_condition(condition2a); |
| 615 | //emlig2->add_condition(condition2b); |
| 616 | |
| 617 | vec condition3a = "0.5 -1.01 1.0"; |
| 618 | //vec condition3b = "0.5 -1.01 1.0"; |
| 619 | |
| 620 | emlig1->add_condition(condition3a); |
| 621 | //emlig2->add_condition(condition3b); |
| 622 | |
| 623 | vec condition4a = "-0.5 -1.0 1.0"; |
| 624 | //vec condition4b = "-0.5 -1.0 1.0"; |
| 625 | |
| 626 | emlig1->add_condition(condition4a); |
| 627 | //cout << "************************************************" << endl; |
| 628 | //emlig2->add_condition(condition4b); |
| 629 | //cout << "************************************************" << endl; |
| 630 | |
| 631 | //cout << emlig1->minimal_vertex->get_coordinates(); |
| 632 | |
| 633 | //emlig1->remove_condition(condition3a); |
| 634 | //emlig1->step_me(0); |
| 635 | //emlig1->remove_condition(condition2a); |
| 636 | //emlig1->remove_condition(condition1a); |
| 637 | //emlig1->remove_condition(condition5); |
| 638 | |
| 639 | |
| 640 | //emlig1->step_me(0); |
| 641 | //emlig2->step_me(0); |
| 642 | |
| 643 | |
| 644 | // DA SE POUZIT PRO VYPIS DO SOUBORU |
| 645 | // emlig1->step_me(0); |
| 646 | |
| 647 | //emlig1->remove_condition(condition1); |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | /* |
| 654 | for(int i = 0;i<100;i++) |
| 655 | { |
| 656 | cout << endl << "Step:" << i << endl; |
| 657 | |
| 658 | double condition[emlig_size+1]; |
| 659 | |
| 660 | for(int k = 0;k<=emlig_size;k++) |
| 661 | { |
| 662 | condition[k] = (rand()-RAND_MAX/2)/1000.0; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | vec* condition_vec = new vec(condition,emlig_size+1); |
| 667 | emlig1->add_condition(*condition_vec); |
| 668 | |
| 669 | /* |
| 670 | for(polyhedron* toprow_ref = emlig1->statistic.rows[emlig_size]; toprow_ref != emlig1->statistic.end_poly; toprow_ref = toprow_ref->next_poly) |
| 671 | { |
| 672 | cout << ((toprow*)toprow_ref)->probability << endl; |
| 673 | } |
| 674 | */ |
| 675 | /* |
| 676 | cout << emlig1->statistic_rowsize(emlig_size) << endl << endl; |
| 677 | |
| 678 | /* |
| 679 | if(i-emlig1->number_of_parameters >= 0) |
| 680 | { |
| 681 | pause(30); |
| 682 | } |
| 683 | */ |
| 684 | |
| 685 | // emlig1->step_me(i); |
| 686 | |
| 687 | /* |
| 688 | vector<int> sizevector; |
| 689 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
| 690 | { |
| 691 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
| 692 | } |
| 693 | */ |
| 694 | //} |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | /* |
| 700 | emlig1->step_me(1); |
| 701 | |
| 702 | vec condition = "2.0 0.0 1.0"; |
| 703 | |
| 704 | emlig1->add_condition(condition); |
| 705 | |
| 706 | vector<int> sizevector; |
| 707 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
| 708 | { |
| 709 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
| 710 | } |
| 711 | |
| 712 | emlig1->step_me(2); |
| 713 | |
| 714 | condition = "2.0 1.0 0.0"; |
| 715 | |
| 716 | emlig1->add_condition(condition); |
| 717 | |
| 718 | sizevector.clear(); |
| 719 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
| 720 | { |
| 721 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
| 722 | } |
| 723 | */ |