Show
Ignore:
Timestamp:
04/29/11 13:26:04 (13 years ago)
Author:
sindj
Message:

Importance sampling. JS

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/main.cpp

    r1338 r1343  
    1818 
    1919const int emlig_size = 2; 
     20const int utility_constant = 2; 
    2021 
    2122 
     
    138139        vector<vector<string>> strings; 
    139140 
    140         char* file_strings[3] = {"c:\\dataCDClosePercDiff", "c:\\ar_student_single","c:\\ar_cauchy_single"}; 
     141        char* file_strings[3] = {"c:\\dataADClosePercDiff","c:\\ar_student_single","c:\\ar_cauchy_single"}; 
    141142 
    142143        for(int i = 0;i<3;i++) 
     
    170171                vector<vec> conditions; 
    171172                //emlig* emliga = new emlig(2); 
    172                 RARX* my_rarx = new RARX(2,30,false); 
     173                RARX* my_rarx = new RARX(3,30,true); 
    173174                 
    174175                 
     
    199200                                conditions[k-3].ins(0,strings[j][k]); 
    200201 
    201                                 //cout << "Condition:" << conditions[k-3] << endl; 
     202                                cout << "Condition:" << conditions[k-3] << endl; 
    202203 
    203204                                my_rarx->bayes(conditions[k-3]); 
     
    210211                                         
    211212                                 
    212                                 if(k>8) 
     213                                if(k>10) 
    213214                                { 
    214215                                        //my_rarx->posterior->step_me(0); 
    215216 
    216                                         mat samples = my_rarx->posterior->sample_mat(50); 
     217                                        mat samples = my_rarx->posterior->sample_mat(10); 
     218 
     219                                        pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(20); 
     220 
     221                                        cout << imp_samples.first << endl; 
    217222                                         
    218223                                        vec sample_prediction; 
    219                                         for(int t = 0;t<50;t++) 
     224                                        for(int t = 0;t<samples.cols();t++) 
    220225                                        { 
    221226                                                vec lap_sample = conditions[k-3].left(2); 
    222                                                 //lap_sample.ins(lap_sample.size(),1.0); 
     227                                                lap_sample.ins(lap_sample.size(),1.0); 
    223228                                                 
    224229                                                lap_sample.ins(0,LapRNG()); 
     
    229234                                         
    230235                                        vec sample_pow = sample_prediction; 
     236                                         
     237                                        // cout << sample_prediction << endl; 
    231238                                        vec poly_coefs; 
    232239                                        bool stop_iteration = false; 
    233                                         int en = 0; 
     240                                        int en = 1; 
    234241                                        do 
    235242                                        { 
    236243                                                double poly_coef = ones(sample_pow.size())*sample_pow/sample_pow.size(); 
     244 
     245                                                poly_coef = poly_coef*en*fact(utility_constant-2+en)/fact(utility_constant-2); 
    237246 
    238247                                                if(abs(poly_coef)>numeric_limits<double>::epsilon()) 
    239248                                                { 
    240249                                                        sample_pow = elem_mult(sample_pow,sample_prediction); 
    241                                                         poly_coefs.ins(poly_coefs.size(),((-1)^en)*poly_coef); 
     250                                                        poly_coefs.ins(0,pow(-1.0,en+1)*poly_coef); 
    242251                                                } 
    243252                                                else 
     
    247256                                                 
    248257                                                en++; 
     258 
     259                                                if(en>20) 
     260                                                { 
     261                                                        stop_iteration = true; 
     262                                                } 
    249263                                        } 
    250264                                        while(!stop_iteration); 
     265 
     266                                        /* 
     267                                        ofstream myfile_coef;                                            
     268 
     269                                        myfile_coef.open("c:\\coefs.txt",ios::app); 
     270                                         
     271                                        for(int t = 0;t<poly_coefs.size();t++) 
     272                                        { 
     273                                                myfile_coef << poly_coefs[t] << ",";                                     
     274                                        } 
     275 
     276                                        myfile_coef << endl; 
     277                                        myfile_coef.close(); 
     278                                        */ 
    251279 
    252280                                        // cout << "Coefficients: " << poly_coefs << endl; 
    253281                                                                                 
     282                                        /* 
     283                                        vec bas_coef = vec("1.0 2.0 -8.0"); 
     284                                        cout << "Coefs: " << bas_coef << endl; 
     285                                        cvec actions2 = roots(bas_coef); 
     286                                        cout << "Roots: " << actions2 << endl; 
     287                                        */ 
     288                                         
    254289                                        cvec actions = roots(poly_coefs); 
     290                                         
     291 
    255292                                        bool is_max = false; 
    256293                                        for(int t = 0;t<actions.size();t++) 
    257294                                        { 
    258                                                 if(actions[t].imag() == 0 && actions[t].real()>-1 && actions[t].real()<1) 
     295                                                if(actions[t].imag() == 0) 
    259296                                                { 
    260                                                         cout << "Action:" << actions[t].real() << endl; 
    261                                                         is_max = true; 
     297                                                         
     298 
     299                                                        double second_derivative = 0; 
     300                                                        for(int q = 1;q<poly_coefs.size();q++) 
     301                                                        { 
     302                                                                second_derivative+=poly_coefs[q]*pow(actions[t].real(),q-1)*q; 
     303                                                        } 
     304 
     305                                                        if(second_derivative<0) 
     306                                                        { 
     307                                                                cout << "Action:" << actions[t].real() << endl; 
     308 
     309                                                                is_max = true; 
     310                                                        } 
    262311                                                } 
    263312                                        }