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