root/applications/robust/robustlib.h @ 1262

Revision 1262, 35.2 kB (checked in by sindj, 14 years ago)

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