Changeset 1186 for applications/robust
- Timestamp:
- 09/15/10 19:14:46 (14 years ago)
- Location:
- applications/robust
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/robust/main.cpp
r976 r1186 9 9 #include "robustlib.h" 10 10 11 using namespace bdm;11 //using namespace bdm; 12 12 13 13 int main ( int argc, char* argv[] ) { 14 14 15 emlig* emlig1 = new emlig(10); 16 15 17 return 0; 16 18 } -
applications/robust/robustlib.h
r1172 r1186 86 86 }; 87 87 88 /// A 88 /// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse 89 /// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. 89 90 class vertex : public polyhedron 90 91 { 92 /// A dynamic array representing coordinates of the vertex 91 93 vector<double> coordinates; 92 94 93 95 public: 94 96 97 /// Default constructor 95 98 vertex(); 96 99 100 /// Constructor of a vertex from a set of coordinates 97 101 vertex(vector<double> coordinates) 98 102 { … … 100 104 } 101 105 106 /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter 107 /// space of certain dimension is established, but the dimension is not known when the vertex is created. 102 108 void push_coordinate(double coordinate) 103 109 { … … 105 111 } 106 112 113 /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer 114 /// (not given by reference), but a new copy is created (they are given by value). 107 115 vector<double> get_coordinates() 108 116 { 109 117 vector<double> returned_vec; 110 118 111 copy(this->coordinates.begin(),this->coordinates.end(),returned_vec.begin()); 119 for(int i = 0;i<coordinates.size();i++) 120 { 121 returned_vec.push_back(coordinates[i]); 122 } 112 123 113 124 return returned_vec; … … 115 126 }; 116 127 128 /// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates 129 /// it from polyhedrons in other rows. 117 130 class toprow : public polyhedron 118 131 { 132 /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation 119 133 vector<double> condition; 120 134 121 135 public: 122 136 137 /// Default constructor 123 138 toprow(); 124 139 140 /// Constructor creating a toprow from the condition 125 141 toprow(vector<double> condition) 126 142 { … … 134 150 135 151 //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density 136 class emlig : publiceEF152 class emlig // : eEF 137 153 { 138 154 155 /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result 156 /// of data update from Bayesian estimation or set by the user if this emlig is a prior density 139 157 vector<vector<polyhedron*>> statistic; 140 158 141 159 public: 142 160 161 /// A default constructor creates an emlig with predefined statistic representing only the range of the given 162 /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. 143 163 emlig(int number_of_parameters) 144 164 { … … 146 166 } 147 167 168 /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a 169 /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. 148 170 emlig(vector<vector<polyhedron*>> statistic) 149 171 { … … 151 173 } 152 174 153 154 155 175 protected: 156 176 177 /// A method for creating plain default statistic representing only the range of the parameter space. 157 178 void create_statistic(int number_of_parameters) 158 179 { 180 // An empty vector of coordinates. 159 181 vector<double> origin_coord; 160 182 183 // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. 161 184 vertex *origin = new vertex(origin_coord); 162 185 186 // It has itself as a vertex. There will be a nice use for this when the vertices of its parents are searched in 187 // the recursive creation procedure below. 163 188 origin->vertices.push_back(origin); 164 189 190 // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse 191 // diagram. First we create a vector of polyhedrons.. 165 192 vector<polyhedron*> origin_vec; 166 193 194 // ..we fill it with the origin.. 167 195 origin_vec.push_back(origin); 168 196 197 // ..and we fill the statistic with the created vector. 169 198 statistic.push_back(origin_vec); 170 199 200 // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to 201 // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: 171 202 for(int i=0;i<number_of_parameters;i++) 172 203 { 204 // We first will create two new vertices. These will be the borders of the parameter space in the dimension 205 // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the 206 // right amount of zero cooridnates by reading them from the origin 173 207 vector<double> origin_coord1 = origin->get_coordinates(); 174 vector<double> origin_coord2 = origin->get_coordinates(); 175 176 origin->push_coordinate(0); 177 208 vector<double> origin_coord2 = origin->get_coordinates(); 209 210 // And we incorporate the nonzero coordinates into the new cooordinate vectors 178 211 origin_coord1.push_back(max_range); 179 212 origin_coord2.push_back(-max_range); 180 213 214 // Now we create the points 181 215 vertex *new_point1 = new vertex(origin_coord1); 182 vertex *new_point2 = new vertex(origin_coord2); 183 216 vertex *new_point2 = new vertex(origin_coord2); 217 218 //********************************************************************************************************* 219 // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old 220 // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points 221 // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons 222 // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is 223 // connected to the first (second) newly created vertex by a parent-child relation. 224 //********************************************************************************************************* 225 226 227 // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram 184 228 vector<vector<polyhedron*>> new_statistic1; 185 229 vector<vector<polyhedron*>> new_statistic2; 186 230 187 231 // Copy the statistic by rows 188 232 for(int j=0;j<statistic.size();j++) 189 233 { 234 // an element counter 190 235 int element_number = 0; 191 236 237 vector<polyhedron*> supportnew_1; 238 vector<polyhedron*> supportnew_2; 239 240 new_statistic1.push_back(supportnew_1); 241 new_statistic2.push_back(supportnew_2); 242 243 // for each polyhedron in the given row 192 244 for(vector<polyhedron*>::iterator horiz_ref = statistic[j].begin();horiz_ref<statistic[j].end();horiz_ref++) 193 245 { 246 // Append an extra zero coordinate to each of the vertices for the new dimension 247 // If j==0 => we loop through vertices 248 if(j == 0) 249 { 250 // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates 251 ((vertex*) (*horiz_ref))->push_coordinate(0); 252 } 253 254 // if it has parents 194 255 if(!(*horiz_ref)->parents.empty()) 195 256 { 257 // save the relative address of this child in a vector kids_rel_addresses of all its parents. 258 // This information will later be used for copying the whole Hasse diagram with each of the 259 // relations contained within. 196 260 for(vector<polyhedron*>::iterator parent_ref = (*horiz_ref)->parents.begin();parent_ref < (*horiz_ref)->parents.end();parent_ref++) 197 261 { … … 200 264 } 201 265 266 // ************************************************************************************************** 267 // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron 268 // will be created as a toprow, but this information will be later forgotten and only the polyhedrons 269 // in the top row of the Hasse diagram will be considered toprow for later use. 270 // ************************************************************************************************** 271 272 // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic 273 // this condition will be a vector of zeros. There are two vectors, because we need two copies of 274 // the original Hasse diagram. 202 275 vector<double> vec1(i+2,0); 203 276 vector<double> vec2(i+2,0); 204 277 278 // We create a new toprow with the previously specified condition. 205 279 toprow *current_copy1 = new toprow(vec1); 206 280 toprow *current_copy2 = new toprow(vec2); 207 281 282 // The vertices of the copies will be inherited, because there will be a parent/child relation 283 // between each polyhedron and its offspring (comming from the copy) and a parent has all the 284 // vertices of its child plus more. 208 285 for(vector<vertex*>::iterator vert_ref = (*horiz_ref)->vertices.begin();vert_ref<(*horiz_ref)->vertices.end();vert_ref++) 209 286 { … … 212 289 } 213 290 214 291 // The only new vertex of the offspring should be the newly created point. 215 292 current_copy1->vertices.push_back(new_point1); 216 293 current_copy2->vertices.push_back(new_point2); 217 294 295 // This method guarantees that each polyhedron is already triangulated, therefore its triangulation 296 // is only one set of vertices and it is the set of all its vertices. 218 297 current_copy1->triangulations.push_back(current_copy1->vertices); 219 298 current_copy2->triangulations.push_back(current_copy2->vertices); 220 221 222 299 300 // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying 301 // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the 302 // kids and when we do that and know the child, in the child we will remember the parent we came from. 303 // This way all the parents/children relations are saved in both the parent and the child. 223 304 if(!(*horiz_ref)->kids_rel_addresses.empty()) 224 305 { 225 306 for(vector<int>::iterator kid_ref = (*horiz_ref)->kids_rel_addresses.begin();kid_ref<(*horiz_ref)->kids_rel_addresses.end();kid_ref++) 226 { 227 current_copy1->children.push_back(new_statistic1[i][(*kid_ref)]); 228 current_copy2->children.push_back(new_statistic2[i][(*kid_ref)]); 229 230 new_statistic1[i][(*kid_ref)]->parents.push_back(current_copy1); 231 new_statistic2[i][(*kid_ref)]->parents.push_back(current_copy2); 307 { 308 // find the child and save the relation to the parent 309 current_copy1->children.push_back(new_statistic1[j-1][(*kid_ref)]); 310 current_copy2->children.push_back(new_statistic2[j-1][(*kid_ref)]); 311 312 // in the child save the parents' address 313 new_statistic1[j-1][(*kid_ref)]->parents.push_back(current_copy1); 314 new_statistic2[j-1][(*kid_ref)]->parents.push_back(current_copy2); 232 315 } 233 316 317 // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the 318 // Hasse diagram again) 234 319 (*horiz_ref)->kids_rel_addresses.clear(); 235 320 } 321 // If there were no children previously, we are copying a polyhedron that has been a vertex before. 322 // In this case it is a segment now and it will have a relation to its mother (copywise) and to the 323 // newly created point. Here we create the connection to the new point, again from both sides. 236 324 else 237 325 { 326 // Add the address of the new point in the former vertex 238 327 current_copy1->children.push_back(new_point1); 239 328 current_copy2->children.push_back(new_point2); 240 329 330 // Add the address of the former vertex in the new point 241 331 new_point1->parents.push_back(current_copy1); 242 332 new_point2->parents.push_back(current_copy2); 243 333 } 244 334 335 // Save the mother in its offspring 245 336 current_copy1->children.push_back(*horiz_ref); 246 337 current_copy2->children.push_back(*horiz_ref); 247 338 248 new_statistic1[i+1].push_back(current_copy1); 249 new_statistic2[i+1].push_back(current_copy2); 250 339 // Save the offspring in its mother 340 (*horiz_ref)->parents.push_back(current_copy1); 341 (*horiz_ref)->parents.push_back(current_copy2); 342 343 344 // Add the copies into the relevant statistic. The statistic will later be appended to the previous 345 // Hasse diagram 346 new_statistic1[j].push_back(current_copy1); 347 new_statistic2[j].push_back(current_copy2); 348 349 // Raise the count in the vector of polyhedrons 251 350 element_number++; 351 252 352 } 253 } 254 353 } 354 355 statistic[0].push_back(new_point1); 356 statistic[0].push_back(new_point2); 357 358 // Merge the new statistics into the old one. This will either be the final statistic or we will 359 // reenter the widening loop. 360 for(int j=0;j<new_statistic1.size();j++) 361 { 362 if(j+1==statistic.size()) 363 { 364 vector<polyhedron*> support; 365 statistic.push_back(support); 366 } 367 368 statistic[j+1].insert(statistic[j+1].end(),new_statistic1[j].begin(),new_statistic1[j].end()); 369 statistic[j+1].insert(statistic[j+1].end(),new_statistic2[j].begin(),new_statistic2[j].end()); 370 } 255 371 } 256 372 }