/*! \file \brief Robust Bayesian auto-regression model \author Jan Sindelar. */ #ifndef ROBUST_H #define ROBUST_H #include #include #include #include using namespace bdm; using namespace std; const double max_range = numeric_limits::max()/10e-5; class polyhedron; class vertex; /// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram /// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created. class polyhedron { /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of /// more than just the necessary number of conditions. For example if a newly created line passes through an already /// existing point, the points multiplicity will rise by 1. int multiplicity; public: /// A list of polyhedrons parents within the Hasse diagram. vector parents; /// A list of polyhedrons children withing the Hasse diagram. vector children; /// All the vertices of the given polyhedron vector vertices; /// A list used for storing children that lie in the positive region related to a certain condition vector positivechildren; /// A list used for storing children that lie in the negative region related to a certain condition vector negativechildren; /// Children intersecting the condition vector neutralchildren; /// List of triangulation polyhedrons of the polyhedron given by their relative vertices. vector> triangulations; /// A list of relative addresses serving for Hasse diagram construction. vector kids_rel_addresses; /// Default constructor polyhedron() { multiplicity = 1; } /// Setter for raising multiplicity void RaiseMultiplicity() { multiplicity++; } /// Setter for lowering multiplicity void LowerMultiplicity() { multiplicity--; } /// An obligatory operator, when the class is used within a C++ STL structure like a vector int operator==(polyhedron polyhedron2) { return true; } /// An obligatory operator, when the class is used within a C++ STL structure like a vector int operator<(polyhedron polyhedron2) { return false; } }; /// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse /// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. class vertex : public polyhedron { /// A dynamic array representing coordinates of the vertex vector coordinates; public: /// Default constructor vertex(); /// Constructor of a vertex from a set of coordinates vertex(vector coordinates) { this->coordinates = coordinates; } /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter /// space of certain dimension is established, but the dimension is not known when the vertex is created. void push_coordinate(double coordinate) { coordinates.push_back(coordinate); } /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer /// (not given by reference), but a new copy is created (they are given by value). vector get_coordinates() { vector returned_vec; for(int i = 0;i condition; public: /// Default constructor toprow(); /// Constructor creating a toprow from the condition toprow(vector condition) { this->condition = condition; } }; //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density class emlig // : eEF { /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result /// of data update from Bayesian estimation or set by the user if this emlig is a prior density vector> statistic; public: /// A default constructor creates an emlig with predefined statistic representing only the range of the given /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. emlig(int number_of_parameters) { create_statistic(number_of_parameters); } /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. emlig(vector> statistic) { this->statistic = statistic; } protected: /// A method for creating plain default statistic representing only the range of the parameter space. void create_statistic(int number_of_parameters) { // An empty vector of coordinates. vector origin_coord; // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. vertex *origin = new vertex(origin_coord); // It has itself as a vertex. There will be a nice use for this when the vertices of its parents are searched in // the recursive creation procedure below. origin->vertices.push_back(origin); // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse // diagram. First we create a vector of polyhedrons.. vector origin_vec; // ..we fill it with the origin.. origin_vec.push_back(origin); // ..and we fill the statistic with the created vector. statistic.push_back(origin_vec); // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: for(int i=0;i origin_coord1 = origin->get_coordinates(); vector origin_coord2 = origin->get_coordinates(); // And we incorporate the nonzero coordinates into the new cooordinate vectors origin_coord1.push_back(max_range); origin_coord2.push_back(-max_range); // Now we create the points vertex *new_point1 = new vertex(origin_coord1); vertex *new_point2 = new vertex(origin_coord2); //********************************************************************************************************* // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is // connected to the first (second) newly created vertex by a parent-child relation. //********************************************************************************************************* // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram vector> new_statistic1; vector> new_statistic2; // Copy the statistic by rows for(int j=0;j supportnew_1; vector supportnew_2; new_statistic1.push_back(supportnew_1); new_statistic2.push_back(supportnew_2); // for each polyhedron in the given row for(vector::iterator horiz_ref = statistic[j].begin();horiz_ref we loop through vertices if(j == 0) { // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates ((vertex*) (*horiz_ref))->push_coordinate(0); } // if it has parents if(!(*horiz_ref)->parents.empty()) { // save the relative address of this child in a vector kids_rel_addresses of all its parents. // This information will later be used for copying the whole Hasse diagram with each of the // relations contained within. for(vector::iterator parent_ref = (*horiz_ref)->parents.begin();parent_ref < (*horiz_ref)->parents.end();parent_ref++) { (*parent_ref)->kids_rel_addresses.push_back(element_number); } } // ************************************************************************************************** // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron // will be created as a toprow, but this information will be later forgotten and only the polyhedrons // in the top row of the Hasse diagram will be considered toprow for later use. // ************************************************************************************************** // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic // this condition will be a vector of zeros. There are two vectors, because we need two copies of // the original Hasse diagram. vector vec1(i+2,0); vector vec2(i+2,0); // We create a new toprow with the previously specified condition. toprow *current_copy1 = new toprow(vec1); toprow *current_copy2 = new toprow(vec2); // The vertices of the copies will be inherited, because there will be a parent/child relation // between each polyhedron and its offspring (comming from the copy) and a parent has all the // vertices of its child plus more. for(vector::iterator vert_ref = (*horiz_ref)->vertices.begin();vert_ref<(*horiz_ref)->vertices.end();vert_ref++) { current_copy1->vertices.push_back(*vert_ref); current_copy2->vertices.push_back(*vert_ref); } // The only new vertex of the offspring should be the newly created point. current_copy1->vertices.push_back(new_point1); current_copy2->vertices.push_back(new_point2); // This method guarantees that each polyhedron is already triangulated, therefore its triangulation // is only one set of vertices and it is the set of all its vertices. current_copy1->triangulations.push_back(current_copy1->vertices); current_copy2->triangulations.push_back(current_copy2->vertices); // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the // kids and when we do that and know the child, in the child we will remember the parent we came from. // This way all the parents/children relations are saved in both the parent and the child. if(!(*horiz_ref)->kids_rel_addresses.empty()) { for(vector::iterator kid_ref = (*horiz_ref)->kids_rel_addresses.begin();kid_ref<(*horiz_ref)->kids_rel_addresses.end();kid_ref++) { // find the child and save the relation to the parent current_copy1->children.push_back(new_statistic1[j-1][(*kid_ref)]); current_copy2->children.push_back(new_statistic2[j-1][(*kid_ref)]); // in the child save the parents' address new_statistic1[j-1][(*kid_ref)]->parents.push_back(current_copy1); new_statistic2[j-1][(*kid_ref)]->parents.push_back(current_copy2); } // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the // Hasse diagram again) (*horiz_ref)->kids_rel_addresses.clear(); } // If there were no children previously, we are copying a polyhedron that has been a vertex before. // In this case it is a segment now and it will have a relation to its mother (copywise) and to the // newly created point. Here we create the connection to the new point, again from both sides. else { // Add the address of the new point in the former vertex current_copy1->children.push_back(new_point1); current_copy2->children.push_back(new_point2); // Add the address of the former vertex in the new point new_point1->parents.push_back(current_copy1); new_point2->parents.push_back(current_copy2); } // Save the mother in its offspring current_copy1->children.push_back(*horiz_ref); current_copy2->children.push_back(*horiz_ref); // Save the offspring in its mother (*horiz_ref)->parents.push_back(current_copy1); (*horiz_ref)->parents.push_back(current_copy2); // Add the copies into the relevant statistic. The statistic will later be appended to the previous // Hasse diagram new_statistic1[j].push_back(current_copy1); new_statistic2[j].push_back(current_copy2); // Raise the count in the vector of polyhedrons element_number++; } } statistic[0].push_back(new_point1); statistic[0].push_back(new_point2); // Merge the new statistics into the old one. This will either be the final statistic or we will // reenter the widening loop. for(int j=0;j support; statistic.push_back(support); } statistic[j+1].insert(statistic[j+1].end(),new_statistic1[j].begin(),new_statistic1[j].end()); statistic[j+1].insert(statistic[j+1].end(),new_statistic2[j].begin(),new_statistic2[j].end()); } } } }; //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density class RARX : public BMEF{ }; #endif //TRAGE_H