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