Changeset 1406 for applications

Show
Ignore:
Timestamp:
11/16/11 21:02:52 (12 years ago)
Author:
sindj
Message:

Newton solution of one-step ahead control equation. JS

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/main.cpp

    r1405 r1406  
    1616#include "ddeml.h" 
    1717#include "stdio.h" 
     18#include <itpp/itoptim.h> 
    1819 
    1920//#include "DDEClient.h" 
     
    3031const double apriorno     = 0.01; 
    3132const int max_window_size = 40; 
     33const int utility_order   = 19; 
     34const int prediction_time = 30; 
    3235 
    3336/* 
     
    6770*/ 
    6871 
     72double 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 
     85double 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 
     98double 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 
    69138class model 
    70139{ 
     
    307376         
    308377        itpp::Laplace_RNG LapRNG = Laplace_RNG();        
    309  
     378         
    310379        vector<vector<string>> strings; 
    311380 
     
    474543                        */               
    475544 
    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            
    488589                        myfilew.open(fstring,ios::app); 
    489                         myfilew << avg_prediction << ",";  
     590                        myfilew << data_matrix.get(0,time+1) << endl; 
    490591                        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(); 
     592                } 
     593                 
    501594                 
    502595