root/applications/robust/robustlib.h @ 1219

Revision 1219, 31.9 kB (checked in by sindj, 14 years ago)

Dodelani deleni - uz funguje. Je treba jeste dodelat predavani vrcholu a triangulaci a vypocet integralu v jednotlivych polyhedronech. Pak uz slucovani. 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 <list>
14#include <algorithm>
15       
16using namespace bdm;
17using namespace std;
18using namespace itpp;
19
20const double max_range = 99999999999999999999999.0;//numeric_limits<double>::max()/10e-10;
21
22enum actions {MERGE, SPLIT};
23
24class polyhedron;
25class vertex;
26
27/// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram
28/// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created.
29class polyhedron
30{
31        /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of
32        /// more than just the necessary number of conditions. For example if a newly created line passes through an already
33        /// existing point, the points multiplicity will rise by 1.
34        int multiplicity;       
35
36        int split_state;
37
38        int merge_state;
39
40       
41
42public:
43        /// A list of polyhedrons parents within the Hasse diagram.
44        list<polyhedron*> parents;
45
46        /// A list of polyhedrons children withing the Hasse diagram.
47        list<polyhedron*> children;
48
49        /// All the vertices of the given polyhedron
50        list<vertex*> vertices;
51
52        /// A list used for storing children that lie in the positive region related to a certain condition
53        list<polyhedron*> positivechildren;
54
55        /// A list used for storing children that lie in the negative region related to a certain condition
56        list<polyhedron*> negativechildren;
57
58        /// Children intersecting the condition
59        list<polyhedron*> neutralchildren;
60
61        list<polyhedron*> totallyneutralgrandchildren;
62
63        list<polyhedron*> totallyneutralchildren;
64
65        bool totally_neutral;
66
67        list<polyhedron*> mergechildren;
68
69        polyhedron* positiveparent;
70
71        polyhedron* negativeparent;
72
73        polyhedron* next_poly;
74
75        polyhedron* prev_poly;
76
77        int message_counter;
78
79        /// List of triangulation polyhedrons of the polyhedron given by their relative vertices.
80        list<list<vertex*>> triangulations;
81
82        /// A list of relative addresses serving for Hasse diagram construction.
83        list<int> kids_rel_addresses;
84
85        /// Default constructor
86        polyhedron()
87        {
88                multiplicity = 1;
89
90                message_counter = 0;
91
92                totally_neutral = NULL;
93        }
94       
95        /// Setter for raising multiplicity
96        void raise_multiplicity()
97        {
98                multiplicity++;
99        }
100
101        /// Setter for lowering multiplicity
102        void lower_multiplicity()
103        {
104                multiplicity--;
105        }
106       
107        /// An obligatory operator, when the class is used within a C++ STL structure like a vector
108        int operator==(polyhedron polyhedron2)
109        {
110                return true;
111        }
112
113        /// An obligatory operator, when the class is used within a C++ STL structure like a vector
114        int operator<(polyhedron polyhedron2)
115        {
116                return false;
117        }
118
119       
120
121        void set_state(double state_indicator, actions action)
122        {
123                switch(action)
124                {
125                        case MERGE:
126                                merge_state = (int)sign(state_indicator);                       
127                        break;
128                        case SPLIT:
129                                split_state = (int)sign(state_indicator);
130                        break;
131                }
132        }
133
134        int get_state(actions action)
135        {
136                switch(action)
137                {
138                        case MERGE:
139                                return merge_state;                     
140                        break;
141                        case SPLIT:
142                                return split_state;
143                        break;
144                }
145        }
146
147        int number_of_children()
148        {
149                return children.size();
150        }
151
152       
153};
154
155/// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse
156/// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space.
157class vertex : public polyhedron
158{
159        /// A dynamic array representing coordinates of the vertex
160        vec coordinates;       
161
162       
163
164public:
165
166
167
168        /// Default constructor
169        vertex();
170
171        /// Constructor of a vertex from a set of coordinates
172        vertex(vec coordinates)
173        {
174                this->coordinates = coordinates;
175        }
176
177        /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter
178        /// space of certain dimension is established, but the dimension is not known when the vertex is created.
179        void push_coordinate(double coordinate)
180        {
181                coordinates = concat(coordinates,coordinate);
182        }
183
184        /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer
185        /// (not given by reference), but a new copy is created (they are given by value).
186        vec get_coordinates()
187        {               
188                return coordinates;
189        }
190
191               
192};
193
194/// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates
195/// it from polyhedrons in other rows.
196class toprow : public polyhedron
197{
198       
199public:
200        /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation
201        vec condition;
202
203        /// Default constructor
204        toprow(){};
205
206        /// Constructor creating a toprow from the condition
207        toprow(vec condition)
208        {
209                this->condition = condition;
210        }
211
212};
213
214class condition
215{       
216public:
217        vec value;     
218
219        int multiplicity;
220
221        condition(vec value)
222        {
223                this->value = value;
224                multiplicity = 1;
225        }
226};
227
228class c_statistic
229{
230        polyhedron* end_poly;
231        polyhedron* start_poly;
232
233public:
234        vector<polyhedron*> rows;
235
236        vector<polyhedron*> row_ends;
237
238        c_statistic()
239        {
240                end_poly   = new polyhedron();
241                start_poly = new polyhedron();
242        };
243
244        void append_polyhedron(int row, polyhedron* appended_start, polyhedron* appended_end)
245        {
246                if(row>((int)rows.size())-1)
247                {
248                        if(row>rows.size())
249                        {
250                                throw new exception("You are trying to append a polyhedron whose children are not in the statistic yet!");
251                                return;
252                        }
253
254                        rows.push_back(end_poly);
255                        row_ends.push_back(end_poly);
256                }
257
258                // POSSIBLE FAILURE: the function is not checking if start and end are connected
259
260                if(rows[row] != end_poly)
261                {
262                        appended_start->prev_poly = row_ends[row];
263                        row_ends[row]->next_poly = appended_start;                     
264                                               
265                }
266                else if((row>0 && rows[row-1]!=end_poly)||row==0)
267                {
268                        appended_start->prev_poly = start_poly;
269                        rows[row]= appended_start;                     
270                }
271                else
272                {
273                        throw new exception("Wrong polyhedron insertion into statistic: missing intermediary polyhedron!");
274                }
275
276                appended_end->next_poly = end_poly;
277                row_ends[row] = appended_end;
278        }
279
280        void append_polyhedron(int row, polyhedron* appended_poly)
281        {
282                append_polyhedron(row,appended_poly,appended_poly);
283        }
284
285        void insert_polyhedron(int row, polyhedron* inserted_poly, polyhedron* following_poly)
286        {               
287                if(following_poly != end_poly)
288                {
289                        inserted_poly->next_poly = following_poly;
290                        inserted_poly->prev_poly = following_poly->prev_poly;
291
292                        if(following_poly->prev_poly == start_poly)
293                        {
294                                rows[row] = inserted_poly;
295                        }
296                        else
297                        {                               
298                                inserted_poly->prev_poly->next_poly = inserted_poly;                                                           
299                        }
300
301                        following_poly->prev_poly = inserted_poly;
302                }
303                else
304                {
305                        this->append_polyhedron(row, inserted_poly);
306                }               
307       
308        }
309
310
311       
312
313        void delete_polyhedron(int row, polyhedron* deleted_poly)
314        {
315                if(deleted_poly->prev_poly != start_poly)
316                {
317                        deleted_poly->prev_poly->next_poly = deleted_poly->next_poly;
318                }
319                else
320                {
321                        rows[row] = deleted_poly->next_poly;
322                }
323
324                if(deleted_poly->next_poly!=end_poly)
325                {
326                        deleted_poly->next_poly->prev_poly = deleted_poly->prev_poly;
327                }
328                else
329                {
330                        row_ends[row] = deleted_poly->prev_poly;
331                }
332
333               
334
335                deleted_poly->next_poly = NULL;
336                deleted_poly->prev_poly = NULL;                                 
337        }
338
339        int size()
340        {
341                return rows.size();
342        }
343
344        polyhedron* get_end()
345        {
346                return end_poly;
347        }
348
349        polyhedron* get_start()
350        {
351                return start_poly;
352        }
353
354        int row_size(int row)
355        {
356                if(this->size()>row && row>=0)
357                {
358                        int row_size = 0;
359                       
360                        for(polyhedron* row_poly = rows[row]; row_poly!=end_poly; row_poly=row_poly->next_poly)
361                        {
362                                row_size++;
363                        }
364
365                        return row_size;
366                }
367                else
368                {
369                        throw new exception("There is no row to obtain size from!");
370                }
371        }
372};
373
374
375//! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density
376class emlig // : eEF
377{
378
379        /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result
380        /// of data update from Bayesian estimation or set by the user if this emlig is a prior density
381        c_statistic statistic;
382
383        vector<list<polyhedron*>> for_splitting;
384               
385        vector<list<polyhedron*>> for_merging;
386
387        list<condition*> conditions;
388
389        double normalization_factor;
390
391        void alter_toprow_conditions(vec condition, bool should_be_added)
392        {
393                for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly)
394                {
395                        double product = 0;
396
397                        list<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();
398
399                        do
400                        {
401                                product = (*vertex_ref)->get_coordinates()*condition;
402                        }
403                        while(product == 0);
404
405                        if((product>0 && should_be_added)||(product<0 && !should_be_added))
406                        {
407                                ((toprow*) horiz_ref)->condition += condition;
408                        }
409                        else
410                        {
411                                ((toprow*) horiz_ref)->condition -= condition;
412                        }                               
413                }
414        }
415
416
417
418        void send_state_message(polyhedron* sender, vec toadd, vec toremove, int level)
419        {                       
420
421                bool shouldmerge = (toremove.size() != 0);
422                bool shouldsplit    = (toadd.size() != 0);
423               
424                if(shouldsplit||shouldmerge)
425                {
426                        for(list<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator!=sender->parents.end();parent_iterator++)
427                        {
428                                polyhedron* current_parent = *parent_iterator;
429
430                                current_parent->message_counter++;
431
432                                bool is_last = (current_parent->message_counter == current_parent->number_of_children());
433
434                                if(shouldmerge)
435                                {
436                                        int child_state  = sender->get_state(MERGE);
437                                        int parent_state = current_parent->get_state(MERGE);
438
439                                        if(parent_state == 0)
440                                        {
441                                                current_parent->set_state(child_state, MERGE);
442
443                                                if(child_state == 0)
444                                                {
445                                                        current_parent->mergechildren.push_back(sender);
446                                                }
447                                        }
448                                        else
449                                        {
450                                                if(child_state == 0)
451                                                {
452                                                        if(parent_state > 0)
453                                                        {
454                                                                sender->positiveparent = current_parent;
455                                                        }
456                                                        else
457                                                        {
458                                                                sender->negativeparent = current_parent;
459                                                        }
460                                                }
461                                        }
462
463                                        if(is_last)
464                                        {
465                                                if(parent_state > 0)
466                                                {
467                                                        for(list<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++)
468                                                        {
469                                                                (*merge_child)->positiveparent = current_parent;
470                                                        }
471                                                }
472
473                                                if(parent_state < 0)
474                                                {
475                                                        for(list<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++)
476                                                        {
477                                                                (*merge_child)->negativeparent = current_parent;
478                                                        }
479                                                }
480
481                                                if(parent_state == 0)
482                                                {
483                                                        for_merging[level+1].push_back(current_parent);
484                                                }
485
486                                                current_parent->mergechildren.clear();
487                                        }
488
489                                       
490                                }
491
492                                if(shouldsplit)
493                                        {
494                                                current_parent->totallyneutralgrandchildren.insert(current_parent->totallyneutralgrandchildren.end(),sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end());
495
496                                                switch(sender->get_state(SPLIT))
497                                                {
498                                                case 1:
499                                                        current_parent->positivechildren.push_back(sender);                                                     
500                                                break;
501                                                case 0:
502                                                        current_parent->neutralchildren.push_back(sender);                                                     
503
504                                                        if(current_parent->totally_neutral == NULL)
505                                                        {
506                                                                current_parent->totally_neutral = sender->totally_neutral;
507                                                        }
508                                                        else
509                                                        {
510                                                                current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral;
511                                                        }
512
513                                                        if(sender->totally_neutral)
514                                                        {
515                                                                current_parent->totallyneutralchildren.push_back(sender);
516                                                        }
517                                                       
518                                                break;
519                                                case -1:
520                                                        current_parent->negativechildren.push_back(sender);                                                     
521                                                break;
522                                                }
523
524                                                if(is_last)
525                                                {
526                                                        unique(current_parent->totallyneutralgrandchildren.begin(),current_parent->totallyneutralgrandchildren.end());
527
528                                                        if((current_parent->negativechildren.size()>0&&current_parent->positivechildren.size()>0)||
529                                                                                                                (current_parent->neutralchildren.size()>0&&current_parent->totally_neutral==false))
530                                                        {                                                               
531                                                               
532                                                                        for_splitting[level+1].push_back(current_parent);
533                                                               
534                                                                        current_parent->set_state(0, SPLIT);
535                                                        }
536                                                        else
537                                                        {
538                                                                if(current_parent->negativechildren.size()>0)
539                                                                {
540                                                                        current_parent->set_state(-1, SPLIT);
541
542                                                                        ((toprow*)current_parent)->condition-=toadd;
543                                                                }
544                                                                else if(current_parent->positivechildren.size()>0)
545                                                                {
546                                                                        current_parent->set_state(1, SPLIT);
547
548                                                                        ((toprow*)current_parent)->condition+=toadd;
549                                                                }
550                                                                else
551                                                                {
552                                                                        current_parent->raise_multiplicity();                                                           
553                                                                }
554
555                                                                current_parent->positivechildren.clear();
556                                                                current_parent->negativechildren.clear();
557                                                                current_parent->neutralchildren.clear();
558                                                                current_parent->totallyneutralchildren.clear();
559                                                                current_parent->totallyneutralgrandchildren.clear();
560                                                                current_parent->totally_neutral = NULL;
561                                                                current_parent->kids_rel_addresses.clear();
562                                                                current_parent->message_counter = 0;
563                                                        }
564                                                }
565                                        }
566
567                                        if(is_last)
568                                        {
569                                                send_state_message(current_parent,toadd,toremove,level+1);
570                                        }
571                       
572                        }
573                       
574                }               
575        }
576       
577public: 
578
579        int number_of_parameters;
580
581        /// A default constructor creates an emlig with predefined statistic representing only the range of the given
582        /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor.
583        emlig(int number_of_parameters)
584        {       
585                this->number_of_parameters = number_of_parameters;
586
587                create_statistic(number_of_parameters);         
588        }
589
590        /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a
591        /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters.
592        emlig(c_statistic statistic)
593        {
594                this->statistic = statistic;
595        }
596
597        void step_me(int marker)
598        {
599                for(int i = 0;i<statistic.size();i++)
600                {
601                        for(polyhedron* horiz_ref = statistic.rows[i];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly)
602                        {
603                                char* string = "Checkpoint";
604                        }
605                }
606        }
607
608        int statistic_rowsize(int row)
609        {
610                return statistic.row_size(row);
611        }
612
613        void add_condition(vec toadd)
614        {
615                vec null_vector = "";
616
617                add_and_remove_condition(toadd, null_vector);
618        }
619
620
621        void remove_condition(vec toremove)
622        {
623                vec null_vector = "";
624
625                add_and_remove_condition(null_vector, toremove);
626       
627        }
628
629
630        void add_and_remove_condition(vec toadd, vec toremove)
631        {
632                bool should_remove = (toremove.size() != 0);
633                bool should_add    = (toadd.size() != 0);
634
635                for_splitting.clear();
636                for_merging.clear();
637
638                for(int i = 0;i<statistic.size();i++)
639                {
640                        list<polyhedron*> empty_split;
641                        list<polyhedron*> empty_merge;
642
643                        for_splitting.push_back(empty_split);
644                        for_merging.push_back(empty_merge);
645                }
646
647                list<condition*>::iterator toremove_ref = conditions.end();
648                bool condition_should_be_added = false;
649
650                for(list<condition*>::iterator ref = conditions.begin();ref!=conditions.end();ref++)
651                {
652                        if(should_remove)
653                        {
654                                if((*ref)->value == toremove)
655                                {
656                                        if((*ref)->multiplicity>1)
657                                        {
658                                                (*ref)->multiplicity--;
659
660                                                alter_toprow_conditions(toremove,false);
661
662                                                should_remove = false;
663                                        }
664                                        else
665                                        {
666                                                toremove_ref = ref;                                                     
667                                        }
668                                }
669                        }
670
671                        if(should_add)
672                        {
673                                if((*ref)->value == toadd)
674                                {
675                                        (*ref)->multiplicity++;
676
677                                        alter_toprow_conditions(toadd,true);
678
679                                        should_add = false;
680                                }
681                                else
682                                {
683                                        condition_should_be_added = true;
684                                }
685                        }
686                }
687
688                if(toremove_ref!=conditions.end())
689                {
690                        conditions.erase(toremove_ref);
691                }
692
693                if(condition_should_be_added)
694                {
695                        conditions.push_back(new condition(toadd));
696                }
697
698               
699               
700                for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly)
701                {               
702                        vertex* current_vertex = (vertex*)horizontal_position;
703                       
704                        if(should_add||should_remove)
705                        {
706                                vec appended_vec = current_vertex->get_coordinates();
707                                appended_vec.ins(0,-1.0);
708
709                                if(should_add)
710                                {
711                                        double local_condition = toadd*appended_vec;
712
713                                        current_vertex->set_state(local_condition,SPLIT);
714
715                                        if(local_condition == 0)
716                                        {
717                                                current_vertex->totally_neutral = true;
718
719                                                current_vertex->raise_multiplicity();
720                                        }                                       
721                                }
722                       
723                                if(should_remove)
724                                {
725                                        double local_condition = toremove*appended_vec;
726
727                                        current_vertex->set_state(local_condition,MERGE);
728
729                                        if(local_condition == 0)
730                                        {
731                                                for_merging[0].push_back(current_vertex);
732                                        }
733                                }                               
734                        }
735
736                        send_state_message(current_vertex, toadd, toremove, 0);         
737                       
738                }
739
740                if(should_add)
741                {
742                        int k = 1;
743
744                        vector<list<polyhedron*>>::iterator beginning_ref = ++for_splitting.begin();
745
746                        for(vector<list<polyhedron*>>::iterator vert_ref = beginning_ref;vert_ref<for_splitting.end();vert_ref++)
747                        {                       
748
749                                for(list<polyhedron*>::reverse_iterator split_ref = vert_ref->rbegin();split_ref != vert_ref->rend();split_ref++)
750                                {
751                                        polyhedron* new_totally_neutral_child;
752
753                                        polyhedron* current_polyhedron = (*split_ref);
754                                       
755                                        if(vert_ref == beginning_ref)
756                                        {
757                                                vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates();                                             
758                                                vec coordinates2 = ((vertex*)(*(current_polyhedron->children.begin()++)))->get_coordinates();
759                                                coordinates2.ins(0,-1.0);
760                                               
761                                                double t = (-toadd*coordinates2)/(toadd(1,toadd.size()-1)*coordinates1)+1;
762
763                                                vec new_coordinates = coordinates1*t+(coordinates2(1,coordinates2.size()-1)-coordinates1);                                     
764
765                                                vertex* neutral_vertex = new vertex(new_coordinates);
766
767                                                new_totally_neutral_child = neutral_vertex;
768                                        }
769                                        else
770                                        {
771                                                toprow* neutral_toprow = new toprow();
772
773                                                new_totally_neutral_child = neutral_toprow;
774                                        }
775                                       
776                                        new_totally_neutral_child->children.insert(new_totally_neutral_child->children.end(),
777                                                                                                                current_polyhedron->totallyneutralgrandchildren.begin(),
778                                                                                                                                current_polyhedron->totallyneutralgrandchildren.end());
779
780                                        for(list<polyhedron*>::iterator grand_ref = current_polyhedron->totallyneutralgrandchildren.begin(); grand_ref != current_polyhedron->totallyneutralgrandchildren.end();grand_ref++)
781                                        {
782                                                (*grand_ref)->parents.push_back(new_totally_neutral_child);
783                                        }
784
785                                        toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition+toadd);
786                                        toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition-toadd);
787
788                                        for(list<polyhedron*>::iterator parent_ref = current_polyhedron->parents.begin();parent_ref!=current_polyhedron->parents.end();parent_ref++)
789                                        {
790                                                (*parent_ref)->totallyneutralgrandchildren.push_back(new_totally_neutral_child);
791
792                                                (*parent_ref)->neutralchildren.remove(current_polyhedron);
793                                                (*parent_ref)->children.remove(current_polyhedron);
794
795                                                (*parent_ref)->children.push_back(positive_poly);
796                                                (*parent_ref)->children.push_back(negative_poly);
797                                                (*parent_ref)->positivechildren.push_back(positive_poly);
798                                                (*parent_ref)->negativechildren.push_back(negative_poly);
799                                        }
800
801                                        positive_poly->parents.insert(positive_poly->parents.end(),
802                                                                                                current_polyhedron->parents.begin(),
803                                                                                                                current_polyhedron->parents.end());
804
805                                        negative_poly->parents.insert(negative_poly->parents.end(),
806                                                                                                current_polyhedron->parents.begin(),
807                                                                                                                current_polyhedron->parents.end());
808
809                                        positive_poly->children.push_back(new_totally_neutral_child);
810                                        negative_poly->children.push_back(new_totally_neutral_child);
811
812                                        new_totally_neutral_child->parents.push_back(positive_poly);
813                                        new_totally_neutral_child->parents.push_back(negative_poly);
814
815                                        for(list<polyhedron*>::iterator child_ref = current_polyhedron->positivechildren.begin();child_ref!=current_polyhedron->positivechildren.end();child_ref++)
816                                        {
817                                                (*child_ref)->parents.remove(current_polyhedron);
818                                                (*child_ref)->parents.push_back(positive_poly);
819                                        }                                       
820
821                                        positive_poly->children.insert(positive_poly->children.end(),
822                                                                                                current_polyhedron->positivechildren.begin(),
823                                                                                                                        current_polyhedron->positivechildren.end());
824
825                                        for(list<polyhedron*>::iterator child_ref = current_polyhedron->negativechildren.begin();child_ref!=current_polyhedron->negativechildren.end();child_ref++)
826                                        {
827                                                (*child_ref)->parents.remove(current_polyhedron);
828                                                (*child_ref)->parents.push_back(negative_poly);
829                                        }
830
831                                        negative_poly->children.insert(negative_poly->children.end(),
832                                                                                                current_polyhedron->negativechildren.begin(),
833                                                                                                                        current_polyhedron->negativechildren.end());
834
835                                        statistic.append_polyhedron(k-1, new_totally_neutral_child);
836                                       
837                                        statistic.insert_polyhedron(k, positive_poly, current_polyhedron);
838                                        statistic.insert_polyhedron(k, negative_poly, current_polyhedron);                                     
839
840                                        statistic.delete_polyhedron(k, current_polyhedron);
841
842                                        delete current_polyhedron;
843                                }
844
845                                k++;
846                        }
847                }
848
849                /*
850                vector<int> sizevector;
851                for(int s = 0;s<statistic.size();s++)
852                {
853                        sizevector.push_back(statistic.row_size(s));
854                }*/
855        }
856
857protected:
858
859        /// A method for creating plain default statistic representing only the range of the parameter space.
860    void create_statistic(int number_of_parameters)
861        {
862                // An empty vector of coordinates.
863                vec origin_coord;       
864
865                // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords.
866                vertex *origin = new vertex(origin_coord);
867
868                // It has itself as a vertex. There will be a nice use for this when the vertices of its parents are searched in
869                // the recursive creation procedure below.
870                origin->vertices.push_back(origin);
871
872                /*
873                // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse
874                // diagram. First we create a vector of polyhedrons..
875                list<polyhedron*> origin_vec;
876
877                // ..we fill it with the origin..
878                origin_vec.push_back(origin);
879
880                // ..and we fill the statistic with the created vector.
881                statistic.push_back(origin_vec);
882                */
883
884                statistic = *(new c_statistic());
885
886                statistic.append_polyhedron(0, origin);
887
888                // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to
889                // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows:
890                for(int i=0;i<number_of_parameters;i++)
891                {
892                        // We first will create two new vertices. These will be the borders of the parameter space in the dimension
893                        // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the
894                        // right amount of zero cooridnates by reading them from the origin
895                        vec origin_coord = origin->get_coordinates();                                           
896
897                        // And we incorporate the nonzero coordinates into the new cooordinate vectors
898                        vec origin_coord1 = concat(origin_coord,-max_range); 
899                        vec origin_coord2 = concat(origin_coord,max_range);                             
900                                       
901
902                        // Now we create the points
903                        vertex *new_point1 = new vertex(origin_coord1);
904                        vertex *new_point2 = new vertex(origin_coord2);
905
906                        new_point1->vertices.push_back(new_point1);
907                        new_point2->vertices.push_back(new_point2);
908                       
909                        //*********************************************************************************************************
910                        // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old
911                        // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points
912                        // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons
913                        // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is
914                        // connected to the first (second) newly created vertex by a parent-child relation.
915                        //*********************************************************************************************************
916
917
918                        /*
919                        // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram
920                        vector<vector<polyhedron*>> new_statistic1;
921                        vector<vector<polyhedron*>> new_statistic2;
922                        */
923
924                        c_statistic* new_statistic1 = new c_statistic();
925                        c_statistic* new_statistic2 = new c_statistic();
926
927                       
928                        // Copy the statistic by rows                   
929                        for(int j=0;j<statistic.size();j++)
930                        {
931                               
932
933                                // an element counter
934                                int element_number = 0;
935
936                                /*
937                                vector<polyhedron*> supportnew_1;
938                                vector<polyhedron*> supportnew_2;
939
940                                new_statistic1.push_back(supportnew_1);
941                                new_statistic2.push_back(supportnew_2);
942                                */
943
944                                // for each polyhedron in the given row
945                                for(polyhedron* horiz_ref = statistic.rows[j];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly)
946                                {       
947                                        // Append an extra zero coordinate to each of the vertices for the new dimension
948                                        // If vert_ref is at the first index => we loop through vertices
949                                        if(j == 0)
950                                        {
951                                                // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates
952                                                ((vertex*) horiz_ref)->push_coordinate(0);
953                                        }
954                                        /*
955                                        else
956                                        {
957                                                ((toprow*) (*horiz_ref))->condition.ins(0,0);
958                                        }*/
959
960                                        // if it has parents
961                                        if(!horiz_ref->parents.empty())
962                                        {
963                                                // save the relative address of this child in a vector kids_rel_addresses of all its parents.
964                                                // This information will later be used for copying the whole Hasse diagram with each of the
965                                                // relations contained within.
966                                                for(list<polyhedron*>::iterator parent_ref = horiz_ref->parents.begin();parent_ref != horiz_ref->parents.end();parent_ref++)
967                                                {
968                                                        (*parent_ref)->kids_rel_addresses.push_back(element_number);                                                   
969                                                }                                               
970                                        }
971
972                                        // **************************************************************************************************
973                                        // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron
974                                        // will be created as a toprow, but this information will be later forgotten and only the polyhedrons
975                                        // in the top row of the Hasse diagram will be considered toprow for later use.
976                                        // **************************************************************************************************
977
978                                        // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic
979                                        // this condition will be a vector of zeros. There are two vectors, because we need two copies of
980                                        // the original Hasse diagram.
981                                        vec vec1(number_of_parameters+1);
982                                        vec1.zeros();
983
984                                        vec vec2(number_of_parameters+1);
985                                        vec2.zeros();
986
987                                        // We create a new toprow with the previously specified condition.
988                                        toprow* current_copy1 = new toprow(vec1);
989                                        toprow* current_copy2 = new toprow(vec2);                                       
990
991                                        // The vertices of the copies will be inherited, because there will be a parent/child relation
992                                        // between each polyhedron and its offspring (comming from the copy) and a parent has all the
993                                        // vertices of its child plus more.
994                                        for(list<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();vertex_ref!=horiz_ref->vertices.end();vertex_ref++)
995                                        {
996                                                current_copy1->vertices.push_back(*vertex_ref);
997                                                current_copy2->vertices.push_back(*vertex_ref);                                         
998                                        }
999                                       
1000                                        // The only new vertex of the offspring should be the newly created point.
1001                                        current_copy1->vertices.push_back(new_point1);
1002                                        current_copy2->vertices.push_back(new_point2);
1003                                       
1004                                        // This method guarantees that each polyhedron is already triangulated, therefore its triangulation
1005                                        // is only one set of vertices and it is the set of all its vertices.
1006                                        current_copy1->triangulations.push_back(current_copy1->vertices);
1007                                        current_copy2->triangulations.push_back(current_copy2->vertices);
1008                                       
1009                                        // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying
1010                                        // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the
1011                                        // kids and when we do that and know the child, in the child we will remember the parent we came from.
1012                                        // This way all the parents/children relations are saved in both the parent and the child.
1013                                        if(!horiz_ref->kids_rel_addresses.empty())
1014                                        {
1015                                                for(list<int>::iterator kid_ref = horiz_ref->kids_rel_addresses.begin();kid_ref!=horiz_ref->kids_rel_addresses.end();kid_ref++)
1016                                                {       
1017                                                        polyhedron* new_kid1 = new_statistic1->rows[j-1];
1018                                                        polyhedron* new_kid2 = new_statistic2->rows[j-1];
1019
1020                                                        // THIS IS NOT EFFECTIVE: It could be improved by having the list indexed for new_statistic, but
1021                                                        // not indexed for statistic. Hopefully this will not cause a big slowdown - happens only offline.
1022                                                        if(*kid_ref)
1023                                                        {
1024                                                                for(int k = 1;k<=(*kid_ref);k++)
1025                                                                {
1026                                                                        new_kid1=new_kid1->next_poly;
1027                                                                        new_kid2=new_kid2->next_poly;
1028                                                                }
1029                                                        }
1030                                                       
1031                                                        // find the child and save the relation to the parent
1032                                                        current_copy1->children.push_back(new_kid1);
1033                                                        current_copy2->children.push_back(new_kid2);
1034
1035                                                        // in the child save the parents' address
1036                                                        new_kid1->parents.push_back(current_copy1);
1037                                                        new_kid2->parents.push_back(current_copy2);
1038                                                }                                               
1039
1040                                                // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the
1041                                                // Hasse diagram again)
1042                                                horiz_ref->kids_rel_addresses.clear();
1043                                        }
1044                                        // If there were no children previously, we are copying a polyhedron that has been a vertex before.
1045                                        // In this case it is a segment now and it will have a relation to its mother (copywise) and to the
1046                                        // newly created point. Here we create the connection to the new point, again from both sides.
1047                                        else
1048                                        {
1049                                                // Add the address of the new point in the former vertex
1050                                                current_copy1->children.push_back(new_point1);
1051                                                current_copy2->children.push_back(new_point2);
1052
1053                                                // Add the address of the former vertex in the new point
1054                                                new_point1->parents.push_back(current_copy1);
1055                                                new_point2->parents.push_back(current_copy2);
1056                                        }
1057
1058                                        // Save the mother in its offspring
1059                                        current_copy1->children.push_back(horiz_ref);
1060                                        current_copy2->children.push_back(horiz_ref);
1061
1062                                        // Save the offspring in its mother
1063                                        horiz_ref->parents.push_back(current_copy1);
1064                                        horiz_ref->parents.push_back(current_copy2);   
1065                                                               
1066                                       
1067                                        // Add the copies into the relevant statistic. The statistic will later be appended to the previous
1068                                        // Hasse diagram
1069                                        new_statistic1->append_polyhedron(j,current_copy1);
1070                                        new_statistic2->append_polyhedron(j,current_copy2);
1071                                       
1072                                        // Raise the count in the vector of polyhedrons
1073                                        element_number++;                       
1074                                       
1075                                }
1076                               
1077                        }
1078
1079                        /*
1080                        statistic.begin()->push_back(new_point1);
1081                        statistic.begin()->push_back(new_point2);
1082                        */
1083
1084                        statistic.append_polyhedron(0, new_point1);
1085                        statistic.append_polyhedron(0, new_point2);
1086
1087                        // Merge the new statistics into the old one. This will either be the final statistic or we will
1088                        // reenter the widening loop.
1089                        for(int j=0;j<new_statistic1->size();j++)
1090                        {
1091                                /*
1092                                if(j+1==statistic.size())
1093                                {
1094                                        list<polyhedron*> support;
1095                                        statistic.push_back(support);
1096                                }
1097                               
1098                                (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end());
1099                                (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end());
1100                                */
1101                                statistic.append_polyhedron(j+1,new_statistic1->rows[j],new_statistic1->row_ends[j]);
1102                                statistic.append_polyhedron(j+1,new_statistic2->rows[j],new_statistic2->row_ends[j]);
1103                        }
1104
1105               
1106                }
1107
1108                /*
1109                vector<list<toprow*>> toprow_statistic;
1110                int line_count = 0;
1111
1112                for(vector<list<polyhedron*>>::iterator polyhedron_ref = ++statistic.begin(); polyhedron_ref!=statistic.end();polyhedron_ref++)
1113                {
1114                        list<toprow*> support_list;
1115                        toprow_statistic.push_back(support_list);                                               
1116
1117                        for(list<polyhedron*>::iterator polyhedron_ref2 = polyhedron_ref->begin(); polyhedron_ref2 != polyhedron_ref->end(); polyhedron_ref2++)
1118                        {
1119                                toprow* support_top = (toprow*)(*polyhedron_ref2);
1120
1121                                toprow_statistic[line_count].push_back(support_top);
1122                        }
1123
1124                        line_count++;
1125                }*/
1126
1127                /*
1128                vector<int> sizevector;
1129                for(int s = 0;s<statistic.size();s++)
1130                {
1131                        sizevector.push_back(statistic.row_size(s));
1132                }
1133                */
1134               
1135        }
1136
1137
1138       
1139       
1140};
1141
1142/*
1143
1144//! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density
1145class RARX : public BM
1146{
1147private:
1148
1149        emlig posterior;
1150
1151public:
1152        RARX():BM()
1153        {
1154        };
1155
1156        void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec)
1157        {
1158               
1159        }
1160
1161};*/
1162
1163
1164
1165#endif //TRAGE_H
Note: See TracBrowser for help on using the browser.