Changeset 1337 for applications

Show
Ignore:
Timestamp:
04/22/11 18:35:46 (13 years ago)
Author:
sindj
Message:

Zacatky experimentovani s knihovnou robustlib. JS

Location:
applications/robust
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/main.cpp

    r1336 r1337  
    77 */ 
    88 
     9#include "estim/arx.h" 
    910#include "robustlib.h" 
    1011#include <vector> 
    1112#include <iostream> 
    1213#include <fstream> 
     14#include <itpp/signal/poly.h> 
    1315 
    1416using namespace itpp; 
    15 //using namespace bdm; 
     17using namespace bdm; 
    1618 
    1719const int emlig_size = 2; 
     
    2022int main ( int argc, char* argv[] ) { 
    2123         
     24        itpp::Laplace_RNG LapRNG = Laplace_RNG(); 
     25 
    2226        /* 
    2327        // EXPERIMENT: 100 AR model generated time series of length of 30 from y_t=0.95*y_(t-1)+0.05*y_(t-2)+0.2*e_t,  
     
    134138        vector<vector<string>> strings; 
    135139 
    136         char* file_strings[3] = {"c:\\ar_student_single.txt", "c:\\ar_student_single.txt","c:\\ar_cauchy_single.txt"}; 
     140        char* file_strings[3] = {"c:\\dataADClosePercDiff", "c:\\ar_student_single","c:\\ar_cauchy_single"}; 
    137141 
    138142        for(int i = 0;i<3;i++) 
    139143        {                        
    140                 ifstream myfile(file_strings[i]); 
     144                char dfstring[80]; 
     145                strcpy(dfstring,file_strings[i]); 
     146                strcat(dfstring,".txt"); 
     147                 
     148                ifstream myfile(dfstring); 
    141149                if (myfile.is_open()) 
    142150                {                        
     
    162170                vector<vec> conditions; 
    163171                //emlig* emliga = new emlig(2); 
    164                 RARX* my_rarx = new RARX(3,20,true); 
     172                RARX* my_rarx = new RARX(2,30,false); 
     173                 
     174                /* 
     175                mat V0 = 0.0001 * eye ( 3 ); 
     176                ARX* my_arx = new ARX(0.97); 
     177                my_arx->set_statistics ( 1, V0 ); //nu is default (set to have finite moments) 
     178                my_arx->set_constant ( false ); 
     179                my_arx->validate(); 
     180                */ 
    165181 
    166182                for(int k = 1;k<170;k++) 
     
    186202 
    187203                                my_rarx->bayes(conditions[k-3]); 
    188  
    189                                          
    190204                                 
    191                                 if(k>10) 
     205                                vec cond_vec; 
     206                                cond_vec.ins(0,conditions[k-3][0]); 
     207                                 
     208                                // my_arx->bayes(cond_vec,conditions[k-3].right(2)); 
     209                                         
     210                                 
     211                                if(k>5) 
    192212                                { 
    193213                                        //my_rarx->posterior->step_me(0); 
     
    195215                                        mat samples = my_rarx->posterior->sample_mat(50); 
    196216                                         
     217                                        vec sample_prediction; 
     218                                        for(int t = 0;t<50;t++) 
     219                                        { 
     220                                                vec lap_sample = conditions[k-3].left(2); 
     221                                                 
     222                                                lap_sample.ins(0,LapRNG()); 
     223 
     224                                                sample_prediction.ins(0,lap_sample*samples.get_col(t)); 
     225                                        } 
     226 
     227                                        vec sample_pow = sample_prediction; 
     228                                        vec poly_coefs; 
     229                                        bool stop_iteration = false; 
     230                                        int en = 0; 
     231                                        do 
     232                                        { 
     233                                                double poly_coef = ones(sample_pow.size())*sample_pow/sample_pow.size(); 
     234 
     235                                                if(abs(poly_coef)>numeric_limits<double>::epsilon()) 
     236                                                { 
     237                                                        sample_pow = elem_mult(sample_pow,sample_prediction); 
     238                                                        poly_coefs.ins(poly_coefs.size(),pow(-1,en)*poly_coef); 
     239                                                } 
     240                                                else 
     241                                                { 
     242                                                        stop_iteration = true; 
     243                                                } 
     244                                                 
     245                                                en++; 
     246                                        } 
     247                                        while(!stop_iteration); 
     248 
     249                                        cvec action =  
     250                                         
    197251                                         
    198252                                        cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl; 
    199253 
    200                                         for(int s = 0;s<samples.rows();s++) 
    201                                         { 
    202                                                  
    203                                                 double avg_parameter = samples.get_row(s)*ones(samples.cols())/samples.cols();                                           
    204                                                  
     254                                        double prediction = 0; 
     255                                        for(int s = 1;s<samples.rows();s++) 
     256                                        { 
     257                                                 
     258                                                double avg_parameter = samples.get_row(s)*ones(samples.cols())/samples.cols(); 
     259 
     260                                                prediction += avg_parameter*conditions[k-3][s-1]; 
     261 
     262                                                 
     263                                                 
     264                                                /* 
    205265                                                ofstream myfile; 
    206266                                                char fstring[80]; 
     
    227287                                                } 
    228288                                                myfile.close(); 
    229                                         } 
     289                                                */ 
     290                                        } 
     291 
     292                                        /* 
     293                                        enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2)); 
     294                                        double prediction2 = pred_mat->mean()[0]; 
     295                                        */ 
     296 
     297                                        ofstream myfile; 
     298                                        char fstring[80]; 
     299                                        //char f2string[80]; 
     300                                        strcpy(fstring,file_strings[j]); 
     301                                        //strcpy(f2string,fstring); 
     302 
     303                                        strcat(fstring,"pred.txt"); 
     304                                        //strcat(f2string,"2pred.txt"); 
     305                                         
     306 
     307                                        myfile.open(fstring,ios::app); 
     308                                         
     309                                        //myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; 
     310                                        myfile << prediction; 
     311                                         
     312                                        if(k!=strings[j].size()-1) 
     313                                        { 
     314                                                myfile << ","; 
     315                                        } 
     316                                        else 
     317                                        { 
     318                                                myfile << endl; 
     319                                        } 
     320                                        myfile.close(); 
     321 
     322                                        /* 
     323                                        myfile.open(f2string,ios::app); 
     324                                        myfile << prediction2; 
     325                                         
     326                                        if(k!=strings[j].size()-1) 
     327                                        { 
     328                                                myfile << ","; 
     329                                        } 
     330                                        else 
     331                                        { 
     332                                                myfile << endl; 
     333                                        } 
     334                                        myfile.close(); 
     335                                        */ 
     336 
    230337                                }                                        
    231338                        }        
     
    246353                        cout << "Step: " << i << endl;*/ 
    247354                } 
     355 
     356 
    248357        } 
     358 
     359 
     360        // EXPERIMENT: One step ahead price prediction. Comparison of classical and robust model using optimal trading 
     361    //             with maximization of logarithm of one-step ahead wealth. 
     362 
     363         
    249364                 
    250365                /* 
  • applications/robust/robustlib.h

    r1336 r1337  
    1010#include <stat/exp_family.h> 
    1111#include <itpp/itbase.h> 
     12#include <itpp/base/random.h> 
    1213#include <map> 
    1314#include <limits> 
     
    3031class vertex; 
    3132class emlig; 
    32  
    3333 
    3434/* 
     
    24022402                 
    24032403        } 
    2404  
    2405  
    2406          
    24072404         
    24082405};