Changeset 1336 for applications

Show
Ignore:
Timestamp:
04/20/11 20:11:40 (13 years ago)
Author:
sindj
Message:

Sampling dokoncen, zbyva logaritmovat normalizacni faktor a testovat. JS

Location:
applications/robust
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/main.cpp

    r1331 r1336  
    134134        vector<vector<string>> strings; 
    135135 
    136         char* file_strings[3] = {"c:\\ar_cauchy_single.txt", "c:\\ar_normal_single.txt","c:\\ar_student_single.txt"}; 
     136        char* file_strings[3] = {"c:\\ar_student_single.txt", "c:\\ar_student_single.txt","c:\\ar_cauchy_single.txt"}; 
    137137 
    138138        for(int i = 0;i<3;i++) 
     
    162162                vector<vec> conditions; 
    163163                //emlig* emliga = new emlig(2); 
    164                 RARX* my_rarx = new RARX(2,70); 
     164                RARX* my_rarx = new RARX(3,20,true); 
    165165 
    166166                for(int k = 1;k<170;k++) 
     
    189189                                         
    190190                                 
    191                                 if(k>5) 
     191                                if(k>10) 
    192192                                { 
    193193                                        //my_rarx->posterior->step_me(0); 
    194194 
    195                                         my_rarx->posterior->sample_mat(1); 
    196  
     195                                        mat samples = my_rarx->posterior->sample_mat(50); 
     196                                         
     197                                         
    197198                                        cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl; 
    198199 
    199                                         ofstream myfile; 
    200                                         char fstring[80]; 
    201                                         strcpy(fstring,file_strings[j]); 
    202                                         strcat(fstring,"_res.txt"); 
    203  
    204                                         myfile.open(fstring,ios::app); 
    205                                         myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; 
    206                                         if(k!=strings[j].size()-1) 
     200                                        for(int s = 0;s<samples.rows();s++) 
    207201                                        { 
    208                                                 myfile << ","; 
     202                                                 
     203                                                double avg_parameter = samples.get_row(s)*ones(samples.cols())/samples.cols();                                           
     204                                                 
     205                                                ofstream myfile; 
     206                                                char fstring[80]; 
     207                                                strcpy(fstring,file_strings[j]); 
     208 
     209                                                char es[5]; 
     210                                                strcat(fstring,itoa(s,es,10)); 
     211 
     212                                                strcat(fstring,"_res.txt"); 
     213                                                 
     214 
     215                                                myfile.open(fstring,ios::app); 
     216                                                 
     217                                                //myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; 
     218                                                myfile << avg_parameter; 
     219                                                 
     220                                                if(k!=strings[j].size()-1) 
     221                                                { 
     222                                                        myfile << ","; 
     223                                                } 
     224                                                else 
     225                                                { 
     226                                                        myfile << endl; 
     227                                                } 
     228                                                myfile.close(); 
    209229                                        } 
    210                                         else 
    211                                         { 
    212                                                 myfile << endl; 
    213                                         } 
    214                                         myfile.close(); 
    215230                                }                                        
    216231                        }        
  • applications/robust/robustlib.h

    r1335 r1336  
    1717#include <algorithm> 
    1818         
    19 //using namespace bdm; 
     19using namespace bdm; 
    2020using namespace std; 
    2121using namespace itpp; 
     
    18311831                        while(sample_mat.cols()<n) 
    18321832                        { 
    1833                                 cout << "*************************************" << endl; 
     1833                                //// cout << "*************************************" << endl; 
     1834 
     1835                                 
    18341836                                 
    18351837                                double rnumber = randu()*sum_a; 
     
    18521854                                }                                
    18531855 
    1854                                 cout << "Toprow/Count: " << toprow_count << "/" << ordered_toprows.size() << endl; 
     1856                                //// cout << "Toprow/Count: " << toprow_count << "/" << ordered_toprows.size() << endl; 
    18551857                                // cout << &sampled_toprow << ";"; 
    18561858 
     
    18701872                                } 
    18711873 
    1872                                 cout << "Simplex/Count: " << simplex_count << "/" << sampled_toprow->triangulation.size() << endl; 
    1873                                 cout << "Simplex factor: " << (*s_ref)->probability << endl; 
    1874                                 cout << "Toprow factor:  " << sampled_toprow->probability << endl; 
    1875                                 cout << "Emlig factor:   " << normalization_factor << endl; 
     1874                                //// cout << "Simplex/Count: " << simplex_count << "/" << sampled_toprow->triangulation.size() << endl; 
     1875                                //// cout << "Simplex factor: " << (*s_ref)->probability << endl; 
     1876                                //// cout << "Toprow factor:  " << sampled_toprow->probability << endl; 
     1877                                //// cout << "Emlig factor:   " << normalization_factor << endl; 
    18761878                                // cout << &(*tri_ref) << endl; 
    18771879 
     
    18931895                                                        if(sum_g>rnumber) 
    18941896                                                        { 
    1895                                                                 itpp::Gamma_RNG* gamma = new itpp::Gamma_RNG(conditions.size()-number_of_parameters,1/(*g_ref).second); 
    1896                                                                 sigma = 1/(*gamma)(); 
     1897                                                                //itpp::Gamma_RNG* gamma = new itpp::Gamma_RNG(conditions.size()-number_of_parameters,1/(*g_ref).second); 
     1898                                                                //sigma = 1/(*gamma)(); 
    18971899                                                                 
    1898                                                                 cout << "Sigma mean:   " << (*g_ref).second/(conditions.size()-number_of_parameters-1) << endl;                                                          
     1900                                                                GamRNG.setup(conditions.size()-number_of_parameters,(*g_ref).second); 
     1901                                                                                                                                 
     1902                                                                sigma = 1/GamRNG(); 
     1903 
     1904                                                                // cout << "Sigma mean:   " << (*g_ref).second/(conditions.size()-number_of_parameters-1) << endl;                                                               
    18991905                                                                break; 
    19001906                                                        }                                                        
     
    19361942                                while(!have_sigma); 
    19371943 
    1938                                 cout << "Sigma: " << sigma << endl; 
    1939                                 cout << "Nr. of runs: " << number_of_runs << endl; 
     1944                                //// cout << "Sigma: " << sigma << endl; 
     1945                                //// cout << "Nr. of runs: " << number_of_runs << endl; 
    19401946 
    19411947                                int dimension = (*s_ref)->vertices.size()-1; 
     
    19461952                                vertex* base_vert = *(*s_ref)->vertices.begin(); 
    19471953 
    1948                                 cout << "Base vertex coords(should be close to est. param.): " << base_vert->get_coordinates() << endl; 
     1954                                //// cout << "Base vertex coords(should be close to est. param.): " << base_vert->get_coordinates() << endl; 
    19491955                                 
    19501956                                int row_count = 0; 
     
    19541960                                        vec current_coords = (*vert_ref)->get_coordinates(); 
    19551961 
    1956                                         cout << "Coords of vertex[" << row_count << "]: " << current_coords << endl;  
     1962                                        //// cout << "Coords of vertex[" << row_count << "]: " << current_coords << endl;  
    19571963                                         
    19581964                                        vec relative_coords = current_coords-base_vert->get_coordinates();                               
     
    19631969                                }                                
    19641970                                 
    1965                                 cout << "Jacobian: " << jacobian << endl; 
    1966  
    1967                                 cout << "Gradient before trafo:" << gradient << endl; 
     1971                                //// cout << "Jacobian: " << jacobian << endl; 
     1972 
     1973                                //// cout << "Gradient before trafo:" << gradient << endl; 
    19681974                                                                 
    19691975                                gradient = jacobian*gradient;    
    19701976 
    1971                                 cout << "Gradient after trafo:" << gradient << endl; 
     1977                                //// cout << "Gradient after trafo:" << gradient << endl; 
    19721978 
    19731979                                // vec normal_gradient = gradient/sqrt(gradient*gradient); 
     
    20092015                                extended_rotation.ins_col(0,zeros(extended_rotation.rows())); 
    20102016 
    2011                                 cout << "Extended rotation: " << extended_rotation << endl; 
     2017                                //// cout << "Extended rotation: " << extended_rotation << endl; 
    20122018                                 
    20132019                                vec minima = itpp::min(extended_rotation,2); 
    20142020                                vec maxima = itpp::max(extended_rotation,2); 
    20152021 
    2016                                 cout << "Minima: " << minima << endl; 
    2017                                 cout << "Maxima: " << maxima << endl; 
     2022                                //// cout << "Minima: " << minima << endl; 
     2023                                //// cout << "Maxima: " << maxima << endl; 
    20182024 
    20192025                                vec sample_coordinates;          
     
    20332039                                                vec new_gradient = rotation_matrix*gradient; 
    20342040                                                 
    2035                                                 cout << "New gradient(should have only first component nonzero):" << new_gradient << endl; 
     2041                                                //// cout << "New gradient(should have only first component nonzero):" << new_gradient << endl; 
    20362042 
    20372043                                                // cout << "Max: " << maxima[0] << "  Min: " << minima[0] << "  Grad:" << new_gradient[0] << endl; 
     
    20492055                                } 
    20502056 
    2051                                 cout << "Sampled coordinates(gradient direction): " << sample_coordinates << endl; 
     2057                                //// cout << "Sampled coordinates(gradient direction): " << sample_coordinates << endl; 
    20522058 
    20532059                                sample_coordinates = rotation_matrix.T()*sample_coordinates; 
    20542060 
    2055                                 cout << "Sampled coordinates(backrotated direction):" << sample_coordinates << endl; 
     2061                                //// cout << "Sampled coordinates(backrotated direction):" << sample_coordinates << endl; 
    20562062 
    20572063                                 
     
    20772083                                        sample_coordinates.ins(0,sigma); 
    20782084                                         
    2079                                         cout << "Sampled coordinates(parameter space):" << sample_coordinates << endl; 
     2085                                        //// cout << "Sampled coordinates(parameter space):" << sample_coordinates << endl; 
    20802086 
    20812087                                        sample_mat.ins_col(0,sample_coordinates); 
     2088 
     2089                                        cout << sample_mat.cols() << ","; 
    20822090                                } 
    20832091 
     
    20882096                        } 
    20892097 
     2098                        cout << endl; 
    20902099                        return sample_mat; 
    20912100                } 
     
    24052414{ 
    24062415private: 
    2407  
    2408          
     2416        bool has_constant; 
    24092417 
    24102418        int window_size;         
     
    24152423        emlig* posterior; 
    24162424 
    2417         RARX(int number_of_parameters, const int window_size)//:BM() 
    2418         { 
     2425        RARX(int number_of_parameters, const int window_size, bool has_constant)//:BM() 
     2426        { 
     2427                this->has_constant = has_constant; 
     2428                 
    24192429                posterior = new emlig(number_of_parameters); 
    24202430 
     
    24222432        }; 
    24232433 
    2424         void bayes(const itpp::vec &yt, const itpp::vec &cond = "") 
    2425         { 
    2426                 conditions.push_back(yt);                
     2434        void bayes(itpp::vec yt) 
     2435        { 
     2436                if(has_constant) 
     2437                { 
     2438                        int c_size = yt.size(); 
     2439                         
     2440                        yt.ins(c_size,1.0); 
     2441                } 
     2442                 
     2443                if(yt.size() == posterior->number_of_parameters+1) 
     2444                { 
     2445                        conditions.push_back(yt);                
     2446                } 
     2447                else 
     2448                { 
     2449                        throw new exception("Wrong condition size for bayesian data update!"); 
     2450                } 
    24272451 
    24282452                //posterior->step_me(0);