root/applications/robust/robustlib.h @ 1254

Revision 1254, 34.2 kB (checked in by sindj, 14 years ago)

Rozdelane pocitani integralu. JS

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