Changeset 1324 for applications/robust

Show
Ignore:
Timestamp:
04/07/11 18:33:51 (13 years ago)
Author:
sindj
Message:

Temer dodelane samplovani, rozdelany samling sigma, sampling alpha temer dokoncen. JS

Location:
applications/robust
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/main.cpp

    r1319 r1324  
    134134        vector<vector<string>> strings; 
    135135 
    136         char* file_strings[3] = {"c:\\ar_student_single.txt", "c:\\ar_cauchy_single.txt","c:\\ar_normal_single.txt"}; 
     136        char* file_strings[3] = {"c:\\ar_cauchy_single.txt", "c:\\ar_normal_single.txt","c:\\ar_student_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,30); 
     164                RARX* my_rarx = new RARX(2,70); 
    165165 
    166166                for(int k = 1;k<170;k++) 
     
    191191                                if(k>5) 
    192192                                { 
     193                                        //my_rarx->posterior->step_me(0); 
     194 
     195                                        //my_rarx->posterior->sample_mat(1); 
     196 
    193197                                        cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl; 
    194198 
  • applications/robust/robustlib.cpp

    r1320 r1324  
    44void polyhedron::triangulate(bool should_integrate) 
    55        { 
     6                for(set<simplex*>::iterator t_ref = triangulation.begin();t_ref!=triangulation.end();t_ref++) 
     7                { 
     8                        delete (*t_ref); 
     9                }                
    610                triangulation.clear(); 
    711 
     
    1317                if(vertices.size()==1) 
    1418                { 
    15                         set<vertex*> vert_simplex; 
    16                         vert_simplex.insert((vertex*)this); 
    17  
    18                         triangulation.insert(pair<double,set<vertex*>>(0,vert_simplex)); 
     19                        simplex* vert_simplex = new simplex((vertex*)this);                      
     20 
     21                        triangulation.insert(vert_simplex); 
    1922                } 
    2023 
    2124                for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++) 
    2225                {                        
    23                         for(map<double,set<vertex*>>::iterator t_ref = (*child_ref)->triangulation.begin();t_ref!=(*child_ref)->triangulation.end();t_ref++) 
    24                         { 
    25                                 set<vertex*> new_simplex; 
    26                                 new_simplex.insert((*t_ref).second.begin(),(*t_ref).second.end());                               
    27  
    28                                 pair<set<vertex*>::iterator,bool> ret_val = new_simplex.insert(*vertices.begin()); 
     26                        for(set<simplex*>::iterator s_ref = (*child_ref)->triangulation.begin();s_ref!=(*child_ref)->triangulation.end();s_ref++) 
     27                        { 
     28                                simplex* new_simplex = new simplex((*s_ref)->vertices);                                                          
     29 
     30                                pair<set<vertex*>::iterator,bool> ret_val = new_simplex->vertices.insert(*vertices.begin()); 
    2931 
    3032                                if(ret_val.second == true) 
     
    3941                                        } 
    4042 
    41                                         triangulation.insert(pair<double,set<vertex*>>(cur_prob,new_simplex)); 
    42                                 }  
     43                                        triangulation.insert(new_simplex); 
     44                                } 
     45                                else 
     46                                { 
     47                                        delete new_simplex; 
     48                                } 
    4349                        }        
    4450                }                
     
    7177 
    7278 
    73         double toprow::integrate_simplex(set<vertex*> simplex, char c) 
     79        double toprow::integrate_simplex(simplex* simplex, char c) 
    7480        { 
    7581                // cout << ((toprow*)this)->condition << endl;           
     
    107113                        // cout << as_toprow->condition << endl;                         
    108114 
    109                         int dimension = simplex.size()-1; 
     115                        int dimension = simplex->vertices.size()-1; 
    110116 
    111117                        mat jacobian(dimension,dimension);                       
     
    114120                        map<double,int> as; 
    115121                        vertex* base_vertex; 
    116                         set<vertex*>::iterator base_ref = simplex.begin(); 
     122                        set<vertex*>::iterator base_ref = simplex->vertices.begin(); 
    117123                        bool order_correct; 
    118124                                                 
     
    132138 
    133139                                int row_count = 0; 
    134                                 for(set<vertex*>::iterator vert_ref = simplex.begin(); vert_ref!=simplex.end();vert_ref++) 
     140                                for(set<vertex*>::iterator vert_ref = simplex->vertices.begin(); vert_ref!=simplex->vertices.end();vert_ref++) 
    135141                                { 
    136142                                        if(vert_ref != base_ref) 
     
    146152                                                        base_ref++; 
    147153 
    148                                                         if(base_ref == simplex.end()) 
     154                                                        if(base_ref == simplex->vertices.end()) 
    149155                                                        { 
    150156                                                                throw new exception("Equal local conditions are paired. If this ever occurs, the software has to be modified to include multiplied a_0!!"); 
     
    285291                 
    286292 
    287                         //cout << "Probability:" << int_value << endl; 
    288                          
    289                         return int_value; 
    290  
    291                          
     293                        // cout << "Probability:" << int_value << endl; 
     294                        // pause(0.100); 
     295 
     296                        simplex->probability = int_value; 
     297                         
     298                        return int_value;        
    292299                         
    293300                } 
  • applications/robust/robustlib.h

    r1320 r1324  
    3131class emlig; 
    3232 
     33 
    3334/* 
    3435class t_simplex 
     
    6162                this->value = value; 
    6263                multiplicity = 1; 
     64        } 
     65}; 
     66 
     67class simplex 
     68{ 
     69public: 
     70 
     71        set<vertex*> vertices; 
     72 
     73        double probability; 
     74 
     75        vector<list<pair<double,double>>> gamma_parameters; 
     76 
     77        simplex(set<vertex*> vertices) 
     78        { 
     79                this->vertices.insert(vertices.begin(),vertices.end()); 
     80                probability = 0; 
     81        } 
     82 
     83        simplex(vertex* vertex) 
     84        { 
     85                this->vertices.insert(vertex); 
     86                probability = 0; 
    6387        } 
    6488}; 
     
    164188 
    165189        /// List of triangulation polyhedrons of the polyhedron given by their relative vertices.  
    166         map<double,set<vertex*>> triangulation; 
     190        set<simplex*> triangulation; 
    167191 
    168192        /// A list of relative addresses serving for Hasse diagram construction. 
     
    275299                vertices.insert(this); 
    276300 
    277                 set<vertex*> vert_simplex; 
    278  
    279                 vert_simplex.insert(this); 
    280  
    281                 triangulation.insert(pair<double,set<vertex*>>(0,vert_simplex)); 
     301                simplex* vert_simplex = new simplex(vertices);           
     302 
     303                triangulation.insert(vert_simplex); 
    282304        } 
    283305 
     
    330352        } 
    331353 
    332         double integrate_simplex(set<vertex*> simplex, char c); 
     354        double integrate_simplex(simplex* simplex, char c); 
    333355 
    334356}; 
     357 
    335358 
    336359 
     
    774797                                                                cur_par_toprow->probability = 0.0; 
    775798                                                                 
    776                                                                 map<double,set<vertex*>> new_triangulation; 
    777  
    778                                                                 for(map<double,set<vertex*>>::iterator t_ref = current_parent->triangulation.begin();t_ref!=current_parent->triangulation.end();t_ref++) 
     799                                                                //set<simplex*> new_triangulation; 
     800 
     801                                                                for(set<simplex*>::iterator s_ref = current_parent->triangulation.begin();s_ref!=current_parent->triangulation.end();s_ref++) 
    779802                                                                { 
    780                                                                         double cur_prob = cur_par_toprow->integrate_simplex((*t_ref).second,'C'); 
     803                                                                        double cur_prob = cur_par_toprow->integrate_simplex((*s_ref),'C'); 
    781804                                                                         
    782805                                                                        cur_par_toprow->probability += cur_prob; 
    783806 
    784                                                                         new_triangulation.insert(pair<double,set<vertex*>>(cur_prob,(*t_ref).second)); 
     807                                                                        //new_triangulation.insert(pair<double,set<vertex*>>(cur_prob,(*t_ref).second)); 
    785808                                                                } 
    786809 
    787                                                                 current_parent->triangulation.clear(); 
    788                                                                 current_parent->triangulation.insert(new_triangulation.begin(),new_triangulation.end()); 
     810                                                                //current_parent->triangulation.clear(); 
     811                                                                //current_parent->triangulation.insert(new_triangulation.begin(),new_triangulation.end()); 
    789812                                                        } 
    790813                                                } 
     
    879902                                                                cur_par_toprow->probability = 0.0; 
    880903                                                                         
    881                                                                 map<double,set<vertex*>> new_triangulation; 
     904                                                                //map<double,set<vertex*>> new_triangulation; 
    882905                                                                 
    883                                                                 for(map<double,set<vertex*>>::iterator t_ref = current_parent->triangulation.begin();t_ref!=current_parent->triangulation.end();t_ref++) 
     906                                                                for(set<simplex*>::iterator s_ref = current_parent->triangulation.begin();s_ref!=current_parent->triangulation.end();s_ref++) 
    884907                                                                { 
    885                                                                         double cur_prob = cur_par_toprow->integrate_simplex((*t_ref).second,'C'); 
     908                                                                        double cur_prob = cur_par_toprow->integrate_simplex((*s_ref),'C'); 
    886909                                                                         
    887910                                                                        cur_par_toprow->probability += cur_prob; 
    888911 
    889                                                                         new_triangulation.insert(pair<double,set<vertex*>>(cur_prob,(*t_ref).second)); 
     912                                                                        //new_triangulation.insert(pair<double,set<vertex*>>(cur_prob,(*t_ref).second)); 
    890913                                                                } 
    891914 
    892                                                                 current_parent->triangulation.clear(); 
    893                                                                 current_parent->triangulation.insert(new_triangulation.begin(),new_triangulation.end()); 
     915                                                                //current_parent->triangulation.clear(); 
     916                                                                //current_parent->triangulation.insert(new_triangulation.begin(),new_triangulation.end()); 
    894917                                                        } 
    895918 
     
    960983                for(int i = 0;i<statistic.size();i++) 
    961984                { 
    962                         int zero = 0; 
    963                         int one  = 0; 
    964                         int two  = 0; 
     985                        //int zero = 0; 
     986                        //int one  = 0; 
     987                        //int two  = 0; 
    965988 
    966989                        for(polyhedron* horiz_ref = statistic.rows[i];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) 
    967990                        { 
     991                                 
     992                                 
     993                                if(i==statistic.size()-1) 
     994                                { 
     995                                        cout << ((toprow*)horiz_ref)->condition_sum << "   " << ((toprow*)horiz_ref)->probability << endl; 
     996                                        cout << "Order:" << ((toprow*)horiz_ref)->condition_order << endl; 
     997                                } 
    968998                                 
    969999                                /* 
    970                                 if(i==statistic.size()-1) 
    971                                 { 
    972                                         //cout << ((toprow*)horiz_ref)->condition_sum << "   " << ((toprow*)horiz_ref)->probability << endl; 
    973                                         cout << "Order:" << ((toprow*)horiz_ref)->condition_order << endl; 
    974                                 } 
    9751000                                if(i==0) 
    9761001                                { 
     
    9791004                                */ 
    9801005 
     1006                                /* 
    9811007                                char* string = "Checkpoint"; 
    9821008 
     1009 
    9831010                                if((*horiz_ref).parentconditions.size()==0) 
    9841011                                { 
     
    9931020                                        two++; 
    9941021                                } 
     1022                                */ 
    9951023                                 
    9961024                        } 
     
    10701098        void add_and_remove_condition(vec toadd, vec toremove) 
    10711099        { 
    1072                 step_me(0); 
     1100                //step_me(0); 
    10731101                 
    10741102                likelihood_value = numeric_limits<double>::max(); 
     
    17121740        mat sample_mat(int n) 
    17131741        { 
    1714                 mat sample_mat; 
    1715                 map<double,toprow*> ordered_toprows; 
     1742                 
     1743                 
    17161744 
    17171745                /// \TODO tady je to spatne, tady nesmi byt conditions.size(), viz RARX.bayes() 
    17181746                if(conditions.size()-2-number_of_parameters>=0) 
    17191747                { 
    1720                         double sum = 0; 
    1721  
    1722                         for(polyhedron* top_ref = statistic.rows[number_of_parameters-1];top_ref!=statistic.end_poly;top_ref=top_ref->next_poly) 
     1748                        mat sample_mat; 
     1749                        map<double,toprow*> ordered_toprows;                     
     1750                        double sum_a = 0; 
     1751                         
     1752 
     1753                        for(polyhedron* top_ref = statistic.rows[number_of_parameters];top_ref!=statistic.end_poly;top_ref=top_ref->next_poly) 
    17231754                        { 
    17241755                                toprow* current_top = (toprow*)top_ref; 
    17251756 
    1726                                 sum+=current_top->probability; 
    1727                                 ordered_toprows.insert(pair<double,toprow*>(sum,current_top)); 
    1728                         } 
     1757                                sum_a+=current_top->probability; 
     1758                                ordered_toprows.insert(pair<double,toprow*>(sum_a,current_top)); 
     1759                        } 
     1760 
     1761                        while(sample_mat.cols()<n) 
     1762                        { 
     1763                                double rnumber = randu()*sum_a; 
     1764 
     1765                                // cout << "RND:" << rnumber << endl; 
     1766 
     1767                                // This could be more efficient (log n), but map::upper_bound() doesn't let me dereference returned iterator 
     1768                                toprow* sampled_toprow;                          
     1769                                for(map<double,toprow*>::iterator top_ref = ordered_toprows.begin();top_ref!=ordered_toprows.end();top_ref++) 
     1770                                { 
     1771                                        // cout << "CDF:"<< (*top_ref).first << endl; 
     1772 
     1773                                        if((*top_ref).first >= rnumber) 
     1774                                        { 
     1775                                                sampled_toprow = (*top_ref).second; 
     1776                                                break; 
     1777                                        }                                                
     1778                                }                                
     1779 
     1780                                // cout << &sampled_toprow << ";"; 
     1781 
     1782                                rnumber = randu();                               
     1783 
     1784                                set<simplex*>::iterator s_ref; 
     1785                                double sum_b = 0; 
     1786 
     1787                                for(s_ref = sampled_toprow->triangulation.begin();s_ref!=sampled_toprow->triangulation.end();s_ref++) 
     1788                                { 
     1789                                        sum_b += (*s_ref)->probability; 
     1790 
     1791                                        if(sum_b/sampled_toprow->probability >= rnumber) 
     1792                                                break; 
     1793                                } 
     1794 
     1795                                // cout << &(*tri_ref) << endl; 
     1796 
     1797                                int dimension = (*s_ref)->vertices.size()-1; 
     1798 
     1799                                mat jacobian(dimension,dimension); 
     1800                                vec gradient = sampled_toprow->condition_sum.right(dimension); 
     1801 
     1802                                vertex* base_vert = *(*s_ref)->vertices.begin(); 
     1803                                 
     1804                                int row_count = 0; 
     1805 
     1806                                for(set<vertex*>::iterator vert_ref = ++(*s_ref)->vertices.begin();vert_ref!=(*s_ref)->vertices.end();vert_ref++) 
     1807                                { 
     1808                                        jacobian.set_row(row_count,(*vert_ref)->get_coordinates()-base_vert->get_coordinates()); 
     1809 
     1810                                        row_count++; 
     1811                                } 
     1812 
     1813                                // cout << gradient << endl; 
     1814                                // cout << jacobian << endl; 
     1815 
     1816                                gradient = jacobian*gradient;    
     1817 
     1818                                //cout << gradient << endl; 
     1819 
     1820                                mat rotation_matrix = eye(dimension); 
     1821 
     1822                                double squared_length = 1; 
     1823 
     1824                                for(int i = 1;i<dimension;i++) 
     1825                                { 
     1826                                        double t = gradient[i]/sqrt(squared_length); 
     1827 
     1828                                        double sin_theta = t/sqrt(1+pow(t,2)); 
     1829                                        double cos_theta = 1/sqrt(1+pow(t,2)); 
     1830 
     1831                                        mat partial_rotation = eye(dimension); 
     1832 
     1833                                        partial_rotation.set(0,0,cos_theta); 
     1834                                        partial_rotation.set(i,i,cos_theta); 
     1835                                         
     1836                                        partial_rotation.set(0,i,sin_theta); 
     1837                                        partial_rotation.set(i,0,-sin_theta); 
     1838                                         
     1839                                        rotation_matrix = rotation_matrix*partial_rotation; 
     1840                                         
     1841                                        squared_length += pow(gradient[i],2); 
     1842                                } 
     1843 
     1844                                // cout << rotation_matrix << endl; 
     1845                                // cout << gradient << endl; 
     1846 
     1847                                vec minima = numeric_limits<double>::max()*ones(number_of_parameters); 
     1848                                vec maxima = -numeric_limits<double>::max()*ones(number_of_parameters); 
     1849 
     1850                                vertex* min_grad; 
     1851                                vertex* max_grad; 
     1852                                 
     1853                                for(set<vertex*>::iterator vert_ref = (*s_ref)->vertices.begin();vert_ref!=(*s_ref)->vertices.end();vert_ref++) 
     1854                                { 
     1855                                        vec new_coords = rotation_matrix*jacobian*(*vert_ref)->get_coordinates(); 
     1856 
     1857                                        // cout << new_coords << endl; 
     1858 
     1859                                        for(int j = 0;j<new_coords.size();j++) 
     1860                                        { 
     1861                                                if(new_coords[j]>maxima[j]) 
     1862                                                { 
     1863                                                        maxima[j] = new_coords[j]; 
     1864 
     1865                                                        if(j==0) 
     1866                                                        { 
     1867                                                                max_grad = (*vert_ref); 
     1868                                                        } 
     1869                                                } 
     1870 
     1871                                                if(new_coords[j]<minima[j]) 
     1872                                                { 
     1873                                                        minima[j] = new_coords[j]; 
     1874 
     1875                                                        if(j==0) 
     1876                                                        { 
     1877                                                                min_grad = (*vert_ref); 
     1878                                                        } 
     1879                                                } 
     1880                                        }                                        
     1881                                } 
     1882 
     1883                                vec sample_coordinates; 
     1884 
     1885                                for(int j = 0;j<number_of_parameters;j++) 
     1886                                { 
     1887                                        rnumber = randu(); 
     1888                                         
     1889                                        double coordinate; 
     1890 
     1891                                        if(j==0) 
     1892                                        { 
     1893                                                double pos_value = sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*max_grad->get_coordinates()-sampled_toprow->condition_sum[0]; 
     1894                                                 
     1895                                                 
     1896                                                //coordinate = log(rnumber*(exp(sampled_toprow->condition_sum.size()-1)*max_grad->get_coordinates()-sampled_toprow->condition_sum[0])-exp(sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*min_grad->get_coordinates()-sampled_toprow->condition_sum[0]))+ 
     1897                                        } 
     1898                                } 
     1899 
     1900                                // cout << sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*min_grad->get_coordinates()-sampled_toprow->condition_sum[0] << endl; 
     1901                                // cout << sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*max_grad->get_coordinates()-sampled_toprow->condition_sum[0] << endl; 
     1902 
     1903                        } 
     1904 
     1905                        return sample_mat; 
    17291906                } 
    17301907                else 
    17311908                { 
    17321909                        throw new exception("You are trying to sample from density that is not determined (parameters can't be integrated out)!"); 
    1733                 } 
    1734  
    1735                 while(sample_mat.cols()<n) 
    1736                 { 
    1737                         double rnumber = randu(); 
    1738  
    1739                         toprow* sampled_toprow = ordered_toprows.upper_bound(rnumber)->second; 
    1740  
    1741                         rnumber = randu(); 
    1742  
    1743                         map<double,set<vertex*>>::iterator tri_ref = sampled_toprow->triangulation.begin() 
    1744                         double sum = 0; 
    1745  
    1746                         for(tri_ref = sampled_toprow->triangulation.begin();tri_ref!=sampled_toprow->triangulation.end();tri_ref++) 
    1747                         { 
    1748                                 sum += (*tri_ref).first; 
    1749  
    1750                                 if(sum/sampled_toprow->probability >= rnumber) 
    1751                                         break; 
    1752                         } 
    1753  
    1754  
    1755                 } 
    1756  
    1757                 return sample_mat; 
     1910                 
     1911                        return 0; 
     1912                } 
     1913 
     1914                 
    17581915        } 
    17591916 
     
    19202077                                        // This method guarantees that each polyhedron is already triangulated, therefore its triangulation 
    19212078                                        // is only one set of vertices and it is the set of all its vertices. 
    1922                                         set<vertex*> t_simplex1; 
    1923                                         set<vertex*> t_simplex2; 
    1924  
    1925                                         t_simplex1.insert(current_copy1->vertices.begin(),current_copy1->vertices.end()); 
    1926                                         t_simplex2.insert(current_copy2->vertices.begin(),current_copy2->vertices.end()); 
     2079                                        simplex* t_simplex1 = new simplex(current_copy1->vertices); 
     2080                                        simplex* t_simplex2 = new simplex(current_copy2->vertices);                                      
    19272081                                         
    1928                                         current_copy1->triangulation.insert(pair<double,set<vertex*>>(0,t_simplex1)); 
    1929                                         current_copy2->triangulation.insert(pair<double,set<vertex*>>(0,t_simplex2));                                    
     2082                                        current_copy1->triangulation.insert(t_simplex1); 
     2083                                        current_copy2->triangulation.insert(t_simplex2);                                         
    19302084                                         
    19312085                                        // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying