Changeset 1413
- Timestamp:
- 12/09/11 17:53:36 (13 years ago)
- Location:
- applications/robust
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/robust/main.cpp
r1412 r1413 31 31 const int max_model_order = 2; 32 32 const double apriorno = 0.01; 33 const int max_window_size = 40;33 const int max_window_size = 30; 34 34 const int utility_order = 25; 35 35 const int prediction_time = 30; 36 36 const double min_utility_argument = 0.001; 37 37 const double max_investment = 10.0; 38 const char* commodity = "CL\\"; 38 const int sample_size = 5000; 39 const char* commodity = "BP\\"; 39 40 40 41 /* … … 91 92 } 92 93 93 double valueCRRAUtility(const double &position, const vec&samples, const int order)94 double valueCRRAUtility(const double &position, const pair<vec,vec> &samples, const int order) 94 95 { 95 96 double value = 0; 97 set<double> values; 96 98 97 for(int i=0;i<samples.length();i++) 98 { 99 double sample = samples.get(i); 99 for(int i=0;i<samples.first.length();i++) 100 { 101 double sample = samples.second.get(i); 102 double probability = samples.first.get(i); 100 103 if((position*sample+1)>min_utility_argument) 101 104 { 102 value += sample/pow(position*sample+1,order+1);105 values.insert(probability*sample/pow(position*sample+1,order+1)); 103 106 } 104 107 else 105 108 { 106 value += (min_utility_argument-1)/position/pow(min_utility_argument,order+1);109 values.insert(probability*(min_utility_argument-1)/position/pow(min_utility_argument,order+1)); 107 110 } 108 111 } 109 112 113 for(set<double>::iterator val_ref = values.begin();val_ref!=values.end();val_ref++) 114 { 115 value+=(*val_ref); 116 } 117 110 118 return value; 111 119 } 112 120 113 double gradientCRRAUtility(const double &position, const vec&samples, const int order)121 double gradientCRRAUtility(const double &position, const pair<vec,vec> &samples, const int order) 114 122 { 115 123 double value = 0; 116 117 for(int i=0;i<samples.length();i++) 118 { 119 double sample = samples.get(i); 124 set<double> values; 125 126 for(int i=0;i<samples.first.length();i++) 127 { 128 double sample = samples.second.get(i); 129 double probability = samples.first.get(i); 120 130 if((position*sample+1)>min_utility_argument) 121 131 { 122 value += (-(order+1)*pow(sample,2))/pow(position*sample+1,order+2); 123 } 132 values.insert((-(order+1)*pow(sample,2)*probability)/pow(position*sample+1,order+2)); 133 } 134 } 135 136 for(set<double>::iterator val_ref = values.begin();val_ref!=values.end();val_ref++) 137 { 138 value+=(*val_ref); 124 139 } 125 140 … … 127 142 } 128 143 129 double newtonRaphson(double startingPoint, double epsilon, vecsamples, int order)144 double newtonRaphson(double startingPoint, double epsilon, pair<vec,vec> samples, int order) 130 145 { 146 /* 131 147 if(samples.length()>800) 132 148 { 133 149 samples.del(801,samples.size()-1); 134 150 } 151 */ 135 152 136 153 int count = 0; … … 309 326 if(my_rarx!=NULL) 310 327 { 311 pair<vec,mat> imp_samples = my_rarx->posterior->sample(sample_size, true);328 pair<vec,mat> imp_samples = my_rarx->posterior->sample(sample_size,false); 312 329 313 330 //cout << imp_samples.first << endl; … … 502 519 if(time == max_model_order) 503 520 { 504 521 /* 505 522 myfilew.open(rfstring,ios::app); 506 for(int i = 0; i<models.size();i++)523 for(int i = 0;models.size();i++) 507 524 { 508 525 for(set<pair<int,int>>::iterator ar_ref = models[i]->ar_components.begin();ar_ref != models[i]->ar_components.end();ar_ref++) … … 536 553 myfilew << "888" << endl; 537 554 myfilew.close(); 555 */ 538 556 } 539 557 … … 543 561 int prev_samples_nr; 544 562 bool previous_switch = true; 545 for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) 563 for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) //.begin()+1;model_ref++) 546 564 {//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 547 565 (*model_ref)->data_update(time); //pozret sa preco je toto tu nutne … … 627 645 if(previous_switch) 628 646 { 629 samples_nr = 10000;647 samples_nr = sample_size; 630 648 } 631 649 else … … 663 681 previous_switch = !previous_switch; 664 682 665 double optimalInvestment = newtonRaphson(0,0.00001,predictions .second,utility_order);683 double optimalInvestment = newtonRaphson(0,0.00001,predictions,utility_order); 666 684 667 685 if(abs(optimalInvestment)>max_investment) -
applications/robust/robustlib.h
r1405 r1413 437 437 start_poly = new polyhedron(); 438 438 }; 439 440 ~c_statistic()441 {442 delete end_poly;443 delete start_poly;444 }445 439 446 440 void append_polyhedron(int row, polyhedron* appended_start, polyhedron* appended_end) … … 1103 1097 1104 1098 this->condition_order = condition_order; 1099 } 1100 1101 ~emlig() 1102 { 1103 for(int i = 0;i<statistic.size();i++) 1104 { 1105 polyhedron* current_polyhedron; 1106 while(statistic.rows[i]!=statistic.end_poly) 1107 { 1108 current_polyhedron = statistic.rows[i]; 1109 statistic.delete_polyhedron(i,statistic.rows[i]); 1110 delete current_polyhedron; 1111 } 1112 } 1113 1114 delete statistic.end_poly; 1115 delete statistic.start_poly; 1116 1117 for(list<condition*>::reverse_iterator c_ref = conditions.rbegin();c_ref!=conditions.rend();c_ref++) 1118 { 1119 delete *c_ref; 1120 } 1121 1122 cout << "Emlig deleted." << endl; 1105 1123 } 1106 1124 … … 2302 2320 pair<double,double> probability_and_sigma = choose_sigma(condition_and_simplex.second); 2303 2321 2322 /* 2323 if(samples.cols()<10) 2324 { 2325 cout << "S-P:" << probability_and_sigma.first << ", "; 2326 pause(0.3); 2327 }*/ 2328 2304 2329 int dimension = condition_and_simplex.second->vertices.size(); 2305 2330 … … 2336 2361 2337 2362 double exponent = extended_coords*condition_and_simplex.first; 2338 double sample_prob = 1*condition_and_simplex.second->volume/condition_and_simplex.second->probability/pow(2*probability_and_sigma.second,condition_order)*exp(-exponent/probability_and_sigma.second);//*probability_and_sigma.first; 2363 double sample_prob = 1*condition_and_simplex.second->volume/condition_and_simplex.second->probability/pow(2*probability_and_sigma.second,condition_order)*exp(-exponent/probability_and_sigma.second)*probability_and_sigma.first; 2364 2365 /* 2366 if(samples.cols()<20) 2367 { 2368 cout << "prob:" << sample_prob << endl; 2369 pause(0.2); 2370 } 2371 */ 2339 2372 2340 2373 sample_coords.ins(sample_coords.size(),probability_and_sigma.second); … … 2736 2769 }; 2737 2770 2771 ~RARX() 2772 { 2773 delete posterior; 2774 } 2775 2738 2776 void bayes(itpp::vec yt) 2739 2777 {