1 | /*! |
---|
2 | \file |
---|
3 | \brief Robust Bayesian auto-regression model |
---|
4 | \author Jan Sindelar. |
---|
5 | */ |
---|
6 | |
---|
7 | #ifndef ROBUST_H |
---|
8 | #define ROBUST_H |
---|
9 | |
---|
10 | //#include <stat/exp_family.h> |
---|
11 | #include <itpp/itbase.h> |
---|
12 | #include <map> |
---|
13 | #include <limits> |
---|
14 | #include <vector> |
---|
15 | #include <list> |
---|
16 | #include <set> |
---|
17 | #include <algorithm> |
---|
18 | |
---|
19 | //using namespace bdm; |
---|
20 | using namespace std; |
---|
21 | using namespace itpp; |
---|
22 | |
---|
23 | const double max_range = 10;//numeric_limits<double>::max()/10e-10; |
---|
24 | |
---|
25 | enum actions {MERGE, SPLIT}; |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | class polyhedron; |
---|
30 | class vertex; |
---|
31 | |
---|
32 | /* |
---|
33 | class t_simplex |
---|
34 | { |
---|
35 | public: |
---|
36 | set<vertex*> minima; |
---|
37 | |
---|
38 | set<vertex*> simplex; |
---|
39 | |
---|
40 | t_simplex(vertex* origin_vertex) |
---|
41 | { |
---|
42 | simplex.insert(origin_vertex); |
---|
43 | minima.insert(origin_vertex); |
---|
44 | } |
---|
45 | };*/ |
---|
46 | |
---|
47 | class emlig; |
---|
48 | |
---|
49 | |
---|
50 | /// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram |
---|
51 | /// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created. |
---|
52 | class polyhedron |
---|
53 | { |
---|
54 | /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of |
---|
55 | /// more than just the necessary number of conditions. For example if a newly created line passes through an already |
---|
56 | /// existing point, the points multiplicity will rise by 1. |
---|
57 | int multiplicity; |
---|
58 | |
---|
59 | int split_state; |
---|
60 | |
---|
61 | int merge_state; |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | public: |
---|
66 | emlig* my_emlig; |
---|
67 | |
---|
68 | /// A list of polyhedrons parents within the Hasse diagram. |
---|
69 | list<polyhedron*> parents; |
---|
70 | |
---|
71 | /// A list of polyhedrons children withing the Hasse diagram. |
---|
72 | list<polyhedron*> children; |
---|
73 | |
---|
74 | /// All the vertices of the given polyhedron |
---|
75 | set<vertex*> vertices; |
---|
76 | |
---|
77 | /// A list used for storing children that lie in the positive region related to a certain condition |
---|
78 | list<polyhedron*> positivechildren; |
---|
79 | |
---|
80 | /// A list used for storing children that lie in the negative region related to a certain condition |
---|
81 | list<polyhedron*> negativechildren; |
---|
82 | |
---|
83 | /// Children intersecting the condition |
---|
84 | list<polyhedron*> neutralchildren; |
---|
85 | |
---|
86 | list<polyhedron*> totallyneutralgrandchildren; |
---|
87 | |
---|
88 | list<polyhedron*> totallyneutralchildren; |
---|
89 | |
---|
90 | set<vertex*> positiveneutralvertices; |
---|
91 | |
---|
92 | set<vertex*> negativeneutralvertices; |
---|
93 | |
---|
94 | bool totally_neutral; |
---|
95 | |
---|
96 | list<polyhedron*> mergechildren; |
---|
97 | |
---|
98 | polyhedron* positiveparent; |
---|
99 | |
---|
100 | polyhedron* negativeparent; |
---|
101 | |
---|
102 | polyhedron* next_poly; |
---|
103 | |
---|
104 | polyhedron* prev_poly; |
---|
105 | |
---|
106 | int message_counter; |
---|
107 | |
---|
108 | /// List of triangulation polyhedrons of the polyhedron given by their relative vertices. |
---|
109 | list<set<vertex*>> triangulation; |
---|
110 | |
---|
111 | /// A list of relative addresses serving for Hasse diagram construction. |
---|
112 | list<int> kids_rel_addresses; |
---|
113 | |
---|
114 | /// Default constructor |
---|
115 | polyhedron() |
---|
116 | { |
---|
117 | multiplicity = 1; |
---|
118 | |
---|
119 | message_counter = 0; |
---|
120 | |
---|
121 | totally_neutral = NULL; |
---|
122 | } |
---|
123 | |
---|
124 | /// Setter for raising multiplicity |
---|
125 | void raise_multiplicity() |
---|
126 | { |
---|
127 | multiplicity++; |
---|
128 | } |
---|
129 | |
---|
130 | /// Setter for lowering multiplicity |
---|
131 | void lower_multiplicity() |
---|
132 | { |
---|
133 | multiplicity--; |
---|
134 | } |
---|
135 | |
---|
136 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
---|
137 | int operator==(polyhedron polyhedron2) |
---|
138 | { |
---|
139 | return true; |
---|
140 | } |
---|
141 | |
---|
142 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
---|
143 | int operator<(polyhedron polyhedron2) |
---|
144 | { |
---|
145 | return false; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | void set_state(double state_indicator, actions action) |
---|
151 | { |
---|
152 | switch(action) |
---|
153 | { |
---|
154 | case MERGE: |
---|
155 | merge_state = (int)sign(state_indicator); |
---|
156 | break; |
---|
157 | case SPLIT: |
---|
158 | split_state = (int)sign(state_indicator); |
---|
159 | break; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | int get_state(actions action) |
---|
164 | { |
---|
165 | switch(action) |
---|
166 | { |
---|
167 | case MERGE: |
---|
168 | return merge_state; |
---|
169 | break; |
---|
170 | case SPLIT: |
---|
171 | return split_state; |
---|
172 | break; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | int number_of_children() |
---|
177 | { |
---|
178 | return children.size(); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | void triangulate(bool should_integrate); |
---|
183 | }; |
---|
184 | |
---|
185 | |
---|
186 | /// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse |
---|
187 | /// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. |
---|
188 | class vertex : public polyhedron |
---|
189 | { |
---|
190 | /// A dynamic array representing coordinates of the vertex |
---|
191 | vec coordinates; |
---|
192 | |
---|
193 | public: |
---|
194 | |
---|
195 | double function_value; |
---|
196 | |
---|
197 | /// Default constructor |
---|
198 | vertex(); |
---|
199 | |
---|
200 | /// Constructor of a vertex from a set of coordinates |
---|
201 | vertex(vec coordinates) |
---|
202 | { |
---|
203 | this->coordinates = coordinates; |
---|
204 | |
---|
205 | vertices.insert(this); |
---|
206 | |
---|
207 | set<vertex*> vert_simplex; |
---|
208 | |
---|
209 | vert_simplex.insert(this); |
---|
210 | |
---|
211 | triangulation.push_back(vert_simplex); |
---|
212 | } |
---|
213 | |
---|
214 | /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter |
---|
215 | /// space of certain dimension is established, but the dimension is not known when the vertex is created. |
---|
216 | void push_coordinate(double coordinate) |
---|
217 | { |
---|
218 | coordinates = concat(coordinates,coordinate); |
---|
219 | } |
---|
220 | |
---|
221 | /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer |
---|
222 | /// (not given by reference), but a new copy is created (they are given by value). |
---|
223 | vec get_coordinates() |
---|
224 | { |
---|
225 | return coordinates; |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | }; |
---|
230 | |
---|
231 | |
---|
232 | /// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates |
---|
233 | /// it from polyhedrons in other rows. |
---|
234 | class toprow : public polyhedron |
---|
235 | { |
---|
236 | |
---|
237 | public: |
---|
238 | double probability; |
---|
239 | |
---|
240 | vertex* minimal_vertex; |
---|
241 | |
---|
242 | /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation |
---|
243 | vec condition; |
---|
244 | |
---|
245 | int condition_order; |
---|
246 | |
---|
247 | /// Default constructor |
---|
248 | toprow(){}; |
---|
249 | |
---|
250 | /// Constructor creating a toprow from the condition |
---|
251 | toprow(vec condition, int condition_order) |
---|
252 | { |
---|
253 | this->condition = condition; |
---|
254 | this->condition_order = condition_order; |
---|
255 | } |
---|
256 | |
---|
257 | double integrate_simplex(set<vertex*> simplex, char c); |
---|
258 | |
---|
259 | }; |
---|
260 | |
---|
261 | |
---|
262 | class condition |
---|
263 | { |
---|
264 | public: |
---|
265 | vec value; |
---|
266 | |
---|
267 | int multiplicity; |
---|
268 | |
---|
269 | condition(vec value) |
---|
270 | { |
---|
271 | this->value = value; |
---|
272 | multiplicity = 1; |
---|
273 | } |
---|
274 | }; |
---|
275 | |
---|
276 | |
---|
277 | |
---|
278 | |
---|
279 | class c_statistic |
---|
280 | { |
---|
281 | |
---|
282 | public: |
---|
283 | polyhedron* end_poly; |
---|
284 | polyhedron* start_poly; |
---|
285 | |
---|
286 | vector<polyhedron*> rows; |
---|
287 | |
---|
288 | vector<polyhedron*> row_ends; |
---|
289 | |
---|
290 | c_statistic() |
---|
291 | { |
---|
292 | end_poly = new polyhedron(); |
---|
293 | start_poly = new polyhedron(); |
---|
294 | }; |
---|
295 | |
---|
296 | void append_polyhedron(int row, polyhedron* appended_start, polyhedron* appended_end) |
---|
297 | { |
---|
298 | if(row>((int)rows.size())-1) |
---|
299 | { |
---|
300 | if(row>rows.size()) |
---|
301 | { |
---|
302 | throw new exception("You are trying to append a polyhedron whose children are not in the statistic yet!"); |
---|
303 | return; |
---|
304 | } |
---|
305 | |
---|
306 | rows.push_back(end_poly); |
---|
307 | row_ends.push_back(end_poly); |
---|
308 | } |
---|
309 | |
---|
310 | // POSSIBLE FAILURE: the function is not checking if start and end are connected |
---|
311 | |
---|
312 | if(rows[row] != end_poly) |
---|
313 | { |
---|
314 | appended_start->prev_poly = row_ends[row]; |
---|
315 | row_ends[row]->next_poly = appended_start; |
---|
316 | |
---|
317 | } |
---|
318 | else if((row>0 && rows[row-1]!=end_poly)||row==0) |
---|
319 | { |
---|
320 | appended_start->prev_poly = start_poly; |
---|
321 | rows[row]= appended_start; |
---|
322 | } |
---|
323 | else |
---|
324 | { |
---|
325 | throw new exception("Wrong polyhedron insertion into statistic: missing intermediary polyhedron!"); |
---|
326 | } |
---|
327 | |
---|
328 | appended_end->next_poly = end_poly; |
---|
329 | row_ends[row] = appended_end; |
---|
330 | } |
---|
331 | |
---|
332 | void append_polyhedron(int row, polyhedron* appended_poly) |
---|
333 | { |
---|
334 | append_polyhedron(row,appended_poly,appended_poly); |
---|
335 | } |
---|
336 | |
---|
337 | void insert_polyhedron(int row, polyhedron* inserted_poly, polyhedron* following_poly) |
---|
338 | { |
---|
339 | if(following_poly != end_poly) |
---|
340 | { |
---|
341 | inserted_poly->next_poly = following_poly; |
---|
342 | inserted_poly->prev_poly = following_poly->prev_poly; |
---|
343 | |
---|
344 | if(following_poly->prev_poly == start_poly) |
---|
345 | { |
---|
346 | rows[row] = inserted_poly; |
---|
347 | } |
---|
348 | else |
---|
349 | { |
---|
350 | inserted_poly->prev_poly->next_poly = inserted_poly; |
---|
351 | } |
---|
352 | |
---|
353 | following_poly->prev_poly = inserted_poly; |
---|
354 | } |
---|
355 | else |
---|
356 | { |
---|
357 | this->append_polyhedron(row, inserted_poly); |
---|
358 | } |
---|
359 | |
---|
360 | } |
---|
361 | |
---|
362 | |
---|
363 | |
---|
364 | |
---|
365 | void delete_polyhedron(int row, polyhedron* deleted_poly) |
---|
366 | { |
---|
367 | if(deleted_poly->prev_poly != start_poly) |
---|
368 | { |
---|
369 | deleted_poly->prev_poly->next_poly = deleted_poly->next_poly; |
---|
370 | } |
---|
371 | else |
---|
372 | { |
---|
373 | rows[row] = deleted_poly->next_poly; |
---|
374 | } |
---|
375 | |
---|
376 | if(deleted_poly->next_poly!=end_poly) |
---|
377 | { |
---|
378 | deleted_poly->next_poly->prev_poly = deleted_poly->prev_poly; |
---|
379 | } |
---|
380 | else |
---|
381 | { |
---|
382 | row_ends[row] = deleted_poly->prev_poly; |
---|
383 | } |
---|
384 | |
---|
385 | |
---|
386 | |
---|
387 | deleted_poly->next_poly = NULL; |
---|
388 | deleted_poly->prev_poly = NULL; |
---|
389 | } |
---|
390 | |
---|
391 | int size() |
---|
392 | { |
---|
393 | return rows.size(); |
---|
394 | } |
---|
395 | |
---|
396 | polyhedron* get_end() |
---|
397 | { |
---|
398 | return end_poly; |
---|
399 | } |
---|
400 | |
---|
401 | polyhedron* get_start() |
---|
402 | { |
---|
403 | return start_poly; |
---|
404 | } |
---|
405 | |
---|
406 | int row_size(int row) |
---|
407 | { |
---|
408 | if(this->size()>row && row>=0) |
---|
409 | { |
---|
410 | int row_size = 0; |
---|
411 | |
---|
412 | for(polyhedron* row_poly = rows[row]; row_poly!=end_poly; row_poly=row_poly->next_poly) |
---|
413 | { |
---|
414 | row_size++; |
---|
415 | } |
---|
416 | |
---|
417 | return row_size; |
---|
418 | } |
---|
419 | else |
---|
420 | { |
---|
421 | throw new exception("There is no row to obtain size from!"); |
---|
422 | } |
---|
423 | } |
---|
424 | }; |
---|
425 | |
---|
426 | |
---|
427 | class my_ivec : public ivec |
---|
428 | { |
---|
429 | public: |
---|
430 | my_ivec():ivec(){}; |
---|
431 | |
---|
432 | my_ivec(ivec origin):ivec() |
---|
433 | { |
---|
434 | this->ins(0,origin); |
---|
435 | } |
---|
436 | |
---|
437 | bool operator>(const my_ivec &second) const |
---|
438 | { |
---|
439 | return max(*this)>max(second); |
---|
440 | |
---|
441 | /* |
---|
442 | int size1 = this->size(); |
---|
443 | int size2 = second.size(); |
---|
444 | |
---|
445 | int counter1 = 0; |
---|
446 | while(0==0) |
---|
447 | { |
---|
448 | if((*this)[counter1]==0) |
---|
449 | { |
---|
450 | size1--; |
---|
451 | } |
---|
452 | |
---|
453 | if((*this)[counter1]!=0) |
---|
454 | break; |
---|
455 | |
---|
456 | counter1++; |
---|
457 | } |
---|
458 | |
---|
459 | int counter2 = 0; |
---|
460 | while(0==0) |
---|
461 | { |
---|
462 | if(second[counter2]==0) |
---|
463 | { |
---|
464 | size2--; |
---|
465 | } |
---|
466 | |
---|
467 | if(second[counter2]!=0) |
---|
468 | break; |
---|
469 | |
---|
470 | counter2++; |
---|
471 | } |
---|
472 | |
---|
473 | if(size1!=size2) |
---|
474 | { |
---|
475 | return size1>size2; |
---|
476 | } |
---|
477 | else |
---|
478 | { |
---|
479 | for(int i = 0;i<size1;i++) |
---|
480 | { |
---|
481 | if((*this)[counter1+i]!=second[counter2+i]) |
---|
482 | { |
---|
483 | return (*this)[counter1+i]>second[counter2+i]; |
---|
484 | } |
---|
485 | } |
---|
486 | |
---|
487 | return false; |
---|
488 | }*/ |
---|
489 | } |
---|
490 | |
---|
491 | |
---|
492 | bool operator==(const my_ivec &second) const |
---|
493 | { |
---|
494 | return max(*this)==max(second); |
---|
495 | |
---|
496 | /* |
---|
497 | int size1 = this->size(); |
---|
498 | int size2 = second.size(); |
---|
499 | |
---|
500 | int counter = 0; |
---|
501 | while(0==0) |
---|
502 | { |
---|
503 | if((*this)[counter]==0) |
---|
504 | { |
---|
505 | size1--; |
---|
506 | } |
---|
507 | |
---|
508 | if((*this)[counter]!=0) |
---|
509 | break; |
---|
510 | |
---|
511 | counter++; |
---|
512 | } |
---|
513 | |
---|
514 | counter = 0; |
---|
515 | while(0==0) |
---|
516 | { |
---|
517 | if(second[counter]==0) |
---|
518 | { |
---|
519 | size2--; |
---|
520 | } |
---|
521 | |
---|
522 | if(second[counter]!=0) |
---|
523 | break; |
---|
524 | |
---|
525 | counter++; |
---|
526 | } |
---|
527 | |
---|
528 | if(size1!=size2) |
---|
529 | { |
---|
530 | return false; |
---|
531 | } |
---|
532 | else |
---|
533 | { |
---|
534 | for(int i=0;i<size1;i++) |
---|
535 | { |
---|
536 | if((*this)[size()-1-i]!=second[second.size()-1-i]) |
---|
537 | { |
---|
538 | return false; |
---|
539 | } |
---|
540 | } |
---|
541 | |
---|
542 | return true; |
---|
543 | }*/ |
---|
544 | } |
---|
545 | |
---|
546 | bool operator<(const my_ivec &second) const |
---|
547 | { |
---|
548 | return !(((*this)>second)||((*this)==second)); |
---|
549 | } |
---|
550 | |
---|
551 | bool operator!=(const my_ivec &second) const |
---|
552 | { |
---|
553 | return !((*this)==second); |
---|
554 | } |
---|
555 | |
---|
556 | bool operator<=(const my_ivec &second) const |
---|
557 | { |
---|
558 | return !((*this)>second); |
---|
559 | } |
---|
560 | |
---|
561 | bool operator>=(const my_ivec &second) const |
---|
562 | { |
---|
563 | return !((*this)<second); |
---|
564 | } |
---|
565 | |
---|
566 | my_ivec right(my_ivec original) |
---|
567 | { |
---|
568 | |
---|
569 | } |
---|
570 | }; |
---|
571 | |
---|
572 | |
---|
573 | |
---|
574 | |
---|
575 | |
---|
576 | |
---|
577 | |
---|
578 | //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density |
---|
579 | class emlig // : eEF |
---|
580 | { |
---|
581 | |
---|
582 | /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result |
---|
583 | /// of data update from Bayesian estimation or set by the user if this emlig is a prior density |
---|
584 | |
---|
585 | |
---|
586 | vector<list<polyhedron*>> for_splitting; |
---|
587 | |
---|
588 | vector<list<polyhedron*>> for_merging; |
---|
589 | |
---|
590 | list<condition*> conditions; |
---|
591 | |
---|
592 | double normalization_factor; |
---|
593 | |
---|
594 | |
---|
595 | |
---|
596 | void alter_toprow_conditions(vec condition, bool should_be_added) |
---|
597 | { |
---|
598 | for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
---|
599 | { |
---|
600 | double product = 0; |
---|
601 | |
---|
602 | set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin(); |
---|
603 | |
---|
604 | do |
---|
605 | { |
---|
606 | product = (*vertex_ref)->get_coordinates()*condition; |
---|
607 | } |
---|
608 | while(product == 0); |
---|
609 | |
---|
610 | if((product>0 && should_be_added)||(product<0 && !should_be_added)) |
---|
611 | { |
---|
612 | ((toprow*) horiz_ref)->condition += condition; |
---|
613 | } |
---|
614 | else |
---|
615 | { |
---|
616 | ((toprow*) horiz_ref)->condition -= condition; |
---|
617 | } |
---|
618 | } |
---|
619 | } |
---|
620 | |
---|
621 | |
---|
622 | |
---|
623 | void send_state_message(polyhedron* sender, vec toadd, vec toremove, int level) |
---|
624 | { |
---|
625 | |
---|
626 | bool shouldmerge = (toremove.size() != 0); |
---|
627 | bool shouldsplit = (toadd.size() != 0); |
---|
628 | |
---|
629 | if(shouldsplit||shouldmerge) |
---|
630 | { |
---|
631 | for(list<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator!=sender->parents.end();parent_iterator++) |
---|
632 | { |
---|
633 | polyhedron* current_parent = *parent_iterator; |
---|
634 | |
---|
635 | current_parent->message_counter++; |
---|
636 | |
---|
637 | bool is_last = (current_parent->message_counter == current_parent->number_of_children()); |
---|
638 | |
---|
639 | if(shouldmerge) |
---|
640 | { |
---|
641 | int child_state = sender->get_state(MERGE); |
---|
642 | int parent_state = current_parent->get_state(MERGE); |
---|
643 | |
---|
644 | if(parent_state == 0) |
---|
645 | { |
---|
646 | current_parent->set_state(child_state, MERGE); |
---|
647 | |
---|
648 | if(child_state == 0) |
---|
649 | { |
---|
650 | current_parent->mergechildren.push_back(sender); |
---|
651 | } |
---|
652 | } |
---|
653 | else |
---|
654 | { |
---|
655 | if(child_state == 0) |
---|
656 | { |
---|
657 | if(parent_state > 0) |
---|
658 | { |
---|
659 | sender->positiveparent = current_parent; |
---|
660 | } |
---|
661 | else |
---|
662 | { |
---|
663 | sender->negativeparent = current_parent; |
---|
664 | } |
---|
665 | } |
---|
666 | } |
---|
667 | |
---|
668 | if(is_last) |
---|
669 | { |
---|
670 | if(parent_state > 0) |
---|
671 | { |
---|
672 | for(list<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++) |
---|
673 | { |
---|
674 | (*merge_child)->positiveparent = current_parent; |
---|
675 | } |
---|
676 | } |
---|
677 | |
---|
678 | if(parent_state < 0) |
---|
679 | { |
---|
680 | for(list<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++) |
---|
681 | { |
---|
682 | (*merge_child)->negativeparent = current_parent; |
---|
683 | } |
---|
684 | } |
---|
685 | |
---|
686 | if(parent_state == 0) |
---|
687 | { |
---|
688 | for_merging[level+1].push_back(current_parent); |
---|
689 | } |
---|
690 | |
---|
691 | current_parent->mergechildren.clear(); |
---|
692 | } |
---|
693 | |
---|
694 | |
---|
695 | } |
---|
696 | |
---|
697 | if(shouldsplit) |
---|
698 | { |
---|
699 | current_parent->totallyneutralgrandchildren.insert(current_parent->totallyneutralgrandchildren.end(),sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end()); |
---|
700 | |
---|
701 | switch(sender->get_state(SPLIT)) |
---|
702 | { |
---|
703 | case 1: |
---|
704 | current_parent->positivechildren.push_back(sender); |
---|
705 | current_parent->positiveneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); |
---|
706 | break; |
---|
707 | case 0: |
---|
708 | current_parent->neutralchildren.push_back(sender); |
---|
709 | current_parent->positiveneutralvertices.insert(sender->positiveneutralvertices.begin(),sender->positiveneutralvertices.end()); |
---|
710 | current_parent->negativeneutralvertices.insert(sender->negativeneutralvertices.begin(),sender->negativeneutralvertices.end()); |
---|
711 | |
---|
712 | if(current_parent->totally_neutral == NULL) |
---|
713 | { |
---|
714 | current_parent->totally_neutral = sender->totally_neutral; |
---|
715 | } |
---|
716 | else |
---|
717 | { |
---|
718 | current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral; |
---|
719 | } |
---|
720 | |
---|
721 | if(sender->totally_neutral) |
---|
722 | { |
---|
723 | current_parent->totallyneutralchildren.push_back(sender); |
---|
724 | } |
---|
725 | |
---|
726 | break; |
---|
727 | case -1: |
---|
728 | current_parent->negativechildren.push_back(sender); |
---|
729 | current_parent->negativeneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); |
---|
730 | break; |
---|
731 | } |
---|
732 | |
---|
733 | if(is_last) |
---|
734 | { |
---|
735 | unique(current_parent->totallyneutralgrandchildren.begin(),current_parent->totallyneutralgrandchildren.end()); |
---|
736 | |
---|
737 | if((current_parent->negativechildren.size()>0&¤t_parent->positivechildren.size()>0)|| |
---|
738 | (current_parent->neutralchildren.size()>0&¤t_parent->totally_neutral==false)) |
---|
739 | { |
---|
740 | |
---|
741 | for_splitting[level+1].push_back(current_parent); |
---|
742 | |
---|
743 | current_parent->set_state(0, SPLIT); |
---|
744 | } |
---|
745 | else |
---|
746 | { |
---|
747 | |
---|
748 | |
---|
749 | if(current_parent->negativechildren.size()>0) |
---|
750 | { |
---|
751 | current_parent->set_state(-1, SPLIT); |
---|
752 | |
---|
753 | ((toprow*)current_parent)->condition-=toadd; |
---|
754 | |
---|
755 | |
---|
756 | } |
---|
757 | else if(current_parent->positivechildren.size()>0) |
---|
758 | { |
---|
759 | current_parent->set_state(1, SPLIT); |
---|
760 | |
---|
761 | ((toprow*)current_parent)->condition+=toadd; |
---|
762 | } |
---|
763 | else |
---|
764 | { |
---|
765 | current_parent->raise_multiplicity(); |
---|
766 | } |
---|
767 | |
---|
768 | ((toprow*)current_parent)->condition_order++; |
---|
769 | |
---|
770 | if(level == number_of_parameters - 1) |
---|
771 | { |
---|
772 | toprow* cur_par_toprow = ((toprow*)current_parent); |
---|
773 | cur_par_toprow->probability = 0.0; |
---|
774 | |
---|
775 | for(list<set<vertex*>>::iterator t_ref = current_parent->triangulation.begin();t_ref!=current_parent->triangulation.end();t_ref++) |
---|
776 | { |
---|
777 | cur_par_toprow->probability += cur_par_toprow->integrate_simplex(*t_ref,'C'); |
---|
778 | } |
---|
779 | } |
---|
780 | |
---|
781 | current_parent->positivechildren.clear(); |
---|
782 | current_parent->negativechildren.clear(); |
---|
783 | current_parent->neutralchildren.clear(); |
---|
784 | current_parent->totallyneutralchildren.clear(); |
---|
785 | current_parent->totallyneutralgrandchildren.clear(); |
---|
786 | current_parent->positiveneutralvertices.clear(); |
---|
787 | current_parent->negativeneutralvertices.clear(); |
---|
788 | current_parent->totally_neutral = NULL; |
---|
789 | current_parent->kids_rel_addresses.clear(); |
---|
790 | current_parent->message_counter = 0; |
---|
791 | } |
---|
792 | } |
---|
793 | } |
---|
794 | |
---|
795 | if(is_last) |
---|
796 | { |
---|
797 | send_state_message(current_parent,toadd,toremove,level+1); |
---|
798 | } |
---|
799 | |
---|
800 | } |
---|
801 | |
---|
802 | } |
---|
803 | } |
---|
804 | |
---|
805 | public: |
---|
806 | c_statistic statistic; |
---|
807 | |
---|
808 | vertex* minimal_vertex; |
---|
809 | |
---|
810 | double likelihood_value; |
---|
811 | |
---|
812 | vector<multiset<my_ivec>> correction_factors; |
---|
813 | |
---|
814 | int number_of_parameters; |
---|
815 | |
---|
816 | /// A default constructor creates an emlig with predefined statistic representing only the range of the given |
---|
817 | /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. |
---|
818 | emlig(int number_of_parameters) |
---|
819 | { |
---|
820 | this->number_of_parameters = number_of_parameters; |
---|
821 | |
---|
822 | create_statistic(number_of_parameters); |
---|
823 | |
---|
824 | likelihood_value = numeric_limits<double>::max(); |
---|
825 | } |
---|
826 | |
---|
827 | /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a |
---|
828 | /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. |
---|
829 | emlig(c_statistic statistic) |
---|
830 | { |
---|
831 | this->statistic = statistic; |
---|
832 | |
---|
833 | likelihood_value = numeric_limits<double>::max(); |
---|
834 | } |
---|
835 | |
---|
836 | void step_me(int marker) |
---|
837 | { |
---|
838 | |
---|
839 | for(int i = 0;i<statistic.size();i++) |
---|
840 | { |
---|
841 | for(polyhedron* horiz_ref = statistic.rows[i];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
---|
842 | { |
---|
843 | if(i==statistic.size()-1) |
---|
844 | { |
---|
845 | cout << ((toprow*)horiz_ref)->condition << " " << ((toprow*)horiz_ref)->probability << endl; |
---|
846 | cout << "Order:" << ((toprow*)horiz_ref)->condition_order << endl; |
---|
847 | } |
---|
848 | char* string = "Checkpoint"; |
---|
849 | } |
---|
850 | } |
---|
851 | |
---|
852 | /* |
---|
853 | list<vec> table_entries; |
---|
854 | for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.row_ends[statistic.size()-1];horiz_ref=horiz_ref->next_poly) |
---|
855 | { |
---|
856 | toprow *current_toprow = (toprow*)(horiz_ref); |
---|
857 | for(list<set<vertex*>>::iterator tri_ref = current_toprow->triangulation.begin();tri_ref!=current_toprow->triangulation.end();tri_ref++) |
---|
858 | { |
---|
859 | for(set<vertex*>::iterator vert_ref = (*tri_ref).begin();vert_ref!=(*tri_ref).end();vert_ref++) |
---|
860 | { |
---|
861 | vec table_entry = vec(); |
---|
862 | |
---|
863 | table_entry.ins(0,(*vert_ref)->get_coordinates()*current_toprow->condition.get(1,current_toprow->condition.size()-1)-current_toprow->condition.get(0,0)); |
---|
864 | |
---|
865 | table_entry.ins(0,(*vert_ref)->get_coordinates()); |
---|
866 | |
---|
867 | table_entries.push_back(table_entry); |
---|
868 | } |
---|
869 | } |
---|
870 | } |
---|
871 | |
---|
872 | unique(table_entries.begin(),table_entries.end()); |
---|
873 | |
---|
874 | |
---|
875 | |
---|
876 | for(list<vec>::iterator entry_ref = table_entries.begin();entry_ref!=table_entries.end();entry_ref++) |
---|
877 | { |
---|
878 | ofstream myfile; |
---|
879 | myfile.open("robust_data.txt", ios::out | ios::app); |
---|
880 | if (myfile.is_open()) |
---|
881 | { |
---|
882 | for(int i = 0;i<(*entry_ref).size();i++) |
---|
883 | { |
---|
884 | myfile << (*entry_ref)[i] << ";"; |
---|
885 | } |
---|
886 | myfile << endl; |
---|
887 | |
---|
888 | myfile.close(); |
---|
889 | } |
---|
890 | else |
---|
891 | { |
---|
892 | cout << "File problem." << endl; |
---|
893 | } |
---|
894 | } |
---|
895 | */ |
---|
896 | |
---|
897 | |
---|
898 | return; |
---|
899 | } |
---|
900 | |
---|
901 | int statistic_rowsize(int row) |
---|
902 | { |
---|
903 | return statistic.row_size(row); |
---|
904 | } |
---|
905 | |
---|
906 | void add_condition(vec toadd) |
---|
907 | { |
---|
908 | vec null_vector = ""; |
---|
909 | |
---|
910 | add_and_remove_condition(toadd, null_vector); |
---|
911 | } |
---|
912 | |
---|
913 | |
---|
914 | void remove_condition(vec toremove) |
---|
915 | { |
---|
916 | vec null_vector = ""; |
---|
917 | |
---|
918 | add_and_remove_condition(null_vector, toremove); |
---|
919 | |
---|
920 | } |
---|
921 | |
---|
922 | |
---|
923 | void add_and_remove_condition(vec toadd, vec toremove) |
---|
924 | { |
---|
925 | likelihood_value = numeric_limits<double>::max(); |
---|
926 | |
---|
927 | bool should_remove = (toremove.size() != 0); |
---|
928 | bool should_add = (toadd.size() != 0); |
---|
929 | |
---|
930 | for_splitting.clear(); |
---|
931 | for_merging.clear(); |
---|
932 | |
---|
933 | for(int i = 0;i<statistic.size();i++) |
---|
934 | { |
---|
935 | list<polyhedron*> empty_split; |
---|
936 | list<polyhedron*> empty_merge; |
---|
937 | |
---|
938 | for_splitting.push_back(empty_split); |
---|
939 | for_merging.push_back(empty_merge); |
---|
940 | } |
---|
941 | |
---|
942 | list<condition*>::iterator toremove_ref = conditions.end(); |
---|
943 | bool condition_should_be_added = false; |
---|
944 | |
---|
945 | for(list<condition*>::iterator ref = conditions.begin();ref!=conditions.end();ref++) |
---|
946 | { |
---|
947 | if(should_remove) |
---|
948 | { |
---|
949 | if((*ref)->value == toremove) |
---|
950 | { |
---|
951 | if((*ref)->multiplicity>1) |
---|
952 | { |
---|
953 | (*ref)->multiplicity--; |
---|
954 | |
---|
955 | alter_toprow_conditions(toremove,false); |
---|
956 | |
---|
957 | should_remove = false; |
---|
958 | } |
---|
959 | else |
---|
960 | { |
---|
961 | toremove_ref = ref; |
---|
962 | } |
---|
963 | } |
---|
964 | } |
---|
965 | |
---|
966 | if(should_add) |
---|
967 | { |
---|
968 | if((*ref)->value == toadd) |
---|
969 | { |
---|
970 | (*ref)->multiplicity++; |
---|
971 | |
---|
972 | alter_toprow_conditions(toadd,true); |
---|
973 | |
---|
974 | should_add = false; |
---|
975 | } |
---|
976 | else |
---|
977 | { |
---|
978 | condition_should_be_added = true; |
---|
979 | } |
---|
980 | } |
---|
981 | } |
---|
982 | |
---|
983 | if(toremove_ref!=conditions.end()) |
---|
984 | { |
---|
985 | conditions.erase(toremove_ref); |
---|
986 | } |
---|
987 | |
---|
988 | if(condition_should_be_added) |
---|
989 | { |
---|
990 | conditions.push_back(new condition(toadd)); |
---|
991 | } |
---|
992 | |
---|
993 | |
---|
994 | |
---|
995 | for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly) |
---|
996 | { |
---|
997 | vertex* current_vertex = (vertex*)horizontal_position; |
---|
998 | |
---|
999 | if(should_add||should_remove) |
---|
1000 | { |
---|
1001 | vec appended_vec = current_vertex->get_coordinates(); |
---|
1002 | appended_vec.ins(0,-1.0); |
---|
1003 | |
---|
1004 | if(should_add) |
---|
1005 | { |
---|
1006 | double local_condition = toadd*appended_vec; |
---|
1007 | |
---|
1008 | current_vertex->set_state(local_condition,SPLIT); |
---|
1009 | |
---|
1010 | if(local_condition == 0) |
---|
1011 | { |
---|
1012 | current_vertex->totally_neutral = true; |
---|
1013 | |
---|
1014 | current_vertex->raise_multiplicity(); |
---|
1015 | |
---|
1016 | current_vertex->negativeneutralvertices.insert(current_vertex); |
---|
1017 | current_vertex->positiveneutralvertices.insert(current_vertex); |
---|
1018 | } |
---|
1019 | } |
---|
1020 | |
---|
1021 | if(should_remove) |
---|
1022 | { |
---|
1023 | double local_condition = toremove*appended_vec; |
---|
1024 | |
---|
1025 | current_vertex->set_state(local_condition,MERGE); |
---|
1026 | |
---|
1027 | if(local_condition == 0) |
---|
1028 | { |
---|
1029 | for_merging[0].push_back(current_vertex); |
---|
1030 | } |
---|
1031 | } |
---|
1032 | } |
---|
1033 | |
---|
1034 | send_state_message(current_vertex, toadd, toremove, 0); |
---|
1035 | |
---|
1036 | } |
---|
1037 | |
---|
1038 | if(should_add) |
---|
1039 | { |
---|
1040 | int k = 1; |
---|
1041 | |
---|
1042 | vector<list<polyhedron*>>::iterator beginning_ref = ++for_splitting.begin(); |
---|
1043 | |
---|
1044 | for(vector<list<polyhedron*>>::iterator vert_ref = beginning_ref;vert_ref<for_splitting.end();vert_ref++) |
---|
1045 | { |
---|
1046 | |
---|
1047 | for(list<polyhedron*>::reverse_iterator split_ref = vert_ref->rbegin();split_ref != vert_ref->rend();split_ref++) |
---|
1048 | { |
---|
1049 | polyhedron* new_totally_neutral_child; |
---|
1050 | |
---|
1051 | polyhedron* current_polyhedron = (*split_ref); |
---|
1052 | |
---|
1053 | if(vert_ref == beginning_ref) |
---|
1054 | { |
---|
1055 | vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates(); |
---|
1056 | vec coordinates2 = ((vertex*)(*(++current_polyhedron->children.begin())))->get_coordinates(); |
---|
1057 | |
---|
1058 | vec extended_coord2 = coordinates2; |
---|
1059 | extended_coord2.ins(0,-1.0); |
---|
1060 | |
---|
1061 | double t = (-toadd*extended_coord2)/((toadd(1,toadd.size()-1)*(coordinates1-coordinates2))); |
---|
1062 | |
---|
1063 | vec new_coordinates = coordinates2+t*(coordinates1-coordinates2); |
---|
1064 | |
---|
1065 | // cout << "c1:" << coordinates1 << endl << "c2:" << coordinates2 << endl << "nc:" << new_coordinates << endl; |
---|
1066 | |
---|
1067 | vertex* neutral_vertex = new vertex(new_coordinates); |
---|
1068 | |
---|
1069 | new_totally_neutral_child = neutral_vertex; |
---|
1070 | } |
---|
1071 | else |
---|
1072 | { |
---|
1073 | toprow* neutral_toprow = new toprow(); |
---|
1074 | |
---|
1075 | neutral_toprow->condition = zeros(number_of_parameters+1); |
---|
1076 | neutral_toprow->condition_order = ((toprow*)current_polyhedron)->condition_order+1; |
---|
1077 | |
---|
1078 | new_totally_neutral_child = neutral_toprow; |
---|
1079 | } |
---|
1080 | |
---|
1081 | new_totally_neutral_child->my_emlig = this; |
---|
1082 | |
---|
1083 | new_totally_neutral_child->children.insert(new_totally_neutral_child->children.end(), |
---|
1084 | current_polyhedron->totallyneutralgrandchildren.begin(), |
---|
1085 | current_polyhedron->totallyneutralgrandchildren.end()); |
---|
1086 | |
---|
1087 | for(list<polyhedron*>::iterator grand_ref = current_polyhedron->totallyneutralgrandchildren.begin(); grand_ref != current_polyhedron->totallyneutralgrandchildren.end();grand_ref++) |
---|
1088 | { |
---|
1089 | (*grand_ref)->parents.push_back(new_totally_neutral_child); |
---|
1090 | |
---|
1091 | new_totally_neutral_child->vertices.insert((*grand_ref)->vertices.begin(),(*grand_ref)->vertices.end()); |
---|
1092 | } |
---|
1093 | |
---|
1094 | // cout << ((toprow*)current_polyhedron)->condition << endl << toadd << endl; |
---|
1095 | |
---|
1096 | toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition+toadd, ((toprow*)current_polyhedron)->condition_order+1); |
---|
1097 | toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition-toadd, ((toprow*)current_polyhedron)->condition_order+1); |
---|
1098 | |
---|
1099 | positive_poly->my_emlig = this; |
---|
1100 | negative_poly->my_emlig = this; |
---|
1101 | |
---|
1102 | for(list<polyhedron*>::iterator parent_ref = current_polyhedron->parents.begin();parent_ref!=current_polyhedron->parents.end();parent_ref++) |
---|
1103 | { |
---|
1104 | (*parent_ref)->totallyneutralgrandchildren.push_back(new_totally_neutral_child); |
---|
1105 | |
---|
1106 | (*parent_ref)->neutralchildren.remove(current_polyhedron); |
---|
1107 | (*parent_ref)->children.remove(current_polyhedron); |
---|
1108 | |
---|
1109 | (*parent_ref)->children.push_back(positive_poly); |
---|
1110 | (*parent_ref)->children.push_back(negative_poly); |
---|
1111 | (*parent_ref)->positivechildren.push_back(positive_poly); |
---|
1112 | (*parent_ref)->negativechildren.push_back(negative_poly); |
---|
1113 | } |
---|
1114 | |
---|
1115 | positive_poly->parents.insert(positive_poly->parents.end(), |
---|
1116 | current_polyhedron->parents.begin(), |
---|
1117 | current_polyhedron->parents.end()); |
---|
1118 | |
---|
1119 | negative_poly->parents.insert(negative_poly->parents.end(), |
---|
1120 | current_polyhedron->parents.begin(), |
---|
1121 | current_polyhedron->parents.end()); |
---|
1122 | |
---|
1123 | positive_poly->children.push_back(new_totally_neutral_child); |
---|
1124 | negative_poly->children.push_back(new_totally_neutral_child); |
---|
1125 | |
---|
1126 | new_totally_neutral_child->parents.push_back(positive_poly); |
---|
1127 | new_totally_neutral_child->parents.push_back(negative_poly); |
---|
1128 | |
---|
1129 | for(list<polyhedron*>::iterator child_ref = current_polyhedron->positivechildren.begin();child_ref!=current_polyhedron->positivechildren.end();child_ref++) |
---|
1130 | { |
---|
1131 | (*child_ref)->parents.remove(current_polyhedron); |
---|
1132 | (*child_ref)->parents.push_back(positive_poly); |
---|
1133 | } |
---|
1134 | |
---|
1135 | positive_poly->children.insert(positive_poly->children.end(), |
---|
1136 | current_polyhedron->positivechildren.begin(), |
---|
1137 | current_polyhedron->positivechildren.end()); |
---|
1138 | |
---|
1139 | for(list<polyhedron*>::iterator child_ref = current_polyhedron->negativechildren.begin();child_ref!=current_polyhedron->negativechildren.end();child_ref++) |
---|
1140 | { |
---|
1141 | (*child_ref)->parents.remove(current_polyhedron); |
---|
1142 | (*child_ref)->parents.push_back(negative_poly); |
---|
1143 | } |
---|
1144 | |
---|
1145 | negative_poly->children.insert(negative_poly->children.end(), |
---|
1146 | current_polyhedron->negativechildren.begin(), |
---|
1147 | current_polyhedron->negativechildren.end()); |
---|
1148 | |
---|
1149 | positive_poly->vertices.insert(current_polyhedron->positiveneutralvertices.begin(),current_polyhedron->positiveneutralvertices.end()); |
---|
1150 | positive_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); |
---|
1151 | |
---|
1152 | negative_poly->vertices.insert(current_polyhedron->negativeneutralvertices.begin(),current_polyhedron->negativeneutralvertices.end()); |
---|
1153 | negative_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); |
---|
1154 | |
---|
1155 | new_totally_neutral_child->triangulate(false); |
---|
1156 | |
---|
1157 | positive_poly->triangulate(k==for_splitting.size()-1); |
---|
1158 | negative_poly->triangulate(k==for_splitting.size()-1); |
---|
1159 | |
---|
1160 | statistic.append_polyhedron(k-1, new_totally_neutral_child); |
---|
1161 | |
---|
1162 | statistic.insert_polyhedron(k, positive_poly, current_polyhedron); |
---|
1163 | statistic.insert_polyhedron(k, negative_poly, current_polyhedron); |
---|
1164 | |
---|
1165 | statistic.delete_polyhedron(k, current_polyhedron); |
---|
1166 | |
---|
1167 | delete current_polyhedron; |
---|
1168 | } |
---|
1169 | |
---|
1170 | k++; |
---|
1171 | } |
---|
1172 | } |
---|
1173 | |
---|
1174 | |
---|
1175 | vector<int> sizevector; |
---|
1176 | for(int s = 0;s<statistic.size();s++) |
---|
1177 | { |
---|
1178 | sizevector.push_back(statistic.row_size(s)); |
---|
1179 | } |
---|
1180 | |
---|
1181 | /* |
---|
1182 | for(polyhedron* topr_ref = statistic.rows[statistic.size()-1];topr_ref!=statistic.row_ends[statistic.size()-1]->next_poly;topr_ref=topr_ref->next_poly) |
---|
1183 | { |
---|
1184 | cout << ((toprow*)topr_ref)->condition << endl; |
---|
1185 | } |
---|
1186 | */ |
---|
1187 | |
---|
1188 | } |
---|
1189 | |
---|
1190 | void set_correction_factors(int order) |
---|
1191 | { |
---|
1192 | for(int remaining_order = correction_factors.size();remaining_order<order;remaining_order++) |
---|
1193 | { |
---|
1194 | multiset<my_ivec> factor_templates; |
---|
1195 | multiset<my_ivec> final_factors; |
---|
1196 | |
---|
1197 | my_ivec orig_template = my_ivec(); |
---|
1198 | |
---|
1199 | for(int i = 1;i<number_of_parameters-remaining_order+1;i++) |
---|
1200 | { |
---|
1201 | bool in_cycle = false; |
---|
1202 | for(int j = 0;j<=remaining_order;j++) { |
---|
1203 | |
---|
1204 | multiset<my_ivec>::iterator fac_ref = factor_templates.begin(); |
---|
1205 | |
---|
1206 | do |
---|
1207 | { |
---|
1208 | my_ivec current_template; |
---|
1209 | if(!in_cycle) |
---|
1210 | { |
---|
1211 | current_template = orig_template; |
---|
1212 | in_cycle = true; |
---|
1213 | } |
---|
1214 | else |
---|
1215 | { |
---|
1216 | current_template = (*fac_ref); |
---|
1217 | fac_ref++; |
---|
1218 | } |
---|
1219 | |
---|
1220 | current_template.ins(current_template.size(),i); |
---|
1221 | |
---|
1222 | // cout << "template:" << current_template << endl; |
---|
1223 | |
---|
1224 | if(current_template.size()==remaining_order+1) |
---|
1225 | { |
---|
1226 | final_factors.insert(current_template); |
---|
1227 | } |
---|
1228 | else |
---|
1229 | { |
---|
1230 | factor_templates.insert(current_template); |
---|
1231 | } |
---|
1232 | } |
---|
1233 | while(fac_ref!=factor_templates.end()); |
---|
1234 | } |
---|
1235 | } |
---|
1236 | |
---|
1237 | correction_factors.push_back(final_factors); |
---|
1238 | |
---|
1239 | } |
---|
1240 | } |
---|
1241 | |
---|
1242 | protected: |
---|
1243 | |
---|
1244 | /// A method for creating plain default statistic representing only the range of the parameter space. |
---|
1245 | void create_statistic(int number_of_parameters) |
---|
1246 | { |
---|
1247 | for(int i = 0;i<number_of_parameters;i++) |
---|
1248 | { |
---|
1249 | vec condition_vec = zeros(number_of_parameters+1); |
---|
1250 | condition_vec[i+1] = 1; |
---|
1251 | |
---|
1252 | condition* new_condition = new condition(condition_vec); |
---|
1253 | |
---|
1254 | conditions.push_back(new_condition); |
---|
1255 | } |
---|
1256 | |
---|
1257 | // An empty vector of coordinates. |
---|
1258 | vec origin_coord; |
---|
1259 | |
---|
1260 | // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. |
---|
1261 | vertex *origin = new vertex(origin_coord); |
---|
1262 | |
---|
1263 | origin->my_emlig = this; |
---|
1264 | |
---|
1265 | /* |
---|
1266 | // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse |
---|
1267 | // diagram. First we create a vector of polyhedrons.. |
---|
1268 | list<polyhedron*> origin_vec; |
---|
1269 | |
---|
1270 | // ..we fill it with the origin.. |
---|
1271 | origin_vec.push_back(origin); |
---|
1272 | |
---|
1273 | // ..and we fill the statistic with the created vector. |
---|
1274 | statistic.push_back(origin_vec); |
---|
1275 | */ |
---|
1276 | |
---|
1277 | statistic = *(new c_statistic()); |
---|
1278 | |
---|
1279 | statistic.append_polyhedron(0, origin); |
---|
1280 | |
---|
1281 | // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to |
---|
1282 | // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: |
---|
1283 | for(int i=0;i<number_of_parameters;i++) |
---|
1284 | { |
---|
1285 | // We first will create two new vertices. These will be the borders of the parameter space in the dimension |
---|
1286 | // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the |
---|
1287 | // right amount of zero cooridnates by reading them from the origin |
---|
1288 | vec origin_coord = origin->get_coordinates(); |
---|
1289 | |
---|
1290 | // And we incorporate the nonzero coordinates into the new cooordinate vectors |
---|
1291 | vec origin_coord1 = concat(origin_coord,-max_range); |
---|
1292 | vec origin_coord2 = concat(origin_coord,max_range); |
---|
1293 | |
---|
1294 | |
---|
1295 | // Now we create the points |
---|
1296 | vertex* new_point1 = new vertex(origin_coord1); |
---|
1297 | vertex* new_point2 = new vertex(origin_coord2); |
---|
1298 | |
---|
1299 | new_point1->my_emlig = this; |
---|
1300 | new_point2->my_emlig = this; |
---|
1301 | |
---|
1302 | //********************************************************************************************************* |
---|
1303 | // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old |
---|
1304 | // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points |
---|
1305 | // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons |
---|
1306 | // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is |
---|
1307 | // connected to the first (second) newly created vertex by a parent-child relation. |
---|
1308 | //********************************************************************************************************* |
---|
1309 | |
---|
1310 | |
---|
1311 | /* |
---|
1312 | // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram |
---|
1313 | vector<vector<polyhedron*>> new_statistic1; |
---|
1314 | vector<vector<polyhedron*>> new_statistic2; |
---|
1315 | */ |
---|
1316 | |
---|
1317 | c_statistic* new_statistic1 = new c_statistic(); |
---|
1318 | c_statistic* new_statistic2 = new c_statistic(); |
---|
1319 | |
---|
1320 | |
---|
1321 | // Copy the statistic by rows |
---|
1322 | for(int j=0;j<statistic.size();j++) |
---|
1323 | { |
---|
1324 | |
---|
1325 | |
---|
1326 | // an element counter |
---|
1327 | int element_number = 0; |
---|
1328 | |
---|
1329 | /* |
---|
1330 | vector<polyhedron*> supportnew_1; |
---|
1331 | vector<polyhedron*> supportnew_2; |
---|
1332 | |
---|
1333 | new_statistic1.push_back(supportnew_1); |
---|
1334 | new_statistic2.push_back(supportnew_2); |
---|
1335 | */ |
---|
1336 | |
---|
1337 | // for each polyhedron in the given row |
---|
1338 | for(polyhedron* horiz_ref = statistic.rows[j];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
---|
1339 | { |
---|
1340 | // Append an extra zero coordinate to each of the vertices for the new dimension |
---|
1341 | // If vert_ref is at the first index => we loop through vertices |
---|
1342 | if(j == 0) |
---|
1343 | { |
---|
1344 | // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates |
---|
1345 | ((vertex*) horiz_ref)->push_coordinate(0); |
---|
1346 | } |
---|
1347 | /* |
---|
1348 | else |
---|
1349 | { |
---|
1350 | ((toprow*) (*horiz_ref))->condition.ins(0,0); |
---|
1351 | }*/ |
---|
1352 | |
---|
1353 | // if it has parents |
---|
1354 | if(!horiz_ref->parents.empty()) |
---|
1355 | { |
---|
1356 | // save the relative address of this child in a vector kids_rel_addresses of all its parents. |
---|
1357 | // This information will later be used for copying the whole Hasse diagram with each of the |
---|
1358 | // relations contained within. |
---|
1359 | for(list<polyhedron*>::iterator parent_ref = horiz_ref->parents.begin();parent_ref != horiz_ref->parents.end();parent_ref++) |
---|
1360 | { |
---|
1361 | (*parent_ref)->kids_rel_addresses.push_back(element_number); |
---|
1362 | } |
---|
1363 | } |
---|
1364 | |
---|
1365 | // ************************************************************************************************** |
---|
1366 | // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron |
---|
1367 | // will be created as a toprow, but this information will be later forgotten and only the polyhedrons |
---|
1368 | // in the top row of the Hasse diagram will be considered toprow for later use. |
---|
1369 | // ************************************************************************************************** |
---|
1370 | |
---|
1371 | // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic |
---|
1372 | // this condition will be a vector of zeros. There are two vectors, because we need two copies of |
---|
1373 | // the original Hasse diagram. |
---|
1374 | vec vec1(number_of_parameters+1); |
---|
1375 | vec1.zeros(); |
---|
1376 | |
---|
1377 | vec vec2(number_of_parameters+1); |
---|
1378 | vec2.zeros(); |
---|
1379 | |
---|
1380 | // We create a new toprow with the previously specified condition. |
---|
1381 | toprow* current_copy1 = new toprow(vec1, 0); |
---|
1382 | toprow* current_copy2 = new toprow(vec2, 0); |
---|
1383 | |
---|
1384 | current_copy1->my_emlig = this; |
---|
1385 | current_copy2->my_emlig = this; |
---|
1386 | |
---|
1387 | // The vertices of the copies will be inherited, because there will be a parent/child relation |
---|
1388 | // between each polyhedron and its offspring (comming from the copy) and a parent has all the |
---|
1389 | // vertices of its child plus more. |
---|
1390 | for(set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();vertex_ref!=horiz_ref->vertices.end();vertex_ref++) |
---|
1391 | { |
---|
1392 | current_copy1->vertices.insert(*vertex_ref); |
---|
1393 | current_copy2->vertices.insert(*vertex_ref); |
---|
1394 | } |
---|
1395 | |
---|
1396 | // The only new vertex of the offspring should be the newly created point. |
---|
1397 | current_copy1->vertices.insert(new_point1); |
---|
1398 | current_copy2->vertices.insert(new_point2); |
---|
1399 | |
---|
1400 | // This method guarantees that each polyhedron is already triangulated, therefore its triangulation |
---|
1401 | // is only one set of vertices and it is the set of all its vertices. |
---|
1402 | set<vertex*> t_simplex1; |
---|
1403 | set<vertex*> t_simplex2; |
---|
1404 | |
---|
1405 | t_simplex1.insert(current_copy1->vertices.begin(),current_copy1->vertices.end()); |
---|
1406 | t_simplex2.insert(current_copy2->vertices.begin(),current_copy2->vertices.end()); |
---|
1407 | |
---|
1408 | current_copy1->triangulation.push_back(t_simplex1); |
---|
1409 | current_copy2->triangulation.push_back(t_simplex2); |
---|
1410 | |
---|
1411 | // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying |
---|
1412 | // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the |
---|
1413 | // kids and when we do that and know the child, in the child we will remember the parent we came from. |
---|
1414 | // This way all the parents/children relations are saved in both the parent and the child. |
---|
1415 | if(!horiz_ref->kids_rel_addresses.empty()) |
---|
1416 | { |
---|
1417 | for(list<int>::iterator kid_ref = horiz_ref->kids_rel_addresses.begin();kid_ref!=horiz_ref->kids_rel_addresses.end();kid_ref++) |
---|
1418 | { |
---|
1419 | polyhedron* new_kid1 = new_statistic1->rows[j-1]; |
---|
1420 | polyhedron* new_kid2 = new_statistic2->rows[j-1]; |
---|
1421 | |
---|
1422 | // THIS IS NOT EFFECTIVE: It could be improved by having the list indexed for new_statistic, but |
---|
1423 | // not indexed for statistic. Hopefully this will not cause a big slowdown - happens only offline. |
---|
1424 | if(*kid_ref) |
---|
1425 | { |
---|
1426 | for(int k = 1;k<=(*kid_ref);k++) |
---|
1427 | { |
---|
1428 | new_kid1=new_kid1->next_poly; |
---|
1429 | new_kid2=new_kid2->next_poly; |
---|
1430 | } |
---|
1431 | } |
---|
1432 | |
---|
1433 | // find the child and save the relation to the parent |
---|
1434 | current_copy1->children.push_back(new_kid1); |
---|
1435 | current_copy2->children.push_back(new_kid2); |
---|
1436 | |
---|
1437 | // in the child save the parents' address |
---|
1438 | new_kid1->parents.push_back(current_copy1); |
---|
1439 | new_kid2->parents.push_back(current_copy2); |
---|
1440 | } |
---|
1441 | |
---|
1442 | // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the |
---|
1443 | // Hasse diagram again) |
---|
1444 | horiz_ref->kids_rel_addresses.clear(); |
---|
1445 | } |
---|
1446 | // If there were no children previously, we are copying a polyhedron that has been a vertex before. |
---|
1447 | // In this case it is a segment now and it will have a relation to its mother (copywise) and to the |
---|
1448 | // newly created point. Here we create the connection to the new point, again from both sides. |
---|
1449 | else |
---|
1450 | { |
---|
1451 | // Add the address of the new point in the former vertex |
---|
1452 | current_copy1->children.push_back(new_point1); |
---|
1453 | current_copy2->children.push_back(new_point2); |
---|
1454 | |
---|
1455 | // Add the address of the former vertex in the new point |
---|
1456 | new_point1->parents.push_back(current_copy1); |
---|
1457 | new_point2->parents.push_back(current_copy2); |
---|
1458 | } |
---|
1459 | |
---|
1460 | // Save the mother in its offspring |
---|
1461 | current_copy1->children.push_back(horiz_ref); |
---|
1462 | current_copy2->children.push_back(horiz_ref); |
---|
1463 | |
---|
1464 | // Save the offspring in its mother |
---|
1465 | horiz_ref->parents.push_back(current_copy1); |
---|
1466 | horiz_ref->parents.push_back(current_copy2); |
---|
1467 | |
---|
1468 | |
---|
1469 | // Add the copies into the relevant statistic. The statistic will later be appended to the previous |
---|
1470 | // Hasse diagram |
---|
1471 | new_statistic1->append_polyhedron(j,current_copy1); |
---|
1472 | new_statistic2->append_polyhedron(j,current_copy2); |
---|
1473 | |
---|
1474 | // Raise the count in the vector of polyhedrons |
---|
1475 | element_number++; |
---|
1476 | |
---|
1477 | } |
---|
1478 | |
---|
1479 | } |
---|
1480 | |
---|
1481 | /* |
---|
1482 | statistic.begin()->push_back(new_point1); |
---|
1483 | statistic.begin()->push_back(new_point2); |
---|
1484 | */ |
---|
1485 | |
---|
1486 | statistic.append_polyhedron(0, new_point1); |
---|
1487 | statistic.append_polyhedron(0, new_point2); |
---|
1488 | |
---|
1489 | // Merge the new statistics into the old one. This will either be the final statistic or we will |
---|
1490 | // reenter the widening loop. |
---|
1491 | for(int j=0;j<new_statistic1->size();j++) |
---|
1492 | { |
---|
1493 | /* |
---|
1494 | if(j+1==statistic.size()) |
---|
1495 | { |
---|
1496 | list<polyhedron*> support; |
---|
1497 | statistic.push_back(support); |
---|
1498 | } |
---|
1499 | |
---|
1500 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end()); |
---|
1501 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end()); |
---|
1502 | */ |
---|
1503 | statistic.append_polyhedron(j+1,new_statistic1->rows[j],new_statistic1->row_ends[j]); |
---|
1504 | statistic.append_polyhedron(j+1,new_statistic2->rows[j],new_statistic2->row_ends[j]); |
---|
1505 | } |
---|
1506 | } |
---|
1507 | |
---|
1508 | /* |
---|
1509 | vector<list<toprow*>> toprow_statistic; |
---|
1510 | int line_count = 0; |
---|
1511 | |
---|
1512 | for(vector<list<polyhedron*>>::iterator polyhedron_ref = ++statistic.begin(); polyhedron_ref!=statistic.end();polyhedron_ref++) |
---|
1513 | { |
---|
1514 | list<toprow*> support_list; |
---|
1515 | toprow_statistic.push_back(support_list); |
---|
1516 | |
---|
1517 | for(list<polyhedron*>::iterator polyhedron_ref2 = polyhedron_ref->begin(); polyhedron_ref2 != polyhedron_ref->end(); polyhedron_ref2++) |
---|
1518 | { |
---|
1519 | toprow* support_top = (toprow*)(*polyhedron_ref2); |
---|
1520 | |
---|
1521 | toprow_statistic[line_count].push_back(support_top); |
---|
1522 | } |
---|
1523 | |
---|
1524 | line_count++; |
---|
1525 | }*/ |
---|
1526 | |
---|
1527 | /* |
---|
1528 | vector<int> sizevector; |
---|
1529 | for(int s = 0;s<statistic.size();s++) |
---|
1530 | { |
---|
1531 | sizevector.push_back(statistic.row_size(s)); |
---|
1532 | } |
---|
1533 | */ |
---|
1534 | |
---|
1535 | } |
---|
1536 | |
---|
1537 | |
---|
1538 | |
---|
1539 | |
---|
1540 | }; |
---|
1541 | |
---|
1542 | /* |
---|
1543 | |
---|
1544 | //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density |
---|
1545 | class RARX : public BM |
---|
1546 | { |
---|
1547 | private: |
---|
1548 | |
---|
1549 | emlig posterior; |
---|
1550 | |
---|
1551 | public: |
---|
1552 | RARX():BM() |
---|
1553 | { |
---|
1554 | }; |
---|
1555 | |
---|
1556 | void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec) |
---|
1557 | { |
---|
1558 | |
---|
1559 | } |
---|
1560 | |
---|
1561 | };*/ |
---|
1562 | |
---|
1563 | |
---|
1564 | |
---|
1565 | #endif //TRAGE_H |
---|