99 | | //! auxiliary function used in constructor |
100 | | void init(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times); |
101 | | int init(const string &name, int size); |
102 | | public: |
103 | | //! \name Constructors |
104 | | //!@{ |
105 | | |
106 | | //! Full constructor |
107 | | RV(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times) { init(in_names, in_sizes, in_times); } |
108 | | |
109 | | //! Constructor with times=0 |
110 | | RV(const Array<std::string> &in_names, const ivec &in_sizes) { init(in_names, in_sizes, zeros_i(in_names.length())); } |
111 | | |
112 | | //! Constructor with sizes=1, times=0 |
113 | | RV(const Array<std::string> &in_names) { init(in_names, ones_i(in_names.length()), zeros_i(in_names.length())); } |
114 | | |
115 | | //! Constructor of empty RV |
116 | | RV() : dsize(0), len(0), ids(0), times(0) {} |
117 | | //! Constructor of a single RV with given id |
118 | | RV(string name, int sz, int tm = 0); |
119 | | //!@} |
120 | | |
121 | | //! \name Access functions |
122 | | //!@{ |
123 | | |
124 | | //! State output, e.g. for debugging. |
125 | | friend std::ostream &operator<< (std::ostream &os, const RV &rv); |
126 | | |
127 | | int _dsize() const { return dsize; } |
128 | | |
129 | | //! Recount size of the corresponding data vector |
130 | | int countsize() const; |
131 | | ivec cumsizes() const; |
132 | | int length() const {return len;} |
133 | | int id(int at) const {return ids(at);} |
134 | | int size(int at) const { return RV_SIZES(ids(at)); } |
135 | | int time(int at) const { return times(at); } |
136 | | std::string name(int at) const { return RV_NAMES(ids(at)); } |
137 | | void set_time(int at, int time0) { times(at) = time0; } |
138 | | //!@} |
139 | | |
140 | | //TODO why not inline and later?? |
141 | | |
142 | | //! \name Algebra on Random Variables |
143 | | //!@{ |
144 | | |
145 | | //! Find indices of self in another rv, \return ivec of the same size as self. |
146 | | ivec findself(const RV &rv2) const; |
147 | | //! Compare if \c rv2 is identical to this \c RV |
148 | | bool equal(const RV &rv2) const; |
149 | | //! Add (concat) another variable to the current one, \return true if all rv2 were added, false if rv2 is in conflict |
150 | | bool add(const RV &rv2); |
151 | | //! Subtract another variable from the current one |
152 | | RV subt(const RV &rv2) const; |
153 | | //! Select only variables at indices ind |
154 | | RV subselect(const ivec &ind) const; |
155 | | |
156 | | //! Select only variables at indices ind |
157 | | RV operator()(const ivec &ind) const { return subselect(ind); } |
158 | | |
159 | | //! Select from data vector starting at di1 to di2 |
160 | | RV operator()(int di1, int di2) const; |
161 | | |
162 | | //! Shift \c time by delta. |
163 | | void t(int delta); |
164 | | //!@} |
165 | | |
166 | | //!\name Relation to vectors |
167 | | //!@{ |
168 | | |
169 | | //! generate \c str from rv, by expanding sizes TODO to_string.. |
170 | | str tostr() const; |
171 | | //! when this rv is a part of bigger rv, this function returns indices of self in the data vector of the bigger crv. |
172 | | //! Then, data can be copied via: data_of_this = cdata(ind); |
173 | | ivec dataind(const RV &crv) const; |
174 | | //! generate mutual indices when copying data between self and crv. |
175 | | //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) |
176 | | void dataind(const RV &rv2, ivec &selfi, ivec &rv2i) const; |
177 | | //! Minimum time-offset |
178 | | int mint() const {return min(times);} |
179 | | //!@} |
180 | | |
181 | | // TODO aktualizovat dle soucasneho UI |
182 | | /*! \brief UI for class RV (description of data vectors) |
183 | | |
184 | | \code |
185 | | rv = { |
186 | | type = "rv"; //identifier of the description |
187 | | // UNIQUE IDENTIFIER same names = same variable |
188 | | names = ["a", "b", "c", ...]; // which will be used e.g. in loggers |
189 | | |
190 | | //optional arguments |
191 | | sizes = [1, 2, 3, ...]; // (optional) default = ones() |
192 | | times = [-1, -2, 0, ...]; // time shifts with respect to current time (optional) default = zeros() |
193 | | } |
194 | | \endcode |
195 | | */ |
196 | | void from_setting(const Setting &set); |
197 | | |
198 | | // TODO dodelat void to_setting( Setting &set ) const; |
199 | | |
200 | | //! Invalidate all named RVs. Use before initializing any RV instances, with care... |
201 | | static void clear_all(); |
202 | | }; |
203 | | UIREGISTER(RV); |
| 96 | //! auxiliary function used in constructor |
| 97 | void init ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ); |
| 98 | int init ( const string &name, int size ); |
| 99 | public: |
| 100 | //! \name Constructors |
| 101 | //!@{ |
| 102 | |
| 103 | //! Full constructor |
| 104 | RV ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ) { |
| 105 | init ( in_names, in_sizes, in_times ); |
| 106 | } |
| 107 | |
| 108 | //! Constructor with times=0 |
| 109 | RV ( const Array<std::string> &in_names, const ivec &in_sizes ) { |
| 110 | init ( in_names, in_sizes, zeros_i ( in_names.length() ) ); |
| 111 | } |
| 112 | |
| 113 | //! Constructor with sizes=1, times=0 |
| 114 | RV ( const Array<std::string> &in_names ) { |
| 115 | init ( in_names, ones_i ( in_names.length() ), zeros_i ( in_names.length() ) ); |
| 116 | } |
| 117 | |
| 118 | //! Constructor of empty RV |
| 119 | RV() : dsize ( 0 ), len ( 0 ), ids ( 0 ), times ( 0 ) {} |
| 120 | //! Constructor of a single RV with given id |
| 121 | RV ( string name, int sz, int tm = 0 ); |
| 122 | //!@} |
| 123 | |
| 124 | //! \name Access functions |
| 125 | //!@{ |
| 126 | |
| 127 | //! State output, e.g. for debugging. |
| 128 | friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); |
| 129 | |
| 130 | int _dsize() const { |
| 131 | return dsize; |
| 132 | } |
| 133 | |
| 134 | //! Recount size of the corresponding data vector |
| 135 | int countsize() const; |
| 136 | ivec cumsizes() const; |
| 137 | int length() const { |
| 138 | return len; |
| 139 | } |
| 140 | int id ( int at ) const { |
| 141 | return ids ( at ); |
| 142 | } |
| 143 | int size ( int at ) const { |
| 144 | return RV_SIZES ( ids ( at ) ); |
| 145 | } |
| 146 | int time ( int at ) const { |
| 147 | return times ( at ); |
| 148 | } |
| 149 | std::string name ( int at ) const { |
| 150 | return RV_NAMES ( ids ( at ) ); |
| 151 | } |
| 152 | void set_time ( int at, int time0 ) { |
| 153 | times ( at ) = time0; |
| 154 | } |
| 155 | //!@} |
| 156 | |
| 157 | //TODO why not inline and later?? |
| 158 | |
| 159 | //! \name Algebra on Random Variables |
| 160 | //!@{ |
| 161 | |
| 162 | //! Find indices of self in another rv, \return ivec of the same size as self. |
| 163 | ivec findself ( const RV &rv2 ) const; |
| 164 | //! Compare if \c rv2 is identical to this \c RV |
| 165 | bool equal ( const RV &rv2 ) const; |
| 166 | //! Add (concat) another variable to the current one, \return true if all rv2 were added, false if rv2 is in conflict |
| 167 | bool add ( const RV &rv2 ); |
| 168 | //! Subtract another variable from the current one |
| 169 | RV subt ( const RV &rv2 ) const; |
| 170 | //! Select only variables at indices ind |
| 171 | RV subselect ( const ivec &ind ) const; |
| 172 | |
| 173 | //! Select only variables at indices ind |
| 174 | RV operator() ( const ivec &ind ) const { |
| 175 | return subselect ( ind ); |
| 176 | } |
| 177 | |
| 178 | //! Select from data vector starting at di1 to di2 |
| 179 | RV operator() ( int di1, int di2 ) const; |
| 180 | |
| 181 | //! Shift \c time by delta. |
| 182 | void t ( int delta ); |
| 183 | //!@} |
| 184 | |
| 185 | //!\name Relation to vectors |
| 186 | //!@{ |
| 187 | |
| 188 | //! generate \c str from rv, by expanding sizes TODO to_string.. |
| 189 | str tostr() const; |
| 190 | //! when this rv is a part of bigger rv, this function returns indices of self in the data vector of the bigger crv. |
| 191 | //! Then, data can be copied via: data_of_this = cdata(ind); |
| 192 | ivec dataind ( const RV &crv ) const; |
| 193 | //! generate mutual indices when copying data between self and crv. |
| 194 | //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) |
| 195 | void dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const; |
| 196 | //! Minimum time-offset |
| 197 | int mint() const { |
| 198 | return min ( times ); |
| 199 | } |
| 200 | //!@} |
| 201 | |
| 202 | // TODO aktualizovat dle soucasneho UI |
| 203 | /*! \brief UI for class RV (description of data vectors) |
| 204 | |
| 205 | \code |
| 206 | rv = { |
| 207 | type = "rv"; //identifier of the description |
| 208 | // UNIQUE IDENTIFIER same names = same variable |
| 209 | names = ["a", "b", "c", ...]; // which will be used e.g. in loggers |
| 210 | |
| 211 | //optional arguments |
| 212 | sizes = [1, 2, 3, ...]; // (optional) default = ones() |
| 213 | times = [-1, -2, 0, ...]; // time shifts with respect to current time (optional) default = zeros() |
| 214 | } |
| 215 | \endcode |
| 216 | */ |
| 217 | void from_setting ( const Setting &set ); |
| 218 | |
| 219 | // TODO dodelat void to_setting( Setting &set ) const; |
| 220 | |
| 221 | //! Invalidate all named RVs. Use before initializing any RV instances, with care... |
| 222 | static void clear_all(); |
| 223 | }; |
| 224 | UIREGISTER ( RV ); |
237 | | class epdf : public root |
238 | | { |
239 | | protected: |
240 | | //! dimension of the random variable |
241 | | int dim; |
242 | | //! Description of the random variable |
243 | | RV rv; |
244 | | |
245 | | public: |
246 | | /*! \name Constructors |
247 | | Construction of each epdf should support two types of constructors: |
248 | | \li empty constructor, |
249 | | \li copy constructor, |
250 | | |
251 | | The following constructors should be supported for convenience: |
252 | | \li constructor followed by calling \c set_parameters() |
253 | | \li constructor accepting random variables calling \c set_rv() |
254 | | |
255 | | All internal data structures are constructed as empty. Their values (including sizes) will be set by method \c set_parameters(). This way references can be initialized in constructors. |
256 | | @{*/ |
257 | | epdf() : dim(0), rv() {}; |
258 | | epdf(const epdf &e) : dim(e.dim), rv(e.rv) {}; |
259 | | epdf(const RV &rv0):dim(rv0._dsize()) {set_rv(rv0);}; |
260 | | void set_parameters(int dim0) {dim = dim0;} |
261 | | //!@} |
262 | | |
263 | | //! \name Matematical Operations |
264 | | //!@{ |
265 | | |
266 | | //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ |
267 | | virtual vec sample() const {it_error("not implemneted"); return vec(0);}; |
268 | | //! Returns N samples, \f$ [x_1 , x_2 , \ldots \ \f$ from density \f$ f_x(rv)\f$ |
269 | | virtual mat sample_m(int N) const; |
270 | | //! Compute log-probability of argument \c val |
271 | | //! In case the argument is out of suport return -Infinity |
272 | | virtual double evallog(const vec &val) const {it_error("not implemneted"); return 0.0;}; |
273 | | //! Compute log-probability of multiple values argument \c val |
274 | | virtual vec evallog_m(const mat &Val) const { |
275 | | vec x(Val.cols()); |
276 | | for (int i = 0; i < Val.cols(); i++) {x(i) = evallog(Val.get_col(i)) ;} |
277 | | return x; |
278 | | } |
279 | | //! Compute log-probability of multiple values argument \c val |
280 | | virtual vec evallog_m(const Array<vec> &Avec) const { |
281 | | vec x(Avec.size()); |
282 | | for (int i = 0; i < Avec.size(); i++) {x(i) = evallog(Avec(i)) ;} |
283 | | return x; |
284 | | } |
285 | | //! Return conditional density on the given RV, the remaining rvs will be in conditioning |
286 | | virtual mpdf* condition(const RV &rv) const {it_warning("Not implemented"); return NULL;} |
287 | | |
288 | | //! Return marginal density on the given RV, the remainig rvs are intergrated out |
289 | | virtual epdf* marginal(const RV &rv) const {it_warning("Not implemented"); return NULL;} |
290 | | |
291 | | //! return expected value |
292 | | virtual vec mean() const {it_error("not implemneted"); return vec(0);}; |
293 | | |
294 | | //! return expected variance (not covariance!) |
295 | | virtual vec variance() const {it_error("not implemneted"); return vec(0);}; |
296 | | //! Lower and upper bounds of \c percentage % quantile, returns mean-2*sigma as default |
297 | | virtual void qbounds(vec &lb, vec &ub, double percentage = 0.95) const { |
298 | | vec mea = mean(); |
299 | | vec std = sqrt(variance()); |
300 | | lb = mea - 2 * std; |
301 | | ub = mea + 2 * std; |
302 | | }; |
303 | | //!@} |
304 | | |
305 | | //! \name Connection to other classes |
306 | | //! Description of the random quantity via attribute \c rv is optional. |
307 | | //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization |
308 | | //! and \c conditioning \c rv has to be set. NB: |
309 | | //! @{ |
310 | | |
311 | | //!Name its rv |
312 | | void set_rv(const RV &rv0) {rv = rv0; } //it_assert_debug(isnamed(),""); }; |
313 | | //! True if rv is assigned |
314 | | bool isnamed() const {bool b = (dim == rv._dsize()); return b;} |
315 | | //! Return name (fails when isnamed is false) |
316 | | const RV& _rv() const {it_assert_debug(isnamed(), ""); return rv;} |
317 | | //!@} |
318 | | |
319 | | //! \name Access to attributes |
320 | | //! @{ |
321 | | |
322 | | //! Size of the random variable |
323 | | int dimension() const {return dim;} |
324 | | //! Load from structure with elements: |
325 | | //! \code |
326 | | //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable |
327 | | //! // elements of offsprings |
328 | | //! } |
329 | | //! \endcode |
330 | | //!@} |
331 | | void from_setting(const Setting &set){ |
332 | | RV* r = UI::build<RV>(set,"rv"); |
333 | | if (r){ |
334 | | set_rv(*r); |
335 | | delete r; |
336 | | } |
337 | | } |
| 259 | class epdf : public root { |
| 260 | protected: |
| 261 | //! dimension of the random variable |
| 262 | int dim; |
| 263 | //! Description of the random variable |
| 264 | RV rv; |
| 265 | |
| 266 | public: |
| 267 | /*! \name Constructors |
| 268 | Construction of each epdf should support two types of constructors: |
| 269 | \li empty constructor, |
| 270 | \li copy constructor, |
| 271 | |
| 272 | The following constructors should be supported for convenience: |
| 273 | \li constructor followed by calling \c set_parameters() |
| 274 | \li constructor accepting random variables calling \c set_rv() |
| 275 | |
| 276 | All internal data structures are constructed as empty. Their values (including sizes) will be set by method \c set_parameters(). This way references can be initialized in constructors. |
| 277 | @{*/ |
| 278 | epdf() : dim ( 0 ), rv() {}; |
| 279 | epdf ( const epdf &e ) : dim ( e.dim ), rv ( e.rv ) {}; |
| 280 | epdf ( const RV &rv0 ) : dim ( rv0._dsize() ) { |
| 281 | set_rv ( rv0 ); |
| 282 | }; |
| 283 | void set_parameters ( int dim0 ) { |
| 284 | dim = dim0; |
| 285 | } |
| 286 | //!@} |
| 287 | |
| 288 | //! \name Matematical Operations |
| 289 | //!@{ |
| 290 | |
| 291 | //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ |
| 292 | virtual vec sample() const { |
| 293 | it_error ( "not implemneted" ); |
| 294 | return vec ( 0 ); |
| 295 | }; |
| 296 | //! Returns N samples, \f$ [x_1 , x_2 , \ldots \ \f$ from density \f$ f_x(rv)\f$ |
| 297 | virtual mat sample_m ( int N ) const; |
| 298 | //! Compute log-probability of argument \c val |
| 299 | //! In case the argument is out of suport return -Infinity |
| 300 | virtual double evallog ( const vec &val ) const { |
| 301 | it_error ( "not implemneted" ); |
| 302 | return 0.0; |
| 303 | }; |
| 304 | //! Compute log-probability of multiple values argument \c val |
| 305 | virtual vec evallog_m ( const mat &Val ) const { |
| 306 | vec x ( Val.cols() ); |
| 307 | for ( int i = 0; i < Val.cols(); i++ ) { |
| 308 | x ( i ) = evallog ( Val.get_col ( i ) ) ; |
| 309 | } |
| 310 | return x; |
| 311 | } |
| 312 | //! Compute log-probability of multiple values argument \c val |
| 313 | virtual vec evallog_m ( const Array<vec> &Avec ) const { |
| 314 | vec x ( Avec.size() ); |
| 315 | for ( int i = 0; i < Avec.size(); i++ ) { |
| 316 | x ( i ) = evallog ( Avec ( i ) ) ; |
| 317 | } |
| 318 | return x; |
| 319 | } |
| 320 | //! Return conditional density on the given RV, the remaining rvs will be in conditioning |
| 321 | virtual mpdf* condition ( const RV &rv ) const { |
| 322 | it_warning ( "Not implemented" ); |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | //! Return marginal density on the given RV, the remainig rvs are intergrated out |
| 327 | virtual epdf* marginal ( const RV &rv ) const { |
| 328 | it_warning ( "Not implemented" ); |
| 329 | return NULL; |
| 330 | } |
| 331 | |
| 332 | //! return expected value |
| 333 | virtual vec mean() const { |
| 334 | it_error ( "not implemneted" ); |
| 335 | return vec ( 0 ); |
| 336 | }; |
| 337 | |
| 338 | //! return expected variance (not covariance!) |
| 339 | virtual vec variance() const { |
| 340 | it_error ( "not implemneted" ); |
| 341 | return vec ( 0 ); |
| 342 | }; |
| 343 | //! Lower and upper bounds of \c percentage % quantile, returns mean-2*sigma as default |
| 344 | virtual void qbounds ( vec &lb, vec &ub, double percentage = 0.95 ) const { |
| 345 | vec mea = mean(); |
| 346 | vec std = sqrt ( variance() ); |
| 347 | lb = mea - 2 * std; |
| 348 | ub = mea + 2 * std; |
| 349 | }; |
| 350 | //!@} |
| 351 | |
| 352 | //! \name Connection to other classes |
| 353 | //! Description of the random quantity via attribute \c rv is optional. |
| 354 | //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization |
| 355 | //! and \c conditioning \c rv has to be set. NB: |
| 356 | //! @{ |
| 357 | |
| 358 | //!Name its rv |
| 359 | void set_rv ( const RV &rv0 ) { |
| 360 | rv = rv0; |
| 361 | } //it_assert_debug(isnamed(),""); }; |
| 362 | //! True if rv is assigned |
| 363 | bool isnamed() const { |
| 364 | bool b = ( dim == rv._dsize() ); |
| 365 | return b; |
| 366 | } |
| 367 | //! Return name (fails when isnamed is false) |
| 368 | const RV& _rv() const { |
| 369 | it_assert_debug ( isnamed(), "" ); |
| 370 | return rv; |
| 371 | } |
| 372 | //!@} |
| 373 | |
| 374 | //! \name Access to attributes |
| 375 | //! @{ |
| 376 | |
| 377 | //! Size of the random variable |
| 378 | int dimension() const { |
| 379 | return dim; |
| 380 | } |
| 381 | //! Load from structure with elements: |
| 382 | //! \code |
| 383 | //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable |
| 384 | //! // elements of offsprings |
| 385 | //! } |
| 386 | //! \endcode |
| 387 | //!@} |
| 388 | void from_setting ( const Setting &set ) { |
| 389 | RV* r = UI::build<RV> ( set, "rv" ); |
| 390 | if ( r ) { |
| 391 | set_rv ( *r ); |
| 392 | delete r; |
| 393 | } |
| 394 | } |
354 | | //! pointer to internal epdf |
355 | | shared_ptr<epdf> shep; |
356 | | |
357 | | public: |
358 | | //! \name Constructors |
359 | | //! @{ |
360 | | |
361 | | mpdf():dimc(0), rvc() { } |
362 | | |
363 | | mpdf(const mpdf &m):dimc(m.dimc), rvc(m.rvc), shep(m.shep) { } |
364 | | //!@} |
365 | | |
366 | | //! \name Matematical operations |
367 | | //!@{ |
368 | | |
369 | | //! Returns a sample from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv |
370 | | virtual vec samplecond(const vec &cond); |
371 | | |
372 | | //! Returns \param N samples from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv |
373 | | virtual mat samplecond_m(const vec &cond, int N); |
374 | | |
375 | | //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond |
376 | | virtual void condition(const vec &cond) {it_error("Not implemented");}; |
377 | | |
378 | | //! Shortcut for conditioning and evaluation of the internal epdf. In some cases, this operation can be implemented efficiently. |
379 | | virtual double evallogcond(const vec &dt, const vec &cond); |
380 | | |
381 | | //! Matrix version of evallogcond |
382 | | virtual vec evallogcond_m(const mat &Dt, const vec &cond); |
383 | | |
384 | | //! Array<vec> version of evallogcond |
385 | | virtual vec evallogcond_m(const Array<vec> &Dt, const vec &cond); |
386 | | |
387 | | //! \name Access to attributes |
388 | | //! @{ |
389 | | |
390 | | RV _rv() { return shep->_rv(); } |
391 | | RV _rvc() { it_assert_debug(isnamed(), ""); return rvc; } |
392 | | int dimension() { return shep->dimension(); } |
393 | | int dimensionc() { return dimc; } |
394 | | |
395 | | epdf *e() { return shep.get(); } |
396 | | |
397 | | void set_ep(shared_ptr<epdf> ep) { shep = ep; } |
398 | | |
399 | | //! Load from structure with elements: |
400 | | //! \code |
401 | | //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable |
402 | | //! rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition |
403 | | //! // elements of offsprings |
404 | | //! } |
405 | | //! \endcode |
406 | | //!@} |
407 | | void from_setting(const Setting &set); |
408 | | //!@} |
409 | | |
410 | | //! \name Connection to other objects |
411 | | //!@{ |
412 | | void set_rvc(const RV &rvc0) { rvc = rvc0; } |
413 | | void set_rv(const RV &rv0) { shep->set_rv(rv0); } |
414 | | bool isnamed() { return (shep->isnamed()) && (dimc == rvc._dsize()); } |
415 | | //!@} |
| 410 | //! pointer to internal epdf |
| 411 | shared_ptr<epdf> shep; |
| 412 | |
| 413 | public: |
| 414 | //! \name Constructors |
| 415 | //! @{ |
| 416 | |
| 417 | mpdf() : dimc ( 0 ), rvc() { } |
| 418 | |
| 419 | mpdf ( const mpdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), shep ( m.shep ) { } |
| 420 | //!@} |
| 421 | |
| 422 | //! \name Matematical operations |
| 423 | //!@{ |
| 424 | |
| 425 | //! Returns a sample from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv |
| 426 | virtual vec samplecond ( const vec &cond ); |
| 427 | |
| 428 | //! Returns \param N samples from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv |
| 429 | virtual mat samplecond_m ( const vec &cond, int N ); |
| 430 | |
| 431 | //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond |
| 432 | virtual void condition ( const vec &cond ) { |
| 433 | it_error ( "Not implemented" ); |
| 434 | }; |
| 435 | |
| 436 | //! Shortcut for conditioning and evaluation of the internal epdf. In some cases, this operation can be implemented efficiently. |
| 437 | virtual double evallogcond ( const vec &dt, const vec &cond ); |
| 438 | |
| 439 | //! Matrix version of evallogcond |
| 440 | virtual vec evallogcond_m ( const mat &Dt, const vec &cond ); |
| 441 | |
| 442 | //! Array<vec> version of evallogcond |
| 443 | virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ); |
| 444 | |
| 445 | //! \name Access to attributes |
| 446 | //! @{ |
| 447 | |
| 448 | RV _rv() { |
| 449 | return shep->_rv(); |
| 450 | } |
| 451 | RV _rvc() { |
| 452 | it_assert_debug ( isnamed(), "" ); |
| 453 | return rvc; |
| 454 | } |
| 455 | int dimension() { |
| 456 | return shep->dimension(); |
| 457 | } |
| 458 | int dimensionc() { |
| 459 | return dimc; |
| 460 | } |
| 461 | |
| 462 | epdf *e() { |
| 463 | return shep.get(); |
| 464 | } |
| 465 | |
| 466 | void set_ep ( shared_ptr<epdf> ep ) { |
| 467 | shep = ep; |
| 468 | } |
| 469 | |
| 470 | //! Load from structure with elements: |
| 471 | //! \code |
| 472 | //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable |
| 473 | //! rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition |
| 474 | //! // elements of offsprings |
| 475 | //! } |
| 476 | //! \endcode |
| 477 | //!@} |
| 478 | void from_setting ( const Setting &set ); |
| 479 | //!@} |
| 480 | |
| 481 | //! \name Connection to other objects |
| 482 | //!@{ |
| 483 | void set_rvc ( const RV &rvc0 ) { |
| 484 | rvc = rvc0; |
| 485 | } |
| 486 | void set_rv ( const RV &rv0 ) { |
| 487 | shep->set_rv ( rv0 ); |
| 488 | } |
| 489 | bool isnamed() { |
| 490 | return ( shep->isnamed() ) && ( dimc == rvc._dsize() ); |
| 491 | } |
| 492 | //!@} |
443 | | class datalink |
444 | | { |
445 | | protected: |
446 | | //! Remember how long val should be |
447 | | int downsize; |
448 | | |
449 | | //! Remember how long val of "Up" should be |
450 | | int upsize; |
451 | | |
452 | | //! val-to-val link, indices of the upper val |
453 | | ivec v2v_up; |
454 | | |
455 | | public: |
456 | | //! Constructor |
457 | | datalink():downsize(0), upsize(0) { } |
458 | | datalink(const RV &rv, const RV &rv_up) { set_connection(rv, rv_up); } |
459 | | |
460 | | //! set connection, rv must be fully present in rv_up |
461 | | void set_connection(const RV &rv, const RV &rv_up) { |
462 | | downsize = rv._dsize(); |
463 | | upsize = rv_up._dsize(); |
464 | | v2v_up = rv.dataind(rv_up); |
465 | | |
466 | | it_assert_debug(v2v_up.length() == downsize, "rv is not fully in rv_up"); |
467 | | } |
468 | | |
469 | | //! set connection using indices |
470 | | void set_connection(int ds, int us, const ivec &upind) { |
471 | | downsize = ds; |
472 | | upsize = us; |
473 | | v2v_up = upind; |
474 | | |
475 | | it_assert_debug(v2v_up.length() == downsize, "rv is not fully in rv_up"); |
476 | | } |
477 | | |
478 | | //! Get val for myself from val of "Up" |
479 | | vec pushdown(const vec &val_up) { |
480 | | it_assert_debug(upsize == val_up.length(), "Wrong val_up"); |
481 | | return get_vec(val_up, v2v_up); |
482 | | } |
483 | | |
484 | | //! Fill val of "Up" by my pieces |
485 | | void pushup(vec &val_up, const vec &val) { |
486 | | it_assert_debug(downsize == val.length(), "Wrong val"); |
487 | | it_assert_debug(upsize == val_up.length(), "Wrong val_up"); |
488 | | set_subvector(val_up, v2v_up, val); |
489 | | } |
| 520 | class datalink { |
| 521 | protected: |
| 522 | //! Remember how long val should be |
| 523 | int downsize; |
| 524 | |
| 525 | //! Remember how long val of "Up" should be |
| 526 | int upsize; |
| 527 | |
| 528 | //! val-to-val link, indices of the upper val |
| 529 | ivec v2v_up; |
| 530 | |
| 531 | public: |
| 532 | //! Constructor |
| 533 | datalink() : downsize ( 0 ), upsize ( 0 ) { } |
| 534 | datalink ( const RV &rv, const RV &rv_up ) { |
| 535 | set_connection ( rv, rv_up ); |
| 536 | } |
| 537 | |
| 538 | //! set connection, rv must be fully present in rv_up |
| 539 | void set_connection ( const RV &rv, const RV &rv_up ) { |
| 540 | downsize = rv._dsize(); |
| 541 | upsize = rv_up._dsize(); |
| 542 | v2v_up = rv.dataind ( rv_up ); |
| 543 | |
| 544 | it_assert_debug ( v2v_up.length() == downsize, "rv is not fully in rv_up" ); |
| 545 | } |
| 546 | |
| 547 | //! set connection using indices |
| 548 | void set_connection ( int ds, int us, const ivec &upind ) { |
| 549 | downsize = ds; |
| 550 | upsize = us; |
| 551 | v2v_up = upind; |
| 552 | |
| 553 | it_assert_debug ( v2v_up.length() == downsize, "rv is not fully in rv_up" ); |
| 554 | } |
| 555 | |
| 556 | //! Get val for myself from val of "Up" |
| 557 | vec pushdown ( const vec &val_up ) { |
| 558 | it_assert_debug ( upsize == val_up.length(), "Wrong val_up" ); |
| 559 | return get_vec ( val_up, v2v_up ); |
| 560 | } |
| 561 | |
| 562 | //! Fill val of "Up" by my pieces |
| 563 | void pushup ( vec &val_up, const vec &val ) { |
| 564 | it_assert_debug ( downsize == val.length(), "Wrong val" ); |
| 565 | it_assert_debug ( upsize == val_up.length(), "Wrong val_up" ); |
| 566 | set_subvector ( val_up, v2v_up, val ); |
| 567 | } |
493 | | class datalink_m2e: public datalink |
494 | | { |
495 | | protected: |
496 | | //! Remember how long cond should be |
497 | | int condsize; |
498 | | |
499 | | //!upper_val-to-local_cond link, indices of the upper val |
500 | | ivec v2c_up; |
501 | | |
502 | | //!upper_val-to-local_cond link, indices of the local cond |
503 | | ivec v2c_lo; |
504 | | |
505 | | public: |
506 | | //! Constructor |
507 | | datalink_m2e():condsize(0) { } |
508 | | |
509 | | void set_connection(const RV &rv, const RV &rvc, const RV &rv_up) { |
510 | | datalink::set_connection(rv, rv_up); |
511 | | condsize = rvc._dsize(); |
512 | | //establish v2c connection |
513 | | rvc.dataind(rv_up, v2c_lo, v2c_up); |
514 | | } |
515 | | |
516 | | //!Construct condition |
517 | | vec get_cond(const vec &val_up) { |
518 | | vec tmp(condsize); |
519 | | set_subvector(tmp, v2c_lo, val_up(v2c_up)); |
520 | | return tmp; |
521 | | } |
522 | | |
523 | | void pushup_cond(vec &val_up, const vec &val, const vec &cond) { |
524 | | it_assert_debug(downsize == val.length(), "Wrong val"); |
525 | | it_assert_debug(upsize == val_up.length(), "Wrong val_up"); |
526 | | set_subvector(val_up, v2v_up, val); |
527 | | set_subvector(val_up, v2c_up, cond); |
528 | | } |
| 571 | class datalink_m2e: public datalink { |
| 572 | protected: |
| 573 | //! Remember how long cond should be |
| 574 | int condsize; |
| 575 | |
| 576 | //!upper_val-to-local_cond link, indices of the upper val |
| 577 | ivec v2c_up; |
| 578 | |
| 579 | //!upper_val-to-local_cond link, indices of the local cond |
| 580 | ivec v2c_lo; |
| 581 | |
| 582 | public: |
| 583 | //! Constructor |
| 584 | datalink_m2e() : condsize ( 0 ) { } |
| 585 | |
| 586 | void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up ) { |
| 587 | datalink::set_connection ( rv, rv_up ); |
| 588 | condsize = rvc._dsize(); |
| 589 | //establish v2c connection |
| 590 | rvc.dataind ( rv_up, v2c_lo, v2c_up ); |
| 591 | } |
| 592 | |
| 593 | //!Construct condition |
| 594 | vec get_cond ( const vec &val_up ) { |
| 595 | vec tmp ( condsize ); |
| 596 | set_subvector ( tmp, v2c_lo, val_up ( v2c_up ) ); |
| 597 | return tmp; |
| 598 | } |
| 599 | |
| 600 | void pushup_cond ( vec &val_up, const vec &val, const vec &cond ) { |
| 601 | it_assert_debug ( downsize == val.length(), "Wrong val" ); |
| 602 | it_assert_debug ( upsize == val_up.length(), "Wrong val_up" ); |
| 603 | set_subvector ( val_up, v2v_up, val ); |
| 604 | set_subvector ( val_up, v2c_up, cond ); |
| 605 | } |
533 | | class datalink_m2m: public datalink_m2e |
534 | | { |
535 | | protected: |
536 | | //!cond-to-cond link, indices of the upper cond |
537 | | ivec c2c_up; |
538 | | //!cond-to-cond link, indices of the local cond |
539 | | ivec c2c_lo; |
540 | | |
541 | | public: |
542 | | //! Constructor |
543 | | datalink_m2m() {}; |
544 | | void set_connection(const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up) { |
545 | | datalink_m2e::set_connection(rv, rvc, rv_up); |
546 | | //establish c2c connection |
547 | | rvc.dataind(rvc_up, c2c_lo, c2c_up); |
548 | | it_assert_debug(c2c_lo.length() + v2c_lo.length() == condsize, "cond is not fully given"); |
549 | | } |
550 | | |
551 | | //! Get cond for myself from val and cond of "Up" |
552 | | vec get_cond(const vec &val_up, const vec &cond_up) { |
553 | | vec tmp(condsize); |
554 | | set_subvector(tmp, v2c_lo, val_up(v2c_up)); |
555 | | set_subvector(tmp, c2c_lo, cond_up(c2c_up)); |
556 | | return tmp; |
557 | | } |
558 | | //! Fill |
| 610 | class datalink_m2m: public datalink_m2e { |
| 611 | protected: |
| 612 | //!cond-to-cond link, indices of the upper cond |
| 613 | ivec c2c_up; |
| 614 | //!cond-to-cond link, indices of the local cond |
| 615 | ivec c2c_lo; |
| 616 | |
| 617 | public: |
| 618 | //! Constructor |
| 619 | datalink_m2m() {}; |
| 620 | void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) { |
| 621 | datalink_m2e::set_connection ( rv, rvc, rv_up ); |
| 622 | //establish c2c connection |
| 623 | rvc.dataind ( rvc_up, c2c_lo, c2c_up ); |
| 624 | it_assert_debug ( c2c_lo.length() + v2c_lo.length() == condsize, "cond is not fully given" ); |
| 625 | } |
| 626 | |
| 627 | //! Get cond for myself from val and cond of "Up" |
| 628 | vec get_cond ( const vec &val_up, const vec &cond_up ) { |
| 629 | vec tmp ( condsize ); |
| 630 | set_subvector ( tmp, v2c_lo, val_up ( v2c_up ) ); |
| 631 | set_subvector ( tmp, c2c_lo, cond_up ( c2c_up ) ); |
| 632 | return tmp; |
| 633 | } |
| 634 | //! Fill |
567 | | class logger : public root |
568 | | { |
569 | | protected: |
570 | | //! RVs of all logged variables. |
571 | | Array<RV> entries; |
572 | | //! Names of logged quantities, e.g. names of algorithm variants |
573 | | Array<string> names; |
574 | | public: |
575 | | //!Default constructor |
576 | | logger() : entries(0), names(0) {} |
577 | | |
578 | | //! returns an identifier which will be later needed for calling the \c logit() function |
579 | | //! For empty RV it returns -1, this entry will be ignored by \c logit(). |
580 | | virtual int add(const RV &rv, string prefix = "") { |
581 | | int id; |
582 | | if (rv._dsize() > 0) { |
583 | | id = entries.length(); |
584 | | names = concat(names, prefix); // diff |
585 | | entries.set_length(id + 1, true); |
586 | | entries(id) = rv; |
587 | | } |
588 | | else { id = -1;} |
589 | | return id; // identifier of the last entry |
590 | | } |
591 | | |
592 | | //! log this vector |
593 | | virtual void logit(int id, const vec &v) = 0; |
594 | | //! log this double |
595 | | virtual void logit(int id, const double &d) = 0; |
596 | | |
597 | | //! Shifts storage position for another time step. |
598 | | virtual void step() = 0; |
599 | | |
600 | | //! Finalize storing information |
601 | | virtual void finalize() {}; |
602 | | |
603 | | //! Initialize the storage |
604 | | virtual void init() {}; |
| 643 | class logger : public root { |
| 644 | protected: |
| 645 | //! RVs of all logged variables. |
| 646 | Array<RV> entries; |
| 647 | //! Names of logged quantities, e.g. names of algorithm variants |
| 648 | Array<string> names; |
| 649 | public: |
| 650 | //!Default constructor |
| 651 | logger() : entries ( 0 ), names ( 0 ) {} |
| 652 | |
| 653 | //! returns an identifier which will be later needed for calling the \c logit() function |
| 654 | //! For empty RV it returns -1, this entry will be ignored by \c logit(). |
| 655 | virtual int add ( const RV &rv, string prefix = "" ) { |
| 656 | int id; |
| 657 | if ( rv._dsize() > 0 ) { |
| 658 | id = entries.length(); |
| 659 | names = concat ( names, prefix ); // diff |
| 660 | entries.set_length ( id + 1, true ); |
| 661 | entries ( id ) = rv; |
| 662 | } else { |
| 663 | id = -1; |
| 664 | } |
| 665 | return id; // identifier of the last entry |
| 666 | } |
| 667 | |
| 668 | //! log this vector |
| 669 | virtual void logit ( int id, const vec &v ) = 0; |
| 670 | //! log this double |
| 671 | virtual void logit ( int id, const double &d ) = 0; |
| 672 | |
| 673 | //! Shifts storage position for another time step. |
| 674 | virtual void step() = 0; |
| 675 | |
| 676 | //! Finalize storing information |
| 677 | virtual void finalize() {}; |
| 678 | |
| 679 | //! Initialize the storage |
| 680 | virtual void init() {}; |
661 | | class DS : public root |
662 | | { |
663 | | protected: |
664 | | int dtsize; |
665 | | int utsize; |
666 | | //!Description of data returned by \c getdata(). |
667 | | RV Drv; |
668 | | //!Description of data witten by by \c write(). |
669 | | RV Urv; // |
670 | | //! Remember its own index in Logger L |
671 | | int L_dt, L_ut; |
672 | | public: |
673 | | //! default constructors |
674 | | DS() : Drv(), Urv() {}; |
675 | | //! Returns full vector of observed data=[output, input] |
676 | | virtual void getdata(vec &dt) {it_error("abstract class");}; |
677 | | //! Returns data records at indeces. |
678 | | virtual void getdata(vec &dt, const ivec &indeces) {it_error("abstract class");}; |
679 | | //! Accepts action variable and schedule it for application. |
680 | | virtual void write(vec &ut) {it_error("abstract class");}; |
681 | | //! Accepts action variables at specific indeces |
682 | | virtual void write(vec &ut, const ivec &indeces) {it_error("abstract class");}; |
683 | | |
684 | | //! Moves from \f$ t \f$ to \f$ t+1 \f$, i.e. perfroms the actions and reads response of the system. |
685 | | virtual void step() = 0; |
686 | | |
687 | | //! Register DS for logging into logger L |
688 | | virtual void log_add(logger &L) { |
689 | | it_assert_debug(dtsize == Drv._dsize(), ""); |
690 | | it_assert_debug(utsize == Urv._dsize(), ""); |
691 | | |
692 | | L_dt = L.add(Drv, ""); |
693 | | L_ut = L.add(Urv, ""); |
694 | | } |
695 | | //! Register DS for logging into logger L |
696 | | virtual void logit(logger &L) { |
697 | | vec tmp(Drv._dsize() + Urv._dsize()); |
698 | | getdata(tmp); |
699 | | // d is first in getdata |
700 | | L.logit(L_dt, tmp.left(Drv._dsize())); |
701 | | // u follows after d in getdata |
702 | | L.logit(L_ut, tmp.mid(Drv._dsize(), Urv._dsize())); |
703 | | } |
704 | | //!access function |
705 | | virtual RV _drv() const {return concat(Drv, Urv);} |
706 | | //!access function |
707 | | const RV& _urv() const {return Urv;} |
708 | | //! set random rvariables |
709 | | virtual void set_drv(const RV &drv, const RV &urv) { Drv = drv; Urv = urv;} |
| 746 | class DS : public root { |
| 747 | protected: |
| 748 | int dtsize; |
| 749 | int utsize; |
| 750 | //!Description of data returned by \c getdata(). |
| 751 | RV Drv; |
| 752 | //!Description of data witten by by \c write(). |
| 753 | RV Urv; // |
| 754 | //! Remember its own index in Logger L |
| 755 | int L_dt, L_ut; |
| 756 | public: |
| 757 | //! default constructors |
| 758 | DS() : Drv(), Urv() {}; |
| 759 | //! Returns full vector of observed data=[output, input] |
| 760 | virtual void getdata ( vec &dt ) { |
| 761 | it_error ( "abstract class" ); |
| 762 | }; |
| 763 | //! Returns data records at indeces. |
| 764 | virtual void getdata ( vec &dt, const ivec &indeces ) { |
| 765 | it_error ( "abstract class" ); |
| 766 | }; |
| 767 | //! Accepts action variable and schedule it for application. |
| 768 | virtual void write ( vec &ut ) { |
| 769 | it_error ( "abstract class" ); |
| 770 | }; |
| 771 | //! Accepts action variables at specific indeces |
| 772 | virtual void write ( vec &ut, const ivec &indeces ) { |
| 773 | it_error ( "abstract class" ); |
| 774 | }; |
| 775 | |
| 776 | //! Moves from \f$ t \f$ to \f$ t+1 \f$, i.e. perfroms the actions and reads response of the system. |
| 777 | virtual void step() = 0; |
| 778 | |
| 779 | //! Register DS for logging into logger L |
| 780 | virtual void log_add ( logger &L ) { |
| 781 | it_assert_debug ( dtsize == Drv._dsize(), "" ); |
| 782 | it_assert_debug ( utsize == Urv._dsize(), "" ); |
| 783 | |
| 784 | L_dt = L.add ( Drv, "" ); |
| 785 | L_ut = L.add ( Urv, "" ); |
| 786 | } |
| 787 | //! Register DS for logging into logger L |
| 788 | virtual void logit ( logger &L ) { |
| 789 | vec tmp ( Drv._dsize() + Urv._dsize() ); |
| 790 | getdata ( tmp ); |
| 791 | // d is first in getdata |
| 792 | L.logit ( L_dt, tmp.left ( Drv._dsize() ) ); |
| 793 | // u follows after d in getdata |
| 794 | L.logit ( L_ut, tmp.mid ( Drv._dsize(), Urv._dsize() ) ); |
| 795 | } |
| 796 | //!access function |
| 797 | virtual RV _drv() const { |
| 798 | return concat ( Drv, Urv ); |
| 799 | } |
| 800 | //!access function |
| 801 | const RV& _urv() const { |
| 802 | return Urv; |
| 803 | } |
| 804 | //! set random rvariables |
| 805 | virtual void set_drv ( const RV &drv, const RV &urv ) { |
| 806 | Drv = drv; |
| 807 | Urv = urv; |
| 808 | } |
733 | | class BM : public root |
734 | | { |
735 | | protected: |
736 | | //! Random variable of the data (optional) |
737 | | RV drv; |
738 | | //!Logarithm of marginalized data likelihood. |
739 | | double ll; |
740 | | //! If true, the filter will compute likelihood of the data record and store it in \c ll . Set to false if you want to save computational time. |
741 | | bool evalll; |
742 | | public: |
743 | | //! \name Constructors |
744 | | //! @{ |
745 | | |
746 | | BM() : ll(0), evalll(true), LIDs(4), LFlags(4) { |
747 | | LIDs = -1;/*empty IDs*/ |
748 | | LFlags = 0; |
749 | | LFlags(0) = 1;/*log only mean*/ |
750 | | }; |
751 | | BM(const BM &B) : drv(B.drv), ll(B.ll), evalll(B.evalll) {} |
752 | | //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! |
753 | | //! Prototype: \code BM* _copy_() const {return new BM(*this);} \endcode |
754 | | virtual BM* _copy_() const {return NULL;}; |
755 | | //!@} |
756 | | |
757 | | //! \name Mathematical operations |
758 | | //!@{ |
759 | | |
760 | | /*! \brief Incremental Bayes rule |
761 | | @param dt vector of input data |
762 | | */ |
763 | | virtual void bayes(const vec &dt) = 0; |
764 | | //! Batch Bayes rule (columns of Dt are observations) |
765 | | virtual void bayesB(const mat &Dt); |
766 | | //! Evaluates predictive log-likelihood of the given data record |
767 | | //! I.e. marginal likelihood of the data with the posterior integrated out. |
768 | | virtual double logpred(const vec &dt) const {it_error("Not implemented"); return 0.0;} |
769 | | //! Matrix version of logpred |
770 | | vec logpred_m(const mat &dt) const {vec tmp(dt.cols()); for (int i = 0; i < dt.cols(); i++) {tmp(i) = logpred(dt.get_col(i));} return tmp;} |
771 | | |
772 | | //!Constructs a predictive density \f$ f(d_{t+1} |d_{t}, \ldots d_{0}) \f$ |
773 | | virtual epdf* epredictor() const {it_error("Not implemented"); return NULL;}; |
774 | | //!Constructs a conditional density 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t}) |
775 | | virtual mpdf* predictor() const {it_error("Not implemented"); return NULL;}; |
776 | | //!@} |
777 | | |
778 | | //! \name Extension to conditional BM |
779 | | //! This extension is useful e.g. in Marginalized Particle Filter (\ref bdm::MPF). |
780 | | //! Alternatively, it can be used for automated connection to DS when the condition is observed |
781 | | //!@{ |
782 | | |
783 | | //! Name of extension variable |
784 | | RV rvc; |
785 | | //! access function |
786 | | const RV& _rvc() const {return rvc;} |
787 | | |
788 | | //! Substitute \c val for \c rvc. |
789 | | virtual void condition(const vec &val) {it_error("Not implemented!");}; |
790 | | |
791 | | //!@} |
792 | | |
793 | | |
794 | | //! \name Access to attributes |
795 | | //!@{ |
796 | | |
797 | | const RV& _drv() const {return drv;} |
798 | | void set_drv(const RV &rv) {drv = rv;} |
799 | | void set_rv(const RV &rv) {const_cast<epdf&>(posterior()).set_rv(rv);} |
800 | | double _ll() const {return ll;} |
801 | | void set_evalll(bool evl0) {evalll = evl0;} |
802 | | virtual const epdf& posterior() const = 0; |
803 | | virtual const epdf* _e() const = 0; |
804 | | //!@} |
805 | | |
806 | | //! \name Logging of results |
807 | | //!@{ |
808 | | |
809 | | //! Set boolean options from a string, recognized are: "logbounds,logll" |
810 | | virtual void set_options(const string &opt) { |
811 | | LFlags(0) = 1; |
812 | | if (opt.find("logbounds") != string::npos) {LFlags(1) = 1; LFlags(2) = 1;} |
813 | | if (opt.find("logll") != string::npos) {LFlags(3) = 1;} |
814 | | } |
815 | | //! IDs of storages in loggers 4:[1=mean,2=lb,3=ub,4=ll] |
816 | | ivec LIDs; |
817 | | |
818 | | //! Flags for logging - same size as LIDs, each entry correspond to the same in LIDs |
819 | | ivec LFlags; |
820 | | //! Add all logged variables to a logger |
821 | | virtual void log_add(logger &L, const string &name = "") { |
822 | | // internal |
823 | | RV r; |
824 | | if (posterior().isnamed()) {r = posterior()._rv();} |
825 | | else {r = RV("est", posterior().dimension());}; |
826 | | |
827 | | // Add mean value |
828 | | if (LFlags(0)) LIDs(0) = L.add(r, name + "mean_"); |
829 | | if (LFlags(1)) LIDs(1) = L.add(r, name + "lb_"); |
830 | | if (LFlags(2)) LIDs(2) = L.add(r, name + "ub_"); |
831 | | if (LFlags(3)) LIDs(3) = L.add(RV("ll", 1), name); //TODO: "local" RV |
832 | | } |
833 | | virtual void logit(logger &L) { |
834 | | L.logit(LIDs(0), posterior().mean()); |
835 | | if (LFlags(1) || LFlags(2)) { //if one of them is off, its LID==-1 and will not be stored |
836 | | vec ub, lb; |
837 | | posterior().qbounds(lb, ub); |
838 | | L.logit(LIDs(1), lb); |
839 | | L.logit(LIDs(2), ub); |
840 | | } |
841 | | if (LFlags(3)) L.logit(LIDs(3), ll); |
842 | | } |
843 | | //!@} |
| 832 | class BM : public root { |
| 833 | protected: |
| 834 | //! Random variable of the data (optional) |
| 835 | RV drv; |
| 836 | //!Logarithm of marginalized data likelihood. |
| 837 | double ll; |
| 838 | //! If true, the filter will compute likelihood of the data record and store it in \c ll . Set to false if you want to save computational time. |
| 839 | bool evalll; |
| 840 | public: |
| 841 | //! \name Constructors |
| 842 | //! @{ |
| 843 | |
| 844 | BM() : ll ( 0 ), evalll ( true ), LIDs ( 4 ), LFlags ( 4 ) { |
| 845 | LIDs = -1;/*empty IDs*/ |
| 846 | LFlags = 0; |
| 847 | LFlags ( 0 ) = 1;/*log only mean*/ |
| 848 | }; |
| 849 | BM ( const BM &B ) : drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} |
| 850 | //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! |
| 851 | //! Prototype: \code BM* _copy_() const {return new BM(*this);} \endcode |
| 852 | virtual BM* _copy_() const { |
| 853 | return NULL; |
| 854 | }; |
| 855 | //!@} |
| 856 | |
| 857 | //! \name Mathematical operations |
| 858 | //!@{ |
| 859 | |
| 860 | /*! \brief Incremental Bayes rule |
| 861 | @param dt vector of input data |
| 862 | */ |
| 863 | virtual void bayes ( const vec &dt ) = 0; |
| 864 | //! Batch Bayes rule (columns of Dt are observations) |
| 865 | virtual void bayesB ( const mat &Dt ); |
| 866 | //! Evaluates predictive log-likelihood of the given data record |
| 867 | //! I.e. marginal likelihood of the data with the posterior integrated out. |
| 868 | virtual double logpred ( const vec &dt ) const { |
| 869 | it_error ( "Not implemented" ); |
| 870 | return 0.0; |
| 871 | } |
| 872 | //! Matrix version of logpred |
| 873 | vec logpred_m ( const mat &dt ) const { |
| 874 | vec tmp ( dt.cols() ); |
| 875 | for ( int i = 0; i < dt.cols(); i++ ) { |
| 876 | tmp ( i ) = logpred ( dt.get_col ( i ) ); |
| 877 | } |
| 878 | return tmp; |
| 879 | } |
| 880 | |
| 881 | //!Constructs a predictive density \f$ f(d_{t+1} |d_{t}, \ldots d_{0}) \f$ |
| 882 | virtual epdf* epredictor() const { |
| 883 | it_error ( "Not implemented" ); |
| 884 | return NULL; |
| 885 | }; |
| 886 | //!Constructs a conditional density 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t}) |
| 887 | virtual mpdf* predictor() const { |
| 888 | it_error ( "Not implemented" ); |
| 889 | return NULL; |
| 890 | }; |
| 891 | //!@} |
| 892 | |
| 893 | //! \name Extension to conditional BM |
| 894 | //! This extension is useful e.g. in Marginalized Particle Filter (\ref bdm::MPF). |
| 895 | //! Alternatively, it can be used for automated connection to DS when the condition is observed |
| 896 | //!@{ |
| 897 | |
| 898 | //! Name of extension variable |
| 899 | RV rvc; |
| 900 | //! access function |
| 901 | const RV& _rvc() const { |
| 902 | return rvc; |
| 903 | } |
| 904 | |
| 905 | //! Substitute \c val for \c rvc. |
| 906 | virtual void condition ( const vec &val ) { |
| 907 | it_error ( "Not implemented!" ); |
| 908 | }; |
| 909 | |
| 910 | //!@} |
| 911 | |
| 912 | |
| 913 | //! \name Access to attributes |
| 914 | //!@{ |
| 915 | |
| 916 | const RV& _drv() const { |
| 917 | return drv; |
| 918 | } |
| 919 | void set_drv ( const RV &rv ) { |
| 920 | drv = rv; |
| 921 | } |
| 922 | void set_rv ( const RV &rv ) { |
| 923 | const_cast<epdf&> ( posterior() ).set_rv ( rv ); |
| 924 | } |
| 925 | double _ll() const { |
| 926 | return ll; |
| 927 | } |
| 928 | void set_evalll ( bool evl0 ) { |
| 929 | evalll = evl0; |
| 930 | } |
| 931 | virtual const epdf& posterior() const = 0; |
| 932 | virtual const epdf* _e() const = 0; |
| 933 | //!@} |
| 934 | |
| 935 | //! \name Logging of results |
| 936 | //!@{ |
| 937 | |
| 938 | //! Set boolean options from a string, recognized are: "logbounds,logll" |
| 939 | virtual void set_options ( const string &opt ) { |
| 940 | LFlags ( 0 ) = 1; |
| 941 | if ( opt.find ( "logbounds" ) != string::npos ) { |
| 942 | LFlags ( 1 ) = 1; |
| 943 | LFlags ( 2 ) = 1; |
| 944 | } |
| 945 | if ( opt.find ( "logll" ) != string::npos ) { |
| 946 | LFlags ( 3 ) = 1; |
| 947 | } |
| 948 | } |
| 949 | //! IDs of storages in loggers 4:[1=mean,2=lb,3=ub,4=ll] |
| 950 | ivec LIDs; |
| 951 | |
| 952 | //! Flags for logging - same size as LIDs, each entry correspond to the same in LIDs |
| 953 | ivec LFlags; |
| 954 | //! Add all logged variables to a logger |
| 955 | virtual void log_add ( logger &L, const string &name = "" ) { |
| 956 | // internal |
| 957 | RV r; |
| 958 | if ( posterior().isnamed() ) { |
| 959 | r = posterior()._rv(); |
| 960 | } else { |
| 961 | r = RV ( "est", posterior().dimension() ); |
| 962 | }; |
| 963 | |
| 964 | // Add mean value |
| 965 | if ( LFlags ( 0 ) ) LIDs ( 0 ) = L.add ( r, name + "mean_" ); |
| 966 | if ( LFlags ( 1 ) ) LIDs ( 1 ) = L.add ( r, name + "lb_" ); |
| 967 | if ( LFlags ( 2 ) ) LIDs ( 2 ) = L.add ( r, name + "ub_" ); |
| 968 | if ( LFlags ( 3 ) ) LIDs ( 3 ) = L.add ( RV ( "ll", 1 ), name ); //TODO: "local" RV |
| 969 | } |
| 970 | virtual void logit ( logger &L ) { |
| 971 | L.logit ( LIDs ( 0 ), posterior().mean() ); |
| 972 | if ( LFlags ( 1 ) || LFlags ( 2 ) ) { //if one of them is off, its LID==-1 and will not be stored |
| 973 | vec ub, lb; |
| 974 | posterior().qbounds ( lb, ub ); |
| 975 | L.logit ( LIDs ( 1 ), lb ); |
| 976 | L.logit ( LIDs ( 2 ), ub ); |
| 977 | } |
| 978 | if ( LFlags ( 3 ) ) L.logit ( LIDs ( 3 ), ll ); |
| 979 | } |
| 980 | //!@} |