88 | | class RV : public root |
89 | | { |
90 | | protected: |
91 | | //! size of the data vector |
92 | | int dsize; |
93 | | //! number of individual rvs |
94 | | int len; |
95 | | //! Vector of unique IDs |
96 | | ivec ids; |
97 | | //! Vector of shifts from current time |
98 | | ivec times; |
99 | | |
100 | | private: |
101 | | enum enum_dummy {dummy}; |
102 | | //! auxiliary function used in constructor |
103 | | void init (const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times); |
104 | | int init (const string &name, int size); |
105 | | //! Private constructor from IDs, potentially dangerous since all ids must be valid! |
106 | | //! dummy is there to prevent confusion with RV(" string"); |
107 | | explicit RV (const ivec &ids0, enum_dummy dum): dsize(0), len(ids0.length()), ids(ids0), times(zeros_i(ids0.length())) { |
108 | | dsize = countsize(); |
109 | | } |
110 | | public: |
111 | | //! \name Constructors |
112 | | //!@{ |
113 | | |
114 | | //! Full constructor |
115 | | RV (const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times) { |
116 | | init (in_names, in_sizes, in_times); |
117 | | } |
118 | | |
119 | | //! Constructor with times=0 |
120 | | RV (const Array<std::string> &in_names, const ivec &in_sizes) { |
121 | | init (in_names, in_sizes, zeros_i (in_names.length())); |
122 | | } |
123 | | |
124 | | //! Constructor with sizes=1, times=0 |
125 | | RV (const Array<std::string> &in_names) { |
126 | | init (in_names, ones_i (in_names.length()), zeros_i (in_names.length())); |
127 | | } |
128 | | |
129 | | //! Constructor of empty RV |
130 | | RV() : dsize (0), len (0), ids (0), times (0) {} |
131 | | |
132 | | //! Constructor of a single RV with given id |
133 | | RV (string name, int sz, int tm = 0); |
134 | | |
135 | | // compiler-generated copy constructor is used |
136 | | //!@} |
137 | | |
138 | | //! \name Access functions |
139 | | //!@{ |
140 | | |
141 | | //! State output, e.g. for debugging. |
142 | | friend std::ostream &operator<< (std::ostream &os, const RV &rv); |
143 | | |
144 | | int _dsize() const { |
145 | | return dsize; |
146 | | } |
147 | | |
148 | | //! access function |
149 | | const ivec& _ids() const { return ids;} |
150 | | |
151 | | //! Recount size of the corresponding data vector |
152 | | int countsize() const; |
153 | | ivec cumsizes() const; |
154 | | int length() const { |
155 | | return len; |
156 | | } |
157 | | int id (int at) const { |
158 | | return ids (at); |
159 | | } |
160 | | int size (int at) const { |
161 | | return RV_SIZES (ids (at)); |
162 | | } |
163 | | int time (int at) const { |
164 | | return times (at); |
165 | | } |
166 | | std::string name (int at) const { |
167 | | return RV_NAMES (ids (at)); |
168 | | } |
169 | | void set_time (int at, int time0) { |
170 | | times (at) = time0; |
171 | | } |
172 | | //!@} |
173 | | |
174 | | //! \name Algebra on Random Variables |
175 | | //!@{ |
176 | | |
177 | | //! Find indices of self in another rv, \return ivec of the same size as self. |
178 | | ivec findself (const RV &rv2) const; |
179 | | //! Find indices of self in another rv, ignore time, \return ivec of the same size as self. |
180 | | ivec findself_ids (const RV &rv2) const; |
181 | | //! Compare if \c rv2 is identical to this \c RV |
182 | | bool equal (const RV &rv2) const; |
183 | | //! Add (concat) another variable to the current one, \return true if all rv2 were added, false if rv2 is in conflict |
184 | | bool add (const RV &rv2); |
185 | | //! Subtract another variable from the current one |
186 | | RV subt (const RV &rv2) const; |
187 | | //! Select only variables at indices ind |
188 | | RV subselect (const ivec &ind) const; |
189 | | |
190 | | //! Select only variables at indices ind |
191 | | RV operator() (const ivec &ind) const { |
192 | | return subselect (ind); |
193 | | } |
194 | | |
195 | | //! Select from data vector starting at di1 to di2 |
196 | | RV operator() (int di1, int di2) const; |
197 | | |
198 | | //! Shift \c time by delta. |
199 | | void t (int delta); |
200 | | //!@} |
201 | | |
202 | | //! @{ \name Auxiliary functions |
203 | | //! returns rvs with time set to 0 and removed duplicates |
204 | | RV remove_time() const { return RV(unique(ids), dummy); } |
205 | | //! return rvs with expanded delayes and sorted in the order of: \f$ [ rv_{0}, rv_{-1}, rv_{ |
206 | | RV expand_delayes() const { |
207 | | RV rvt=this->remove_time(); //rv at t=0 |
208 | | RV tmp = rvt; |
209 | | int td = mint(); |
210 | | for ( int i = -1; i >= td; i-- ) { |
| 79 | class RV : public root { |
| 80 | private: |
| 81 | typedef std::map<string, int> str2int_map; |
| 82 | |
| 83 | //! Internal global variable storing sizes of RVs |
| 84 | static ivec SIZES; |
| 85 | //! Internal global variable storing names of RVs |
| 86 | static Array<string> NAMES; |
| 87 | |
| 88 | //! TODO |
| 89 | const static int BUFFER_STEP; |
| 90 | |
| 91 | //! TODO |
| 92 | static str2int_map MAP; |
| 93 | |
| 94 | public: |
| 95 | |
| 96 | protected: |
| 97 | //! size of the data vector |
| 98 | int dsize; |
| 99 | //! number of individual rvs |
| 100 | int len; |
| 101 | //! Vector of unique IDs |
| 102 | ivec ids; |
| 103 | //! Vector of shifts from current time |
| 104 | ivec times; |
| 105 | |
| 106 | private: |
| 107 | enum enum_dummy {dummy}; |
| 108 | |
| 109 | //! auxiliary function used in constructor |
| 110 | void init ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ); |
| 111 | int init ( const string &name, int size ); |
| 112 | //! Private constructor from IDs, potentially dangerous since all ids must be valid! |
| 113 | //! dummy is there to prevent confusion with RV(" string"); |
| 114 | explicit RV ( const ivec &ids0, enum_dummy dum ) : dsize ( 0 ), len ( ids0.length() ), ids ( ids0 ), times ( zeros_i ( ids0.length() ) ) { |
| 115 | dsize = countsize(); |
| 116 | } |
| 117 | public: |
| 118 | //! \name Constructors |
| 119 | //!@{ |
| 120 | |
| 121 | //! Full constructor |
| 122 | RV ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ) { |
| 123 | init ( in_names, in_sizes, in_times ); |
| 124 | } |
| 125 | |
| 126 | //! Constructor with times=0 |
| 127 | RV ( const Array<std::string> &in_names, const ivec &in_sizes ) { |
| 128 | init ( in_names, in_sizes, zeros_i ( in_names.length() ) ); |
| 129 | } |
| 130 | |
| 131 | //! Constructor with sizes=1, times=0 |
| 132 | RV ( const Array<std::string> &in_names ) { |
| 133 | init ( in_names, ones_i ( in_names.length() ), zeros_i ( in_names.length() ) ); |
| 134 | } |
| 135 | |
| 136 | //! Constructor of empty RV |
| 137 | RV() : dsize ( 0 ), len ( 0 ), ids ( 0 ), times ( 0 ) {} |
| 138 | |
| 139 | //! Constructor of a single RV with given id |
| 140 | RV ( string name, int sz, int tm = 0 ); |
| 141 | |
| 142 | // compiler-generated copy constructor is used |
| 143 | //!@} |
| 144 | |
| 145 | //! \name Access functions |
| 146 | //!@{ |
| 147 | |
| 148 | //! State output, e.g. for debugging. |
| 149 | friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); |
| 150 | |
| 151 | int _dsize() const { |
| 152 | return dsize; |
| 153 | } |
| 154 | |
| 155 | //! access function |
| 156 | const ivec& _ids() const { |
| 157 | return ids; |
| 158 | } |
| 159 | |
| 160 | //! Recount size of the corresponding data vector |
| 161 | int countsize() const; |
| 162 | ivec cumsizes() const; |
| 163 | int length() const { |
| 164 | return len; |
| 165 | } |
| 166 | int id ( int at ) const { |
| 167 | return ids ( at ); |
| 168 | } |
| 169 | int size ( int at ) const { |
| 170 | return SIZES ( ids ( at ) ); |
| 171 | } |
| 172 | int time ( int at ) const { |
| 173 | return times ( at ); |
| 174 | } |
| 175 | std::string name ( int at ) const { |
| 176 | return NAMES ( ids ( at ) ); |
| 177 | } |
| 178 | void set_time ( int at, int time0 ) { |
| 179 | times ( at ) = time0; |
| 180 | } |
| 181 | //!@} |
| 182 | |
| 183 | //! \name Algebra on Random Variables |
| 184 | //!@{ |
| 185 | |
| 186 | //! Find indices of self in another rv, \return ivec of the same size as self. |
| 187 | ivec findself ( const RV &rv2 ) const; |
| 188 | //! Find indices of self in another rv, ignore time, \return ivec of the same size as self. |
| 189 | ivec findself_ids ( const RV &rv2 ) const; |
| 190 | //! Compare if \c rv2 is identical to this \c RV |
| 191 | bool equal ( const RV &rv2 ) const; |
| 192 | //! Add (concat) another variable to the current one, \return true if all rv2 were added, false if rv2 is in conflict |
| 193 | bool add ( const RV &rv2 ); |
| 194 | //! Subtract another variable from the current one |
| 195 | RV subt ( const RV &rv2 ) const; |
| 196 | //! Select only variables at indices ind |
| 197 | RV subselect ( const ivec &ind ) const; |
| 198 | |
| 199 | //! Select only variables at indices ind |
| 200 | RV operator() ( const ivec &ind ) const { |
| 201 | return subselect ( ind ); |
| 202 | } |
| 203 | |
| 204 | //! Select from data vector starting at di1 to di2 |
| 205 | RV operator() ( int di1, int di2 ) const; |
| 206 | |
| 207 | //! Shift \c time by delta. |
| 208 | void t ( int delta ); |
| 209 | //!@} |
| 210 | |
| 211 | //! @{ \name Auxiliary functions |
| 212 | //! returns rvs with time set to 0 and removed duplicates |
| 213 | RV remove_time() const { |
| 214 | return RV ( unique ( ids ), dummy ); |
| 215 | } |
| 216 | //! return rvs with expanded delayes and sorted in the order of: \f$ [ rv_{0}, rv_{-1}, rv_{ |
| 217 | RV expand_delayes() const { |
| 218 | RV rvt = this->remove_time(); //rv at t=0 |
| 219 | RV tmp = rvt; |
| 220 | int td = mint(); |
| 221 | for ( int i = -1; i >= td; i-- ) { |
216 | | //!@} |
217 | | |
218 | | //!\name Relation to vectors |
219 | | //!@{ |
220 | | |
221 | | //! generate \c str from rv, by expanding sizes |
222 | | str tostr() const; |
223 | | //! when this rv is a part of bigger rv, this function returns indices of self in the data vector of the bigger crv. |
224 | | //! Then, data can be copied via: data_of_this = cdata(ind); |
225 | | ivec dataind (const RV &crv) const; |
226 | | //! same as dataind but this time crv should not be complete supperset of rv. |
227 | | ivec dataind_part (const RV &crv) const; |
228 | | //! generate mutual indices when copying data between self and crv. |
229 | | //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) |
230 | | void dataind (const RV &rv2, ivec &selfi, ivec &rv2i) const; |
231 | | //! Minimum time-offset |
232 | | int mint() const { |
233 | | return min (times); |
| 225 | return tmp; |
| 226 | } |
| 227 | //!@} |
| 228 | |
| 229 | //!\name Relation to vectors |
| 230 | //!@{ |
| 231 | |
| 232 | //! generate \c str from rv, by expanding sizes |
| 233 | str tostr() const; |
| 234 | //! when this rv is a part of bigger rv, this function returns indices of self in the data vector of the bigger crv. |
| 235 | //! Then, data can be copied via: data_of_this = cdata(ind); |
| 236 | ivec dataind ( const RV &crv ) const; |
| 237 | //! same as dataind but this time crv should not be complete supperset of rv. |
| 238 | ivec dataind_part ( const RV &crv ) const; |
| 239 | //! generate mutual indices when copying data between self and crv. |
| 240 | //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) |
| 241 | void dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const; |
| 242 | //! Minimum time-offset |
| 243 | int mint() const { |
| 244 | return min ( times ); |
| 245 | } |
| 246 | //!@} |
| 247 | |
| 248 | /*! \brief UI for class RV (description of data vectors) |
| 249 | |
| 250 | \code |
| 251 | rv = { |
| 252 | class = "RV"; // class name |
| 253 | // UNIQUE IDENTIFIER same names = same variable |
| 254 | names = ( "a", "b", "c", ...); // which will be used e.g. in loggers |
| 255 | |
| 256 | //optional arguments |
| 257 | sizes = [1, 2, 3, ...]; // (optional) default = ones() |
| 258 | times = [-1, -2, 0, ...]; // time shifts with respect to current time (optional) default = zeros() |
| 259 | } |
| 260 | \endcode |
| 261 | */ |
| 262 | void from_setting ( const Setting &set ); |
| 263 | |
| 264 | // TODO dodelat void to_setting( Setting &set ) const; |
| 265 | |
| 266 | //! Invalidate all named RVs. Use before initializing any RV instances, with care... |
| 267 | static void clear_all(); |
| 268 | }; |
| 269 | UIREGISTER ( RV ); |
| 270 | SHAREDPTR ( RV ); |
| 271 | |
| 272 | //! Concat two random variables |
| 273 | RV concat ( const RV &rv1, const RV &rv2 ); |
| 274 | |
| 275 | //! Class representing function \f$f(x)\f$ of variable \f$x\f$ represented by \c rv |
| 276 | class fnc : public root { |
| 277 | protected: |
| 278 | //! Length of the output vector |
| 279 | int dimy; |
| 280 | public: |
| 281 | //!default constructor |
| 282 | fnc() {}; |
| 283 | //! function evaluates numerical value of \f$f(x)\f$ at \f$x=\f$ \c cond |
| 284 | virtual vec eval ( const vec &cond ) { |
| 285 | return vec ( 0 ); |
| 286 | }; |
| 287 | |
| 288 | //! function substitutes given value into an appropriate position |
| 289 | virtual void condition ( const vec &val ) {}; |
| 290 | |
| 291 | //! access function |
| 292 | int dimension() const { |
| 293 | return dimy; |
| 294 | } |
| 295 | }; |
| 296 | |
| 297 | class mpdf; |
| 298 | |
| 299 | //! Probability density function with numerical statistics, e.g. posterior density. |
| 300 | |
| 301 | class epdf : public root { |
| 302 | protected: |
| 303 | //! dimension of the random variable |
| 304 | int dim; |
| 305 | //! Description of the random variable |
| 306 | RV rv; |
| 307 | |
| 308 | public: |
| 309 | /*! \name Constructors |
| 310 | Construction of each epdf should support two types of constructors: |
| 311 | \li empty constructor, |
| 312 | \li copy constructor, |
| 313 | |
| 314 | The following constructors should be supported for convenience: |
| 315 | \li constructor followed by calling \c set_parameters() |
| 316 | \li constructor accepting random variables calling \c set_rv() |
| 317 | |
| 318 | 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. |
| 319 | @{*/ |
| 320 | epdf() : dim ( 0 ), rv() {}; |
| 321 | epdf ( const epdf &e ) : dim ( e.dim ), rv ( e.rv ) {}; |
| 322 | epdf ( const RV &rv0 ) : dim ( rv0._dsize() ) { |
| 323 | set_rv ( rv0 ); |
| 324 | }; |
| 325 | void set_parameters ( int dim0 ) { |
| 326 | dim = dim0; |
| 327 | } |
| 328 | //!@} |
| 329 | |
| 330 | //! \name Matematical Operations |
| 331 | //!@{ |
| 332 | |
| 333 | //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ |
| 334 | virtual vec sample() const { |
| 335 | bdm_error ( "not implemented" ); |
| 336 | return vec(); |
| 337 | } |
| 338 | |
| 339 | //! Returns N samples, \f$ [x_1 , x_2 , \ldots \ \f$ from density \f$ f_x(rv)\f$ |
| 340 | virtual mat sample_m ( int N ) const; |
| 341 | |
| 342 | //! Compute log-probability of argument \c val |
| 343 | //! In case the argument is out of suport return -Infinity |
| 344 | virtual double evallog ( const vec &val ) const { |
| 345 | bdm_error ( "not implemented" ); |
| 346 | return 0.0; |
| 347 | } |
| 348 | |
| 349 | //! Compute log-probability of multiple values argument \c val |
| 350 | virtual vec evallog_m ( const mat &Val ) const; |
| 351 | |
| 352 | //! Compute log-probability of multiple values argument \c val |
| 353 | virtual vec evallog_m ( const Array<vec> &Avec ) const; |
| 354 | |
| 355 | //! Return conditional density on the given RV, the remaining rvs will be in conditioning |
| 356 | virtual shared_ptr<mpdf> condition ( const RV &rv ) const; |
| 357 | |
| 358 | //! Return marginal density on the given RV, the remainig rvs are intergrated out |
| 359 | virtual shared_ptr<epdf> marginal ( const RV &rv ) const; |
| 360 | |
| 361 | //! return expected value |
| 362 | virtual vec mean() const { |
| 363 | bdm_error ( "not implemneted" ); |
| 364 | return vec(); |
| 365 | } |
| 366 | |
| 367 | //! return expected variance (not covariance!) |
| 368 | virtual vec variance() const { |
| 369 | bdm_error ( "not implemneted" ); |
| 370 | return vec(); |
| 371 | } |
| 372 | |
| 373 | //! Lower and upper bounds of \c percentage % quantile, returns mean-2*sigma as default |
| 374 | virtual void qbounds ( vec &lb, vec &ub, double percentage = 0.95 ) const { |
| 375 | vec mea = mean(); |
| 376 | vec std = sqrt ( variance() ); |
| 377 | lb = mea - 2 * std; |
| 378 | ub = mea + 2 * std; |
| 379 | }; |
| 380 | //!@} |
| 381 | |
| 382 | //! \name Connection to other classes |
| 383 | //! Description of the random quantity via attribute \c rv is optional. |
| 384 | //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization |
| 385 | //! and \c conditioning \c rv has to be set. NB: |
| 386 | //! @{ |
| 387 | |
| 388 | //!Name its rv |
| 389 | void set_rv ( const RV &rv0 ) { |
| 390 | rv = rv0; |
| 391 | } |
| 392 | |
| 393 | //! True if rv is assigned |
| 394 | bool isnamed() const { |
| 395 | bool b = ( dim == rv._dsize() ); |
| 396 | return b; |
| 397 | } |
| 398 | |
| 399 | //! Return name (fails when isnamed is false) |
| 400 | const RV& _rv() const { |
| 401 | bdm_assert_debug ( isnamed(), "" ); |
| 402 | return rv; |
| 403 | } |
| 404 | //!@} |
| 405 | |
| 406 | //! \name Access to attributes |
| 407 | //! @{ |
| 408 | |
| 409 | //! Size of the random variable |
| 410 | int dimension() const { |
| 411 | return dim; |
| 412 | } |
| 413 | //! Load from structure with elements: |
| 414 | //! \code |
| 415 | //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable |
| 416 | //! // elements of offsprings |
| 417 | //! } |
| 418 | //! \endcode |
| 419 | //!@} |
| 420 | void from_setting ( const Setting &set ) { |
| 421 | shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); |
| 422 | if ( r ) { |
| 423 | set_rv ( *r ); |
235 | | //!@} |
236 | | |
237 | | /*! \brief UI for class RV (description of data vectors) |
238 | | |
239 | | \code |
240 | | rv = { |
241 | | class = "RV"; // class name |
242 | | // UNIQUE IDENTIFIER same names = same variable |
243 | | names = ( "a", "b", "c", ...); // which will be used e.g. in loggers |
244 | | |
245 | | //optional arguments |
246 | | sizes = [1, 2, 3, ...]; // (optional) default = ones() |
247 | | times = [-1, -2, 0, ...]; // time shifts with respect to current time (optional) default = zeros() |
| 425 | } |
| 426 | |
| 427 | }; |
| 428 | SHAREDPTR ( epdf ); |
| 429 | |
| 430 | //! Conditional probability density, e.g. modeling \f$ f( x | y) \f$, where \f$ x \f$ is random variable, \c rv, and \f$ y \f$ is conditioning variable, \c rvc. |
| 431 | class mpdf : public root { |
| 432 | protected: |
| 433 | //!dimension of the condition |
| 434 | int dimc; |
| 435 | //! random variable in condition |
| 436 | RV rvc; |
| 437 | private: |
| 438 | //! internal epdf, used only as cache to avoid virtual calls of \c _rv() and \c _dimension() |
| 439 | epdf* ep; |
| 440 | |
| 441 | protected: |
| 442 | //! set internal pointer \c ep to point to given \c iepdf |
| 443 | void set_ep ( epdf &iepdf ) { |
| 444 | ep = &iepdf; |
| 445 | } |
| 446 | //! set internal pointer \c ep to point to given \c iepdf |
| 447 | void set_ep ( epdf *iepdfp ) { |
| 448 | ep = iepdfp; |
| 449 | } |
| 450 | |
| 451 | public: |
| 452 | //! \name Constructors |
| 453 | //! @{ |
| 454 | |
| 455 | mpdf() : dimc ( 0 ), rvc(), ep ( NULL ) { } |
| 456 | |
| 457 | mpdf ( const mpdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), ep ( m.ep ) { } |
| 458 | //!@} |
| 459 | |
| 460 | //! \name Matematical operations |
| 461 | //!@{ |
| 462 | |
| 463 | //! 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 |
| 464 | virtual vec samplecond ( const vec &cond ) { |
| 465 | bdm_error ( "Not implemented" ); |
| 466 | return vec(); |
| 467 | } |
| 468 | |
| 469 | //! 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 |
| 470 | virtual mat samplecond_m ( const vec &cond, int N ); |
| 471 | |
| 472 | //! Shortcut for conditioning and evaluation of the internal epdf. In some cases, this operation can be implemented efficiently. |
| 473 | virtual double evallogcond ( const vec &dt, const vec &cond ) { |
| 474 | bdm_error ( "Not implemented" ); |
| 475 | return 0.0; |
| 476 | } |
| 477 | |
| 478 | //! Matrix version of evallogcond |
| 479 | virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) { |
| 480 | vec v ( Dt.cols() ); |
| 481 | for ( int i = 0; i < Dt.cols(); i++ ) { |
| 482 | v ( i ) = evallogcond ( Dt.get_col ( i ), cond ); |
249 | | \endcode |
250 | | */ |
251 | | void from_setting (const Setting &set); |
252 | | |
253 | | // TODO dodelat void to_setting( Setting &set ) const; |
254 | | //! Invalidate all named RVs. Use before initializing any RV instances, with care... |
255 | | static void clear_all(); |
256 | | }; |
257 | | UIREGISTER (RV); |
258 | | SHAREDPTR (RV); |
259 | | |
260 | | //! Concat two random variables |
261 | | RV concat (const RV &rv1, const RV &rv2); |
262 | | |
263 | | //!Default empty RV that can be used as default argument |
264 | | extern RV RV0; |
265 | | |
266 | | //! Class representing function \f$f(x)\f$ of variable \f$x\f$ represented by \c rv |
267 | | |
268 | | class fnc : public root |
269 | | { |
270 | | protected: |
271 | | //! Length of the output vector |
272 | | int dimy; |
273 | | public: |
274 | | //!default constructor |
275 | | fnc() {}; |
276 | | //! function evaluates numerical value of \f$f(x)\f$ at \f$x=\f$ \c cond |
277 | | virtual vec eval (const vec &cond) { |
278 | | return vec (0); |
279 | | }; |
280 | | |
281 | | //! function substitutes given value into an appropriate position |
282 | | virtual void condition (const vec &val) {}; |
283 | | |
284 | | //! access function |
285 | | int dimension() const { |
286 | | return dimy; |
287 | | } |
288 | | }; |
289 | | |
290 | | class mpdf; |
291 | | |
292 | | //! Probability density function with numerical statistics, e.g. posterior density. |
293 | | |
294 | | class epdf : public root |
295 | | { |
296 | | protected: |
297 | | //! dimension of the random variable |
298 | | int dim; |
299 | | //! Description of the random variable |
300 | | RV rv; |
301 | | |
302 | | public: |
303 | | /*! \name Constructors |
304 | | Construction of each epdf should support two types of constructors: |
305 | | \li empty constructor, |
306 | | \li copy constructor, |
307 | | |
308 | | The following constructors should be supported for convenience: |
309 | | \li constructor followed by calling \c set_parameters() |
310 | | \li constructor accepting random variables calling \c set_rv() |
311 | | |
312 | | 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. |
313 | | @{*/ |
314 | | epdf() : dim (0), rv() {}; |
315 | | epdf (const epdf &e) : dim (e.dim), rv (e.rv) {}; |
316 | | epdf (const RV &rv0) : dim (rv0._dsize()) { |
317 | | set_rv (rv0); |
318 | | }; |
319 | | void set_parameters (int dim0) { |
320 | | dim = dim0; |
321 | | } |
322 | | //!@} |
323 | | |
324 | | //! \name Matematical Operations |
325 | | //!@{ |
326 | | |
327 | | //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ |
328 | | virtual vec sample() const { |
329 | | bdm_error ("not implemented"); |
330 | | return vec(); |
331 | | } |
332 | | |
333 | | //! Returns N samples, \f$ [x_1 , x_2 , \ldots \ \f$ from density \f$ f_x(rv)\f$ |
334 | | virtual mat sample_m (int N) const; |
335 | | |
336 | | //! Compute log-probability of argument \c val |
337 | | //! In case the argument is out of suport return -Infinity |
338 | | virtual double evallog (const vec &val) const { |
339 | | bdm_error ("not implemented"); |
340 | | return 0.0; |
341 | | } |
342 | | |
343 | | //! Compute log-probability of multiple values argument \c val |
344 | | virtual vec evallog_m (const mat &Val) const; |
345 | | |
346 | | //! Compute log-probability of multiple values argument \c val |
347 | | virtual vec evallog_m (const Array<vec> &Avec) const; |
348 | | |
349 | | //! Return conditional density on the given RV, the remaining rvs will be in conditioning |
350 | | virtual shared_ptr<mpdf> condition (const RV &rv) const; |
351 | | |
352 | | //! Return marginal density on the given RV, the remainig rvs are intergrated out |
353 | | virtual shared_ptr<epdf> marginal (const RV &rv) const; |
354 | | |
355 | | //! return expected value |
356 | | virtual vec mean() const { |
357 | | bdm_error ("not implemneted"); |
358 | | return vec(); |
359 | | } |
360 | | |
361 | | //! return expected variance (not covariance!) |
362 | | virtual vec variance() const { |
363 | | bdm_error ("not implemneted"); |
364 | | return vec(); |
365 | | } |
366 | | |
367 | | //! Lower and upper bounds of \c percentage % quantile, returns mean-2*sigma as default |
368 | | virtual void qbounds (vec &lb, vec &ub, double percentage = 0.95) const { |
369 | | vec mea = mean(); |
370 | | vec std = sqrt (variance()); |
371 | | lb = mea - 2 * std; |
372 | | ub = mea + 2 * std; |
373 | | }; |
374 | | //!@} |
375 | | |
376 | | //! \name Connection to other classes |
377 | | //! Description of the random quantity via attribute \c rv is optional. |
378 | | //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization |
379 | | //! and \c conditioning \c rv has to be set. NB: |
380 | | //! @{ |
381 | | |
382 | | //!Name its rv |
383 | | void set_rv (const RV &rv0) { |
384 | | rv = rv0; |
385 | | } |
386 | | |
387 | | //! True if rv is assigned |
388 | | bool isnamed() const { |
389 | | bool b = (dim == rv._dsize()); |
390 | | return b; |
391 | | } |
392 | | |
393 | | //! Return name (fails when isnamed is false) |
394 | | const RV& _rv() const { |
395 | | bdm_assert_debug (isnamed(), ""); |
396 | | return rv; |
397 | | } |
398 | | //!@} |
399 | | |
400 | | //! \name Access to attributes |
401 | | //! @{ |
402 | | |
403 | | //! Size of the random variable |
404 | | int dimension() const { |
405 | | return dim; |
406 | | } |
407 | | //! Load from structure with elements: |
408 | | //! \code |
409 | | //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable |
410 | | //! // elements of offsprings |
411 | | //! } |
412 | | //! \endcode |
413 | | //!@} |
414 | | void from_setting (const Setting &set) { |
415 | | shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); |
416 | | if (r) { |
417 | | set_rv (*r); |
418 | | } |
419 | | } |
420 | | |
421 | | }; |
422 | | SHAREDPTR(epdf); |
423 | | |
424 | | //! Conditional probability density, e.g. modeling \f$ f( x | y) \f$, where \f$ x \f$ is random variable, \c rv, and \f$ y \f$ is conditioning variable, \c rvc. |
425 | | class mpdf : public root |
426 | | { |
427 | | protected: |
428 | | //!dimension of the condition |
429 | | int dimc; |
430 | | //! random variable in condition |
431 | | RV rvc; |
432 | | private: |
433 | | //! internal epdf, used only as cache to avoid virtual calls of \c _rv() and \c _dimension() |
434 | | epdf* ep; |
435 | | |
436 | | protected: |
437 | | //! set internal pointer \c ep to point to given \c iepdf |
438 | | void set_ep (epdf &iepdf) { |
439 | | ep = &iepdf; |
440 | | } |
441 | | //! set internal pointer \c ep to point to given \c iepdf |
442 | | void set_ep (epdf *iepdfp) { |
443 | | ep = iepdfp; |
444 | | } |
445 | | |
446 | | public: |
447 | | //! \name Constructors |
448 | | //! @{ |
449 | | |
450 | | mpdf() : dimc (0), rvc(), ep (NULL) { } |
451 | | |
452 | | mpdf (const mpdf &m) : dimc (m.dimc), rvc (m.rvc), ep (m.ep) { } |
453 | | //!@} |
454 | | |
455 | | //! \name Matematical operations |
456 | | //!@{ |
457 | | |
458 | | //! 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 |
459 | | virtual vec samplecond (const vec &cond) { |
460 | | bdm_error ("Not implemented"); |
461 | | return vec(); |
462 | | } |
463 | | |
464 | | //! 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 |
465 | | virtual mat samplecond_m (const vec &cond, int N); |
466 | | |
467 | | //! Shortcut for conditioning and evaluation of the internal epdf. In some cases, this operation can be implemented efficiently. |
468 | | virtual double evallogcond (const vec &dt, const vec &cond) { |
469 | | bdm_error ("Not implemented"); |
470 | | return 0.0; |
471 | | } |
472 | | |
473 | | //! Matrix version of evallogcond |
474 | | virtual vec evallogcond_m (const mat &Dt, const vec &cond) { |
475 | | vec v(Dt.cols()); |
476 | | for(int i=0;i<Dt.cols();i++){v(i)= evallogcond(Dt.get_col(i),cond);} |
477 | | return v; |
478 | | } |
479 | | |
480 | | //! Array<vec> version of evallogcond |
481 | | virtual vec evallogcond_m (const Array<vec> &Dt, const vec &cond) { |
482 | | bdm_error ("Not implemented"); |
483 | | return vec(); |
484 | | } |
485 | | |
486 | | //! \name Access to attributes |
487 | | //! @{ |
488 | | |
489 | | const RV& _rv() const { |
490 | | return ep->_rv(); |
491 | | } |
492 | | const RV& _rvc() { |
493 | | return rvc; |
494 | | } |
495 | | |
496 | | int dimension() const { |
497 | | return ep->dimension(); |
498 | | } |
499 | | int dimensionc() { |
500 | | return dimc; |
501 | | } |
502 | | |
503 | | //! Load from structure with elements: |
504 | | //! \code |
505 | | //! { class = "mpdf_offspring", |
506 | | //! rv = {class="RV", names=(...),}; // RV describing meaning of random variable |
507 | | //! rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition |
508 | | //! // elements of offsprings |
509 | | //! } |
510 | | //! \endcode |
511 | | //!@} |
512 | | void from_setting (const Setting &set); |
513 | | //!@} |
514 | | |
515 | | //! \name Connection to other objects |
516 | | //!@{ |
517 | | void set_rvc (const RV &rvc0) { |
518 | | rvc = rvc0; |
519 | | } |
520 | | void set_rv (const RV &rv0) { |
521 | | ep->set_rv (rv0); |
522 | | } |
523 | | bool isnamed() { |
524 | | return (ep->isnamed()) && (dimc == rvc._dsize()); |
525 | | } |
526 | | //!@} |
527 | | }; |
528 | | SHAREDPTR(mpdf); |
| 484 | return v; |
| 485 | } |
| 486 | |
| 487 | //! Array<vec> version of evallogcond |
| 488 | virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) { |
| 489 | bdm_error ( "Not implemented" ); |
| 490 | return vec(); |
| 491 | } |
| 492 | |
| 493 | //! \name Access to attributes |
| 494 | //! @{ |
| 495 | |
| 496 | const RV& _rv() const { |
| 497 | return ep->_rv(); |
| 498 | } |
| 499 | const RV& _rvc() { |
| 500 | return rvc; |
| 501 | } |
| 502 | |
| 503 | int dimension() const { |
| 504 | return ep->dimension(); |
| 505 | } |
| 506 | int dimensionc() { |
| 507 | return dimc; |
| 508 | } |
| 509 | |
| 510 | //! Load from structure with elements: |
| 511 | //! \code |
| 512 | //! { class = "mpdf_offspring", |
| 513 | //! rv = {class="RV", names=(...),}; // RV describing meaning of random variable |
| 514 | //! rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition |
| 515 | //! // elements of offsprings |
| 516 | //! } |
| 517 | //! \endcode |
| 518 | //!@} |
| 519 | void from_setting ( const Setting &set ); |
| 520 | //!@} |
| 521 | |
| 522 | //! \name Connection to other objects |
| 523 | //!@{ |
| 524 | void set_rvc ( const RV &rvc0 ) { |
| 525 | rvc = rvc0; |
| 526 | } |
| 527 | void set_rv ( const RV &rv0 ) { |
| 528 | ep->set_rv ( rv0 ); |
| 529 | } |
| 530 | bool isnamed() { |
| 531 | return ( ep->isnamed() ) && ( dimc == rvc._dsize() ); |
| 532 | } |
| 533 | //!@} |
| 534 | }; |
| 535 | SHAREDPTR ( mpdf ); |
532 | | class mpdf_internal: public mpdf |
533 | | { |
534 | | protected : |
535 | | //! Internal epdf used for sampling |
536 | | EPDF iepdf; |
537 | | public: |
538 | | //! constructor |
539 | | mpdf_internal() : mpdf(), iepdf() {set_ep (iepdf);} |
540 | | |
541 | | //! Update \c iepdf so that it represents this mpdf conditioned on \c rvc = cond |
542 | | //! This function provides convenient reimplementation in offsprings |
543 | | virtual void condition (const vec &cond) { |
544 | | bdm_error ("Not implemented"); |
545 | | } |
546 | | |
547 | | //!access function to iepdf |
548 | | EPDF& e() {return iepdf;} |
549 | | |
550 | | //! Reimplements samplecond using \c condition() |
551 | | vec samplecond (const vec &cond); |
552 | | //! Reimplements evallogcond using \c condition() |
553 | | double evallogcond (const vec &val, const vec &cond); |
554 | | //! Efficient version of evallogcond for matrices |
555 | | virtual vec evallogcond_m (const mat &Dt, const vec &cond); |
556 | | //! Efficient version of evallogcond for Array<vec> |
557 | | virtual vec evallogcond_m (const Array<vec> &Dt, const vec &cond); |
558 | | //! Efficient version of samplecond |
559 | | virtual mat samplecond_m (const vec &cond, int N); |
| 539 | class mpdf_internal: public mpdf { |
| 540 | protected : |
| 541 | //! Internal epdf used for sampling |
| 542 | EPDF iepdf; |
| 543 | public: |
| 544 | //! constructor |
| 545 | mpdf_internal() : mpdf(), iepdf() { |
| 546 | set_ep ( iepdf ); |
| 547 | } |
| 548 | |
| 549 | //! Update \c iepdf so that it represents this mpdf conditioned on \c rvc = cond |
| 550 | //! This function provides convenient reimplementation in offsprings |
| 551 | virtual void condition ( const vec &cond ) { |
| 552 | bdm_error ( "Not implemented" ); |
| 553 | } |
| 554 | |
| 555 | //!access function to iepdf |
| 556 | EPDF& e() { |
| 557 | return iepdf; |
| 558 | } |
| 559 | |
| 560 | //! Reimplements samplecond using \c condition() |
| 561 | vec samplecond ( const vec &cond ); |
| 562 | //! Reimplements evallogcond using \c condition() |
| 563 | double evallogcond ( const vec &val, const vec &cond ); |
| 564 | //! Efficient version of evallogcond for matrices |
| 565 | virtual vec evallogcond_m ( const mat &Dt, const vec &cond ); |
| 566 | //! Efficient version of evallogcond for Array<vec> |
| 567 | virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ); |
| 568 | //! Efficient version of samplecond |
| 569 | virtual mat samplecond_m ( const vec &cond, int N ); |
587 | | class datalink |
588 | | { |
589 | | protected: |
590 | | //! Remember how long val should be |
591 | | int downsize; |
592 | | |
593 | | //! Remember how long val of "Up" should be |
594 | | int upsize; |
595 | | |
596 | | //! val-to-val link, indices of the upper val |
597 | | ivec v2v_up; |
598 | | |
599 | | public: |
600 | | //! Constructor |
601 | | datalink() : downsize (0), upsize (0) { } |
602 | | |
603 | | //! Convenience constructor |
604 | | datalink (const RV &rv, const RV &rv_up) { |
605 | | set_connection (rv, rv_up); |
606 | | } |
607 | | |
608 | | //! set connection, rv must be fully present in rv_up |
609 | | void set_connection (const RV &rv, const RV &rv_up); |
610 | | |
611 | | //! set connection using indices |
612 | | void set_connection (int ds, int us, const ivec &upind); |
613 | | |
614 | | //! Get val for myself from val of "Up" |
615 | | vec pushdown (const vec &val_up) { |
616 | | vec tmp(downsize); |
617 | | filldown(val_up, tmp); |
618 | | return tmp; |
619 | | } |
620 | | //! Get val for vector val_down from val of "Up" |
621 | | void filldown (const vec &val_up, vec &val_down) { |
622 | | bdm_assert_debug (upsize == val_up.length(), "Wrong val_up"); |
623 | | val_down=val_up(v2v_up); |
624 | | } |
625 | | |
626 | | //! Fill val of "Up" by my pieces |
627 | | void pushup (vec &val_up, const vec &val) { |
628 | | bdm_assert_debug (downsize == val.length(), "Wrong val"); |
629 | | bdm_assert_debug (upsize == val_up.length(), "Wrong val_up"); |
630 | | set_subvector (val_up, v2v_up, val); |
631 | | } |
632 | | //! access functions |
633 | | int _upsize(){return upsize;} |
634 | | //! access functions |
635 | | int _downsize(){return downsize;} |
| 597 | class datalink { |
| 598 | protected: |
| 599 | //! Remember how long val should be |
| 600 | int downsize; |
| 601 | |
| 602 | //! Remember how long val of "Up" should be |
| 603 | int upsize; |
| 604 | |
| 605 | //! val-to-val link, indices of the upper val |
| 606 | ivec v2v_up; |
| 607 | |
| 608 | public: |
| 609 | //! Constructor |
| 610 | datalink() : downsize ( 0 ), upsize ( 0 ) { } |
| 611 | |
| 612 | //! Convenience constructor |
| 613 | datalink ( const RV &rv, const RV &rv_up ) { |
| 614 | set_connection ( rv, rv_up ); |
| 615 | } |
| 616 | |
| 617 | //! set connection, rv must be fully present in rv_up |
| 618 | void set_connection ( const RV &rv, const RV &rv_up ); |
| 619 | |
| 620 | //! set connection using indices |
| 621 | void set_connection ( int ds, int us, const ivec &upind ); |
| 622 | |
| 623 | //! Get val for myself from val of "Up" |
| 624 | vec pushdown ( const vec &val_up ) { |
| 625 | vec tmp ( downsize ); |
| 626 | filldown ( val_up, tmp ); |
| 627 | return tmp; |
| 628 | } |
| 629 | //! Get val for vector val_down from val of "Up" |
| 630 | void filldown ( const vec &val_up, vec &val_down ) { |
| 631 | bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" ); |
| 632 | val_down = val_up ( v2v_up ); |
| 633 | } |
| 634 | //! Fill val of "Up" by my pieces |
| 635 | void pushup ( vec &val_up, const vec &val ) { |
| 636 | bdm_assert_debug ( downsize == val.length(), "Wrong val" ); |
| 637 | bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" ); |
| 638 | set_subvector ( val_up, v2v_up, val ); |
| 639 | } |
| 640 | //! access functions |
| 641 | int _upsize() { |
| 642 | return upsize; |
| 643 | } |
| 644 | //! access functions |
| 645 | int _downsize() { |
| 646 | return downsize; |
| 647 | } |
676 | | //! Get val for myself from val of "Up" |
677 | | vec pushdown (const vec &val_up) { |
678 | | vec tmp(downsize); |
679 | | filldown(val_up, tmp); |
680 | | return tmp; |
681 | | } |
682 | | |
683 | | void filldown(const vec &val_up, vec &val_down){ |
684 | | bdm_assert_debug(val_down.length()>=downsize, "short val_down"); |
685 | | |
686 | | set_subvector(val_down, v2v_down, val_up(v2v_up)); // copy direct values |
687 | | set_subvector(val_down, h2v_down, history(h2v_hist)); // copy delayed values |
688 | | } |
689 | | |
690 | | void set_connection(const RV &rv, const RV &rv_up){ |
691 | | // create link between up and down |
692 | | datalink_part::set_connection(rv, rv_up); |
693 | | |
694 | | // create rvs of history |
695 | | // we can store only what we get in rv_up - everything else is removed |
696 | | ivec valid_ids=rv.findself_ids(rv_up); |
697 | | RV rv_hist = rv.subselect( find(valid_ids>=0) ); // select only rvs that are in rv_up |
698 | | rv_hist = rv_hist.expand_delayes(); // full regressor - including time 0 |
699 | | Hrv=rv_hist.subt(rv.remove_time()); // remove time 0 |
700 | | history = zeros(Hrv._dsize()); |
701 | | |
702 | | Hrv.dataind(rv,h2v_hist,h2v_down); |
703 | | |
704 | | downsize = v2v_down.length()+h2v_down.length(); |
705 | | upsize = v2v_up.length(); |
| 688 | //! Get val for myself from val of "Up" |
| 689 | vec pushdown ( const vec &val_up ) { |
| 690 | vec tmp ( downsize ); |
| 691 | filldown ( val_up, tmp ); |
| 692 | return tmp; |
| 693 | } |
| 694 | |
| 695 | void filldown ( const vec &val_up, vec &val_down ) { |
| 696 | bdm_assert_debug ( val_down.length() >= downsize, "short val_down" ); |
| 697 | |
| 698 | set_subvector ( val_down, v2v_down, val_up ( v2v_up ) ); // copy direct values |
| 699 | set_subvector ( val_down, h2v_down, history ( h2v_hist ) ); // copy delayed values |
| 700 | } |
| 701 | |
| 702 | void set_connection ( const RV &rv, const RV &rv_up ) { |
| 703 | // create link between up and down |
| 704 | datalink_part::set_connection ( rv, rv_up ); |
| 705 | |
| 706 | // create rvs of history |
| 707 | // we can store only what we get in rv_up - everything else is removed |
| 708 | ivec valid_ids = rv.findself_ids ( rv_up ); |
| 709 | RV rv_hist = rv.subselect ( find ( valid_ids >= 0 ) ); // select only rvs that are in rv_up |
| 710 | rv_hist = rv_hist.expand_delayes(); // full regressor - including time 0 |
| 711 | Hrv = rv_hist.subt ( rv.remove_time() ); // remove time 0 |
| 712 | history = zeros ( Hrv._dsize() ); |
| 713 | |
| 714 | Hrv.dataind ( rv, h2v_hist, h2v_down ); |
| 715 | |
| 716 | downsize = v2v_down.length() + h2v_down.length(); |
| 717 | upsize = v2v_up.length(); |
757 | | class datalink_m2m: public datalink_m2e |
758 | | { |
759 | | protected: |
760 | | //!cond-to-cond link, indices of the upper cond |
761 | | ivec c2c_up; |
762 | | //!cond-to-cond link, indices of the local cond |
763 | | ivec c2c_lo; |
764 | | |
765 | | public: |
766 | | //! Constructor |
767 | | datalink_m2m() {}; |
768 | | //! Set connection between the vectors |
769 | | void set_connection (const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up) { |
770 | | datalink_m2e::set_connection (rv, rvc, rv_up); |
771 | | //establish c2c connection |
772 | | rvc.dataind (rvc_up, c2c_lo, c2c_up); |
773 | | bdm_assert_debug (c2c_lo.length() + v2c_lo.length() == condsize, "cond is not fully given"); |
774 | | } |
775 | | |
776 | | //! Get cond for myself from val and cond of "Up" |
777 | | vec get_cond (const vec &val_up, const vec &cond_up) { |
778 | | vec tmp (condsize); |
779 | | set_subvector (tmp, v2c_lo, val_up (v2c_up)); |
780 | | set_subvector (tmp, c2c_lo, cond_up (c2c_up)); |
781 | | return tmp; |
782 | | } |
783 | | //! Fill |
| 773 | class datalink_m2m: public datalink_m2e { |
| 774 | protected: |
| 775 | //!cond-to-cond link, indices of the upper cond |
| 776 | ivec c2c_up; |
| 777 | //!cond-to-cond link, indices of the local cond |
| 778 | ivec c2c_lo; |
| 779 | |
| 780 | public: |
| 781 | //! Constructor |
| 782 | datalink_m2m() {}; |
| 783 | //! Set connection between the vectors |
| 784 | void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) { |
| 785 | datalink_m2e::set_connection ( rv, rvc, rv_up ); |
| 786 | //establish c2c connection |
| 787 | rvc.dataind ( rvc_up, c2c_lo, c2c_up ); |
| 788 | bdm_assert_debug ( c2c_lo.length() + v2c_lo.length() == condsize, "cond is not fully given" ); |
| 789 | } |
| 790 | |
| 791 | //! Get cond for myself from val and cond of "Up" |
| 792 | vec get_cond ( const vec &val_up, const vec &cond_up ) { |
| 793 | vec tmp ( condsize ); |
| 794 | set_subvector ( tmp, v2c_lo, val_up ( v2c_up ) ); |
| 795 | set_subvector ( tmp, c2c_lo, cond_up ( c2c_up ) ); |
| 796 | return tmp; |
| 797 | } |
| 798 | //! Fill |
877 | | class DS : public root |
878 | | { |
879 | | protected: |
880 | | int dtsize; |
881 | | int utsize; |
882 | | //!Description of data returned by \c getdata(). |
883 | | RV Drv; |
884 | | //!Description of data witten by by \c write(). |
885 | | RV Urv; // |
886 | | //! Remember its own index in Logger L |
887 | | int L_dt, L_ut; |
888 | | public: |
889 | | //! default constructors |
890 | | DS() : Drv(), Urv() {}; |
891 | | |
892 | | //! Returns full vector of observed data=[output, input] |
893 | | virtual void getdata (vec &dt) { |
894 | | bdm_error ("abstract class"); |
895 | | } |
896 | | |
897 | | //! Returns data records at indeces. |
898 | | virtual void getdata (vec &dt, const ivec &indeces) { |
899 | | bdm_error ("abstract class"); |
900 | | } |
901 | | |
902 | | //! Accepts action variable and schedule it for application. |
903 | | virtual void write (vec &ut) { |
904 | | bdm_error ("abstract class"); |
905 | | } |
906 | | |
907 | | //! Accepts action variables at specific indeces |
908 | | virtual void write (vec &ut, const ivec &indeces) { |
909 | | bdm_error ("abstract class"); |
910 | | } |
911 | | |
912 | | //! Moves from \f$ t \f$ to \f$ t+1 \f$, i.e. perfroms the actions and reads response of the system. |
913 | | virtual void step() = 0; |
914 | | |
915 | | //! Register DS for logging into logger L |
916 | | virtual void log_add (logger &L) { |
917 | | bdm_assert_debug (dtsize == Drv._dsize(), "invalid DS: dtsize ("+ num2str(dtsize)+ ") different from Drv "+ num2str(Drv._dsize())); |
918 | | bdm_assert_debug (utsize == Urv._dsize(), "invalid DS: utsize ("+ num2str(utsize)+ ") different from Urv "+ num2str(Urv._dsize())); |
919 | | |
920 | | L_dt = L.add (Drv, ""); |
921 | | L_ut = L.add (Urv, ""); |
922 | | } |
923 | | //! Register DS for logging into logger L |
924 | | virtual void logit (logger &L) { |
925 | | vec tmp (Drv._dsize() + Urv._dsize()); |
926 | | getdata (tmp); |
927 | | // d is first in getdata |
928 | | L.logit (L_dt, tmp.left (Drv._dsize())); |
929 | | // u follows after d in getdata |
930 | | L.logit (L_ut, tmp.mid (Drv._dsize(), Urv._dsize())); |
931 | | } |
932 | | //!access function |
933 | | virtual const RV& _drv() const { |
934 | | // return concat (Drv, Urv);// why!!! |
935 | | return Drv;// why!!! |
936 | | } |
937 | | //!access function |
938 | | const RV& _urv() const { |
939 | | return Urv; |
940 | | } |
941 | | //! set random variables |
942 | | virtual void set_drv (const RV &drv, const RV &urv) { |
943 | | Drv = drv; |
944 | | Urv = urv; |
945 | | } |
| 900 | class DS : public root { |
| 901 | protected: |
| 902 | int dtsize; |
| 903 | int utsize; |
| 904 | //!Description of data returned by \c getdata(). |
| 905 | RV Drv; |
| 906 | //!Description of data witten by by \c write(). |
| 907 | RV Urv; // |
| 908 | //! Remember its own index in Logger L |
| 909 | int L_dt, L_ut; |
| 910 | public: |
| 911 | //! default constructors |
| 912 | DS() : Drv(), Urv() {}; |
| 913 | |
| 914 | //! Returns full vector of observed data=[output, input] |
| 915 | virtual void getdata ( vec &dt ) { |
| 916 | bdm_error ( "abstract class" ); |
| 917 | } |
| 918 | |
| 919 | //! Returns data records at indeces. |
| 920 | virtual void getdata ( vec &dt, const ivec &indeces ) { |
| 921 | bdm_error ( "abstract class" ); |
| 922 | } |
| 923 | |
| 924 | //! Accepts action variable and schedule it for application. |
| 925 | virtual void write ( vec &ut ) { |
| 926 | bdm_error ( "abstract class" ); |
| 927 | } |
| 928 | |
| 929 | //! Accepts action variables at specific indeces |
| 930 | virtual void write ( vec &ut, const ivec &indeces ) { |
| 931 | bdm_error ( "abstract class" ); |
| 932 | } |
| 933 | |
| 934 | //! Moves from \f$ t \f$ to \f$ t+1 \f$, i.e. perfroms the actions and reads response of the system. |
| 935 | virtual void step() = 0; |
| 936 | |
| 937 | //! Register DS for logging into logger L |
| 938 | virtual void log_add ( logger &L ) { |
| 939 | bdm_assert_debug ( dtsize == Drv._dsize(), "invalid DS: dtsize (" + num2str ( dtsize ) + ") different from Drv " + num2str ( Drv._dsize() ) ); |
| 940 | bdm_assert_debug ( utsize == Urv._dsize(), "invalid DS: utsize (" + num2str ( utsize ) + ") different from Urv " + num2str ( Urv._dsize() ) ); |
| 941 | |
| 942 | L_dt = L.add ( Drv, "" ); |
| 943 | L_ut = L.add ( Urv, "" ); |
| 944 | } |
| 945 | //! Register DS for logging into logger L |
| 946 | virtual void logit ( logger &L ) { |
| 947 | vec tmp ( Drv._dsize() + Urv._dsize() ); |
| 948 | getdata ( tmp ); |
| 949 | // d is first in getdata |
| 950 | L.logit ( L_dt, tmp.left ( Drv._dsize() ) ); |
| 951 | // u follows after d in getdata |
| 952 | L.logit ( L_ut, tmp.mid ( Drv._dsize(), Urv._dsize() ) ); |
| 953 | } |
| 954 | //!access function |
| 955 | virtual const RV& _drv() const { |
| 956 | // return concat (Drv, Urv);// why!!! |
| 957 | return Drv;// why!!! |
| 958 | } |
| 959 | //!access function |
| 960 | const RV& _urv() const { |
| 961 | return Urv; |
| 962 | } |
| 963 | //! set random variables |
| 964 | virtual void set_drv ( const RV &drv, const RV &urv ) { |
| 965 | Drv = drv; |
| 966 | Urv = urv; |
| 967 | } |
969 | | class BM : public root |
970 | | { |
971 | | protected: |
972 | | //! Random variable of the data (optional) |
973 | | RV drv; |
974 | | //!Logarithm of marginalized data likelihood. |
975 | | double ll; |
976 | | //! 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. |
977 | | bool evalll; |
978 | | public: |
979 | | //! \name Constructors |
980 | | //! @{ |
981 | | |
982 | | BM() : ll (0), evalll (true), LIDs (4), LFlags (4) { |
983 | | LIDs = -1;/*empty IDs*/ |
984 | | LFlags = 0; |
985 | | LFlags (0) = 1; /*log only mean*/ |
| 991 | class BM : public root { |
| 992 | protected: |
| 993 | //! Random variable of the data (optional) |
| 994 | RV drv; |
| 995 | //!Logarithm of marginalized data likelihood. |
| 996 | double ll; |
| 997 | //! 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. |
| 998 | bool evalll; |
| 999 | public: |
| 1000 | //! \name Constructors |
| 1001 | //! @{ |
| 1002 | |
| 1003 | BM() : ll ( 0 ), evalll ( true ), LIDs ( 4 ), LFlags ( 4 ) { |
| 1004 | LIDs = -1;/*empty IDs*/ |
| 1005 | LFlags = 0; |
| 1006 | LFlags ( 0 ) = 1; /*log only mean*/ |
| 1007 | }; |
| 1008 | BM ( const BM &B ) : drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} |
| 1009 | //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! |
| 1010 | //! Prototype: \code BM* _copy_() const {return new BM(*this);} \endcode |
| 1011 | virtual BM* _copy_() const { |
| 1012 | return NULL; |
| 1013 | }; |
| 1014 | //!@} |
| 1015 | |
| 1016 | //! \name Mathematical operations |
| 1017 | //!@{ |
| 1018 | |
| 1019 | /*! \brief Incremental Bayes rule |
| 1020 | @param dt vector of input data |
| 1021 | */ |
| 1022 | virtual void bayes ( const vec &dt ) = 0; |
| 1023 | //! Batch Bayes rule (columns of Dt are observations) |
| 1024 | virtual void bayesB ( const mat &Dt ); |
| 1025 | //! Evaluates predictive log-likelihood of the given data record |
| 1026 | //! I.e. marginal likelihood of the data with the posterior integrated out. |
| 1027 | virtual double logpred ( const vec &dt ) const { |
| 1028 | bdm_error ( "Not implemented" ); |
| 1029 | return 0.0; |
| 1030 | } |
| 1031 | |
| 1032 | //! Matrix version of logpred |
| 1033 | vec logpred_m ( const mat &dt ) const { |
| 1034 | vec tmp ( dt.cols() ); |
| 1035 | for ( int i = 0; i < dt.cols(); i++ ) { |
| 1036 | tmp ( i ) = logpred ( dt.get_col ( i ) ); |
| 1037 | } |
| 1038 | return tmp; |
| 1039 | } |
| 1040 | |
| 1041 | //!Constructs a predictive density \f$ f(d_{t+1} |d_{t}, \ldots d_{0}) \f$ |
| 1042 | virtual epdf* epredictor() const { |
| 1043 | bdm_error ( "Not implemented" ); |
| 1044 | return NULL; |
| 1045 | }; |
| 1046 | //!Constructs conditional density of 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t}) \f$ |
| 1047 | virtual mpdf* predictor() const { |
| 1048 | bdm_error ( "Not implemented" ); |
| 1049 | return NULL; |
| 1050 | }; |
| 1051 | //!@} |
| 1052 | |
| 1053 | //! \name Extension to conditional BM |
| 1054 | //! This extension is useful e.g. in Marginalized Particle Filter (\ref bdm::MPF). |
| 1055 | //! Alternatively, it can be used for automated connection to DS when the condition is observed |
| 1056 | //!@{ |
| 1057 | |
| 1058 | //! Name of extension variable |
| 1059 | RV rvc; |
| 1060 | //! access function |
| 1061 | const RV& _rvc() const { |
| 1062 | return rvc; |
| 1063 | } |
| 1064 | |
| 1065 | //! Substitute \c val for \c rvc. |
| 1066 | virtual void condition ( const vec &val ) { |
| 1067 | bdm_error ( "Not implemented!" ); |
| 1068 | } |
| 1069 | |
| 1070 | //!@} |
| 1071 | |
| 1072 | |
| 1073 | //! \name Access to attributes |
| 1074 | //!@{ |
| 1075 | |
| 1076 | const RV& _drv() const { |
| 1077 | return drv; |
| 1078 | } |
| 1079 | void set_drv ( const RV &rv ) { |
| 1080 | drv = rv; |
| 1081 | } |
| 1082 | void set_rv ( const RV &rv ) { |
| 1083 | const_cast<epdf&> ( posterior() ).set_rv ( rv ); |
| 1084 | } |
| 1085 | double _ll() const { |
| 1086 | return ll; |
| 1087 | } |
| 1088 | void set_evalll ( bool evl0 ) { |
| 1089 | evalll = evl0; |
| 1090 | } |
| 1091 | virtual const epdf& posterior() const = 0; |
| 1092 | //!@} |
| 1093 | |
| 1094 | //! \name Logging of results |
| 1095 | //!@{ |
| 1096 | |
| 1097 | //! Set boolean options from a string, recognized are: "logbounds,logll" |
| 1098 | virtual void set_options ( const string &opt ) { |
| 1099 | LFlags ( 0 ) = 1; |
| 1100 | if ( opt.find ( "logbounds" ) != string::npos ) { |
| 1101 | LFlags ( 1 ) = 1; |
| 1102 | LFlags ( 2 ) = 1; |
| 1103 | } |
| 1104 | if ( opt.find ( "logll" ) != string::npos ) { |
| 1105 | LFlags ( 3 ) = 1; |
| 1106 | } |
| 1107 | } |
| 1108 | //! IDs of storages in loggers 4:[1=mean,2=lb,3=ub,4=ll] |
| 1109 | ivec LIDs; |
| 1110 | |
| 1111 | //! Flags for logging - same size as LIDs, each entry correspond to the same in LIDs |
| 1112 | ivec LFlags; |
| 1113 | //! Add all logged variables to a logger |
| 1114 | virtual void log_add ( logger &L, const string &name = "" ) { |
| 1115 | // internal |
| 1116 | RV r; |
| 1117 | if ( posterior().isnamed() ) { |
| 1118 | r = posterior()._rv(); |
| 1119 | } else { |
| 1120 | r = RV ( "est", posterior().dimension() ); |
1043 | | |
1044 | | //! Substitute \c val for \c rvc. |
1045 | | virtual void condition (const vec &val) { |
1046 | | bdm_error ("Not implemented!"); |
1047 | | } |
1048 | | |
1049 | | //!@} |
1050 | | |
1051 | | |
1052 | | //! \name Access to attributes |
1053 | | //!@{ |
1054 | | |
1055 | | const RV& _drv() const { |
1056 | | return drv; |
1057 | | } |
1058 | | void set_drv (const RV &rv) { |
1059 | | drv = rv; |
1060 | | } |
1061 | | void set_rv (const RV &rv) { |
1062 | | const_cast<epdf&> (posterior()).set_rv (rv); |
1063 | | } |
1064 | | double _ll() const { |
1065 | | return ll; |
1066 | | } |
1067 | | void set_evalll (bool evl0) { |
1068 | | evalll = evl0; |
1069 | | } |
1070 | | virtual const epdf& posterior() const = 0; |
1071 | | //!@} |
1072 | | |
1073 | | //! \name Logging of results |
1074 | | //!@{ |
1075 | | |
1076 | | //! Set boolean options from a string, recognized are: "logbounds,logll" |
1077 | | virtual void set_options (const string &opt) { |
1078 | | LFlags (0) = 1; |
1079 | | if (opt.find ("logbounds") != string::npos) { |
1080 | | LFlags (1) = 1; |
1081 | | LFlags (2) = 1; |
1082 | | } |
1083 | | if (opt.find ("logll") != string::npos) { |
1084 | | LFlags (3) = 1; |
1085 | | } |
1086 | | } |
1087 | | //! IDs of storages in loggers 4:[1=mean,2=lb,3=ub,4=ll] |
1088 | | ivec LIDs; |
1089 | | |
1090 | | //! Flags for logging - same size as LIDs, each entry correspond to the same in LIDs |
1091 | | ivec LFlags; |
1092 | | //! Add all logged variables to a logger |
1093 | | virtual void log_add (logger &L, const string &name = "") { |
1094 | | // internal |
1095 | | RV r; |
1096 | | if (posterior().isnamed()) { |
1097 | | r = posterior()._rv(); |
1098 | | } else { |
1099 | | r = RV ("est", posterior().dimension()); |
1100 | | }; |
1101 | | |
1102 | | // Add mean value |
1103 | | if (LFlags (0)) LIDs (0) = L.add (r, name + "mean_"); |
1104 | | if (LFlags (1)) LIDs (1) = L.add (r, name + "lb_"); |
1105 | | if (LFlags (2)) LIDs (2) = L.add (r, name + "ub_"); |
1106 | | if (LFlags (3)) LIDs (3) = L.add (RV ("ll", 1), name); //TODO: "local" RV |
1107 | | } |
1108 | | virtual void logit (logger &L) { |
1109 | | L.logit (LIDs (0), posterior().mean()); |
1110 | | if (LFlags (1) || LFlags (2)) { //if one of them is off, its LID==-1 and will not be stored |
1111 | | vec ub, lb; |
1112 | | posterior().qbounds (lb, ub); |
1113 | | L.logit (LIDs (1), lb); |
1114 | | L.logit (LIDs (2), ub); |
1115 | | } |
1116 | | if (LFlags (3)) L.logit (LIDs (3), ll); |
1117 | | } |
1118 | | //!@} |
1119 | | void from_setting (const Setting &set){ |
1120 | | shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional ); |
1121 | | if (r) { |
1122 | | set_drv (*r); |
1123 | | } |
1124 | | string opt; |
1125 | | if(!UI::get(opt, set, "options", UI::optional)) { |
1126 | | set_options(opt); |
1127 | | } |
1128 | | } |
| 1149 | } |