root/applications/robust/robustlib.h @ 1186

Revision 1186, 14.2 kB (checked in by sindj, 14 years ago)

Dodelana a odzkousena metoda create_statistic() v emlig. Zatim nefunguje dedeni od eEF. 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 <algorithm>
14       
15using namespace bdm;
16using namespace std;
17
18const double max_range = numeric_limits<double>::max()/10e-5;
19
20class polyhedron;
21class vertex;
22
23/// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram
24/// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created.
25class polyhedron
26{
27        /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of
28        /// more than just the necessary number of conditions. For example if a newly created line passes through an already
29        /// existing point, the points multiplicity will rise by 1.
30        int multiplicity;       
31
32public:
33        /// A list of polyhedrons parents within the Hasse diagram.
34        vector<polyhedron*> parents;
35
36        /// A list of polyhedrons children withing the Hasse diagram.
37        vector<polyhedron*> children;
38
39        /// All the vertices of the given polyhedron
40        vector<vertex*> vertices;
41
42        /// A list used for storing children that lie in the positive region related to a certain condition
43        vector<polyhedron*> positivechildren;
44
45        /// A list used for storing children that lie in the negative region related to a certain condition
46        vector<polyhedron*> negativechildren;
47
48        /// Children intersecting the condition
49        vector<polyhedron*> neutralchildren;
50
51        /// List of triangulation polyhedrons of the polyhedron given by their relative vertices.
52        vector<vector<vertex*>> triangulations;
53
54        /// A list of relative addresses serving for Hasse diagram construction.
55        vector<int> kids_rel_addresses;
56
57        /// Default constructor
58        polyhedron()
59        {
60                multiplicity = 1;       
61        }
62       
63        /// Setter for raising multiplicity
64        void RaiseMultiplicity()
65        {
66                multiplicity++;
67        }
68
69        /// Setter for lowering multiplicity
70        void LowerMultiplicity()
71        {
72                multiplicity--;
73        }
74       
75        /// An obligatory operator, when the class is used within a C++ STL structure like a vector
76        int operator==(polyhedron polyhedron2)
77        {
78                return true;
79        }
80
81        /// An obligatory operator, when the class is used within a C++ STL structure like a vector
82        int operator<(polyhedron polyhedron2)
83        {
84                return false;
85        }
86};
87
88/// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse
89/// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space.
90class vertex : public polyhedron
91{
92        /// A dynamic array representing coordinates of the vertex
93        vector<double> coordinates;
94
95public:
96
97        /// Default constructor
98        vertex();
99
100        /// Constructor of a vertex from a set of coordinates
101        vertex(vector<double> coordinates)
102        {
103                this->coordinates = coordinates;
104        }
105
106        /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter
107        /// space of certain dimension is established, but the dimension is not known when the vertex is created.
108        void push_coordinate(double coordinate)
109        {
110                coordinates.push_back(coordinate);
111        }
112
113        /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer
114        /// (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        }
126};
127
128/// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates
129/// it from polyhedrons in other rows.
130class toprow : public polyhedron
131{
132        /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation
133        vector<double> condition;
134
135public:
136
137        /// Default constructor
138        toprow();
139
140        /// Constructor creating a toprow from the condition
141        toprow(vector<double> condition)
142        {
143                this->condition = condition;
144        }
145
146};
147
148
149
150
151//! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density
152class emlig // : eEF
153{
154
155        /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result
156        /// of data update from Bayesian estimation or set by the user if this emlig is a prior density
157        vector<vector<polyhedron*>> statistic;
158       
159public: 
160
161        /// A default constructor creates an emlig with predefined statistic representing only the range of the given
162        /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor.
163        emlig(int number_of_parameters)
164        {
165                create_statistic(number_of_parameters);
166        }
167
168        /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a
169        /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters.
170        emlig(vector<vector<polyhedron*>> statistic)
171        {
172                this->statistic = statistic;
173        }
174
175protected:
176
177        /// A method for creating plain default statistic representing only the range of the parameter space.
178    void create_statistic(int number_of_parameters)
179        {
180                // An empty vector of coordinates.
181                vector<double> origin_coord;   
182
183                // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords.
184                vertex *origin = new vertex(origin_coord);
185
186                // It has itself as a vertex. There will be a nice use for this when the vertices of its parents are searched in
187                // the recursive creation procedure below.
188                origin->vertices.push_back(origin);
189
190                // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse
191                // diagram. First we create a vector of polyhedrons..
192                vector<polyhedron*> origin_vec;
193
194                // ..we fill it with the origin..
195                origin_vec.push_back(origin);
196
197                // ..and we fill the statistic with the created vector.
198                statistic.push_back(origin_vec);
199
200                // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to
201                // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows:
202                for(int i=0;i<number_of_parameters;i++)
203                {
204                        // We first will create two new vertices. These will be the borders of the parameter space in the dimension
205                        // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the
206                        // 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();                       
209
210                        // 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);
213
214                        // Now we create the points
215                        vertex *new_point1 = new vertex(origin_coord1);
216                        vertex *new_point2 = new vertex(origin_coord2);
217                       
218                        //*********************************************************************************************************
219                        // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old
220                        // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points
221                        // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons
222                        // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is
223                        // connected to the first (second) newly created vertex by a parent-child relation.
224                        //*********************************************************************************************************
225
226
227                        // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram
228                        vector<vector<polyhedron*>> new_statistic1;
229                        vector<vector<polyhedron*>> new_statistic2;
230
231                        // Copy the statistic by rows
232                        for(int j=0;j<statistic.size();j++)
233                        {
234                                // an element counter
235                                int element_number = 0;
236
237                                vector<polyhedron*> supportnew_1;
238                                vector<polyhedron*> supportnew_2;
239
240                                new_statistic1.push_back(supportnew_1);
241                                new_statistic2.push_back(supportnew_2);
242
243                                // for each polyhedron in the given row
244                                for(vector<polyhedron*>::iterator horiz_ref = statistic[j].begin();horiz_ref<statistic[j].end();horiz_ref++)
245                                {       
246                                        // Append an extra zero coordinate to each of the vertices for the new dimension
247                                        // If j==0 => we loop through vertices
248                                        if(j == 0)
249                                        {
250                                                // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates
251                                                ((vertex*) (*horiz_ref))->push_coordinate(0);
252                                        }
253
254                                        // if it has parents
255                                        if(!(*horiz_ref)->parents.empty())
256                                        {
257                                                // save the relative address of this child in a vector kids_rel_addresses of all its parents.
258                                                // This information will later be used for copying the whole Hasse diagram with each of the
259                                                // relations contained within.
260                                                for(vector<polyhedron*>::iterator parent_ref = (*horiz_ref)->parents.begin();parent_ref < (*horiz_ref)->parents.end();parent_ref++)
261                                                {
262                                                        (*parent_ref)->kids_rel_addresses.push_back(element_number);                                                   
263                                                }                                               
264                                        }
265
266                                        // **************************************************************************************************
267                                        // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron
268                                        // will be created as a toprow, but this information will be later forgotten and only the polyhedrons
269                                        // in the top row of the Hasse diagram will be considered toprow for later use.
270                                        // **************************************************************************************************
271
272                                        // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic
273                                        // this condition will be a vector of zeros. There are two vectors, because we need two copies of
274                                        // the original Hasse diagram.
275                                        vector<double> vec1(i+2,0);
276                                        vector<double> vec2(i+2,0);
277
278                                        // We create a new toprow with the previously specified condition.
279                                        toprow *current_copy1 = new toprow(vec1);
280                                        toprow *current_copy2 = new toprow(vec2);                                       
281
282                                        // The vertices of the copies will be inherited, because there will be a parent/child relation
283                                        // between each polyhedron and its offspring (comming from the copy) and a parent has all the
284                                        // vertices of its child plus more.
285                                        for(vector<vertex*>::iterator vert_ref = (*horiz_ref)->vertices.begin();vert_ref<(*horiz_ref)->vertices.end();vert_ref++)
286                                        {
287                                                current_copy1->vertices.push_back(*vert_ref);
288                                                current_copy2->vertices.push_back(*vert_ref);                                           
289                                        }
290                                       
291                                        // The only new vertex of the offspring should be the newly created point.
292                                        current_copy1->vertices.push_back(new_point1);
293                                        current_copy2->vertices.push_back(new_point2);
294                                       
295                                        // This method guarantees that each polyhedron is already triangulated, therefore its triangulation
296                                        // is only one set of vertices and it is the set of all its vertices.
297                                        current_copy1->triangulations.push_back(current_copy1->vertices);
298                                        current_copy2->triangulations.push_back(current_copy2->vertices);
299                                       
300                                        // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying
301                                        // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the
302                                        // kids and when we do that and know the child, in the child we will remember the parent we came from.
303                                        // This way all the parents/children relations are saved in both the parent and the child.
304                                        if(!(*horiz_ref)->kids_rel_addresses.empty())
305                                        {
306                                                for(vector<int>::iterator kid_ref = (*horiz_ref)->kids_rel_addresses.begin();kid_ref<(*horiz_ref)->kids_rel_addresses.end();kid_ref++)
307                                                {       
308                                                        // find the child and save the relation to the parent
309                                                        current_copy1->children.push_back(new_statistic1[j-1][(*kid_ref)]);
310                                                        current_copy2->children.push_back(new_statistic2[j-1][(*kid_ref)]);
311
312                                                        // in the child save the parents' address
313                                                        new_statistic1[j-1][(*kid_ref)]->parents.push_back(current_copy1);
314                                                        new_statistic2[j-1][(*kid_ref)]->parents.push_back(current_copy2);
315                                                }                                               
316
317                                                // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the
318                                                // Hasse diagram again)
319                                                (*horiz_ref)->kids_rel_addresses.clear();
320                                        }
321                                        // If there were no children previously, we are copying a polyhedron that has been a vertex before.
322                                        // In this case it is a segment now and it will have a relation to its mother (copywise) and to the
323                                        // newly created point. Here we create the connection to the new point, again from both sides.
324                                        else
325                                        {
326                                                // Add the address of the new point in the former vertex
327                                                current_copy1->children.push_back(new_point1);
328                                                current_copy2->children.push_back(new_point2);
329
330                                                // Add the address of the former vertex in the new point
331                                                new_point1->parents.push_back(current_copy1);
332                                                new_point2->parents.push_back(current_copy2);
333                                        }
334
335                                        // Save the mother in its offspring
336                                        current_copy1->children.push_back(*horiz_ref);
337                                        current_copy2->children.push_back(*horiz_ref);
338
339                                        // Save the offspring in its mother
340                                        (*horiz_ref)->parents.push_back(current_copy1);
341                                        (*horiz_ref)->parents.push_back(current_copy2); 
342                                                               
343                                       
344                                        // Add the copies into the relevant statistic. The statistic will later be appended to the previous
345                                        // Hasse diagram
346                                        new_statistic1[j].push_back(current_copy1);
347                                        new_statistic2[j].push_back(current_copy2);
348                                       
349                                        // Raise the count in the vector of polyhedrons
350                                        element_number++;
351                                       
352                                }                               
353                        }
354
355                        statistic[0].push_back(new_point1);
356                        statistic[0].push_back(new_point2);
357
358                        // Merge the new statistics into the old one. This will either be the final statistic or we will
359                        // reenter the widening loop.
360                        for(int j=0;j<new_statistic1.size();j++)
361                        {
362                                if(j+1==statistic.size())
363                                {
364                                        vector<polyhedron*> support;
365                                        statistic.push_back(support);
366                                }
367                               
368                                statistic[j+1].insert(statistic[j+1].end(),new_statistic1[j].begin(),new_statistic1[j].end());
369                                statistic[j+1].insert(statistic[j+1].end(),new_statistic2[j].begin(),new_statistic2[j].end());
370                        }
371                }
372        }
373
374
375       
376       
377};
378
379//! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density
380class RARX : public BMEF{
381};
382
383
384#endif //TRAGE_H
Note: See TracBrowser for help on using the browser.