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 | |
---|
21 | namespace bdm { |
---|
22 | |
---|
23 | class support_base: public root { |
---|
24 | protected: |
---|
25 | //! dimension |
---|
26 | int dim; |
---|
27 | //! Number of data points |
---|
28 | int Npoints; |
---|
29 | //! active vector for first_vec and next_vec |
---|
30 | vec actvec; |
---|
31 | //! volume of the active point |
---|
32 | double actvolume; |
---|
33 | public: |
---|
34 | //! default constructor |
---|
35 | support_base() : dim ( 0 ), Npoints ( 0 ) { |
---|
36 | } |
---|
37 | |
---|
38 | //! set the first vector to corner and store result in actvec |
---|
39 | virtual const vec& first_vec() NOT_IMPLEMENTED(empty_vec); |
---|
40 | |
---|
41 | //! Get next active vector, call ONLY after first_vector()! |
---|
42 | virtual const vec& next_vec() NOT_IMPLEMENTED(empty_vec);; |
---|
43 | |
---|
44 | //! return active vector, call ONLY after first_vector()! |
---|
45 | virtual const vec& act_vec() { |
---|
46 | return actvec; |
---|
47 | }; |
---|
48 | //! return active vector, call ONLY after first_vector()! |
---|
49 | virtual const double& act_volume() { |
---|
50 | return actvolume; |
---|
51 | }; |
---|
52 | |
---|
53 | //! Access function |
---|
54 | int points() const { |
---|
55 | return Npoints; |
---|
56 | } |
---|
57 | |
---|
58 | }; |
---|
59 | UIREGISTER ( support_base ); |
---|
60 | |
---|
61 | //! Rectangular support |
---|
62 | //! Support points are located inbetween ranges! For example: |
---|
63 | //! For ranges=[0,1] and gridsizes=[1] the support point is 0.5 |
---|
64 | class rectangular_support: public support_base { |
---|
65 | protected: |
---|
66 | //! Array of boundaries (2D vectors: [begining,end]) for each dimension |
---|
67 | Array<vec> ranges; |
---|
68 | //! Number of support points in each dimension |
---|
69 | ivec gridsizes; |
---|
70 | //! indices of active vector |
---|
71 | vec actvec_ind; |
---|
72 | // |
---|
73 | vec steps; |
---|
74 | public: |
---|
75 | //! default constructor |
---|
76 | rectangular_support() : support_base() { |
---|
77 | } |
---|
78 | |
---|
79 | //! set parameters |
---|
80 | void set_parameters ( const Array<vec> &ranges0, const ivec &gridsize0 ); |
---|
81 | |
---|
82 | //! Internal functio to set temporaries correctly |
---|
83 | void initialize(); |
---|
84 | |
---|
85 | //! return vector at position given by vector of indices |
---|
86 | vec get_vec ( const ivec &inds ); |
---|
87 | |
---|
88 | //! convert dimension indices into linear index, the indexing is in the same way as in \c next_vec() |
---|
89 | long linear_index ( const ivec inds ); |
---|
90 | |
---|
91 | //! set the first vector to corner and store result in actvec |
---|
92 | const vec& first_vec(); |
---|
93 | |
---|
94 | //! Get next active vector, call ONLY after first_vector()! |
---|
95 | const vec& next_vec(); |
---|
96 | |
---|
97 | //! return active vector, call ONLY after first_vector()! |
---|
98 | const vec& act_vec() { |
---|
99 | return actvec; |
---|
100 | }; |
---|
101 | |
---|
102 | //! \todo to je asi navic .. v predkovi! |
---|
103 | ivec nearest_point ( const vec &val ); |
---|
104 | |
---|
105 | //! Access function |
---|
106 | int points() const { |
---|
107 | return Npoints; |
---|
108 | } |
---|
109 | |
---|
110 | //! access function |
---|
111 | //! \todo opet pouze do potomka.. |
---|
112 | const vec& _steps() const { |
---|
113 | return steps; |
---|
114 | } |
---|
115 | |
---|
116 | void from_setting ( const Setting &set ); |
---|
117 | }; |
---|
118 | UIREGISTER ( rectangular_support ); |
---|
119 | |
---|
120 | //! Discrete support with stored support points |
---|
121 | class discrete_support: public support_base { |
---|
122 | protected: |
---|
123 | //! storage of support points |
---|
124 | Array<vec> Spoints; |
---|
125 | //! index in iterators |
---|
126 | int idx; |
---|
127 | //! volumes for each point |
---|
128 | vec volumes; |
---|
129 | public: |
---|
130 | //! Default constructor |
---|
131 | discrete_support() : Spoints ( 0 ), idx ( 0 ), volumes(0) {} |
---|
132 | //! Access function |
---|
133 | int points() const { |
---|
134 | return Spoints.length(); |
---|
135 | } |
---|
136 | //! set the first vector to corner and store result in actvec |
---|
137 | const vec& first_vec() { |
---|
138 | |
---|
139 | bdm_assert_debug ( Spoints.length() > 0, "Empty support" ); |
---|
140 | idx = 0; |
---|
141 | actvolume=volumes(0); |
---|
142 | return Spoints ( idx ); |
---|
143 | } |
---|
144 | //! set next vector after calling first_vec() |
---|
145 | const vec& next_vec() { |
---|
146 | bdm_assert_debug ( Spoints.length() > idx - 1, "Out of support points" ); |
---|
147 | idx++; |
---|
148 | actvolume=volumes(idx); |
---|
149 | actvec = Spoints ( idx ); |
---|
150 | return actvec; |
---|
151 | } |
---|
152 | |
---|
153 | /*! |
---|
154 | \code |
---|
155 | class = "discrete_support"; |
---|
156 | points = ( [1,2..], [2,2..], ...); // list of points |
---|
157 | === OR === |
---|
158 | epdf = {class="epdf_offspring",...}; // epdf rfom which to sample |
---|
159 | npoints = 100; // number of samples |
---|
160 | \endcode |
---|
161 | */ |
---|
162 | void from_setting ( const Setting &set ); |
---|
163 | |
---|
164 | void validate(){ |
---|
165 | bdm_assert(volumes.length()==Spoints.length(),"dimension mismatch"); |
---|
166 | Npoints=Spoints.length(); |
---|
167 | if (Npoints>0) |
---|
168 | dim = Spoints(0).length(); |
---|
169 | } |
---|
170 | //! access function |
---|
171 | Array<vec> & _Spoints() { |
---|
172 | return Spoints; |
---|
173 | } |
---|
174 | }; |
---|
175 | UIREGISTER ( discrete_support ); |
---|
176 | |
---|
177 | //! Function defined by values on a fixed grid and interpolated inbetween them |
---|
178 | class grid_fnc: public fnc { |
---|
179 | protected: |
---|
180 | //! grid - function support |
---|
181 | shared_ptr<support_base> sup; |
---|
182 | //! function values on the grid |
---|
183 | vec values; |
---|
184 | public: |
---|
185 | //! constructor function |
---|
186 | void set_support ( shared_ptr<support_base> &sup0 ) { |
---|
187 | sup = sup0; |
---|
188 | values = zeros ( sup->points() ); |
---|
189 | } |
---|
190 | //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer |
---|
191 | void set_values ( double ( *evalptr ) ( const vec& ) ); |
---|
192 | |
---|
193 | //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer |
---|
194 | void set_values ( const epdf &ep ); |
---|
195 | |
---|
196 | //! get value nearest to the given point |
---|
197 | double nearest_val ( const vec &val ) { |
---|
198 | NOT_IMPLEMENTED(0.0); |
---|
199 | // return values ( sup.linear_index ( sup.nearest_point ( val ) ) ); |
---|
200 | } |
---|
201 | |
---|
202 | double integrate() ; |
---|
203 | |
---|
204 | vec eval ( const vec &val ) { |
---|
205 | return vec_1 ( nearest_val ( val ) ); |
---|
206 | } |
---|
207 | const vec & _values() const { |
---|
208 | return values; |
---|
209 | } |
---|
210 | }; |
---|
211 | UIREGISTER ( grid_fnc ); |
---|
212 | |
---|
213 | //! \brief Piecewise constant pdf on rectangular support |
---|
214 | //! |
---|
215 | //! Each point on the grid represents a centroid around which the density is constant. |
---|
216 | //! This is a trivial point-mass density where all points have the same mass. |
---|
217 | class egrid: public epdf { |
---|
218 | protected: |
---|
219 | //! support of the pdf - grid |
---|
220 | rectangular_support sup; |
---|
221 | //! values at the grid |
---|
222 | vec values; |
---|
223 | public: |
---|
224 | //! we assume that evallog is not called too often otherwise we should cache log(values) |
---|
225 | double evallog ( const vec &val ) { |
---|
226 | return log ( values ( sup.linear_index ( sup.nearest_point ( val ) ) ) ); |
---|
227 | } |
---|
228 | }; |
---|
229 | } |
---|
230 | #endif //DISCR_H |
---|