[976] | 1 | #include "robustlib.h" |
---|
| 2 | |
---|
[1282] | 3 | |
---|
[1335] | 4 | double 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 | |
---|
[1338] | 36 | /* |
---|
| 37 | if(should_integrate&&new_simplex->vertices.size()!=3) |
---|
| 38 | { |
---|
| 39 | cout << "Error: Wrong vertex count for integration!"; |
---|
| 40 | } |
---|
| 41 | */ |
---|
| 42 | |
---|
[1301] | 43 | if(should_integrate) |
---|
| 44 | { |
---|
[1320] | 45 | cur_prob = ((toprow *)this)->integrate_simplex(new_simplex, 'S'); |
---|
| 46 | |
---|
| 47 | ((toprow *)this)->probability += cur_prob; |
---|
[1301] | 48 | } |
---|
[1320] | 49 | |
---|
[1324] | 50 | triangulation.insert(new_simplex); |
---|
| 51 | } |
---|
| 52 | else |
---|
| 53 | { |
---|
| 54 | delete new_simplex; |
---|
| 55 | } |
---|
[1301] | 56 | } |
---|
[1335] | 57 | } |
---|
| 58 | |
---|
| 59 | if(should_integrate) |
---|
| 60 | { |
---|
| 61 | return ((toprow *)this)->probability; |
---|
| 62 | } |
---|
| 63 | else |
---|
| 64 | { |
---|
| 65 | return 0.0; |
---|
| 66 | } |
---|
[1301] | 67 | |
---|
| 68 | /* |
---|
| 69 | for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++) |
---|
| 70 | { |
---|
| 71 | for(list<set<vertex*>>::iterator t_ref = (*child_ref)->triangulation.begin();t_ref!=(*child_ref)->triangulation.end();t_ref++) |
---|
| 72 | { |
---|
| 73 | set<vertex*> new_simplex; |
---|
[1299] | 74 | new_simplex.insert((*t_ref).begin(),(*t_ref).end()); |
---|
[1254] | 75 | |
---|
[1299] | 76 | for(set<vertex*>::iterator suitable_vert_ref = vertices.begin();*suitable_vert_ref<*(*t_ref).begin();suitable_vert_ref++) |
---|
| 77 | { |
---|
[1301] | 78 | set<vertex*> suitable_simplex; |
---|
| 79 | suitable_simplex.insert(new_simplex.begin(),new_simplex.end()); |
---|
| 80 | |
---|
| 81 | suitable_simplex.insert(*suitable_vert_ref); |
---|
[1254] | 82 | |
---|
[1301] | 83 | triangulation.push_back(suitable_simplex); |
---|
[1254] | 84 | |
---|
[1271] | 85 | if(should_integrate) |
---|
[1254] | 86 | { |
---|
[1301] | 87 | ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(suitable_simplex, 'S'); |
---|
[1271] | 88 | } |
---|
[1299] | 89 | } |
---|
[1271] | 90 | } |
---|
[1301] | 91 | }*/ |
---|
[1271] | 92 | } |
---|
[1268] | 93 | |
---|
| 94 | |
---|
[1324] | 95 | double toprow::integrate_simplex(simplex* simplex, char c) |
---|
[1271] | 96 | { |
---|
[1282] | 97 | // cout << ((toprow*)this)->condition << endl; |
---|
[1362] | 98 | int sigma_order = ((toprow*)this)->condition_order-simplex->vertices.size()-1; |
---|
[1282] | 99 | |
---|
[1271] | 100 | // cout << "C:" << condition_order << " N:" << my_emlig->number_of_parameters << " C+N:" << condition_order-my_emlig->number_of_parameters << endl; |
---|
| 101 | // pause(0.1); |
---|
[1254] | 102 | |
---|
[1280] | 103 | if(sigma_order >= 0) |
---|
[1271] | 104 | { |
---|
[1275] | 105 | |
---|
[1282] | 106 | //cout << endl; |
---|
| 107 | //cout << ((toprow*)this)->condition << endl; |
---|
| 108 | //cout << "C:" << condition_order+2 << " N:" << my_emlig->number_of_parameters << " C+N:" << condition_order-my_emlig->number_of_parameters+2 << endl; |
---|
| 109 | |
---|
[1275] | 110 | |
---|
[1271] | 111 | emlig* current_emlig; |
---|
[1331] | 112 | simplex->clear_gammas(); |
---|
[1379] | 113 | simplex->probability = 0; |
---|
[1254] | 114 | |
---|
[1271] | 115 | if(this->my_emlig!=NULL) |
---|
| 116 | { |
---|
| 117 | current_emlig = this->my_emlig; |
---|
| 118 | } |
---|
| 119 | else |
---|
| 120 | { |
---|
| 121 | throw exception("The statistic of the polyhedron you are trying to integrate over doesn't belong to any emlig!"); |
---|
| 122 | } |
---|
[1254] | 123 | |
---|
[1376] | 124 | toprow* as_toprow = (toprow*)this; |
---|
[1254] | 125 | |
---|
[1324] | 126 | int dimension = simplex->vertices.size()-1; |
---|
[1254] | 127 | |
---|
[1275] | 128 | mat jacobian(dimension,dimension); |
---|
[1254] | 129 | |
---|
[1271] | 130 | map<double,int> as; |
---|
[1379] | 131 | vertex* base_vertex = *simplex->vertices.begin(); |
---|
[1376] | 132 | |
---|
[1379] | 133 | int row_count = 0; |
---|
[1376] | 134 | for(set<vertex*>::iterator vert_ref = simplex->vertices.begin(); vert_ref!=simplex->vertices.end();vert_ref++) |
---|
[1271] | 135 | { |
---|
[1376] | 136 | if(vert_ref!=simplex->vertices.begin()) |
---|
| 137 | { |
---|
| 138 | vec relative_coords = (*vert_ref)->get_coordinates()-base_vertex->get_coordinates(); |
---|
[1379] | 139 | jacobian.set_row(row_count-1,relative_coords); |
---|
[1376] | 140 | } |
---|
[1268] | 141 | |
---|
[1379] | 142 | vec extended_coords = (*vert_ref)->get_coordinates(); |
---|
| 143 | extended_coords.ins(0,-1); |
---|
| 144 | |
---|
| 145 | double a = extended_coords*as_toprow->condition_sum; |
---|
[1376] | 146 | if(a<current_emlig->min_ll) |
---|
[1271] | 147 | { |
---|
[1376] | 148 | current_emlig->minimal_vertex = (*vert_ref); |
---|
| 149 | current_emlig->min_ll = a; |
---|
| 150 | } |
---|
[1275] | 151 | |
---|
[1376] | 152 | //cout << "a0:" << a_0 << " a0 coords:" << base_vertex->get_coordinates() << " am:" << a_m << " am coords:" << (*vert_ref)->get_coordinates() << endl; |
---|
[1275] | 153 | |
---|
[1376] | 154 | // cout << "Absolute coords:(V" << row_count << ")" << (*vert_ref)->get_coordinates() << endl; |
---|
| 155 | //cout << "Relative coords:(V" << row_count << ")" << relative_coords << endl; |
---|
[1275] | 156 | |
---|
[1376] | 157 | pair<map<double,int>::iterator,bool> returned = as.insert(pair<double,int>(a,1)); |
---|
| 158 | if(returned.second == false) |
---|
| 159 | { |
---|
| 160 | (*returned.first).second++; |
---|
[1379] | 161 | } |
---|
| 162 | |
---|
| 163 | row_count++; |
---|
| 164 | } |
---|
[1272] | 165 | |
---|
[1282] | 166 | /* |
---|
[1281] | 167 | int as_count = 1; |
---|
| 168 | for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++) |
---|
| 169 | { |
---|
| 170 | cout << "a_" << as_count << ": " << (*as_ref).first << " "; |
---|
| 171 | as_count++; |
---|
[1365] | 172 | } |
---|
[1379] | 173 | */ |
---|
[1269] | 174 | |
---|
[1379] | 175 | vector<double> gamma_facs; |
---|
[1271] | 176 | // cout << jacobian << endl; |
---|
[1379] | 177 | double det_jacobian = abs(det(jacobian)); |
---|
| 178 | for(map<double,int>::iterator a_ref = as.begin();a_ref!=as.end();a_ref++) |
---|
[1271] | 179 | { |
---|
[1379] | 180 | double k_multiplier = 1; |
---|
[1272] | 181 | |
---|
[1376] | 182 | int a_order = (*a_ref).second; |
---|
[1379] | 183 | current_emlig->set_correction_factors(a_order-1); |
---|
[1275] | 184 | |
---|
[1379] | 185 | vector<double> factors; |
---|
[1376] | 186 | for(map<double,int>::iterator a2_ref = as.begin();a2_ref!=as.end();a2_ref++) |
---|
| 187 | { |
---|
| 188 | if(a2_ref!=a_ref) |
---|
[1275] | 189 | { |
---|
[1376] | 190 | for(int k = 0;k<(*a2_ref).second;k++) |
---|
[1271] | 191 | { |
---|
[1379] | 192 | factors.push_back((*a2_ref).first-(*a_ref).first); |
---|
[1272] | 193 | } |
---|
[1270] | 194 | |
---|
[1379] | 195 | k_multiplier /= pow((*a2_ref).first-(*a_ref).first,(*a2_ref).second); |
---|
[1376] | 196 | } |
---|
[1379] | 197 | } |
---|
[1254] | 198 | |
---|
[1379] | 199 | double bracket_combination = 0; |
---|
[1365] | 200 | |
---|
[1379] | 201 | for(int k = 0;k < a_order;k++) |
---|
[1275] | 202 | { |
---|
[1379] | 203 | if(k==gamma_facs.size()) |
---|
[1275] | 204 | { |
---|
[1379] | 205 | gamma_facs.push_back(0.0); |
---|
[1325] | 206 | } |
---|
[1379] | 207 | |
---|
| 208 | double factor_multiplier = pow((double)-1,k)/pow((*a_ref).first,sigma_order-k);//pow((double)-1,k+1) |
---|
| 209 | |
---|
| 210 | if(k!=0) |
---|
| 211 | { |
---|
| 212 | ivec control_vec = ivec(); |
---|
| 213 | control_vec.ins(0,my_emlig->number_of_parameters-a_order+1); |
---|
| 214 | for(multiset<my_ivec>::iterator combi_ref = this->my_emlig->correction_factors[k-1].begin();combi_ref!=this->my_emlig->correction_factors[k-1].upper_bound(my_ivec(control_vec));combi_ref++) |
---|
| 215 | { |
---|
| 216 | double bracket_factor = 1/fact(a_order-1-k); |
---|
| 217 | |
---|
| 218 | for(int j = 0;j<(*combi_ref).size();j++) |
---|
| 219 | { |
---|
| 220 | //cout << "Factor vector:" << (*combi_ref) << endl; |
---|
| 221 | |
---|
| 222 | bracket_factor /= factors[(*combi_ref)[j]-1]; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | double value = bracket_factor*factor_multiplier*k_multiplier; |
---|
[1254] | 226 | |
---|
[1379] | 227 | simplex->insert_gamma(k,value,(*a_ref).first); |
---|
| 228 | gamma_facs[k] += value; |
---|
| 229 | } |
---|
[1349] | 230 | } |
---|
[1379] | 231 | else |
---|
| 232 | { |
---|
| 233 | double value = k_multiplier*factor_multiplier/fact(a_order-1); |
---|
| 234 | |
---|
| 235 | simplex->insert_gamma(0,value,(*a_ref).first); |
---|
| 236 | gamma_facs[k] += value; |
---|
| 237 | } |
---|
[1325] | 238 | } |
---|
[1272] | 239 | } |
---|
[1271] | 240 | |
---|
[1379] | 241 | double int_value = 0; |
---|
| 242 | int gamma_size = (int)gamma_facs.size(); |
---|
| 243 | if(sigma_order-gamma_size>=0) |
---|
| 244 | { |
---|
| 245 | for(int k = 0;k<gamma_size;k++) |
---|
| 246 | { |
---|
| 247 | int_value += det_jacobian*fact(sigma_order-gamma_size+k)/fact(jacobian.rows())*gamma_facs[k]/pow(2.0,sigma_order); |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | else |
---|
| 251 | { |
---|
| 252 | int_value = -1; |
---|
| 253 | } |
---|
[1271] | 254 | |
---|
[1379] | 255 | simplex->probability = int_value; |
---|
| 256 | //cout << "Probability:" << int_value << endl; |
---|
| 257 | return int_value; |
---|
[1271] | 258 | } |
---|
| 259 | else |
---|
| 260 | { |
---|
[1362] | 261 | cout << "Improper probability density." << endl; |
---|
[1271] | 262 | return 0.0; |
---|
| 263 | } |
---|
[1254] | 264 | } |
---|