Changeset 1335 for applications/robust

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

Dokoncovani samplingu, uz to skoro funguje, jen to spatne generuje sigma. JS

Location:
applications/robust
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/robustlib.cpp

    r1331 r1335  
    22 
    33 
    4 void polyhedron::triangulate(bool should_integrate) 
     4double polyhedron::triangulate(bool should_integrate) 
    55        { 
    66                for(set<simplex*>::iterator t_ref = triangulation.begin();t_ref!=triangulation.end();t_ref++) 
     
    4848                                } 
    4949                        }        
    50                 }                
     50                }        
     51 
     52                if(should_integrate) 
     53                { 
     54                        return ((toprow *)this)->probability; 
     55                } 
     56                else 
     57                { 
     58                        return 0.0; 
     59                } 
    5160                 
    5261                /* 
     
    165174                                                 
    166175 
    167                                                 if(a_0<current_emlig->likelihood_value) 
     176                                                if(a_0<current_emlig->min_ll) 
    168177                                                { 
    169178                                                        current_emlig->minimal_vertex = base_vertex; 
    170                                                         current_emlig->likelihood_value = a_0; 
     179                                                        current_emlig->min_ll = a_0; 
    171180                                                } 
    172181 
    173182                                                double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition_sum[0]; 
    174                                                 if(a_m<current_emlig->likelihood_value) 
     183                                                if(a_m<current_emlig->min_ll) 
    175184                                                { 
    176185                                                        current_emlig->minimal_vertex = (*vert_ref); 
    177                                                         current_emlig->likelihood_value = a_m;                                           
     186                                                        current_emlig->min_ll = a_m;                                             
    178187                                                } 
    179188 
     
    212221                        }*/ 
    213222 
    214                         double int_value = 0; 
     223                        double int_value = 0;                    
    215224 
    216225                        // cout << jacobian << endl; 
  • applications/robust/robustlib.h

    r1334 r1335  
    8282 
    8383        double negative_gamma_sum; 
     84 
     85        double min_beta; 
    8486         
    8587 
     
    99101        { 
    100102                positive_gamma_parameters.clear(); 
    101                 negative_gamma_parameters.clear(); 
    102                  
     103                negative_gamma_parameters.clear();               
    103104                 
    104105                positive_gamma_sum = 0; 
    105106                negative_gamma_sum = 0; 
     107 
     108                min_beta = numeric_limits<double>::max(); 
    106109        } 
    107110 
     
    131134 
    132135                        negative_gamma_parameters[order].insert(pair<double,double>(-weight,beta)); 
     136                } 
     137 
     138                if(beta < min_beta) 
     139                { 
     140                        min_beta = beta; 
    133141                } 
    134142        } 
     
    321329 
    322330        /// A method for triangulation of given polyhedron. 
    323         void triangulate(bool should_integrate);         
     331        double triangulate(bool should_integrate);       
    324332}; 
    325333 
     
    854862                                                                        //new_triangulation.insert(pair<double,set<vertex*>>(cur_prob,(*t_ref).second)); 
    855863                                                                } 
     864 
     865                                                                cur_par_toprow->my_emlig->normalization_factor += cur_par_toprow->probability; 
    856866 
    857867                                                                //current_parent->triangulation.clear(); 
     
    959969                                                                        //new_triangulation.insert(pair<double,set<vertex*>>(cur_prob,(*t_ref).second)); 
    960970                                                                } 
     971 
     972                                                                cur_par_toprow->my_emlig->normalization_factor += cur_par_toprow->probability; 
    961973 
    962974                                                                //current_parent->triangulation.clear(); 
     
    9991011        vertex* minimal_vertex; 
    10001012 
    1001         double likelihood_value; 
     1013        double min_ll; 
    10021014 
    10031015        vector<multiset<my_ivec>> correction_factors; 
     
    10131025                create_statistic(number_of_parameters); 
    10141026 
    1015                 likelihood_value = numeric_limits<double>::max(); 
     1027                min_ll = numeric_limits<double>::max(); 
    10161028        } 
    10171029 
     
    10221034                this->statistic = statistic;     
    10231035 
    1024                 likelihood_value = numeric_limits<double>::max(); 
     1036                min_ll = numeric_limits<double>::max(); 
    10251037        } 
    10261038 
     
    11461158        { 
    11471159                //step_me(0); 
    1148                  
    1149                 likelihood_value = numeric_limits<double>::max(); 
     1160                normalization_factor = 0; 
     1161                min_ll = numeric_limits<double>::max(); 
    11501162 
    11511163                bool should_remove = (toremove.size() != 0); 
     
    14801492                                                current_positive->totally_neutral = (current_positive->totally_neutral && current_negative->totally_neutral); 
    14811493 
    1482                                                 current_positive->triangulate(k==for_splitting.size()-1); 
     1494                                                current_positive->my_emlig->normalization_factor += current_positive->triangulate(k==for_splitting.size()-1); 
    14831495                                                 
    14841496                                                statistic.delete_polyhedron(k,current_negative); 
     
    16931705                                        new_totally_neutral_child->triangulate(false); 
    16941706 
    1695                                         positive_poly->triangulate(k==for_splitting.size()-1); 
    1696                                         negative_poly->triangulate(k==for_splitting.size()-1); 
     1707                                        positive_poly->my_emlig->normalization_factor += positive_poly->triangulate(k==for_splitting.size()-1); 
     1708                                        negative_poly->my_emlig->normalization_factor += negative_poly->triangulate(k==for_splitting.size()-1); 
    16971709                                         
    16981710                                        statistic.append_polyhedron(k-1, new_totally_neutral_child);                                     
     
    17901802                /// \TODO tady je to spatne, tady nesmi byt conditions.size(), viz RARX.bayes() 
    17911803                if(conditions.size()-2-number_of_parameters>=0) 
    1792                 { 
    1793                          
    1794                          
     1804                {                        
    17951805                        mat sample_mat; 
    17961806                        map<double,toprow*> ordered_toprows;                     
    17971807                        double sum_a = 0; 
     1808                         
     1809                        //cout << "Likelihoods of toprows:" << endl; 
    17981810 
    17991811                        for(polyhedron* top_ref = statistic.rows[number_of_parameters];top_ref!=statistic.end_poly;top_ref=top_ref->next_poly) 
     
    18021814 
    18031815                                sum_a+=current_top->probability; 
     1816                                /* 
     1817                                cout << current_top->probability << "   "; 
     1818 
     1819                                for(set<vertex*>::iterator vert_ref = (*top_ref).vertices.begin();vert_ref!=(*top_ref).vertices.end();vert_ref++) 
     1820                                { 
     1821                                        cout << round(100*(*vert_ref)->get_coordinates())/100 << " ; "; 
     1822                                } 
     1823                                */ 
     1824 
     1825                                // cout << endl; 
    18041826                                ordered_toprows.insert(pair<double,toprow*>(sum_a,current_top)); 
    1805                         } 
     1827                        }                        
     1828                         
     1829                        // cout << "Sum N: " << normalization_factor << endl; 
    18061830 
    18071831                        while(sample_mat.cols()<n) 
     
    18471871 
    18481872                                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; 
    18491876                                // cout << &(*tri_ref) << endl; 
    18501877 
     1878                                int number_of_runs = 0; 
    18511879                                bool have_sigma = false; 
    18521880                                double sigma = 0; 
     
    18621890                                                        sum_g += (*g_ref).first/(*s_ref)->positive_gamma_sum; 
    18631891 
     1892                                                         
    18641893                                                        if(sum_g>rnumber) 
    18651894                                                        { 
    18661895                                                                itpp::Gamma_RNG* gamma = new itpp::Gamma_RNG(conditions.size()-number_of_parameters,1/(*g_ref).second); 
    18671896                                                                sigma = 1/(*gamma)(); 
     1897                                                                 
     1898                                                                cout << "Sigma mean:   " << (*g_ref).second/(conditions.size()-number_of_parameters-1) << endl;                                                          
    18681899                                                                break; 
    1869                                                         }                                                
     1900                                                        }                                                        
    18701901                                                } 
    18711902 
     
    18831914                                                for(multimap<double,double>::iterator pg_ref = (*v_ref).begin();pg_ref!=(*v_ref).end();pg_ref++) 
    18841915                                                { 
    1885                                                         pg_sum += exp(-(*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; 
     1916                                                        pg_sum += exp(((*s_ref)->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; 
    18861917                                                }                                        
    18871918                                        } 
     
    18921923                                                for(multimap<double,double>::iterator ng_ref = (*v_ref).begin();ng_ref!=(*v_ref).end();ng_ref++) 
    18931924                                                { 
    1894                                                         ng_sum += exp(-(*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; 
     1925                                                        ng_sum += exp(((*s_ref)->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; 
    18951926                                                }                                        
    18961927                                        } 
     
    19001931                                                have_sigma = true; 
    19011932                                        } 
     1933 
     1934                                        number_of_runs++; 
    19021935                                } 
    19031936                                while(!have_sigma); 
     1937 
     1938                                cout << "Sigma: " << sigma << endl; 
     1939                                cout << "Nr. of runs: " << number_of_runs << endl; 
    19041940 
    19051941                                int dimension = (*s_ref)->vertices.size()-1; 
     
    19091945 
    19101946                                vertex* base_vert = *(*s_ref)->vertices.begin(); 
     1947 
     1948                                cout << "Base vertex coords(should be close to est. param.): " << base_vert->get_coordinates() << endl; 
    19111949                                 
    19121950                                int row_count = 0; 
     
    19141952                                for(set<vertex*>::iterator vert_ref = ++(*s_ref)->vertices.begin();vert_ref!=(*s_ref)->vertices.end();vert_ref++) 
    19151953                                { 
    1916                                         vec relative_coords = (*vert_ref)->get_coordinates()-base_vert->get_coordinates();                               
     1954                                        vec current_coords = (*vert_ref)->get_coordinates(); 
     1955 
     1956                                        cout << "Coords of vertex[" << row_count << "]: " << current_coords << endl;  
     1957                                         
     1958                                        vec relative_coords = current_coords-base_vert->get_coordinates();                               
    19171959 
    19181960                                        jacobian.set_row(row_count,relative_coords); 
    19191961 
    19201962                                        row_count++; 
    1921                                 } 
     1963                                }                                
    19221964                                 
    19231965                                cout << "Jacobian: " << jacobian << endl; 
     
    19261968                                                                 
    19271969                                gradient = jacobian*gradient;    
     1970 
     1971                                cout << "Gradient after trafo:" << gradient << endl; 
    19281972 
    19291973                                // vec normal_gradient = gradient/sqrt(gradient*gradient); 
     
    19431987                                        x_axis = rotation_matrix*x_axis; 
    19441988 
    1945                                         double t = gradient[i]/gradient*x_axis; 
     1989                                        double t = abs(gradient[i]/gradient*x_axis); 
    19461990 
    19471991                                        double sin_theta = sign(gradient[i])*t/sqrt(1+pow(t,2)); 
     
    19612005 
    19622006                                // cout << rotation_matrix << endl; 
    1963                                 cout << "Gradient after trafo:" << gradient << endl; 
    1964  
    1965                                 vec minima = numeric_limits<double>::max()*ones(number_of_parameters); 
    1966                                 vec maxima = -numeric_limits<double>::max()*ones(number_of_parameters); 
    1967  
    1968                                 vertex* min_grad; 
    1969                                 vertex* max_grad; 
    1970                                                                  
    1971                                 for(set<vertex*>::iterator vert_ref = (*s_ref)->vertices.begin();vert_ref!=(*s_ref)->vertices.end();vert_ref++) 
    1972                                 { 
    1973                                         vec new_coords = rotation_matrix*jacobian*(*vert_ref)->get_coordinates(); 
    1974  
    1975                                         // cout << new_coords << endl; 
    1976  
    1977                                         for(int j = 0;j<new_coords.size();j++) 
    1978                                         { 
    1979                                                 if(new_coords[j]>maxima[j]) 
    1980                                                 { 
    1981                                                         maxima[j] = new_coords[j]; 
    1982  
    1983                                                         if(j==0) 
    1984                                                         { 
    1985                                                                 max_grad = (*vert_ref); 
    1986                                                         } 
    1987                                                 } 
    1988  
    1989                                                 if(new_coords[j]<minima[j]) 
    1990                                                 { 
    1991                                                         minima[j] = new_coords[j]; 
    1992  
    1993                                                         if(j==0) 
    1994                                                         { 
    1995                                                                 min_grad = (*vert_ref); 
    1996                                                         } 
    1997                                                 } 
    1998                                         }                                        
    1999                                 } 
    2000  
    2001                                 vec sample_coordinates;                          
     2007                                 
     2008                                mat extended_rotation = rotation_matrix; 
     2009                                extended_rotation.ins_col(0,zeros(extended_rotation.rows())); 
     2010 
     2011                                cout << "Extended rotation: " << extended_rotation << endl; 
     2012                                 
     2013                                vec minima = itpp::min(extended_rotation,2); 
     2014                                vec maxima = itpp::max(extended_rotation,2); 
     2015 
     2016                                cout << "Minima: " << minima << endl; 
     2017                                cout << "Maxima: " << maxima << endl; 
     2018 
     2019                                vec sample_coordinates;          
     2020                                bool is_inside = true; 
     2021                                 
     2022                                vec new_sample; 
     2023                                sample_coordinates = new_sample; 
    20022024 
    20032025                                for(int j = 0;j<number_of_parameters;j++) 
     
    20082030 
    20092031                                        if(j==0) 
    2010                                         { 
    2011                                                 double pos_value = sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*max_grad->get_coordinates()-sampled_toprow->condition_sum[0]; 
     2032                                        {                                                
     2033                                                vec new_gradient = rotation_matrix*gradient; 
    20122034                                                 
    2013                                                 vec new_gradient = rotation_matrix*gradient; 
    2014  
    20152035                                                cout << "New gradient(should have only first component nonzero):" << new_gradient << endl; 
    20162036 
    2017                                                 cout << "Max: " << maxima[0] << "  Min: " << minima[0] << "  Grad:" << new_gradient[0] << endl; 
     2037                                                // cout << "Max: " << maxima[0] << "  Min: " << minima[0] << "  Grad:" << new_gradient[0] << endl; 
    20182038                                                 
    2019                                                 double log_bracket = rnumber*exp(new_gradient[0]*maxima[0]/sigma)+(1-rnumber)*exp(new_gradient[0]*minima[0]/sigma); 
     2039                                                double log_bracket = 1-rnumber*(1-exp(new_gradient[0]/sigma*(minima[0]-maxima[0]))); 
    20202040                                                 
    2021                                                 coordinate = log(log_bracket); 
    2022                                         } 
     2041                                                coordinate = minima[0]-sigma/new_gradient[0]*log(log_bracket); 
     2042                                        } 
     2043                                        else 
     2044                                        { 
     2045                                                coordinate = minima[j]+rnumber*(maxima[j]-minima[j]); 
     2046                                        } 
     2047 
     2048                                        sample_coordinates.ins(j,coordinate); 
     2049                                } 
     2050 
     2051                                cout << "Sampled coordinates(gradient direction): " << sample_coordinates << endl; 
     2052 
     2053                                sample_coordinates = rotation_matrix.T()*sample_coordinates; 
     2054 
     2055                                cout << "Sampled coordinates(backrotated direction):" << sample_coordinates << endl; 
     2056 
     2057                                 
     2058                                for(int j = 0;j<sample_coordinates.size();j++) 
     2059                                { 
     2060                                        if(sample_coordinates[j]<0) 
     2061                                        { 
     2062                                                is_inside = false; 
     2063                                        } 
     2064                                } 
     2065 
     2066                                double above_criterion = ones(sample_coordinates.size())*sample_coordinates; 
     2067 
     2068                                if(above_criterion>1) 
     2069                                { 
     2070                                        is_inside = false; 
     2071                                } 
     2072 
     2073                                if(is_inside) 
     2074                                {                                        
     2075                                        sample_coordinates = jacobian.T()*sample_coordinates+(*base_vert).get_coordinates(); 
     2076                                         
     2077                                        sample_coordinates.ins(0,sigma); 
     2078                                         
     2079                                        cout << "Sampled coordinates(parameter space):" << sample_coordinates << endl; 
     2080 
     2081                                        sample_mat.ins_col(0,sample_coordinates); 
    20232082                                } 
    20242083 
     
    20262085                                // cout << sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*max_grad->get_coordinates()-sampled_toprow->condition_sum[0] << endl; 
    20272086 
     2087                                 
    20282088                        } 
    20292089