Changeset 1335 for applications/robust
- Timestamp:
- 04/19/11 18:15:22 (14 years ago)
- Location:
- applications/robust
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/robust/robustlib.cpp
r1331 r1335 2 2 3 3 4 voidpolyhedron::triangulate(bool should_integrate)4 double polyhedron::triangulate(bool should_integrate) 5 5 { 6 6 for(set<simplex*>::iterator t_ref = triangulation.begin();t_ref!=triangulation.end();t_ref++) … … 48 48 } 49 49 } 50 } 50 } 51 52 if(should_integrate) 53 { 54 return ((toprow *)this)->probability; 55 } 56 else 57 { 58 return 0.0; 59 } 51 60 52 61 /* … … 165 174 166 175 167 if(a_0<current_emlig-> likelihood_value)176 if(a_0<current_emlig->min_ll) 168 177 { 169 178 current_emlig->minimal_vertex = base_vertex; 170 current_emlig-> likelihood_value= a_0;179 current_emlig->min_ll = a_0; 171 180 } 172 181 173 182 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) 175 184 { 176 185 current_emlig->minimal_vertex = (*vert_ref); 177 current_emlig-> likelihood_value= a_m;186 current_emlig->min_ll = a_m; 178 187 } 179 188 … … 212 221 }*/ 213 222 214 double int_value = 0; 223 double int_value = 0; 215 224 216 225 // cout << jacobian << endl; -
applications/robust/robustlib.h
r1334 r1335 82 82 83 83 double negative_gamma_sum; 84 85 double min_beta; 84 86 85 87 … … 99 101 { 100 102 positive_gamma_parameters.clear(); 101 negative_gamma_parameters.clear(); 102 103 negative_gamma_parameters.clear(); 103 104 104 105 positive_gamma_sum = 0; 105 106 negative_gamma_sum = 0; 107 108 min_beta = numeric_limits<double>::max(); 106 109 } 107 110 … … 131 134 132 135 negative_gamma_parameters[order].insert(pair<double,double>(-weight,beta)); 136 } 137 138 if(beta < min_beta) 139 { 140 min_beta = beta; 133 141 } 134 142 } … … 321 329 322 330 /// A method for triangulation of given polyhedron. 323 voidtriangulate(bool should_integrate);331 double triangulate(bool should_integrate); 324 332 }; 325 333 … … 854 862 //new_triangulation.insert(pair<double,set<vertex*>>(cur_prob,(*t_ref).second)); 855 863 } 864 865 cur_par_toprow->my_emlig->normalization_factor += cur_par_toprow->probability; 856 866 857 867 //current_parent->triangulation.clear(); … … 959 969 //new_triangulation.insert(pair<double,set<vertex*>>(cur_prob,(*t_ref).second)); 960 970 } 971 972 cur_par_toprow->my_emlig->normalization_factor += cur_par_toprow->probability; 961 973 962 974 //current_parent->triangulation.clear(); … … 999 1011 vertex* minimal_vertex; 1000 1012 1001 double likelihood_value;1013 double min_ll; 1002 1014 1003 1015 vector<multiset<my_ivec>> correction_factors; … … 1013 1025 create_statistic(number_of_parameters); 1014 1026 1015 likelihood_value= numeric_limits<double>::max();1027 min_ll = numeric_limits<double>::max(); 1016 1028 } 1017 1029 … … 1022 1034 this->statistic = statistic; 1023 1035 1024 likelihood_value= numeric_limits<double>::max();1036 min_ll = numeric_limits<double>::max(); 1025 1037 } 1026 1038 … … 1146 1158 { 1147 1159 //step_me(0); 1148 1149 likelihood_value= numeric_limits<double>::max();1160 normalization_factor = 0; 1161 min_ll = numeric_limits<double>::max(); 1150 1162 1151 1163 bool should_remove = (toremove.size() != 0); … … 1480 1492 current_positive->totally_neutral = (current_positive->totally_neutral && current_negative->totally_neutral); 1481 1493 1482 current_positive-> triangulate(k==for_splitting.size()-1);1494 current_positive->my_emlig->normalization_factor += current_positive->triangulate(k==for_splitting.size()-1); 1483 1495 1484 1496 statistic.delete_polyhedron(k,current_negative); … … 1693 1705 new_totally_neutral_child->triangulate(false); 1694 1706 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); 1697 1709 1698 1710 statistic.append_polyhedron(k-1, new_totally_neutral_child); … … 1790 1802 /// \TODO tady je to spatne, tady nesmi byt conditions.size(), viz RARX.bayes() 1791 1803 if(conditions.size()-2-number_of_parameters>=0) 1792 { 1793 1794 1804 { 1795 1805 mat sample_mat; 1796 1806 map<double,toprow*> ordered_toprows; 1797 1807 double sum_a = 0; 1808 1809 //cout << "Likelihoods of toprows:" << endl; 1798 1810 1799 1811 for(polyhedron* top_ref = statistic.rows[number_of_parameters];top_ref!=statistic.end_poly;top_ref=top_ref->next_poly) … … 1802 1814 1803 1815 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; 1804 1826 ordered_toprows.insert(pair<double,toprow*>(sum_a,current_top)); 1805 } 1827 } 1828 1829 // cout << "Sum N: " << normalization_factor << endl; 1806 1830 1807 1831 while(sample_mat.cols()<n) … … 1847 1871 1848 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; 1849 1876 // cout << &(*tri_ref) << endl; 1850 1877 1878 int number_of_runs = 0; 1851 1879 bool have_sigma = false; 1852 1880 double sigma = 0; … … 1862 1890 sum_g += (*g_ref).first/(*s_ref)->positive_gamma_sum; 1863 1891 1892 1864 1893 if(sum_g>rnumber) 1865 1894 { 1866 1895 itpp::Gamma_RNG* gamma = new itpp::Gamma_RNG(conditions.size()-number_of_parameters,1/(*g_ref).second); 1867 1896 sigma = 1/(*gamma)(); 1897 1898 cout << "Sigma mean: " << (*g_ref).second/(conditions.size()-number_of_parameters-1) << endl; 1868 1899 break; 1869 } 1900 } 1870 1901 } 1871 1902 … … 1883 1914 for(multimap<double,double>::iterator pg_ref = (*v_ref).begin();pg_ref!=(*v_ref).end();pg_ref++) 1884 1915 { 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; 1886 1917 } 1887 1918 } … … 1892 1923 for(multimap<double,double>::iterator ng_ref = (*v_ref).begin();ng_ref!=(*v_ref).end();ng_ref++) 1893 1924 { 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; 1895 1926 } 1896 1927 } … … 1900 1931 have_sigma = true; 1901 1932 } 1933 1934 number_of_runs++; 1902 1935 } 1903 1936 while(!have_sigma); 1937 1938 cout << "Sigma: " << sigma << endl; 1939 cout << "Nr. of runs: " << number_of_runs << endl; 1904 1940 1905 1941 int dimension = (*s_ref)->vertices.size()-1; … … 1909 1945 1910 1946 vertex* base_vert = *(*s_ref)->vertices.begin(); 1947 1948 cout << "Base vertex coords(should be close to est. param.): " << base_vert->get_coordinates() << endl; 1911 1949 1912 1950 int row_count = 0; … … 1914 1952 for(set<vertex*>::iterator vert_ref = ++(*s_ref)->vertices.begin();vert_ref!=(*s_ref)->vertices.end();vert_ref++) 1915 1953 { 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(); 1917 1959 1918 1960 jacobian.set_row(row_count,relative_coords); 1919 1961 1920 1962 row_count++; 1921 } 1963 } 1922 1964 1923 1965 cout << "Jacobian: " << jacobian << endl; … … 1926 1968 1927 1969 gradient = jacobian*gradient; 1970 1971 cout << "Gradient after trafo:" << gradient << endl; 1928 1972 1929 1973 // vec normal_gradient = gradient/sqrt(gradient*gradient); … … 1943 1987 x_axis = rotation_matrix*x_axis; 1944 1988 1945 double t = gradient[i]/gradient*x_axis;1989 double t = abs(gradient[i]/gradient*x_axis); 1946 1990 1947 1991 double sin_theta = sign(gradient[i])*t/sqrt(1+pow(t,2)); … … 1961 2005 1962 2006 // 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; 2002 2024 2003 2025 for(int j = 0;j<number_of_parameters;j++) … … 2008 2030 2009 2031 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; 2012 2034 2013 vec new_gradient = rotation_matrix*gradient;2014 2015 2035 cout << "New gradient(should have only first component nonzero):" << new_gradient << endl; 2016 2036 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; 2018 2038 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]))); 2020 2040 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); 2023 2082 } 2024 2083 … … 2026 2085 // cout << sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*max_grad->get_coordinates()-sampled_toprow->condition_sum[0] << endl; 2027 2086 2087 2028 2088 } 2029 2089