Changeset 1343 for applications/robust

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

Importance sampling. JS

Location:
applications/robust
Files:
2 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                                        } 
  • applications/robust/robustlib.h

    r1338 r1343  
    113113                if(weight>=0) 
    114114                { 
    115                         if(positive_gamma_parameters.size()<order+1) 
     115                        while(positive_gamma_parameters.size()<order+1) 
    116116                        { 
    117117                                multimap<double,double> map; 
     
    125125                else 
    126126                { 
    127                         if(negative_gamma_parameters.size()<order+1) 
     127                        while(negative_gamma_parameters.size()<order+1) 
    128128                        { 
    129129                                multimap<double,double> map; 
     
    15771577                        sizevector.push_back(statistic.row_size(s)); 
    15781578                        cout << statistic.row_size(s) << ", "; 
    1579                 } 
    1580                 */ 
     1579                }*/ 
     1580                 
    15811581 
    15821582                //cout << endl; 
     
    17391739                } 
    17401740 
    1741                 /* 
    1742                 sizevector.clear(); 
     1741                vector<int> sizevector; 
     1742                //sizevector.clear(); 
    17431743                for(int s = 0;s<statistic.size();s++) 
    17441744                { 
     
    17481748                 
    17491749                cout << endl; 
    1750                 */ 
     1750                 
    17511751 
    17521752                /* 
     
    18131813                } 
    18141814 
    1815  
     1815        pair<vec,simplex*> choose_simplex() 
     1816        { 
     1817                double rnumber = randu(); 
     1818 
     1819                // cout << "RND:" << rnumber << endl; 
     1820 
     1821                // This could be more efficient (log n), but map::upper_bound() doesn't let me dereference returned iterator 
     1822                double  prob_sum     = 0;        
     1823                toprow* sampled_toprow;                          
     1824                for(polyhedron* top_ref = statistic.rows[statistic.size()-1];top_ref!=statistic.row_ends[statistic.size()-1];top_ref=top_ref->next_poly) 
     1825                { 
     1826                        // cout << "CDF:"<< (*top_ref).first << endl; 
     1827 
     1828                        toprow* current_toprow = ((toprow*)top_ref); 
     1829 
     1830                        prob_sum += current_toprow->probability; 
     1831 
     1832                        if(prob_sum >= rnumber*normalization_factor) 
     1833                        { 
     1834                                sampled_toprow = (toprow*)top_ref; 
     1835                                break; 
     1836                        }                                                
     1837                }                                
     1838 
     1839                //// cout << "Toprow/Count: " << toprow_count << "/" << ordered_toprows.size() << endl; 
     1840                // cout << &sampled_toprow << ";"; 
     1841 
     1842                rnumber = randu();                               
     1843 
     1844                set<simplex*>::iterator s_ref; 
     1845                prob_sum = 0;            
     1846                for(s_ref = sampled_toprow->triangulation.begin();s_ref!=sampled_toprow->triangulation.end();s_ref++) 
     1847                {                
     1848                        prob_sum += (*s_ref)->probability; 
     1849 
     1850                        if(prob_sum/sampled_toprow->probability >= rnumber) 
     1851                                break; 
     1852                } 
     1853 
     1854                return pair<vec,simplex*>(sampled_toprow->condition_sum,*s_ref);         
     1855        } 
     1856 
     1857        pair<double,double> choose_sigma(simplex* sampled_simplex) 
     1858        { 
     1859                double rnumber = randu(); 
     1860                double sigma; 
     1861                                         
     1862                double sum_g = 0; 
     1863                for(int i = 0;i<sampled_simplex->positive_gamma_parameters.size();i++) 
     1864                { 
     1865                        for(multimap<double,double>::iterator g_ref = sampled_simplex->positive_gamma_parameters[i].begin();g_ref != sampled_simplex->positive_gamma_parameters[i].end();g_ref++) 
     1866                        { 
     1867                                sum_g += (*g_ref).first/sampled_simplex->positive_gamma_sum; 
     1868 
     1869                                                         
     1870                                if(sum_g>rnumber) 
     1871                                { 
     1872                                        //itpp::Gamma_RNG* gamma = new itpp::Gamma_RNG(conditions.size()-number_of_parameters,1/(*g_ref).second); 
     1873                                        //sigma = 1/(*gamma)(); 
     1874                                                                 
     1875                                        GamRNG.setup(conditions.size()-number_of_parameters,(*g_ref).second); 
     1876                                                                                                                                 
     1877                                        sigma = 1/GamRNG(); 
     1878 
     1879                                        // cout << "Sigma mean:   " << (*g_ref).second/(conditions.size()-number_of_parameters-1) << endl;                                                               
     1880                                        break; 
     1881                                }                                                        
     1882                        } 
     1883 
     1884                        if(sigma!=0) 
     1885                        { 
     1886                                break; 
     1887                        } 
     1888                } 
     1889 
     1890                rnumber = randu(); 
     1891 
     1892                double pg_sum = 0; 
     1893                for(vector<multimap<double,double>>::iterator v_ref = sampled_simplex->positive_gamma_parameters.begin();v_ref!=sampled_simplex->positive_gamma_parameters.end();v_ref++) 
     1894                { 
     1895                        for(multimap<double,double>::iterator pg_ref = (*v_ref).begin();pg_ref!=(*v_ref).end();pg_ref++) 
     1896                        { 
     1897                                pg_sum += exp((sampled_simplex->min_beta-(*pg_ref).second)/sigma)*pow((*pg_ref).second/sigma,(int)conditions.size()-number_of_parameters-1)*(*pg_ref).second/fact(conditions.size()-number_of_parameters-1)*(*pg_ref).first; 
     1898                        }                                        
     1899                } 
     1900 
     1901                double ng_sum = 0; 
     1902                for(vector<multimap<double,double>>::iterator v_ref = sampled_simplex->negative_gamma_parameters.begin();v_ref!=sampled_simplex->negative_gamma_parameters.end();v_ref++) 
     1903                { 
     1904                        for(multimap<double,double>::iterator ng_ref = (*v_ref).begin();ng_ref!=(*v_ref).end();ng_ref++) 
     1905                        { 
     1906                                ng_sum += exp((sampled_simplex->min_beta-(*ng_ref).second)/sigma)*pow((*ng_ref).second/sigma,(int)conditions.size()-number_of_parameters-1)*(*ng_ref).second/fact(conditions.size()-number_of_parameters-1)*(*ng_ref).first; 
     1907                        }                                        
     1908                } 
     1909 
     1910                return pair<double,double>((pg_sum-ng_sum)/pg_sum,sigma); 
     1911        } 
    18161912 
    18171913        mat sample_mat(int n) 
     
    21142210                        } 
    21152211 
    2116                         // cout << endl; 
     2212                        cout << endl; 
    21172213                        return sample_mat; 
    21182214                } 
     
    21252221 
    21262222                 
     2223        } 
     2224 
     2225        pair<vec,mat> importance_sample(int n) 
     2226        { 
     2227                vec probabilities; 
     2228                mat samples; 
     2229                 
     2230                for(int i = 0;i<n;i++) 
     2231                { 
     2232                        pair<vec,simplex*> condition_and_simplex = choose_simplex(); 
     2233 
     2234                        pair<double,double> probability_and_sigma = choose_sigma(condition_and_simplex.second); 
     2235 
     2236                        int dimension = condition_and_simplex.second->vertices.size()-1; 
     2237 
     2238                        mat jacobian(dimension,dimension); 
     2239                        vec gradient = condition_and_simplex.first.right(dimension); 
     2240 
     2241                        vertex* base_vert = *condition_and_simplex.second->vertices.begin(); 
     2242 
     2243                        //// cout << "Base vertex coords(should be close to est. param.): " << base_vert->get_coordinates() << endl; 
     2244                                 
     2245                        int row_count = 0; 
     2246 
     2247                        for(set<vertex*>::iterator vert_ref = ++condition_and_simplex.second->vertices.begin();vert_ref!=condition_and_simplex.second->vertices.end();vert_ref++) 
     2248                        { 
     2249                                vec current_coords = (*vert_ref)->get_coordinates(); 
     2250 
     2251                                //// cout << "Coords of vertex[" << row_count << "]: " << current_coords << endl;  
     2252                                         
     2253                                vec relative_coords = current_coords-base_vert->get_coordinates();                               
     2254 
     2255                                jacobian.set_row(row_count,relative_coords); 
     2256 
     2257                                row_count++; 
     2258                        }                                
     2259                                 
     2260                        //// cout << "Jacobian: " << jacobian << endl;                   
     2261 
     2262                        /// \todo Is this correct? Are the random coordinates really jointly uniform? I don't know. 
     2263                        vec sample_coords; 
     2264                        double sampling_diff = 1; 
     2265                        for(int j = 0;j<number_of_parameters;j++) 
     2266                        { 
     2267                                double rnumber = randu()*sampling_diff; 
     2268 
     2269                                sample_coords.ins(0,rnumber); 
     2270 
     2271                                sampling_diff -= rnumber; 
     2272                        } 
     2273 
     2274                        sample_coords = jacobian.T()*sample_coords+(*base_vert).get_coordinates(); 
     2275 
     2276                        vec extended_coords = sample_coords; 
     2277                        extended_coords.ins(0,-1.0); 
     2278 
     2279                        double sample_prob = 1/condition_and_simplex.second->probability/pow(probability_and_sigma.second,(int)conditions.size()-number_of_parameters)*exp(1/probability_and_sigma.second*(extended_coords*condition_and_simplex.first)); 
     2280                        sample_prob *= probability_and_sigma.first; 
     2281 
     2282                        sample_coords.ins(0,probability_and_sigma.second); 
     2283 
     2284                        samples.ins_col(0,sample_coords); 
     2285                        probabilities.ins(0,sample_prob); 
     2286                } 
     2287         
     2288                return pair<vec,mat>(probabilities,samples); 
    21272289        } 
    21282290