| | 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 | |
| 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 | | |
| | 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 |