root/applications/robust/robustlib.h @ 1252

Revision 1242, 35.8 kB (checked in by sindj, 14 years ago)

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