[976] | 1 | #include "robustlib.h" |
---|
| 2 | |
---|
[1282] | 3 | |
---|
[1254] | 4 | void polyhedron::triangulate(bool should_integrate) |
---|
[1273] | 5 | { |
---|
[1324] | 6 | for(set<simplex*>::iterator t_ref = triangulation.begin();t_ref!=triangulation.end();t_ref++) |
---|
| 7 | { |
---|
| 8 | delete (*t_ref); |
---|
| 9 | } |
---|
[1301] | 10 | triangulation.clear(); |
---|
[1299] | 11 | |
---|
[1271] | 12 | if(should_integrate) |
---|
| 13 | { |
---|
| 14 | ((toprow *)this)->probability = 0.0; |
---|
| 15 | } |
---|
| 16 | |
---|
[1301] | 17 | if(vertices.size()==1) |
---|
| 18 | { |
---|
[1324] | 19 | simplex* vert_simplex = new simplex((vertex*)this); |
---|
[1301] | 20 | |
---|
[1324] | 21 | triangulation.insert(vert_simplex); |
---|
[1301] | 22 | } |
---|
| 23 | |
---|
[1254] | 24 | for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++) |
---|
[1271] | 25 | { |
---|
[1324] | 26 | for(set<simplex*>::iterator s_ref = (*child_ref)->triangulation.begin();s_ref!=(*child_ref)->triangulation.end();s_ref++) |
---|
[1254] | 27 | { |
---|
[1324] | 28 | simplex* new_simplex = new simplex((*s_ref)->vertices); |
---|
[1301] | 29 | |
---|
[1324] | 30 | pair<set<vertex*>::iterator,bool> ret_val = new_simplex->vertices.insert(*vertices.begin()); |
---|
[1301] | 31 | |
---|
| 32 | if(ret_val.second == true) |
---|
| 33 | { |
---|
[1320] | 34 | double cur_prob = 0; |
---|
[1301] | 35 | |
---|
| 36 | if(should_integrate) |
---|
| 37 | { |
---|
[1320] | 38 | cur_prob = ((toprow *)this)->integrate_simplex(new_simplex, 'S'); |
---|
| 39 | |
---|
| 40 | ((toprow *)this)->probability += cur_prob; |
---|
[1301] | 41 | } |
---|
[1320] | 42 | |
---|
[1324] | 43 | triangulation.insert(new_simplex); |
---|
| 44 | } |
---|
| 45 | else |
---|
| 46 | { |
---|
| 47 | delete new_simplex; |
---|
| 48 | } |
---|
[1301] | 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /* |
---|
| 53 | for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++) |
---|
| 54 | { |
---|
| 55 | for(list<set<vertex*>>::iterator t_ref = (*child_ref)->triangulation.begin();t_ref!=(*child_ref)->triangulation.end();t_ref++) |
---|
| 56 | { |
---|
| 57 | set<vertex*> new_simplex; |
---|
[1299] | 58 | new_simplex.insert((*t_ref).begin(),(*t_ref).end()); |
---|
[1254] | 59 | |
---|
[1299] | 60 | for(set<vertex*>::iterator suitable_vert_ref = vertices.begin();*suitable_vert_ref<*(*t_ref).begin();suitable_vert_ref++) |
---|
| 61 | { |
---|
[1301] | 62 | set<vertex*> suitable_simplex; |
---|
| 63 | suitable_simplex.insert(new_simplex.begin(),new_simplex.end()); |
---|
| 64 | |
---|
| 65 | suitable_simplex.insert(*suitable_vert_ref); |
---|
[1254] | 66 | |
---|
[1301] | 67 | triangulation.push_back(suitable_simplex); |
---|
[1254] | 68 | |
---|
[1271] | 69 | if(should_integrate) |
---|
[1254] | 70 | { |
---|
[1301] | 71 | ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(suitable_simplex, 'S'); |
---|
[1271] | 72 | } |
---|
[1299] | 73 | } |
---|
[1271] | 74 | } |
---|
[1301] | 75 | }*/ |
---|
[1271] | 76 | } |
---|
[1268] | 77 | |
---|
| 78 | |
---|
[1324] | 79 | double toprow::integrate_simplex(simplex* simplex, char c) |
---|
[1271] | 80 | { |
---|
[1282] | 81 | // cout << ((toprow*)this)->condition << endl; |
---|
| 82 | |
---|
[1280] | 83 | int condition_order = ((toprow*)this)->condition_order-2; // -2 by bylo, pokud chceme uniformni apriorno |
---|
[1254] | 84 | |
---|
[1280] | 85 | int sigma_order = condition_order-my_emlig->number_of_parameters; |
---|
| 86 | |
---|
[1271] | 87 | // cout << "C:" << condition_order << " N:" << my_emlig->number_of_parameters << " C+N:" << condition_order-my_emlig->number_of_parameters << endl; |
---|
| 88 | // pause(0.1); |
---|
[1254] | 89 | |
---|
[1280] | 90 | if(sigma_order >= 0) |
---|
[1271] | 91 | { |
---|
[1275] | 92 | |
---|
[1282] | 93 | //cout << endl; |
---|
| 94 | //cout << ((toprow*)this)->condition << endl; |
---|
| 95 | //cout << "C:" << condition_order+2 << " N:" << my_emlig->number_of_parameters << " C+N:" << condition_order-my_emlig->number_of_parameters+2 << endl; |
---|
| 96 | |
---|
[1275] | 97 | |
---|
[1271] | 98 | emlig* current_emlig; |
---|
[1331] | 99 | simplex->clear_gammas(); |
---|
[1254] | 100 | |
---|
[1271] | 101 | if(this->my_emlig!=NULL) |
---|
| 102 | { |
---|
| 103 | current_emlig = this->my_emlig; |
---|
| 104 | } |
---|
| 105 | else |
---|
| 106 | { |
---|
| 107 | throw exception("The statistic of the polyhedron you are trying to integrate over doesn't belong to any emlig!"); |
---|
| 108 | } |
---|
[1254] | 109 | |
---|
[1271] | 110 | toprow* as_toprow = (toprow*)this; |
---|
[1254] | 111 | |
---|
[1300] | 112 | vec cur_condition = as_toprow->condition_sum.get(1,as_toprow->condition_sum.size()-1); |
---|
[1254] | 113 | |
---|
[1275] | 114 | // cout << as_toprow->condition << endl; |
---|
[1272] | 115 | |
---|
[1324] | 116 | int dimension = simplex->vertices.size()-1; |
---|
[1254] | 117 | |
---|
[1275] | 118 | mat jacobian(dimension,dimension); |
---|
[1254] | 119 | |
---|
[1275] | 120 | double a_0; |
---|
[1271] | 121 | map<double,int> as; |
---|
[1275] | 122 | vertex* base_vertex; |
---|
[1324] | 123 | set<vertex*>::iterator base_ref = simplex->vertices.begin(); |
---|
[1275] | 124 | bool order_correct; |
---|
| 125 | |
---|
[1254] | 126 | |
---|
[1275] | 127 | do |
---|
[1271] | 128 | { |
---|
[1275] | 129 | order_correct = true; |
---|
| 130 | as.clear(); |
---|
[1268] | 131 | |
---|
[1275] | 132 | base_vertex = (*base_ref); |
---|
[1268] | 133 | |
---|
[1282] | 134 | |
---|
[1301] | 135 | // cout << endl << "Base coords:" << base_vertex->get_coordinates() << endl; |
---|
[1254] | 136 | |
---|
[1300] | 137 | a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition_sum[0]; |
---|
[1282] | 138 | |
---|
[1281] | 139 | |
---|
[1275] | 140 | int row_count = 0; |
---|
[1324] | 141 | for(set<vertex*>::iterator vert_ref = simplex->vertices.begin(); vert_ref!=simplex->vertices.end();vert_ref++) |
---|
[1271] | 142 | { |
---|
[1275] | 143 | if(vert_ref != base_ref) |
---|
| 144 | { |
---|
[1276] | 145 | vec relative_coords = (*vert_ref)->get_coordinates()-base_vertex->get_coordinates(); |
---|
[1275] | 146 | |
---|
| 147 | jacobian.set_row(row_count,relative_coords); |
---|
| 148 | |
---|
[1281] | 149 | double new_a = -relative_coords*cur_condition; |
---|
[1275] | 150 | |
---|
[1276] | 151 | if(new_a + a_0 == 0 || new_a == 0) |
---|
[1275] | 152 | { |
---|
| 153 | base_ref++; |
---|
| 154 | |
---|
[1324] | 155 | if(base_ref == simplex->vertices.end()) |
---|
[1275] | 156 | { |
---|
| 157 | throw new exception("Equal local conditions are paired. If this ever occurs, the software has to be modified to include multiplied a_0!!"); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | order_correct = false; |
---|
| 161 | |
---|
| 162 | break; |
---|
[1282] | 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | if(a_0<current_emlig->likelihood_value) |
---|
| 168 | { |
---|
| 169 | current_emlig->minimal_vertex = base_vertex; |
---|
| 170 | current_emlig->likelihood_value = a_0; |
---|
[1275] | 171 | } |
---|
[1282] | 172 | |
---|
[1300] | 173 | double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition_sum[0]; |
---|
[1282] | 174 | if(a_m<current_emlig->likelihood_value) |
---|
| 175 | { |
---|
| 176 | current_emlig->minimal_vertex = (*vert_ref); |
---|
| 177 | current_emlig->likelihood_value = a_m; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | //cout << "a0:" << a_0 << " a0 coords:" << base_vertex->get_coordinates() << " am:" << a_m << " am coords:" << (*vert_ref)->get_coordinates() << endl; |
---|
| 181 | |
---|
[1301] | 182 | // cout << "Absolute coords:(V" << row_count << ")" << (*vert_ref)->get_coordinates() << endl; |
---|
[1282] | 183 | //cout << "Relative coords:(V" << row_count << ")" << relative_coords << endl; |
---|
[1275] | 184 | |
---|
| 185 | pair<map<double,int>::iterator,bool> returned = as.insert(pair<double,int>(new_a,1)); |
---|
| 186 | if(returned.second == false) |
---|
| 187 | { |
---|
| 188 | (*returned.first).second++; |
---|
| 189 | } |
---|
| 190 | /* |
---|
| 191 | else |
---|
| 192 | { |
---|
| 193 | cout << "a[" << row_count << "] = " << new_a << endl; |
---|
| 194 | } |
---|
| 195 | */ |
---|
| 196 | |
---|
| 197 | //as.ins(as.size(),new_a); |
---|
| 198 | |
---|
| 199 | row_count++; |
---|
| 200 | } |
---|
[1271] | 201 | } |
---|
| 202 | } |
---|
[1275] | 203 | while(!order_correct); |
---|
[1272] | 204 | |
---|
[1282] | 205 | /* |
---|
[1281] | 206 | cout << "a_0: " << a_0 << " "; |
---|
| 207 | int as_count = 1; |
---|
| 208 | for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++) |
---|
| 209 | { |
---|
| 210 | cout << "a_" << as_count << ": " << (*as_ref).first << " "; |
---|
| 211 | as_count++; |
---|
[1282] | 212 | }*/ |
---|
[1269] | 213 | |
---|
[1271] | 214 | double int_value = 0; |
---|
[1268] | 215 | |
---|
[1271] | 216 | // cout << jacobian << endl; |
---|
[1268] | 217 | |
---|
[1272] | 218 | double det_jacobian = abs(det(jacobian)); |
---|
| 219 | double correction_term = det_jacobian; |
---|
[1271] | 220 | for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++) |
---|
| 221 | { |
---|
[1275] | 222 | double multiplier = det_jacobian; |
---|
[1272] | 223 | |
---|
[1275] | 224 | int as1_order = (*as_ref).second; |
---|
| 225 | |
---|
[1280] | 226 | correction_term /= pow(-(*as_ref).first,as1_order); |
---|
[1275] | 227 | |
---|
| 228 | current_emlig->set_correction_factors(as1_order); |
---|
| 229 | |
---|
| 230 | vector<double> factors; |
---|
| 231 | int number_of_factors = 0; |
---|
| 232 | for(map<double,int>::iterator as2_ref = as.begin();as2_ref!=as.end();as2_ref++) |
---|
[1272] | 233 | { |
---|
| 234 | |
---|
[1275] | 235 | if(as2_ref!=as_ref) |
---|
| 236 | { |
---|
| 237 | for(int k = 0;k<(*as2_ref).second;k++) |
---|
[1271] | 238 | { |
---|
[1275] | 239 | factors.push_back((*as_ref).first-(*as2_ref).first); |
---|
[1272] | 240 | } |
---|
[1270] | 241 | |
---|
[1275] | 242 | multiplier /= pow((*as_ref).first-(*as2_ref).first,(*as2_ref).second); |
---|
| 243 | number_of_factors += (*as2_ref).second; |
---|
| 244 | } |
---|
| 245 | else |
---|
[1271] | 246 | { |
---|
[1275] | 247 | factors.push_back((*as_ref).first); |
---|
[1270] | 248 | |
---|
[1275] | 249 | multiplier /= (*as_ref).first; |
---|
| 250 | number_of_factors += 1; |
---|
[1272] | 251 | } |
---|
[1275] | 252 | |
---|
[1325] | 253 | } |
---|
| 254 | |
---|
| 255 | |
---|
[1254] | 256 | |
---|
[1325] | 257 | double bracket = fact(as1_order-1)/pow(a_0-(*as_ref).first,sigma_order+1); |
---|
[1331] | 258 | |
---|
| 259 | simplex->insert_gamma(0,bracket*multiplier,a_0-(*as_ref).first); |
---|
[1280] | 260 | |
---|
[1325] | 261 | bracket *= fact(sigma_order); |
---|
| 262 | |
---|
[1275] | 263 | for(int k = 0;k < as1_order-1;k++) |
---|
| 264 | { |
---|
[1325] | 265 | double local_bracket = 0; |
---|
| 266 | double bracket_factor = -pow((double)-1,k+1)/fact(as1_order-1-k)/pow(a_0-(*as_ref).first,sigma_order-k); |
---|
[1272] | 267 | |
---|
[1280] | 268 | ivec control_vec = ivec(); |
---|
[1325] | 269 | control_vec.ins(0,my_emlig->number_of_parameters-as1_order+1); |
---|
[1280] | 270 | |
---|
| 271 | for(multiset<my_ivec>::iterator combi_ref = this->my_emlig->correction_factors[k].begin();combi_ref!=this->my_emlig->correction_factors[k].upper_bound(my_ivec(control_vec));combi_ref++) |
---|
[1275] | 272 | { |
---|
| 273 | double bracket_combination = 1; |
---|
[1280] | 274 | for(int j = 0;j<(*combi_ref).size();j++) |
---|
[1275] | 275 | { |
---|
[1281] | 276 | //cout << "Factor vector:" << (*combi_ref) << endl; |
---|
[1280] | 277 | |
---|
| 278 | bracket_combination /= factors[(*combi_ref)[j]-1]; |
---|
[1325] | 279 | } |
---|
| 280 | |
---|
| 281 | local_bracket += bracket_factor*bracket_combination; |
---|
| 282 | } |
---|
[1254] | 283 | |
---|
[1331] | 284 | simplex->insert_gamma(k+1,local_bracket*multiplier,a_0-(*as_ref).first); |
---|
[1325] | 285 | |
---|
| 286 | bracket += local_bracket*fact(sigma_order-k); |
---|
| 287 | } |
---|
| 288 | |
---|
[1275] | 289 | int_value += multiplier*bracket; |
---|
| 290 | |
---|
[1272] | 291 | } |
---|
[1271] | 292 | |
---|
[1325] | 293 | double correction_term_base = correction_term/pow(a_0,sigma_order+1); |
---|
[1271] | 294 | |
---|
[1331] | 295 | simplex->insert_gamma(0,correction_term_base,a_0); |
---|
[1272] | 296 | |
---|
[1325] | 297 | correction_term = fact(sigma_order)*correction_term_base; |
---|
| 298 | |
---|
| 299 | //cout << c << int_value << endl; |
---|
| 300 | |
---|
[1272] | 301 | int_value += correction_term; |
---|
[1275] | 302 | |
---|
[1272] | 303 | |
---|
[1325] | 304 | //cout << "Probability:" << int_value << endl; |
---|
| 305 | //pause(0.100); |
---|
[1272] | 306 | |
---|
[1324] | 307 | simplex->probability = int_value; |
---|
[1271] | 308 | |
---|
[1324] | 309 | return int_value; |
---|
[1275] | 310 | |
---|
[1271] | 311 | } |
---|
| 312 | else |
---|
| 313 | { |
---|
| 314 | return 0.0; |
---|
| 315 | } |
---|
[1254] | 316 | } |
---|