root/applications/robust/robustlib.h @ 1212

Revision 1212, 24.8 kB (checked in by sindj, 14 years ago)

Pokracovani split - nedokonceno. JS

Line 
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 <limits>
12#include <vector>
13#include <algorithm>
14       
15using namespace bdm;
16using namespace std;
17using namespace itpp;
18
19const double max_range = numeric_limits<double>::max()/10e-5;
20
21enum actions {MERGE, SPLIT};
22
23class polyhedron;
24class vertex;
25
26/// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram
27/// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created.
28class polyhedron
29{
30        /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of
31        /// more than just the necessary number of conditions. For example if a newly created line passes through an already
32        /// existing point, the points multiplicity will rise by 1.
33        int multiplicity;       
34
35        int split_state;
36
37        int merge_state;
38
39       
40
41public:
42        /// A list of polyhedrons parents within the Hasse diagram.
43        vector<polyhedron*> parents;
44
45        /// A list of polyhedrons children withing the Hasse diagram.
46        vector<polyhedron*> children;
47
48        /// All the vertices of the given polyhedron
49        vector<vertex*> vertices;
50
51        /// A list used for storing children that lie in the positive region related to a certain condition
52        vector<polyhedron*> positivechildren;
53
54        /// A list used for storing children that lie in the negative region related to a certain condition
55        vector<polyhedron*> negativechildren;
56
57        /// Children intersecting the condition
58        vector<polyhedron*> neutralchildren;
59
60        vector<polyhedron*> totallyneutralgrandchildren;
61
62        vector<polyhedron*> totallyneutralchildren;
63
64        bool totally_neutral;
65
66        vector<polyhedron*> mergechildren;
67
68        polyhedron* positiveparent;
69
70        polyhedron* negativeparent;
71
72        int message_counter;
73
74        /// List of triangulation polyhedrons of the polyhedron given by their relative vertices.
75        vector<vector<vertex*>> triangulations;
76
77        /// A list of relative addresses serving for Hasse diagram construction.
78        vector<int> kids_rel_addresses;
79
80        /// Default constructor
81        polyhedron()
82        {
83                multiplicity = 1;
84
85                message_counter = 0;
86
87                totally_neutral = NULL;
88        }
89       
90        /// Setter for raising multiplicity
91        void raise_multiplicity()
92        {
93                multiplicity++;
94        }
95
96        /// Setter for lowering multiplicity
97        void lower_multiplicity()
98        {
99                multiplicity--;
100        }
101       
102        /// An obligatory operator, when the class is used within a C++ STL structure like a vector
103        int operator==(polyhedron polyhedron2)
104        {
105                return true;
106        }
107
108        /// An obligatory operator, when the class is used within a C++ STL structure like a vector
109        int operator<(polyhedron polyhedron2)
110        {
111                return false;
112        }
113
114       
115
116        void set_state(double state_indicator, actions action)
117        {
118                switch(action)
119                {
120                        case MERGE:
121                                merge_state = (int)sign(state_indicator);                       
122                        break;
123                        case SPLIT:
124                                split_state = (int)sign(state_indicator);
125                        break;
126                }
127        }
128
129        int get_state(actions action)
130        {
131                switch(action)
132                {
133                        case MERGE:
134                                return merge_state;                     
135                        break;
136                        case SPLIT:
137                                return split_state;
138                        break;
139                }
140        }
141
142        int number_of_children()
143        {
144                return children.size();
145        }
146
147       
148};
149
150/// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse
151/// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space.
152class vertex : public polyhedron
153{
154        /// A dynamic array representing coordinates of the vertex
155        vec coordinates;       
156
157       
158
159public:
160
161
162
163        /// Default constructor
164        vertex();
165
166        /// Constructor of a vertex from a set of coordinates
167        vertex(vec coordinates)
168        {
169                this->coordinates = coordinates;
170        }
171
172        /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter
173        /// space of certain dimension is established, but the dimension is not known when the vertex is created.
174        void push_coordinate(double coordinate)
175        {
176                coordinates = concat(coordinates,coordinate);
177        }
178
179        /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer
180        /// (not given by reference), but a new copy is created (they are given by value).
181        vec get_coordinates()
182        {               
183                return coordinates;
184        }
185
186               
187};
188
189/// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates
190/// it from polyhedrons in other rows.
191class toprow : public polyhedron
192{
193       
194public:
195        /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation
196        vec condition;
197
198        /// Default constructor
199        toprow();
200
201        /// Constructor creating a toprow from the condition
202        toprow(vec condition)
203        {
204                this->condition = condition;
205        }
206
207};
208
209class condition
210{       
211public:
212        vec value;     
213
214        int multiplicity;
215
216        condition(vec value)
217        {
218                this->value = value;
219                multiplicity = 1;
220        }
221};
222
223
224//! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density
225class emlig // : eEF
226{
227
228        /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result
229        /// of data update from Bayesian estimation or set by the user if this emlig is a prior density
230        vector<vector<polyhedron*>> statistic;
231
232        vector<vector<polyhedron*>> for_splitting;
233               
234        vector<vector<polyhedron*>> for_merging;
235
236        vector<condition*> conditions;
237
238        double normalization_factor;
239
240        void alter_toprow_conditions(vec condition, bool should_be_added)
241        {
242                for(vector<polyhedron*>::iterator horiz_ref = statistic[statistic.size()-1].begin();horiz_ref<statistic[statistic.size()-1].end();horiz_ref++)
243                {
244                        double product = 0;
245
246                        vector<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin();
247
248                        do
249                        {
250                                product = (*vertex_ref)->get_coordinates()*condition;
251                        }
252                        while(product == 0);
253
254                        if((product>0 && should_be_added)||(product<0 && !should_be_added))
255                        {
256                                ((toprow*) (*horiz_ref))->condition += condition;
257                        }
258                        else
259                        {
260                                ((toprow*) (*horiz_ref))->condition -= condition;
261                        }                                                       
262                }
263        }
264
265
266        void send_state_message(polyhedron* sender, vec toadd, vec toremove, int level)
267        {                       
268
269                bool should_remove = (toremove.size() != 0);
270                bool should_add    = (toadd.size() != 0);
271               
272                if(shouldsplit||shouldmerge)
273                {
274                        for(vector<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator<sender->parents.end();parent_iterator++)
275                        {
276                                polyhedron* current_parent = *parent_iterator;
277
278                                current_parent->message_counter++;
279
280                                bool is_last = (current_parent->message_counter == current_parent->number_of_children());
281
282                                if(shouldmerge)
283                                {
284                                        int child_state  = sender->get_state(MERGE);
285                                        int parent_state = current_parent->get_state(MERGE);
286
287                                        if(parent_state == 0)
288                                        {
289                                                current_parent->set_state(child_state, MERGE);
290
291                                                if(child_state == 0)
292                                                {
293                                                        current_parent->mergechildren.push_back(sender);
294                                                }
295                                        }
296                                        else
297                                        {
298                                                if(child_state == 0)
299                                                {
300                                                        if(parent_state > 0)
301                                                        {
302                                                                sender->positiveparent = current_parent;
303                                                        }
304                                                        else
305                                                        {
306                                                                sender->negativeparent = current_parent;
307                                                        }
308                                                }
309                                        }
310
311                                        if(is_last)
312                                        {
313                                                if(parent_state > 0)
314                                                {
315                                                        for(vector<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child < current_parent->mergechildren.end();merge_child++)
316                                                        {
317                                                                (*merge_child)->positiveparent = current_parent;
318                                                        }
319                                                }
320
321                                                if(parent_state < 0)
322                                                {
323                                                        for(vector<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child < current_parent->mergechildren.end();merge_child++)
324                                                        {
325                                                                (*merge_child)->negativeparent = current_parent;
326                                                        }
327                                                }
328
329                                                if(parent_state == 0)
330                                                {
331                                                        for_merging[level+1].push_back(current_parent);
332                                                }
333
334                                                current_parent->mergechildren.clear();
335                                        }
336
337                                       
338                                }
339
340                                if(shouldsplit)
341                                        {
342                                                current_parent->totallyneutralgrandchildren.insert(current_parent->totallyneutralgrandchildren.end(),sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end());
343
344                                                switch(sender->get_state(SPLIT))
345                                                {
346                                                case 1:
347                                                        current_parent->positivechildren.push_back(sender);     
348                                                break;
349                                                case 0:
350                                                        current_parent->neutralchildren.push_back(sender);
351
352                                                        if(current_parent->totally_neutral == NULL)
353                                                        {
354                                                                current_parent->totally_neutral = sender->totally_neutral;
355                                                        }
356                                                        else
357                                                        {
358                                                                current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral;
359                                                        }
360
361                                                        if(sender->totally_neutral)
362                                                        {
363                                                                current_parent->totallyneutralchildren.push_back(sender);
364                                                        }
365                                                       
366                                                break;
367                                                case -1:
368                                                        current_parent->negativechildren.push_back(sender);
369                                                break;
370                                                }
371
372                                                if(is_last)
373                                                {
374                                                        unique(current_parent->totallyneutralgrandchildren.begin(),current_parent->totallyneutralgrandchildren.end());
375
376                                                        if((current_parent->negativechildren.size()>0&&current_parent->positivechildren.size()>0)||
377                                                                                                                (current_parent->neutralchildren.size()>0&&current_parent->totally_neutral==false))
378                                                        {                                                               
379                                                               
380                                                                        for_splitting[level+1].push_back(current_parent);
381                                                               
382                                                                        current_parent->set_state(0, SPLIT);
383                                                        }
384                                                        else
385                                                        {
386                                                                if(current_parent->negativechildren.size()>0)
387                                                                {
388                                                                        current_parent->set_state(-1, SPLIT);
389
390                                                                        ((toprow*)current_parent)->condition-=toadd;
391                                                                }
392                                                                else if(current_parent->positivechildren.size()>0)
393                                                                {
394                                                                        current_parent->set_state(1, SPLIT);
395
396                                                                        ((toprow*)current_parent)->condition+=toadd;
397                                                                }
398                                                                else
399                                                                {
400                                                                        current_parent->raise_multiplicity();                                                           
401                                                                }
402
403                                                                current_parent->positivechildren.clear();
404                                                                current_parent->negativechildren.clear();
405                                                                current_parent->neutralchildren.clear();
406                                                                current_parent->totallyneutralchildren.clear();
407                                                                current_parent->totallyneutralgrandchildren.clear();
408                                                                current_parent->totally_neutral = NULL; 
409                                                        }
410                                                }
411                                        }
412
413                                        if(is_last)
414                                        {
415                                                send_state_message(current_parent,shouldsplit,shouldmerge,level+1);
416                                        }
417                       
418                        }
419                       
420                }               
421        }
422       
423public: 
424
425        /// A default constructor creates an emlig with predefined statistic representing only the range of the given
426        /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor.
427        emlig(int number_of_parameters)
428        {
429                create_statistic(number_of_parameters);
430
431                for(vector<vector<polyhedron*>::iterator local_iter = statistic.begin();local_iter<statistic.end();local_iter++)
432                {
433                        vector<polyhedron*> empty_split;
434                        vector<polyhedron*> empty_merge;
435
436                        for_splitting.push_back(empty_split);
437                        for_merging.push_back(empty_merge);
438                }
439        }
440
441        /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a
442        /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters.
443        emlig(vector<vector<polyhedron*>> statistic)
444        {
445                this->statistic = statistic;
446        }
447
448        void add_condition(vec toadd)
449        {
450                vec null_vector = "";
451
452                add_and_remove_condition(toadd, null_vector);
453        }
454
455        void remove_condition(vec toremove)
456        {
457                vec null_vector = "";
458
459                add_and_remove_condition(null_vector, toremove);
460       
461        }
462
463        void add_and_remove_condition(vec toadd, vec toremove)
464        {
465                bool should_remove = (toremove.size() != 0);
466                bool should_add    = (toadd.size() != 0);
467
468                vector<condition*>::iterator toremove_ref = conditions.end();
469                bool condition_should_be_added = false;
470
471                for(vector<condition*>::iterator ref = conditions.begin();ref<conditions.end();ref++)
472                {
473                        if(should_remove)
474                        {
475                                if((*ref)->value == toremove)
476                                {
477                                        if((*ref)->multiplicity>1)
478                                        {
479                                                (*ref)->multiplicity--;
480
481                                                alter_toprow_conditions(toremove,false);
482
483                                                should_remove = false;
484                                        }
485                                        else
486                                        {
487                                                toremove_ref = ref;                                                     
488                                        }
489                                }
490                        }
491
492                        if(should_add)
493                        {
494                                if((*ref)->value == toadd)
495                                {
496                                        (*ref)->multiplicity++;
497
498                                        alter_toprow_conditions(toadd,true);
499
500                                        should_add = false;
501                                }
502                                else
503                                {
504                                        condition_should_be_added = true;
505                                }
506                        }
507                }
508
509                if(toremove_ref!=conditions.end())
510                {
511                        conditions.erase(toremove_ref);
512                }
513
514                if(condition_should_be_added)
515                {
516                        conditions.push_back(new condition(toadd));
517                }
518
519               
520
521                for(vector<polyhedron*>::iterator horizontal_position = statistic[0].begin();horizontal_position<statistic[0].end();horizontal_position++)
522                {               
523                        vertex* current_vertex = (vertex*)(*horizontal_position);
524                       
525                        if(should_add||should_remove)
526                        {
527                                vec appended_vec = current_vertex->get_coordinates();
528                                appended_vec.ins(0,-1.0);
529
530                                if(should_add)
531                                {
532                                        double local_condition = toadd*appended_vec;
533
534                                        current_vertex->set_state(local_condition,SPLIT);
535
536                                        if(local_condition == 0)
537                                        {
538                                                current_vertex->totally_neutral = true;
539
540                                                current_vertex->multiplicity++;
541                                        }                                       
542                                }
543                       
544                                if(should_remove)
545                                {
546                                        double local_condition = toremove*appended_vec;
547
548                                        current_vertex->set_state(local_condition,MERGE);
549
550                                        if(local_condition == 0)
551                                        {
552                                                for_merging[0].push_back(current_vertex);
553                                        }
554                                }                               
555                        }
556
557                        send_state_message(current_vertex, toadd, toremove, 0);                 
558                }
559
560                if(should_add)
561                {
562                        for(vector<vector<polyhedron*>>::iterator vert_ref = for_splitting.begin();vert_ref<for_splitting.end();vert_ref++)
563                        {
564                                int original_size = (*vert_ref).size();
565
566                                for(int split_counter = 0;split_counter<original_size;split_counter++)
567                                {
568                                        polyhedron* current_polyhedron = (*vert_ref)[original_size-1-split_counter];
569
570                                        polyhedron* new_totally_neutral_child;
571                                       
572                                        if(vert_ref == for_splitting.begin())
573                                        {
574                                                vec coordinates1 = ((vertex*)current_polyhedron->children[0])->get_coordinates();                                               
575                                                vec coordinates2 = ((vertex*)current_polyhedron->children[1])->get_coordinates();
576                                                coordinates2.ins(0,-1.0);
577                                               
578                                                double t = (-toadd*coordinates2)/(toadd(1,toadd.size()-1)*coordinates1)+1;
579
580                                                vec new_coordinates = coordinates1*t+(coordinates2(1,coordinates2.size()-1)-coordinates1);                                     
581
582                                                vertex* neutral_vertex = new vertex(new_coordinates);
583
584                                                new_totally_neutral_child = neutral_vertex;
585                                        }
586                                        else
587                                        {
588                                                toprow* neutral_toprow = new toprow();
589
590                                                new_totally_neutral_child = neutral_toprow;
591                                        }
592                                       
593                                        new_totally_neutral_child->children.insert(new_totally_neutral_child->children.end(),
594                                                                                                                current_polyhedron->totallyneutralgrandchildren.begin(),
595                                                                                                                                current_polyhedron->totallyneutralgrandchildren.end());
596
597                                        toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition+toadd);
598                                        toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition-toadd);
599
600                                        positive_poly->children.push_back(new_totally_neutral_child);
601                                        negative_poly->children.push_back(new_totally_neutral_child);
602
603                                        positive_poly->parents.insert(positive_poly->parents.end(),
604                                                                                                current_polyhedron->parents.begin(),
605                                                                                                                current_polyhedron->parents.end());
606
607                                        negative_poly->parents.insert(negative_poly->parents.end(),
608                                                                                                current_polyhedron->parents.begin(),
609                                                                                                                current_polyhedron->parents.end());
610
611                                        positive_poly->children.insert(positive_poly->children.end(),
612                                                                                                current_polyhedron->positivechildren.begin(),
613                                                                                                                        current_polyhedron->positivechildren.end());
614
615                                        negative_poly->children.insert(negative_poly->children.end(),
616                                                                                                current_polyhedron->negativechildren.begin(),
617                                                                                                                        current_polyhedron->negativechildren.end());
618
619                                       
620                                       
621
622
623                                       
624                                }
625                        }
626                }
627
628
629        }
630
631protected:
632
633        /// A method for creating plain default statistic representing only the range of the parameter space.
634    void create_statistic(int number_of_parameters)
635        {
636                // An empty vector of coordinates.
637                vec origin_coord;       
638
639                // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords.
640                vertex *origin = new vertex(origin_coord);
641
642                // It has itself as a vertex. There will be a nice use for this when the vertices of its parents are searched in
643                // the recursive creation procedure below.
644                origin->vertices.push_back(origin);
645
646                // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse
647                // diagram. First we create a vector of polyhedrons..
648                vector<polyhedron*> origin_vec;
649
650                // ..we fill it with the origin..
651                origin_vec.push_back(origin);
652
653                // ..and we fill the statistic with the created vector.
654                statistic.push_back(origin_vec);
655
656                // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to
657                // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows:
658                for(int i=0;i<number_of_parameters;i++)
659                {
660                        // We first will create two new vertices. These will be the borders of the parameter space in the dimension
661                        // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the
662                        // right amount of zero cooridnates by reading them from the origin
663                        vec origin_coord = origin->get_coordinates();                                           
664
665                        // And we incorporate the nonzero coordinates into the new cooordinate vectors
666                        vec origin_coord1 = concat(origin_coord,max_range);
667                        vec origin_coord2 = concat(origin_coord,-max_range);
668
669                        // Now we create the points
670                        vertex *new_point1 = new vertex(origin_coord1);
671                        vertex *new_point2 = new vertex(origin_coord2);
672                       
673                        //*********************************************************************************************************
674                        // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old
675                        // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points
676                        // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons
677                        // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is
678                        // connected to the first (second) newly created vertex by a parent-child relation.
679                        //*********************************************************************************************************
680
681
682                        // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram
683                        vector<vector<polyhedron*>> new_statistic1;
684                        vector<vector<polyhedron*>> new_statistic2;
685
686                        // Copy the statistic by rows
687                        for(int j=0;j<statistic.size();j++)
688                        {
689                                // an element counter
690                                int element_number = 0;
691
692                                vector<polyhedron*> supportnew_1;
693                                vector<polyhedron*> supportnew_2;
694
695                                new_statistic1.push_back(supportnew_1);
696                                new_statistic2.push_back(supportnew_2);
697
698                                // for each polyhedron in the given row
699                                for(vector<polyhedron*>::iterator horiz_ref = statistic[j].begin();horiz_ref<statistic[j].end();horiz_ref++)
700                                {       
701                                        // Append an extra zero coordinate to each of the vertices for the new dimension
702                                        // If j==0 => we loop through vertices
703                                        if(j == 0)
704                                        {
705                                                // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates
706                                                ((vertex*) (*horiz_ref))->push_coordinate(0);
707                                        }
708
709                                        // if it has parents
710                                        if(!(*horiz_ref)->parents.empty())
711                                        {
712                                                // save the relative address of this child in a vector kids_rel_addresses of all its parents.
713                                                // This information will later be used for copying the whole Hasse diagram with each of the
714                                                // relations contained within.
715                                                for(vector<polyhedron*>::iterator parent_ref = (*horiz_ref)->parents.begin();parent_ref < (*horiz_ref)->parents.end();parent_ref++)
716                                                {
717                                                        (*parent_ref)->kids_rel_addresses.push_back(element_number);                                                   
718                                                }                                               
719                                        }
720
721                                        // **************************************************************************************************
722                                        // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron
723                                        // will be created as a toprow, but this information will be later forgotten and only the polyhedrons
724                                        // in the top row of the Hasse diagram will be considered toprow for later use.
725                                        // **************************************************************************************************
726
727                                        // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic
728                                        // this condition will be a vector of zeros. There are two vectors, because we need two copies of
729                                        // the original Hasse diagram.
730                                        vec vec1(i+2);
731                                        vec1.zeros();
732
733                                        vec vec2(i+2);
734                                        vec2.zeros();
735
736                                        // We create a new toprow with the previously specified condition.
737                                        toprow *current_copy1 = new toprow(vec1);
738                                        toprow *current_copy2 = new toprow(vec2);                                       
739
740                                        // The vertices of the copies will be inherited, because there will be a parent/child relation
741                                        // between each polyhedron and its offspring (comming from the copy) and a parent has all the
742                                        // vertices of its child plus more.
743                                        for(vector<vertex*>::iterator vert_ref = (*horiz_ref)->vertices.begin();vert_ref<(*horiz_ref)->vertices.end();vert_ref++)
744                                        {
745                                                current_copy1->vertices.push_back(*vert_ref);
746                                                current_copy2->vertices.push_back(*vert_ref);                                           
747                                        }
748                                       
749                                        // The only new vertex of the offspring should be the newly created point.
750                                        current_copy1->vertices.push_back(new_point1);
751                                        current_copy2->vertices.push_back(new_point2);
752                                       
753                                        // This method guarantees that each polyhedron is already triangulated, therefore its triangulation
754                                        // is only one set of vertices and it is the set of all its vertices.
755                                        current_copy1->triangulations.push_back(current_copy1->vertices);
756                                        current_copy2->triangulations.push_back(current_copy2->vertices);
757                                       
758                                        // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying
759                                        // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the
760                                        // kids and when we do that and know the child, in the child we will remember the parent we came from.
761                                        // This way all the parents/children relations are saved in both the parent and the child.
762                                        if(!(*horiz_ref)->kids_rel_addresses.empty())
763                                        {
764                                                for(vector<int>::iterator kid_ref = (*horiz_ref)->kids_rel_addresses.begin();kid_ref<(*horiz_ref)->kids_rel_addresses.end();kid_ref++)
765                                                {       
766                                                        // find the child and save the relation to the parent
767                                                        current_copy1->children.push_back(new_statistic1[j-1][(*kid_ref)]);
768                                                        current_copy2->children.push_back(new_statistic2[j-1][(*kid_ref)]);
769
770                                                        // in the child save the parents' address
771                                                        new_statistic1[j-1][(*kid_ref)]->parents.push_back(current_copy1);
772                                                        new_statistic2[j-1][(*kid_ref)]->parents.push_back(current_copy2);
773                                                }                                               
774
775                                                // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the
776                                                // Hasse diagram again)
777                                                (*horiz_ref)->kids_rel_addresses.clear();
778                                        }
779                                        // If there were no children previously, we are copying a polyhedron that has been a vertex before.
780                                        // In this case it is a segment now and it will have a relation to its mother (copywise) and to the
781                                        // newly created point. Here we create the connection to the new point, again from both sides.
782                                        else
783                                        {
784                                                // Add the address of the new point in the former vertex
785                                                current_copy1->children.push_back(new_point1);
786                                                current_copy2->children.push_back(new_point2);
787
788                                                // Add the address of the former vertex in the new point
789                                                new_point1->parents.push_back(current_copy1);
790                                                new_point2->parents.push_back(current_copy2);
791                                        }
792
793                                        // Save the mother in its offspring
794                                        current_copy1->children.push_back(*horiz_ref);
795                                        current_copy2->children.push_back(*horiz_ref);
796
797                                        // Save the offspring in its mother
798                                        (*horiz_ref)->parents.push_back(current_copy1);
799                                        (*horiz_ref)->parents.push_back(current_copy2); 
800                                                               
801                                       
802                                        // Add the copies into the relevant statistic. The statistic will later be appended to the previous
803                                        // Hasse diagram
804                                        new_statistic1[j].push_back(current_copy1);
805                                        new_statistic2[j].push_back(current_copy2);
806                                       
807                                        // Raise the count in the vector of polyhedrons
808                                        element_number++;
809                                       
810                                }                               
811                        }
812
813                        statistic[0].push_back(new_point1);
814                        statistic[0].push_back(new_point2);
815
816                        // Merge the new statistics into the old one. This will either be the final statistic or we will
817                        // reenter the widening loop.
818                        for(int j=0;j<new_statistic1.size();j++)
819                        {
820                                if(j+1==statistic.size())
821                                {
822                                        vector<polyhedron*> support;
823                                        statistic.push_back(support);
824                                }
825                               
826                                statistic[j+1].insert(statistic[j+1].end(),new_statistic1[j].begin(),new_statistic1[j].end());
827                                statistic[j+1].insert(statistic[j+1].end(),new_statistic2[j].begin(),new_statistic2[j].end());
828                        }
829                }
830        }
831
832
833       
834       
835};
836
837/*
838
839//! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density
840class RARX : public BM
841{
842private:
843
844        emlig posterior;
845
846public:
847        RARX():BM()
848        {
849        };
850
851        void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec)
852        {
853               
854        }
855
856};*/
857
858
859
860#endif //TRAGE_H
Note: See TracBrowser for help on using the browser.