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