[556] | 1 | /*! |
---|
| 2 | \file |
---|
| 3 | \brief Probability distributions for discrete support densities |
---|
| 4 | \author Vaclav Smidl. |
---|
| 5 | |
---|
| 6 | ----------------------------------- |
---|
| 7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
| 8 | |
---|
| 9 | Using IT++ for numerical operations |
---|
| 10 | ----------------------------------- |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | #ifndef DISCR_H |
---|
| 14 | #define DISCR_H |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | #include "../shared_ptr.h" |
---|
| 18 | #include "../base/bdmbase.h" |
---|
| 19 | #include "../math/chmat.h" |
---|
| 20 | |
---|
[643] | 21 | namespace bdm { |
---|
[556] | 22 | |
---|
[643] | 23 | //! Rectangular support |
---|
| 24 | //! Support points are located inbetween ranges! For example: |
---|
| 25 | //! For ranges=[0,1] and gridsizes=[1] the support point is 0.5 |
---|
| 26 | class rectangular_support: public root { |
---|
| 27 | protected: |
---|
| 28 | //! Array of boundaries (2D vectors: [begining,end]) for each dimension |
---|
| 29 | Array<vec> ranges; |
---|
| 30 | //! Number of support points in each dimension |
---|
| 31 | ivec gridsizes; |
---|
| 32 | //! dimension |
---|
| 33 | int dim; |
---|
| 34 | //! Number of data points |
---|
| 35 | int Npoints; |
---|
| 36 | //! active vector for first_vec and next_vec |
---|
| 37 | vec actvec; |
---|
[896] | 38 | //! indices of active vector |
---|
[643] | 39 | vec actvec_ind; |
---|
| 40 | //! length of steps in each dimension |
---|
| 41 | vec steps; |
---|
| 42 | public: |
---|
| 43 | //! default constructor |
---|
| 44 | rectangular_support() : dim ( 0 ), Npoints ( 0 ) { |
---|
| 45 | } |
---|
[556] | 46 | |
---|
[643] | 47 | //! set parameters |
---|
| 48 | void set_parameters ( const Array<vec> &ranges0, const ivec &gridsize0 ); |
---|
[558] | 49 | |
---|
[643] | 50 | //! Internal functio to set temporaries correctly |
---|
| 51 | void initialize(); |
---|
[558] | 52 | |
---|
[896] | 53 | //! return vector at position given by vector of indices |
---|
[643] | 54 | vec get_vec ( const ivec &inds ); |
---|
| 55 | |
---|
[896] | 56 | //! convert dimension indices into linear index, the indexing is in the same way as in \c next_vec() |
---|
[643] | 57 | long linear_index ( const ivec inds ); |
---|
| 58 | |
---|
| 59 | //! set the first vector to corner and store result in actvec |
---|
| 60 | const vec& first_vec(); |
---|
| 61 | |
---|
| 62 | //! Get next active vector, call ONLY after first_vector()! |
---|
| 63 | const vec& next_vec(); |
---|
[737] | 64 | |
---|
[730] | 65 | //! return active vector, call ONLY after first_vector()! |
---|
[737] | 66 | const vec& act_vec() { |
---|
| 67 | return actvec; |
---|
| 68 | }; |
---|
| 69 | |
---|
[643] | 70 | //! \todo to je asi navic .. v predkovi! |
---|
| 71 | ivec nearest_point ( const vec &val ); |
---|
| 72 | |
---|
| 73 | //! Access function |
---|
| 74 | int points() const { |
---|
| 75 | return Npoints; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | //! access function |
---|
| 79 | //! \todo opet pouze do potomka.. |
---|
| 80 | const vec& _steps() const { |
---|
| 81 | return steps; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | void from_setting ( const Setting &set ); |
---|
| 85 | }; |
---|
| 86 | UIREGISTER ( rectangular_support ); |
---|
| 87 | |
---|
| 88 | //! Discrete support with stored support points |
---|
| 89 | class discrete_support: public root { |
---|
| 90 | protected: |
---|
| 91 | //! storage of support points |
---|
| 92 | Array<vec> Spoints; |
---|
| 93 | //! index in iterators |
---|
| 94 | int idx; |
---|
| 95 | public: |
---|
| 96 | //! Default constructor |
---|
| 97 | discrete_support() : Spoints ( 0 ), idx ( 0 ) {} |
---|
| 98 | //! Access function |
---|
| 99 | int points() const { |
---|
| 100 | return Spoints.length(); |
---|
| 101 | } |
---|
| 102 | //! set the first vector to corner and store result in actvec |
---|
| 103 | const vec& first_vec() { |
---|
| 104 | bdm_assert_debug ( Spoints.length() > 0, "Empty support" ); |
---|
| 105 | idx = 0; |
---|
| 106 | return Spoints ( idx ); |
---|
| 107 | } |
---|
| 108 | //! set next vector after calling first_vec() |
---|
| 109 | const vec& next_vec() { |
---|
| 110 | bdm_assert_debug ( Spoints.length() > idx - 1, "Out of support points" ); |
---|
| 111 | return Spoints ( ++idx ); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /*! |
---|
| 115 | \code |
---|
| 116 | class = "discrete_support"; |
---|
| 117 | points = ( [1,2..], [2,2..], ...); // list of points |
---|
| 118 | === OR === |
---|
| 119 | epdf = {class="epdf_offspring",...}; // epdf rfom which to sample |
---|
| 120 | npoints = 100; // number of samples |
---|
| 121 | \endcode |
---|
| 122 | */ |
---|
| 123 | void from_setting ( const Setting &set ); |
---|
| 124 | |
---|
| 125 | //! access function |
---|
| 126 | Array<vec> & _Spoints() { |
---|
| 127 | return Spoints; |
---|
| 128 | } |
---|
| 129 | }; |
---|
| 130 | UIREGISTER ( discrete_support ); |
---|
| 131 | |
---|
[660] | 132 | //! Function defined by values on a fixed grid and interpolated inbetween them |
---|
[643] | 133 | class grid_fnc: public fnc { |
---|
| 134 | protected: |
---|
[660] | 135 | //! grid - function support |
---|
[643] | 136 | rectangular_support sup; |
---|
[660] | 137 | //! function values on the grid |
---|
[643] | 138 | vec values; |
---|
| 139 | public: |
---|
| 140 | //! constructor function |
---|
| 141 | void set_support ( rectangular_support &sup0 ) { |
---|
| 142 | sup = sup0; |
---|
| 143 | values = zeros ( sup.points() ); |
---|
| 144 | } |
---|
| 145 | //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer |
---|
| 146 | void set_values ( double ( *evalptr ) ( const vec& ) ); |
---|
[737] | 147 | |
---|
[730] | 148 | //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer |
---|
[737] | 149 | void set_values ( const epdf &ep ); |
---|
| 150 | |
---|
[643] | 151 | //! get value nearest to the given point |
---|
| 152 | double nearest_val ( const vec &val ) { |
---|
| 153 | return values ( sup.linear_index ( sup.nearest_point ( val ) ) ); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | vec eval ( const vec &val ) { |
---|
| 157 | return vec_1 ( nearest_val ( val ) ); |
---|
| 158 | } |
---|
[737] | 159 | const vec & _values() const { |
---|
| 160 | return values; |
---|
| 161 | } |
---|
[643] | 162 | }; |
---|
| 163 | UIREGISTER ( grid_fnc ); |
---|
| 164 | |
---|
| 165 | //! Piecewise constant pdf on rectangular support |
---|
| 166 | //! Each point on the grid represents a centroid around which the density is constant. |
---|
| 167 | //! This is a trivial point-mass density where all points have the same mass. |
---|
| 168 | class egrid: public epdf { |
---|
| 169 | protected: |
---|
[660] | 170 | //! support of the pdf - grid |
---|
[643] | 171 | rectangular_support sup; |
---|
[660] | 172 | //! values at the grid |
---|
[643] | 173 | vec values; |
---|
| 174 | public: |
---|
| 175 | //! we assume that evallog is not called too often otherwise we should cache log(values) |
---|
| 176 | double evallog ( const vec &val ) { |
---|
| 177 | return log ( values ( sup.linear_index ( sup.nearest_point ( val ) ) ) ); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | }; |
---|
[556] | 181 | } |
---|
| 182 | #endif //DISCR_H |
---|