root/applications/robust/robustlib.h @ 1234

Revision 1234, 34.4 kB (checked in by sindj, 14 years ago)

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