Changeset 1204 for applications/robust

Show
Ignore:
Timestamp:
09/29/10 17:37:40 (14 years ago)
Author:
sindj
Message:

Rozpracovani funkci na Bayes data update. JS

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/robustlib.h

    r1186 r1204  
    1515using namespace bdm; 
    1616using namespace std; 
     17using namespace itpp; 
    1718 
    1819const double max_range = numeric_limits<double>::max()/10e-5; 
     
    3031        int multiplicity;        
    3132 
     33        int split_state; 
     34 
     35        int merge_state; 
     36 
     37         
     38 
    3239public: 
    3340        /// A list of polyhedrons parents within the Hasse diagram. 
     
    4956        vector<polyhedron*> neutralchildren; 
    5057 
     58        vector<polyhedron*> mergechildren; 
     59 
     60        polyhedron* positiveparent; 
     61 
     62        polyhedron* negativeparent; 
     63 
     64        int message_counter; 
     65 
    5166        /// List of triangulation polyhedrons of the polyhedron given by their relative vertices.  
    5267        vector<vector<vertex*>> triangulations; 
     
    5873        polyhedron() 
    5974        { 
    60                 multiplicity = 1;        
     75                multiplicity = 1; 
     76 
     77                message_counter = 0; 
    6178        } 
    6279         
    6380        /// Setter for raising multiplicity 
    64         void RaiseMultiplicity() 
     81        void raise_multiplicity() 
    6582        { 
    6683                multiplicity++; 
     
    6885 
    6986        /// Setter for lowering multiplicity 
    70         void LowerMultiplicity() 
     87        void lower_multiplicity() 
    7188        { 
    7289                multiplicity--; 
     
    83100        { 
    84101                return false; 
     102        } 
     103 
     104        void set_state(double state_indicator, actions action) 
     105        { 
     106                switch(action) 
     107                { 
     108                        case MERGE: 
     109                                merge_state = (int)sign(state_indicator);                        
     110                        break; 
     111                        case SPLIT: 
     112                                split_state = (int)sign(state_indicator); 
     113                        break; 
     114                } 
     115        } 
     116 
     117        int get_state(actions action) 
     118        { 
     119                switch(action) 
     120                { 
     121                        case MERGE: 
     122                                return merge_state;                      
     123                        break; 
     124                        case SPLIT: 
     125                                return split_state; 
     126                        break; 
     127                } 
     128        } 
     129 
     130        int number_of_children() 
     131        { 
     132                return children.size()+positivechildren.size()+negativechildren.size()+neutralchildren.size(); 
     133        } 
     134 
     135        void send_state_message(bool shouldsplit, bool shouldmerge) 
     136        { 
     137                if(shouldsplit||shouldmerge) 
     138                { 
     139                        for(vector<polyhedron*>::iterator parent_iterator = parents.begin();parent_iterator<parents.end();parent_iterator++) 
     140                        { 
     141                                polyhedron* current_parent = *parent_iterator; 
     142 
     143                                current_parent->message_counter++; 
     144 
     145                                bool is_last = (current_parent->message_counter == current_parent->number_of_children()); 
     146 
     147                                if(shouldmerge) 
     148                                { 
     149                                        int child_state  = get_state(MERGE); 
     150                                        int parent_state = current_parent->get_state(MERGE); 
     151 
     152                                        if(parent_state == NULL || parent_state == 0) 
     153                                        { 
     154                                                current_parent->set_state(child_state, MERGE); 
     155 
     156                                                if(child_state == 0) 
     157                                                { 
     158                                                        current_parent->mergechildren.push_back(this); 
     159                                                } 
     160                                        } 
     161                                        else 
     162                                        { 
     163                                                if(child_state == 0) 
     164                                                { 
     165                                                        if(parent_state > 0) 
     166                                                        { 
     167                                                                positiveparent = current_parent; 
     168                                                        } 
     169                                                        else 
     170                                                        { 
     171                                                                negativeparent = current_parent; 
     172                                                        } 
     173                                                } 
     174                                        } 
     175 
     176                                        if(is_last) 
     177                                        { 
     178                                                if(parent_state > 0) 
     179                                                { 
     180                                                        for(vector<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child < current_parent->mergechildren.end();merge_child++) 
     181                                                        { 
     182                                                                (*merge_child)->positiveparent = current_parent; 
     183                                                        } 
     184                                                } 
     185 
     186                                                if(parent_state < 0) 
     187                                                { 
     188                                                        for(vector<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child < current_parent->mergechildren.end();merge_child++) 
     189                                                        { 
     190                                                                (*merge_child)->negativeparent = current_parent; 
     191                                                        } 
     192                                                } 
     193 
     194                                                current_parent->mergechildren.clear(); 
     195                                        } 
     196 
     197 
     198                                } 
     199                         
     200                        } 
     201                } 
    85202        } 
    86203}; 
     
    91208{ 
    92209        /// A dynamic array representing coordinates of the vertex 
    93         vector<double> coordinates; 
     210        vec coordinates;         
     211 
     212        enum actions {MERGE, SPLIT}; 
    94213 
    95214public: 
     215 
     216 
    96217 
    97218        /// Default constructor 
     
    99220 
    100221        /// Constructor of a vertex from a set of coordinates 
    101         vertex(vector<double> coordinates) 
     222        vertex(vec coordinates) 
    102223        { 
    103224                this->coordinates = coordinates; 
     
    108229        void push_coordinate(double coordinate) 
    109230        { 
    110                 coordinates.push_back(coordinate); 
     231                coordinates = concat(coordinates,coordinate); 
    111232        } 
    112233 
    113234        /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer  
    114235        /// (not given by reference), but a new copy is created (they are given by value). 
    115         vector<double> get_coordinates() 
    116         { 
    117                 vector<double> returned_vec; 
    118  
    119                 for(int i = 0;i<coordinates.size();i++) 
    120                 { 
    121                         returned_vec.push_back(coordinates[i]); 
    122                 } 
    123  
    124                 return returned_vec; 
    125         } 
     236        vec get_coordinates() 
     237        {                
     238                return coordinates; 
     239        } 
     240 
     241                 
    126242}; 
    127243 
     
    130246class toprow : public polyhedron 
    131247{ 
     248         
     249public: 
    132250        /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation 
    133         vector<double> condition; 
    134  
    135 public: 
     251        vec condition; 
    136252 
    137253        /// Default constructor 
     
    139255 
    140256        /// Constructor creating a toprow from the condition 
    141         toprow(vector<double> condition) 
     257        toprow(vec condition) 
    142258        { 
    143259                this->condition = condition; 
     
    146262}; 
    147263 
    148  
     264class condition 
     265{        
     266public: 
     267        vec value;       
     268 
     269        int multiplicity; 
     270 
     271        condition(vec value) 
     272        { 
     273                this->value = value; 
     274                multiplicity = 1; 
     275        } 
     276} 
    149277 
    150278 
     
    156284        /// of data update from Bayesian estimation or set by the user if this emlig is a prior density 
    157285        vector<vector<polyhedron*>> statistic; 
     286 
     287        vector<condition*> conditions; 
     288 
     289        double normalization_factor; 
     290 
     291        void alter_toprow_conditions(vec condition, bool should_be_added) 
     292        { 
     293                for(vector<polyhedron*>::iterator horiz_ref = statistic[statistic.size()-1].begin();horiz_ref<statistic[statistic.size()-1].end();horiz_ref++) 
     294                { 
     295                        double product = 0; 
     296 
     297                        vector<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin(); 
     298 
     299                        do 
     300                        { 
     301                                product = (*vertex_ref)->coordinates*condition; 
     302                        } 
     303                        while(product == 0) 
     304 
     305                        if((product>0 && should_be_added)||(product<0 && !should_be_added)) 
     306                        { 
     307                                ((toprow*) (*horiz_ref))->condition += condition; 
     308                        } 
     309                        else 
     310                        { 
     311                                ((toprow*) (*horiz_ref))->condition -= condition; 
     312                        }                                                        
     313                } 
     314        } 
    158315         
    159316public:  
     
    173330        } 
    174331 
     332        void add_and_remove_condition(vec toremove, vec toadd) 
     333        { 
     334                vector<condition*>::iterator toremove_ref = conditions.end(); 
     335                bool condition_should_be_added = false; 
     336 
     337                for(vector<condition*>::iterator ref = conditions.begin();ref<conditions.end();ref++) 
     338                { 
     339                        if(toremove != NULL) 
     340                        { 
     341                                if((*ref)->value == toremove) 
     342                                { 
     343                                        if(multiplicity>1) 
     344                                        { 
     345                                                multiplicity--; 
     346 
     347                                                alter_toprow_conditions(toremove,false); 
     348 
     349                                                toremove = NULL; 
     350                                        } 
     351                                        else 
     352                                        { 
     353                                                toremove_ref = ref;                                                      
     354                                        } 
     355                                } 
     356                        } 
     357 
     358                        if(toadd != NULL) 
     359                        { 
     360                                if((*iterator)->value == toadd) 
     361                                { 
     362                                        (*iterator)->multiplicity++; 
     363 
     364                                        alter_toprow_conditions(toadd,true); 
     365 
     366                                        toadd = NULL; 
     367                                } 
     368                                else 
     369                                { 
     370                                        condition_should_be_added = true; 
     371                                } 
     372                        } 
     373                } 
     374 
     375                if(toremove_ref!=conditions.end()) 
     376                { 
     377                        conditions.erase(toremove_ref); 
     378                } 
     379 
     380                if(condition_should_be_added) 
     381                { 
     382                        conditions.push_back(new condition(toadd)); 
     383                } 
     384 
     385                vector<vector<polyhedron*>> for_splitting; 
     386                vector<vector<polyhedron*>> for_merging; 
     387 
     388                for(vector<polyhedron*>::iterator horizontal_position = statistic[0].begin();horizontal_position<statistic[0].end();horizontal_position++) 
     389                {                
     390                        vertex* current_vertex = (vertex*)horizontal_position; 
     391                         
     392                        if(toadd != NULL) 
     393                        { 
     394                                current_vertex->set_state(toadd*current_vertex->coordinates,SPLIT); 
     395                        } 
     396                         
     397                        if(toremove != NULL) 
     398                        { 
     399                                current_vertex->set_state(toremove*current_vertex->coordinates,MERGE); 
     400                        } 
     401 
     402                        current_vertex->send_state_message(toadd != NULL, toremove != NULL); 
     403                } 
     404        } 
     405 
    175406protected: 
    176407 
     
    179410        { 
    180411                // An empty vector of coordinates. 
    181                 vector<double> origin_coord;     
     412                vec origin_coord;        
    182413 
    183414                // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. 
     
    205436                        // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the  
    206437                        // right amount of zero cooridnates by reading them from the origin 
    207                         vector<double> origin_coord1 = origin->get_coordinates(); 
    208                         vector<double> origin_coord2 = origin->get_coordinates();                        
     438                        vec origin_coord = origin->get_coordinates();                                            
    209439 
    210440                        // And we incorporate the nonzero coordinates into the new cooordinate vectors 
    211                         origin_coord1.push_back(max_range); 
    212                         origin_coord2.push_back(-max_range); 
     441                        vec origin_coord1 = concat(origin_coord,max_range); 
     442                        vec origin_coord2 = concat(origin_coord,-max_range); 
    213443 
    214444                        // Now we create the points 
     
    273503                                        // this condition will be a vector of zeros. There are two vectors, because we need two copies of  
    274504                                        // the original Hasse diagram. 
    275                                         vector<double> vec1(i+2,0); 
    276                                         vector<double> vec2(i+2,0); 
     505                                        vec vec1(i+2); 
     506                                        vec1.zeros(); 
     507 
     508                                        vec vec2(i+2); 
     509                                        vec2.zeros(); 
    277510 
    278511                                        // We create a new toprow with the previously specified condition. 
     
    378611 
    379612//! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density 
    380 class RARX : public BMEF{ 
     613class RARX : public BM 
     614{ 
     615private: 
     616 
     617        emlig posterior; 
     618 
     619public: 
     620        RARX():BM() 
     621        { 
     622        }; 
     623 
     624        void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec) 
     625        { 
     626                 
     627        } 
     628 
    381629}; 
    382630