[8] | 1 | /*! |
---|
| 2 | \file |
---|
| 3 | \brief Probability distributions for Exponential Family models. |
---|
| 4 | \author Vaclav Smidl. |
---|
| 5 | |
---|
| 6 | ----------------------------------- |
---|
| 7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
| 8 | |
---|
| 9 | Using IT++ for numerical operations |
---|
| 10 | ----------------------------------- |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | #ifndef EF_H |
---|
| 14 | #define EF_H |
---|
| 15 | |
---|
[262] | 16 | |
---|
[461] | 17 | #include "../shared_ptr.h" |
---|
[384] | 18 | #include "../base/bdmbase.h" |
---|
[262] | 19 | #include "../math/chmat.h" |
---|
[8] | 20 | |
---|
[737] | 21 | namespace bdm { |
---|
[8] | 22 | |
---|
[32] | 23 | |
---|
| 24 | //! Global Uniform_RNG |
---|
[488] | 25 | extern Uniform_RNG UniRNG; |
---|
[33] | 26 | //! Global Normal_RNG |
---|
[488] | 27 | extern Normal_RNG NorRNG; |
---|
[33] | 28 | //! Global Gamma_RNG |
---|
[488] | 29 | extern Gamma_RNG GamRNG; |
---|
[32] | 30 | |
---|
[488] | 31 | /*! |
---|
| 32 | * \brief General conjugate exponential family posterior density. |
---|
[8] | 33 | |
---|
[488] | 34 | * More?... |
---|
| 35 | */ |
---|
[28] | 36 | |
---|
[737] | 37 | class eEF : public epdf { |
---|
| 38 | public: |
---|
[32] | 39 | // eEF() :epdf() {}; |
---|
[737] | 40 | //! default constructor |
---|
| 41 | eEF () : epdf () {}; |
---|
| 42 | //! logarithm of the normalizing constant, \f$\mathcal{I}\f$ |
---|
| 43 | virtual double lognc() const = 0; |
---|
[565] | 44 | |
---|
[737] | 45 | //!Evaluate normalized log-probability |
---|
[766] | 46 | virtual double evallog_nn ( const vec &val ) const NOT_IMPLEMENTED(0); |
---|
[565] | 47 | |
---|
[737] | 48 | //!Evaluate normalized log-probability |
---|
| 49 | virtual double evallog ( const vec &val ) const { |
---|
| 50 | double tmp; |
---|
| 51 | tmp = evallog_nn ( val ) - lognc(); |
---|
| 52 | return tmp; |
---|
| 53 | } |
---|
| 54 | //!Evaluate normalized log-probability for many samples |
---|
| 55 | virtual vec evallog_mat ( const mat &Val ) const { |
---|
| 56 | vec x ( Val.cols() ); |
---|
| 57 | for ( int i = 0; i < Val.cols(); i++ ) { |
---|
| 58 | x ( i ) = evallog_nn ( Val.get_col ( i ) ) ; |
---|
[488] | 59 | } |
---|
[737] | 60 | return x - lognc(); |
---|
| 61 | } |
---|
| 62 | //!Evaluate normalized log-probability for many samples |
---|
| 63 | virtual vec evallog_mat ( const Array<vec> &Val ) const { |
---|
| 64 | vec x ( Val.length() ); |
---|
| 65 | for ( int i = 0; i < Val.length(); i++ ) { |
---|
| 66 | x ( i ) = evallog_nn ( Val ( i ) ) ; |
---|
[488] | 67 | } |
---|
[737] | 68 | return x - lognc(); |
---|
| 69 | } |
---|
[565] | 70 | |
---|
[737] | 71 | //!Power of the density, used e.g. to flatten the density |
---|
[766] | 72 | virtual void pow ( double p ) NOT_IMPLEMENTED_VOID; |
---|
[488] | 73 | }; |
---|
[8] | 74 | |
---|
[33] | 75 | |
---|
[170] | 76 | //! Estimator for Exponential family |
---|
[737] | 77 | class BMEF : public BM { |
---|
| 78 | protected: |
---|
| 79 | //! forgetting factor |
---|
| 80 | double frg; |
---|
| 81 | //! cached value of lognc() in the previous step (used in evaluation of \c ll ) |
---|
| 82 | double last_lognc; |
---|
| 83 | public: |
---|
| 84 | //! Default constructor (=empty constructor) |
---|
| 85 | BMEF ( double frg0 = 1.0 ) : BM (), frg ( frg0 ) {} |
---|
| 86 | //! Copy constructor |
---|
| 87 | BMEF ( const BMEF &B ) : BM ( B ), frg ( B.frg ), last_lognc ( B.last_lognc ) {} |
---|
| 88 | //!get statistics from another model |
---|
[766] | 89 | virtual void set_statistics ( const BMEF* BM0 ) NOT_IMPLEMENTED_VOID; |
---|
[565] | 90 | |
---|
[737] | 91 | //! Weighted update of sufficient statistics (Bayes rule) |
---|
| 92 | virtual void bayes_weighted ( const vec &data, const vec &cond = empty_vec, const double w = 1.0 ) {}; |
---|
| 93 | //original Bayes |
---|
| 94 | void bayes ( const vec &yt, const vec &cond = empty_vec ); |
---|
[565] | 95 | |
---|
[737] | 96 | //!Flatten the posterior according to the given BMEF (of the same type!) |
---|
[766] | 97 | virtual void flatten ( const BMEF * B ) NOT_IMPLEMENTED_VOID; |
---|
[198] | 98 | |
---|
[766] | 99 | double logpred ( const vec &yt ) const NOT_IMPLEMENTED(0); |
---|
| 100 | |
---|
| 101 | virtual epdf* epredictor() const NOT_IMPLEMENTED(NULL); |
---|
[746] | 102 | |
---|
[766] | 103 | virtual pdf* predictor() const NOT_IMPLEMENTED(NULL); |
---|
| 104 | |
---|
[746] | 105 | void to_setting ( Setting &set ) const |
---|
| 106 | { |
---|
| 107 | BM::to_setting( set ); |
---|
[796] | 108 | UI::save(frg, set, "frg"); |
---|
[766] | 109 | // TODO DOPLNIT? ALE MOMENTALNE CHYBI FROM_SETTING PRO INSPIRACI |
---|
[746] | 110 | } |
---|
[796] | 111 | void from_setting( const Setting &set) { |
---|
| 112 | BM::from_setting(set); |
---|
| 113 | if ( !UI::get ( frg, set, "frg" ) ) |
---|
| 114 | frg = 1.0; |
---|
| 115 | |
---|
| 116 | } |
---|
[488] | 117 | }; |
---|
[170] | 118 | |
---|
[797] | 119 | /*! Dirac delta density with predefined transformation |
---|
| 120 | |
---|
| 121 | Density of the type:\f[ f(x_t | y_t) = \delta (x_t - g(y_t)) \f] |
---|
| 122 | where \f$ x_t \f$ is the \c rv, \f$ y_t \f$ is the \c rvc and g is a deterministic transformation of class fn. |
---|
| 123 | */ |
---|
| 124 | class mgdirac: public pdf{ |
---|
| 125 | protected: |
---|
| 126 | shared_ptr<fnc> g; |
---|
| 127 | public: |
---|
| 128 | vec samplecond(const vec &cond) { |
---|
| 129 | bdm_assert_debug(cond.length()==g->dimensionc(),"given cond in not compatible with g"); |
---|
| 130 | vec tmp = g->eval(cond); |
---|
| 131 | return tmp; |
---|
| 132 | } |
---|
| 133 | double evallogcond ( const vec &yt, const vec &cond ){ |
---|
| 134 | return std::numeric_limits< double >::max(); |
---|
| 135 | } |
---|
| 136 | void from_setting(const Setting& set){ |
---|
| 137 | pdf::from_setting(set); |
---|
| 138 | g=UI::build<fnc>(set,"g",UI::compulsory); |
---|
| 139 | validate(); |
---|
| 140 | } |
---|
| 141 | void to_setting(Setting &set) const{ |
---|
| 142 | pdf::to_setting(set); |
---|
| 143 | UI::save(g.get(), set, "g"); |
---|
| 144 | } |
---|
| 145 | void validate() { |
---|
| 146 | dim = g->dimension(); |
---|
| 147 | dimc = g->dimensionc(); |
---|
| 148 | } |
---|
| 149 | }; |
---|
| 150 | UIREGISTER(mgdirac); |
---|
| 151 | |
---|
| 152 | |
---|
[504] | 153 | template<class sq_T, template <typename> class TEpdf> |
---|
[488] | 154 | class mlnorm; |
---|
[178] | 155 | |
---|
[488] | 156 | /*! |
---|
| 157 | * \brief Gaussian density with positive definite (decomposed) covariance matrix. |
---|
[8] | 158 | |
---|
[488] | 159 | * More?... |
---|
| 160 | */ |
---|
| 161 | template<class sq_T> |
---|
[737] | 162 | class enorm : public eEF { |
---|
| 163 | protected: |
---|
| 164 | //! mean value |
---|
| 165 | vec mu; |
---|
| 166 | //! Covariance matrix in decomposed form |
---|
| 167 | sq_T R; |
---|
| 168 | public: |
---|
| 169 | //!\name Constructors |
---|
| 170 | //!@{ |
---|
[270] | 171 | |
---|
[737] | 172 | enorm () : eEF (), mu (), R () {}; |
---|
| 173 | enorm ( const vec &mu, const sq_T &R ) { |
---|
| 174 | set_parameters ( mu, R ); |
---|
| 175 | } |
---|
| 176 | void set_parameters ( const vec &mu, const sq_T &R ); |
---|
| 177 | /*! Create Normal density |
---|
| 178 | \f[ f(rv) = N(\mu, R) \f] |
---|
| 179 | from structure |
---|
| 180 | \code |
---|
| 181 | class = 'enorm<ldmat>', (OR) 'enorm<chmat>', (OR) 'enorm<fsqmat>'; |
---|
| 182 | mu = []; // mean value |
---|
| 183 | R = []; // variance, square matrix of appropriate dimension |
---|
| 184 | \endcode |
---|
| 185 | */ |
---|
| 186 | void from_setting ( const Setting &root ); |
---|
[773] | 187 | void to_setting ( Setting &root ) const ; |
---|
| 188 | |
---|
[737] | 189 | void validate() { |
---|
| 190 | bdm_assert ( mu.length() == R.rows(), "mu and R parameters do not match" ); |
---|
| 191 | dim = mu.length(); |
---|
| 192 | } |
---|
| 193 | //!@} |
---|
[270] | 194 | |
---|
[737] | 195 | //! \name Mathematical operations |
---|
| 196 | //!@{ |
---|
[270] | 197 | |
---|
[737] | 198 | //! dupdate in exponential form (not really handy) |
---|
| 199 | void dupdate ( mat &v, double nu = 1.0 ); |
---|
[28] | 200 | |
---|
[737] | 201 | vec sample() const; |
---|
[450] | 202 | |
---|
[737] | 203 | double evallog_nn ( const vec &val ) const; |
---|
| 204 | double lognc () const; |
---|
| 205 | vec mean() const { |
---|
| 206 | return mu; |
---|
| 207 | } |
---|
| 208 | vec variance() const { |
---|
| 209 | return diag ( R.to_mat() ); |
---|
| 210 | } |
---|
[299] | 211 | // mlnorm<sq_T>* condition ( const RV &rvn ) const ; <=========== fails to cmpile. Why? |
---|
[737] | 212 | shared_ptr<pdf> condition ( const RV &rvn ) const; |
---|
[504] | 213 | |
---|
[737] | 214 | // target not typed to mlnorm<sq_T, enorm<sq_T> > & |
---|
| 215 | // because that doesn't compile (perhaps because we |
---|
| 216 | // haven't finished defining enorm yet), but the type |
---|
| 217 | // is required |
---|
| 218 | void condition ( const RV &rvn, pdf &target ) const; |
---|
[504] | 219 | |
---|
[737] | 220 | shared_ptr<epdf> marginal ( const RV &rvn ) const; |
---|
| 221 | void marginal ( const RV &rvn, enorm<sq_T> &target ) const; |
---|
| 222 | //!@} |
---|
[270] | 223 | |
---|
[737] | 224 | //! \name Access to attributes |
---|
| 225 | //!@{ |
---|
[270] | 226 | |
---|
[737] | 227 | vec& _mu() { |
---|
| 228 | return mu; |
---|
| 229 | } |
---|
| 230 | const vec& _mu() const { |
---|
| 231 | return mu; |
---|
| 232 | } |
---|
| 233 | void set_mu ( const vec mu0 ) { |
---|
| 234 | mu = mu0; |
---|
| 235 | } |
---|
| 236 | sq_T& _R() { |
---|
| 237 | return R; |
---|
| 238 | } |
---|
| 239 | const sq_T& _R() const { |
---|
| 240 | return R; |
---|
| 241 | } |
---|
| 242 | //!@} |
---|
[28] | 243 | |
---|
[488] | 244 | }; |
---|
[737] | 245 | UIREGISTER2 ( enorm, chmat ); |
---|
[529] | 246 | SHAREDPTR2 ( enorm, chmat ); |
---|
[737] | 247 | UIREGISTER2 ( enorm, ldmat ); |
---|
[529] | 248 | SHAREDPTR2 ( enorm, ldmat ); |
---|
[737] | 249 | UIREGISTER2 ( enorm, fsqmat ); |
---|
[529] | 250 | SHAREDPTR2 ( enorm, fsqmat ); |
---|
[8] | 251 | |
---|
[388] | 252 | |
---|
[488] | 253 | /*! |
---|
| 254 | * \brief Gauss-inverse-Wishart density stored in LD form |
---|
[96] | 255 | |
---|
[488] | 256 | * For \f$p\f$-variate densities, given rv.count() should be \f$p\times\f$ V.rows(). |
---|
| 257 | * |
---|
| 258 | */ |
---|
[737] | 259 | class egiw : public eEF { |
---|
| 260 | protected: |
---|
| 261 | //! Extended information matrix of sufficient statistics |
---|
| 262 | ldmat V; |
---|
| 263 | //! Number of data records (degrees of freedom) of sufficient statistics |
---|
| 264 | double nu; |
---|
| 265 | //! Dimension of the output |
---|
| 266 | int dimx; |
---|
| 267 | //! Dimension of the regressor |
---|
| 268 | int nPsi; |
---|
| 269 | public: |
---|
| 270 | //!\name Constructors |
---|
| 271 | //!@{ |
---|
| 272 | egiw() : eEF() {}; |
---|
| 273 | egiw ( int dimx0, ldmat V0, double nu0 = -1.0 ) : eEF() { |
---|
| 274 | set_parameters ( dimx0, V0, nu0 ); |
---|
| 275 | }; |
---|
[270] | 276 | |
---|
[737] | 277 | void set_parameters ( int dimx0, ldmat V0, double nu0 = -1.0 ); |
---|
| 278 | //!@} |
---|
[96] | 279 | |
---|
[737] | 280 | vec sample() const; |
---|
| 281 | mat sample_mat ( int n ) const; |
---|
| 282 | vec mean() const; |
---|
| 283 | vec variance() const; |
---|
| 284 | void sample_mat ( mat &Mi, chmat &Ri ) const; |
---|
[330] | 285 | |
---|
[737] | 286 | void factorize ( mat &M, ldmat &Vz, ldmat &Lam ) const; |
---|
| 287 | //! LS estimate of \f$\theta\f$ |
---|
| 288 | vec est_theta() const; |
---|
[330] | 289 | |
---|
[737] | 290 | //! Covariance of the LS estimate |
---|
| 291 | ldmat est_theta_cov() const; |
---|
[96] | 292 | |
---|
[737] | 293 | //! expected values of the linear coefficient and the covariance matrix are written to \c M and \c R , respectively |
---|
| 294 | void mean_mat ( mat &M, mat&R ) const; |
---|
| 295 | //! In this instance, val= [theta, r]. For multivariate instances, it is stored columnwise val = [theta_1 theta_2 ... r_1 r_2 ] |
---|
| 296 | double evallog_nn ( const vec &val ) const; |
---|
| 297 | double lognc () const; |
---|
| 298 | void pow ( double p ) { |
---|
| 299 | V *= p; |
---|
| 300 | nu *= p; |
---|
| 301 | }; |
---|
[270] | 302 | |
---|
[737] | 303 | //! \name Access attributes |
---|
| 304 | //!@{ |
---|
| 305 | |
---|
| 306 | ldmat& _V() { |
---|
| 307 | return V; |
---|
| 308 | } |
---|
| 309 | const ldmat& _V() const { |
---|
| 310 | return V; |
---|
| 311 | } |
---|
| 312 | double& _nu() { |
---|
| 313 | return nu; |
---|
| 314 | } |
---|
| 315 | const double& _nu() const { |
---|
| 316 | return nu; |
---|
| 317 | } |
---|
| 318 | const int & _dimx() const { |
---|
| 319 | return dimx; |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | /*! Create Gauss-inverse-Wishart density |
---|
| 323 | \f[ f(rv) = GiW(V,\nu) \f] |
---|
| 324 | from structure |
---|
| 325 | \code |
---|
| 326 | class = 'egiw'; |
---|
| 327 | V = []; // square matrix |
---|
| 328 | dV = []; // vector of diagonal of V (when V not given) |
---|
| 329 | nu = []; // scalar \nu ((almost) degrees of freedom) |
---|
| 330 | // when missing, it will be computed to obtain proper pdf |
---|
| 331 | dimx = []; // dimension of the wishart part |
---|
| 332 | rv = RV({'name'}) // description of RV |
---|
| 333 | rvc = RV({'name'}) // description of RV in condition |
---|
| 334 | \endcode |
---|
| 335 | */ |
---|
| 336 | |
---|
| 337 | void from_setting ( const Setting &set ) { |
---|
| 338 | epdf::from_setting ( set ); |
---|
| 339 | UI::get ( dimx, set, "dimx", UI::compulsory ); |
---|
| 340 | if ( !UI::get ( nu, set, "nu", UI::optional ) ) { |
---|
| 341 | nu = -1; |
---|
[488] | 342 | } |
---|
[737] | 343 | mat V; |
---|
| 344 | if ( !UI::get ( V, set, "V", UI::optional ) ) { |
---|
| 345 | vec dV; |
---|
| 346 | UI::get ( dV, set, "dV", UI::compulsory ); |
---|
| 347 | set_parameters ( dimx, ldmat ( dV ), nu ); |
---|
| 348 | |
---|
| 349 | } else { |
---|
| 350 | set_parameters ( dimx, V, nu ); |
---|
[665] | 351 | } |
---|
[737] | 352 | } |
---|
| 353 | |
---|
| 354 | void to_setting ( Setting& set ) const { |
---|
| 355 | epdf::to_setting ( set ); |
---|
| 356 | UI::save ( dimx, set, "dimx" ); |
---|
| 357 | UI::save ( V.to_mat(), set, "V" ); |
---|
| 358 | UI::save ( nu, set, "nu" ); |
---|
| 359 | }; |
---|
| 360 | |
---|
| 361 | void validate() { |
---|
| 362 | // check sizes, rvs etc. |
---|
| 363 | } |
---|
[739] | 364 | void log_register ( bdm::logger& L, const string& prefix ); |
---|
[737] | 365 | |
---|
[739] | 366 | void log_write() const; |
---|
[737] | 367 | //!@} |
---|
[488] | 368 | }; |
---|
[529] | 369 | UIREGISTER ( egiw ); |
---|
| 370 | SHAREDPTR ( egiw ); |
---|
[96] | 371 | |
---|
[488] | 372 | /*! \brief Dirichlet posterior density |
---|
[173] | 373 | |
---|
[488] | 374 | Continuous Dirichlet density of \f$n\f$-dimensional variable \f$x\f$ |
---|
| 375 | \f[ |
---|
| 376 | f(x|\beta) = \frac{\Gamma[\gamma]}{\prod_{i=1}^{n}\Gamma(\beta_i)} \prod_{i=1}^{n}x_i^{\beta_i-1} |
---|
| 377 | \f] |
---|
| 378 | where \f$\gamma=\sum_i \beta_i\f$. |
---|
| 379 | */ |
---|
[737] | 380 | class eDirich: public eEF { |
---|
| 381 | protected: |
---|
| 382 | //!sufficient statistics |
---|
| 383 | vec beta; |
---|
| 384 | public: |
---|
| 385 | //!\name Constructors |
---|
| 386 | //!@{ |
---|
[270] | 387 | |
---|
[737] | 388 | eDirich () : eEF () {}; |
---|
| 389 | eDirich ( const eDirich &D0 ) : eEF () { |
---|
| 390 | set_parameters ( D0.beta ); |
---|
| 391 | }; |
---|
| 392 | eDirich ( const vec &beta0 ) { |
---|
| 393 | set_parameters ( beta0 ); |
---|
| 394 | }; |
---|
| 395 | void set_parameters ( const vec &beta0 ) { |
---|
| 396 | beta = beta0; |
---|
| 397 | dim = beta.length(); |
---|
| 398 | } |
---|
| 399 | //!@} |
---|
[270] | 400 | |
---|
[737] | 401 | //! using sampling procedure from wikipedia |
---|
| 402 | vec sample() const { |
---|
| 403 | vec y ( beta.length() ); |
---|
| 404 | for ( int i = 0; i < beta.length(); i++ ) { |
---|
| 405 | GamRNG.setup ( beta ( i ), 1 ); |
---|
| 406 | #pragma omp critical |
---|
| 407 | y ( i ) = GamRNG(); |
---|
[565] | 408 | } |
---|
[737] | 409 | return y / sum ( y ); |
---|
| 410 | } |
---|
[565] | 411 | |
---|
[737] | 412 | vec mean() const { |
---|
| 413 | return beta / sum ( beta ); |
---|
| 414 | }; |
---|
| 415 | vec variance() const { |
---|
| 416 | double gamma = sum ( beta ); |
---|
| 417 | return elem_mult ( beta, ( gamma - beta ) ) / ( gamma*gamma* ( gamma + 1 ) ); |
---|
| 418 | } |
---|
| 419 | //! In this instance, val is ... |
---|
| 420 | double evallog_nn ( const vec &val ) const { |
---|
| 421 | double tmp; |
---|
| 422 | tmp = ( beta - 1 ) * log ( val ); |
---|
| 423 | return tmp; |
---|
| 424 | } |
---|
[565] | 425 | |
---|
[737] | 426 | double lognc () const { |
---|
| 427 | double tmp; |
---|
| 428 | double gam = sum ( beta ); |
---|
| 429 | double lgb = 0.0; |
---|
| 430 | for ( int i = 0; i < beta.length(); i++ ) { |
---|
| 431 | lgb += lgamma ( beta ( i ) ); |
---|
[565] | 432 | } |
---|
[737] | 433 | tmp = lgb - lgamma ( gam ); |
---|
| 434 | return tmp; |
---|
| 435 | } |
---|
[565] | 436 | |
---|
[737] | 437 | //!access function |
---|
| 438 | vec& _beta() { |
---|
| 439 | return beta; |
---|
| 440 | } |
---|
| 441 | /*! configuration structure |
---|
| 442 | \code |
---|
| 443 | class = 'eDirich'; |
---|
| 444 | beta = []; //parametr beta |
---|
| 445 | \endcode |
---|
| 446 | */ |
---|
| 447 | void from_setting ( const Setting &set ) { |
---|
| 448 | epdf::from_setting ( set ); |
---|
| 449 | UI::get ( beta, set, "beta", UI::compulsory ); |
---|
| 450 | validate(); |
---|
| 451 | } |
---|
| 452 | void validate() { |
---|
| 453 | //check rv |
---|
| 454 | dim = beta.length(); |
---|
| 455 | } |
---|
[746] | 456 | |
---|
| 457 | void to_setting ( Setting &set ) const |
---|
| 458 | { |
---|
| 459 | eEF::to_setting( set ); |
---|
| 460 | UI::save( beta, set, "beta" ); |
---|
| 461 | } |
---|
[488] | 462 | }; |
---|
[737] | 463 | UIREGISTER ( eDirich ); |
---|
[96] | 464 | |
---|
[637] | 465 | /*! Random Walk on Dirichlet |
---|
[737] | 466 | Using simple assignment |
---|
[637] | 467 | \f[ \beta = rvc / k + \beta_c \f] |
---|
| 468 | hence, mean value = rvc, variance = (k+1)*mean*mean; |
---|
[737] | 469 | |
---|
[637] | 470 | The greater k is, the greater is the variance of the random walk; |
---|
[737] | 471 | |
---|
[637] | 472 | \f$ \beta_c \f$ is used as regularizing element to avoid corner cases, i.e. when one element of rvc is zero. |
---|
| 473 | By default is it set to 0.1; |
---|
| 474 | */ |
---|
| 475 | |
---|
[693] | 476 | class mDirich: public pdf_internal<eDirich> { |
---|
[737] | 477 | protected: |
---|
| 478 | //! constant \f$ k \f$ of the random walk |
---|
| 479 | double k; |
---|
| 480 | //! cache of beta_i |
---|
| 481 | vec &_beta; |
---|
| 482 | //! stabilizing coefficient \f$ \beta_c \f$ |
---|
| 483 | vec betac; |
---|
| 484 | public: |
---|
| 485 | mDirich() : pdf_internal<eDirich>(), _beta ( iepdf._beta() ) {}; |
---|
| 486 | void condition ( const vec &val ) { |
---|
| 487 | _beta = val / k + betac; |
---|
| 488 | }; |
---|
| 489 | /*! Create Dirichlet random walk |
---|
| 490 | \f[ f(rv|rvc) = Di(rvc*k) \f] |
---|
| 491 | from structure |
---|
| 492 | \code |
---|
| 493 | class = 'mDirich'; |
---|
| 494 | k = 1; // multiplicative constant k |
---|
| 495 | --- optional --- |
---|
| 496 | rv = RV({'name'},size) // description of RV |
---|
| 497 | beta0 = []; // initial value of beta |
---|
| 498 | betac = []; // initial value of beta |
---|
| 499 | \endcode |
---|
| 500 | */ |
---|
| 501 | void from_setting ( const Setting &set ) { |
---|
| 502 | pdf::from_setting ( set ); // reads rv and rvc |
---|
| 503 | if ( _rv()._dsize() > 0 ) { |
---|
| 504 | rvc = _rv().copy_t ( -1 ); |
---|
[637] | 505 | } |
---|
[737] | 506 | vec beta0; |
---|
| 507 | if ( !UI::get ( beta0, set, "beta0", UI::optional ) ) { |
---|
| 508 | beta0 = ones ( _rv()._dsize() ); |
---|
| 509 | } |
---|
| 510 | if ( !UI::get ( betac, set, "betac", UI::optional ) ) { |
---|
| 511 | betac = 0.1 * ones ( _rv()._dsize() ); |
---|
| 512 | } |
---|
| 513 | _beta = beta0; |
---|
| 514 | |
---|
| 515 | UI::get ( k, set, "k", UI::compulsory ); |
---|
| 516 | validate(); |
---|
| 517 | } |
---|
| 518 | void validate() { |
---|
| 519 | pdf_internal<eDirich>::validate(); |
---|
| 520 | bdm_assert ( _beta.length() == betac.length(), "beta0 and betac are not compatible" ); |
---|
| 521 | if ( _rv()._dsize() > 0 ) { |
---|
| 522 | bdm_assert ( ( _rv()._dsize() == dimension() ) , "Size of rv does not match with beta" ); |
---|
| 523 | } |
---|
| 524 | dimc = _beta.length(); |
---|
| 525 | }; |
---|
[637] | 526 | }; |
---|
[737] | 527 | UIREGISTER ( mDirich ); |
---|
[637] | 528 | |
---|
[181] | 529 | //! \brief Estimator for Multinomial density |
---|
[737] | 530 | class multiBM : public BMEF { |
---|
| 531 | protected: |
---|
| 532 | //! Conjugate prior and posterior |
---|
| 533 | eDirich est; |
---|
| 534 | //! Pointer inside est to sufficient statistics |
---|
| 535 | vec β |
---|
| 536 | public: |
---|
| 537 | //!Default constructor |
---|
| 538 | multiBM () : BMEF (), est (), beta ( est._beta() ) { |
---|
| 539 | if ( beta.length() > 0 ) { |
---|
| 540 | last_lognc = est.lognc(); |
---|
| 541 | } else { |
---|
| 542 | last_lognc = 0.0; |
---|
[488] | 543 | } |
---|
[737] | 544 | } |
---|
| 545 | //!Copy constructor |
---|
| 546 | multiBM ( const multiBM &B ) : BMEF ( B ), est ( B.est ), beta ( est._beta() ) {} |
---|
| 547 | //! Sets sufficient statistics to match that of givefrom mB0 |
---|
| 548 | void set_statistics ( const BM* mB0 ) { |
---|
| 549 | const multiBM* mB = dynamic_cast<const multiBM*> ( mB0 ); |
---|
| 550 | beta = mB->beta; |
---|
| 551 | } |
---|
[739] | 552 | void bayes ( const vec &yt, const vec &cond = empty_vec ); |
---|
[176] | 553 | |
---|
[739] | 554 | double logpred ( const vec &yt ) const; |
---|
[170] | 555 | |
---|
[739] | 556 | void flatten ( const BMEF* B ); |
---|
| 557 | |
---|
[737] | 558 | //! return correctly typed posterior (covariant return) |
---|
| 559 | const eDirich& posterior() const { |
---|
| 560 | return est; |
---|
| 561 | }; |
---|
| 562 | //! constructor function |
---|
| 563 | void set_parameters ( const vec &beta0 ) { |
---|
| 564 | est.set_parameters ( beta0 ); |
---|
[741] | 565 | est.validate(); |
---|
[737] | 566 | if ( evalll ) { |
---|
| 567 | last_lognc = est.lognc(); |
---|
[488] | 568 | } |
---|
[737] | 569 | } |
---|
[746] | 570 | |
---|
[737] | 571 | void to_setting ( Setting &set ) const { |
---|
| 572 | BMEF::to_setting ( set ); |
---|
[746] | 573 | UI::save( &est, set, "prior" ); |
---|
[737] | 574 | } |
---|
[488] | 575 | }; |
---|
[746] | 576 | UIREGISTER( multiBM ); |
---|
[170] | 577 | |
---|
[488] | 578 | /*! |
---|
| 579 | \brief Gamma posterior density |
---|
[32] | 580 | |
---|
[488] | 581 | Multivariate Gamma density as product of independent univariate densities. |
---|
| 582 | \f[ |
---|
| 583 | f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i) |
---|
| 584 | \f] |
---|
| 585 | */ |
---|
[32] | 586 | |
---|
[737] | 587 | class egamma : public eEF { |
---|
| 588 | protected: |
---|
| 589 | //! Vector \f$\alpha\f$ |
---|
| 590 | vec alpha; |
---|
| 591 | //! Vector \f$\beta\f$ |
---|
| 592 | vec beta; |
---|
| 593 | public : |
---|
| 594 | //! \name Constructors |
---|
| 595 | //!@{ |
---|
| 596 | egamma () : eEF (), alpha ( 0 ), beta ( 0 ) {}; |
---|
| 597 | egamma ( const vec &a, const vec &b ) { |
---|
| 598 | set_parameters ( a, b ); |
---|
| 599 | }; |
---|
| 600 | void set_parameters ( const vec &a, const vec &b ) { |
---|
| 601 | alpha = a, beta = b; |
---|
| 602 | dim = alpha.length(); |
---|
| 603 | }; |
---|
| 604 | //!@} |
---|
[270] | 605 | |
---|
[737] | 606 | vec sample() const; |
---|
| 607 | double evallog ( const vec &val ) const; |
---|
| 608 | double lognc () const; |
---|
| 609 | //! Returns pointer to internal alpha. Potentially dengerous: use with care! |
---|
| 610 | vec& _alpha() { |
---|
| 611 | return alpha; |
---|
| 612 | } |
---|
| 613 | //! Returns pointer to internal beta. Potentially dengerous: use with care! |
---|
| 614 | vec& _beta() { |
---|
| 615 | return beta; |
---|
| 616 | } |
---|
| 617 | vec mean() const { |
---|
| 618 | return elem_div ( alpha, beta ); |
---|
| 619 | } |
---|
| 620 | vec variance() const { |
---|
| 621 | return elem_div ( alpha, elem_mult ( beta, beta ) ); |
---|
| 622 | } |
---|
[225] | 623 | |
---|
[737] | 624 | /*! Create Gamma density |
---|
| 625 | \f[ f(rv|rvc) = \Gamma(\alpha, \beta) \f] |
---|
| 626 | from structure |
---|
| 627 | \code |
---|
| 628 | class = 'egamma'; |
---|
| 629 | alpha = [...]; // vector of alpha |
---|
| 630 | beta = [...]; // vector of beta |
---|
| 631 | rv = RV({'name'}) // description of RV |
---|
| 632 | \endcode |
---|
| 633 | */ |
---|
| 634 | void from_setting ( const Setting &set ) { |
---|
| 635 | epdf::from_setting ( set ); // reads rv |
---|
| 636 | UI::get ( alpha, set, "alpha", UI::compulsory ); |
---|
| 637 | UI::get ( beta, set, "beta", UI::compulsory ); |
---|
| 638 | validate(); |
---|
| 639 | } |
---|
| 640 | void validate() { |
---|
| 641 | bdm_assert ( alpha.length() == beta.length(), "parameters do not match" ); |
---|
| 642 | dim = alpha.length(); |
---|
| 643 | } |
---|
[488] | 644 | }; |
---|
[737] | 645 | UIREGISTER ( egamma ); |
---|
[529] | 646 | SHAREDPTR ( egamma ); |
---|
| 647 | |
---|
[488] | 648 | /*! |
---|
| 649 | \brief Inverse-Gamma posterior density |
---|
[225] | 650 | |
---|
[488] | 651 | Multivariate inverse-Gamma density as product of independent univariate densities. |
---|
| 652 | \f[ |
---|
| 653 | f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i) |
---|
| 654 | \f] |
---|
[283] | 655 | |
---|
[488] | 656 | Vector \f$\beta\f$ has different meaning (in fact it is 1/beta as used in definition of iG) |
---|
[225] | 657 | |
---|
[488] | 658 | Inverse Gamma can be converted to Gamma using \f[ |
---|
| 659 | x\sim iG(a,b) => 1/x\sim G(a,1/b) |
---|
| 660 | \f] |
---|
| 661 | This relation is used in sampling. |
---|
| 662 | */ |
---|
[270] | 663 | |
---|
[737] | 664 | class eigamma : public egamma { |
---|
| 665 | protected: |
---|
| 666 | public : |
---|
| 667 | //! \name Constructors |
---|
| 668 | //! All constructors are inherited |
---|
| 669 | //!@{ |
---|
| 670 | //!@} |
---|
[32] | 671 | |
---|
[737] | 672 | vec sample() const { |
---|
| 673 | return 1.0 / egamma::sample(); |
---|
| 674 | }; |
---|
| 675 | //! Returns poiter to alpha and beta. Potentially dangerous: use with care! |
---|
| 676 | vec mean() const { |
---|
| 677 | return elem_div ( beta, alpha - 1 ); |
---|
| 678 | } |
---|
| 679 | vec variance() const { |
---|
| 680 | vec mea = mean(); |
---|
| 681 | return elem_div ( elem_mult ( mea, mea ), alpha - 2 ); |
---|
| 682 | } |
---|
[488] | 683 | }; |
---|
| 684 | /* |
---|
| 685 | //! Weighted mixture of epdfs with external owned components. |
---|
| 686 | class emix : public epdf { |
---|
| 687 | protected: |
---|
| 688 | int n; |
---|
| 689 | vec &w; |
---|
| 690 | Array<epdf*> Coms; |
---|
| 691 | public: |
---|
| 692 | //! Default constructor |
---|
| 693 | emix ( const RV &rv, vec &w0): epdf(rv), n(w0.length()), w(w0), Coms(n) {}; |
---|
| 694 | void set_parameters( int &i, double wi, epdf* ep){w(i)=wi;Coms(i)=ep;} |
---|
| 695 | vec mean(){vec pom; for(int i=0;i<n;i++){pom+=Coms(i)->mean()*w(i);} return pom;}; |
---|
| 696 | }; |
---|
| 697 | */ |
---|
| 698 | |
---|
[32] | 699 | //! Uniform distributed density on a rectangular support |
---|
| 700 | |
---|
[737] | 701 | class euni: public epdf { |
---|
| 702 | protected: |
---|
[32] | 703 | //! lower bound on support |
---|
[737] | 704 | vec low; |
---|
[32] | 705 | //! upper bound on support |
---|
[737] | 706 | vec high; |
---|
[32] | 707 | //! internal |
---|
[737] | 708 | vec distance; |
---|
[32] | 709 | //! normalizing coefficients |
---|
[737] | 710 | double nk; |
---|
[33] | 711 | //! cache of log( \c nk ) |
---|
[737] | 712 | double lnk; |
---|
| 713 | public: |
---|
| 714 | //! \name Constructors |
---|
| 715 | //!@{ |
---|
| 716 | euni () : epdf () {} |
---|
| 717 | euni ( const vec &low0, const vec &high0 ) { |
---|
| 718 | set_parameters ( low0, high0 ); |
---|
| 719 | } |
---|
| 720 | void set_parameters ( const vec &low0, const vec &high0 ) { |
---|
| 721 | distance = high0 - low0; |
---|
| 722 | low = low0; |
---|
| 723 | high = high0; |
---|
| 724 | nk = prod ( 1.0 / distance ); |
---|
| 725 | lnk = log ( nk ); |
---|
| 726 | dim = low.length(); |
---|
| 727 | } |
---|
| 728 | //!@} |
---|
[270] | 729 | |
---|
[737] | 730 | double evallog ( const vec &val ) const { |
---|
| 731 | if ( any ( val < low ) && any ( val > high ) ) { |
---|
| 732 | return -inf; |
---|
| 733 | } else return lnk; |
---|
| 734 | } |
---|
| 735 | vec sample() const { |
---|
| 736 | vec smp ( dim ); |
---|
[270] | 737 | #pragma omp critical |
---|
[737] | 738 | UniRNG.sample_vector ( dim , smp ); |
---|
| 739 | return low + elem_mult ( distance, smp ); |
---|
| 740 | } |
---|
| 741 | //! set values of \c low and \c high |
---|
| 742 | vec mean() const { |
---|
| 743 | return ( high - low ) / 2.0; |
---|
| 744 | } |
---|
| 745 | vec variance() const { |
---|
| 746 | return ( pow ( high, 2 ) + pow ( low, 2 ) + elem_mult ( high, low ) ) / 3.0; |
---|
| 747 | } |
---|
| 748 | /*! Create Uniform density |
---|
| 749 | \f[ f(rv) = U(low,high) \f] |
---|
| 750 | from structure |
---|
| 751 | \code |
---|
| 752 | class = 'euni' |
---|
| 753 | high = [...]; // vector of upper bounds |
---|
| 754 | low = [...]; // vector of lower bounds |
---|
| 755 | rv = RV({'name'}); // description of RV |
---|
| 756 | \endcode |
---|
| 757 | */ |
---|
| 758 | void from_setting ( const Setting &set ) { |
---|
| 759 | epdf::from_setting ( set ); // reads rv and rvc |
---|
[471] | 760 | |
---|
[737] | 761 | UI::get ( high, set, "high", UI::compulsory ); |
---|
| 762 | UI::get ( low, set, "low", UI::compulsory ); |
---|
| 763 | set_parameters ( low, high ); |
---|
| 764 | validate(); |
---|
| 765 | } |
---|
| 766 | void validate() { |
---|
| 767 | bdm_assert ( high.length() == low.length(), "Incompatible high and low vectors" ); |
---|
| 768 | dim = high.length(); |
---|
| 769 | bdm_assert ( min ( distance ) > 0.0, "bad support" ); |
---|
| 770 | } |
---|
[488] | 771 | }; |
---|
[737] | 772 | UIREGISTER ( euni ); |
---|
[32] | 773 | |
---|
[665] | 774 | //! Uniform density with conditional mean value |
---|
[737] | 775 | class mguni : public pdf_internal<euni> { |
---|
[665] | 776 | //! function of the mean value |
---|
| 777 | shared_ptr<fnc> mean; |
---|
| 778 | //! distance from mean to both sides |
---|
| 779 | vec delta; |
---|
[737] | 780 | public: |
---|
| 781 | void condition ( const vec &cond ) { |
---|
| 782 | vec mea = mean->eval ( cond ); |
---|
| 783 | iepdf.set_parameters ( mea - delta, mea + delta ); |
---|
| 784 | } |
---|
| 785 | //! load from |
---|
| 786 | void from_setting ( const Setting &set ) { |
---|
| 787 | pdf::from_setting ( set ); //reads rv and rvc |
---|
| 788 | UI::get ( delta, set, "delta", UI::compulsory ); |
---|
| 789 | mean = UI::build<fnc> ( set, "mean", UI::compulsory ); |
---|
| 790 | |
---|
| 791 | iepdf.set_parameters ( -delta, delta ); |
---|
| 792 | dimc = mean->dimensionc(); |
---|
| 793 | validate(); |
---|
| 794 | } |
---|
[665] | 795 | }; |
---|
[737] | 796 | UIREGISTER ( mguni ); |
---|
[488] | 797 | /*! |
---|
| 798 | \brief Normal distributed linear function with linear function of mean value; |
---|
[32] | 799 | |
---|
[536] | 800 | Mean value \f$ \mu=A*\mbox{rvc}+\mu_0 \f$. |
---|
[488] | 801 | */ |
---|
| 802 | template < class sq_T, template <typename> class TEpdf = enorm > |
---|
[737] | 803 | class mlnorm : public pdf_internal< TEpdf<sq_T> > { |
---|
| 804 | protected: |
---|
| 805 | //! Internal epdf that arise by conditioning on \c rvc |
---|
| 806 | mat A; |
---|
| 807 | //! Constant additive term |
---|
| 808 | vec mu_const; |
---|
[487] | 809 | // vec& _mu; //cached epdf.mu; !!!!!! WHY NOT? |
---|
[737] | 810 | public: |
---|
| 811 | //! \name Constructors |
---|
| 812 | //!@{ |
---|
| 813 | mlnorm() : pdf_internal< TEpdf<sq_T> >() {}; |
---|
| 814 | mlnorm ( const mat &A, const vec &mu0, const sq_T &R ) : pdf_internal< TEpdf<sq_T> >() { |
---|
| 815 | set_parameters ( A, mu0, R ); |
---|
| 816 | } |
---|
[461] | 817 | |
---|
[737] | 818 | //! Set \c A and \c R |
---|
| 819 | void set_parameters ( const mat &A0, const vec &mu0, const sq_T &R0 ) { |
---|
| 820 | this->iepdf.set_parameters ( zeros ( A0.rows() ), R0 ); |
---|
| 821 | A = A0; |
---|
| 822 | mu_const = mu0; |
---|
| 823 | this->dimc = A0.cols(); |
---|
| 824 | } |
---|
| 825 | //!@} |
---|
| 826 | //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it. |
---|
| 827 | void condition ( const vec &cond ) { |
---|
| 828 | this->iepdf._mu() = A * cond + mu_const; |
---|
[487] | 829 | //R is already assigned; |
---|
[737] | 830 | } |
---|
[198] | 831 | |
---|
[737] | 832 | //!access function |
---|
| 833 | const vec& _mu_const() const { |
---|
| 834 | return mu_const; |
---|
| 835 | } |
---|
| 836 | //!access function |
---|
| 837 | const mat& _A() const { |
---|
| 838 | return A; |
---|
| 839 | } |
---|
| 840 | //!access function |
---|
| 841 | mat _R() const { |
---|
| 842 | return this->iepdf._R().to_mat(); |
---|
| 843 | } |
---|
| 844 | //!access function |
---|
| 845 | sq_T __R() const { |
---|
| 846 | return this->iepdf._R(); |
---|
| 847 | } |
---|
[8] | 848 | |
---|
[737] | 849 | //! Debug stream |
---|
| 850 | template<typename sq_M> |
---|
| 851 | friend std::ostream &operator<< ( std::ostream &os, mlnorm<sq_M, enorm> &ml ); |
---|
[488] | 852 | |
---|
[737] | 853 | /*! Create Normal density with linear function of mean value |
---|
| 854 | \f[ f(rv|rvc) = N(A*rvc+const, R) \f] |
---|
| 855 | from structure |
---|
| 856 | \code |
---|
| 857 | class = 'mlnorm<ldmat>', (OR) 'mlnorm<chmat>', (OR) 'mlnorm<fsqmat>'; |
---|
| 858 | A = []; // matrix or vector of appropriate dimension |
---|
| 859 | const = []; // vector of constant term |
---|
| 860 | R = []; // square matrix of appropriate dimension |
---|
| 861 | \endcode |
---|
| 862 | */ |
---|
| 863 | void from_setting ( const Setting &set ) { |
---|
| 864 | pdf::from_setting ( set ); |
---|
| 865 | |
---|
| 866 | UI::get ( A, set, "A", UI::compulsory ); |
---|
| 867 | UI::get ( mu_const, set, "const", UI::compulsory ); |
---|
| 868 | mat R0; |
---|
| 869 | UI::get ( R0, set, "R", UI::compulsory ); |
---|
| 870 | set_parameters ( A, mu_const, R0 ); |
---|
| 871 | validate(); |
---|
| 872 | }; |
---|
[763] | 873 | void to_setting (Setting &set) const { |
---|
| 874 | pdf::to_setting(set); |
---|
| 875 | UI::save ( A, set, "A"); |
---|
| 876 | UI::save ( mu_const, set, "const"); |
---|
| 877 | UI::save ( _R(), set, "R"); |
---|
| 878 | } |
---|
[737] | 879 | void validate() { |
---|
| 880 | pdf_internal<TEpdf<sq_T> >::validate(); |
---|
| 881 | bdm_assert ( A.rows() == mu_const.length(), "mlnorm: A vs. mu mismatch" ); |
---|
| 882 | bdm_assert ( A.rows() == _R().rows(), "mlnorm: A vs. R mismatch" ); |
---|
| 883 | |
---|
| 884 | } |
---|
[488] | 885 | }; |
---|
[737] | 886 | UIREGISTER2 ( mlnorm, ldmat ); |
---|
[529] | 887 | SHAREDPTR2 ( mlnorm, ldmat ); |
---|
[737] | 888 | UIREGISTER2 ( mlnorm, fsqmat ); |
---|
[529] | 889 | SHAREDPTR2 ( mlnorm, fsqmat ); |
---|
[737] | 890 | UIREGISTER2 ( mlnorm, chmat ); |
---|
[529] | 891 | SHAREDPTR2 ( mlnorm, chmat ); |
---|
[488] | 892 | |
---|
[693] | 893 | //! pdf with general function for mean value |
---|
[488] | 894 | template<class sq_T> |
---|
[737] | 895 | class mgnorm : public pdf_internal< enorm< sq_T > > { |
---|
| 896 | private: |
---|
[487] | 897 | // vec μ WHY NOT? |
---|
[737] | 898 | shared_ptr<fnc> g; |
---|
[527] | 899 | |
---|
[737] | 900 | public: |
---|
| 901 | //!default constructor |
---|
| 902 | mgnorm() : pdf_internal<enorm<sq_T> >() { } |
---|
| 903 | //!set mean function |
---|
| 904 | inline void set_parameters ( const shared_ptr<fnc> &g0, const sq_T &R0 ); |
---|
| 905 | inline void condition ( const vec &cond ); |
---|
[357] | 906 | |
---|
| 907 | |
---|
[737] | 908 | /*! Create Normal density with given function of mean value |
---|
| 909 | \f[ f(rv|rvc) = N( g(rvc), R) \f] |
---|
| 910 | from structure |
---|
| 911 | \code |
---|
| 912 | class = 'mgnorm'; |
---|
| 913 | g.class = 'fnc'; // function for mean value evolution |
---|
| 914 | g._fields_of_fnc = ...; |
---|
[357] | 915 | |
---|
[737] | 916 | R = [1, 0; // covariance matrix |
---|
| 917 | 0, 1]; |
---|
| 918 | --OR -- |
---|
| 919 | dR = [1, 1]; // diagonal of cavariance matrix |
---|
[357] | 920 | |
---|
[737] | 921 | rv = RV({'name'}) // description of RV |
---|
| 922 | rvc = RV({'name'}) // description of RV in condition |
---|
| 923 | \endcode |
---|
| 924 | */ |
---|
[357] | 925 | |
---|
[737] | 926 | void from_setting ( const Setting &set ) { |
---|
| 927 | pdf::from_setting ( set ); |
---|
| 928 | shared_ptr<fnc> g = UI::build<fnc> ( set, "g", UI::compulsory ); |
---|
[357] | 929 | |
---|
[737] | 930 | mat R; |
---|
| 931 | vec dR; |
---|
| 932 | if ( UI::get ( dR, set, "dR" ) ) |
---|
| 933 | R = diag ( dR ); |
---|
| 934 | else |
---|
| 935 | UI::get ( R, set, "R", UI::compulsory ); |
---|
[280] | 936 | |
---|
[737] | 937 | set_parameters ( g, R ); |
---|
| 938 | validate(); |
---|
| 939 | } |
---|
| 940 | void validate() { |
---|
| 941 | bdm_assert ( g->dimension() == this->dimension(), "incompatible function" ); |
---|
| 942 | } |
---|
[488] | 943 | }; |
---|
[357] | 944 | |
---|
[737] | 945 | UIREGISTER2 ( mgnorm, chmat ); |
---|
[529] | 946 | SHAREDPTR2 ( mgnorm, chmat ); |
---|
[357] | 947 | |
---|
[262] | 948 | |
---|
[488] | 949 | /*! (Approximate) Student t density with linear function of mean value |
---|
[262] | 950 | |
---|
[488] | 951 | The internal epdf of this class is of the type of a Gaussian (enorm). |
---|
| 952 | However, each conditioning is trying to assure the best possible approximation by taking into account the zeta function. See [] for reference. |
---|
[270] | 953 | |
---|
[488] | 954 | Perhaps a moment-matching technique? |
---|
| 955 | */ |
---|
[737] | 956 | class mlstudent : public mlnorm<ldmat, enorm> { |
---|
| 957 | protected: |
---|
| 958 | //! Variable \f$ \Lambda \f$ from theory |
---|
| 959 | ldmat Lambda; |
---|
| 960 | //! Reference to variable \f$ R \f$ |
---|
| 961 | ldmat &_R; |
---|
| 962 | //! Variable \f$ R_e \f$ |
---|
| 963 | ldmat Re; |
---|
| 964 | public: |
---|
| 965 | mlstudent () : mlnorm<ldmat, enorm> (), |
---|
| 966 | Lambda (), _R ( iepdf._R() ) {} |
---|
| 967 | //! constructor function |
---|
| 968 | void set_parameters ( const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0 ) { |
---|
| 969 | iepdf.set_parameters ( mu0, R0 );// was Lambda, why? |
---|
| 970 | A = A0; |
---|
| 971 | mu_const = mu0; |
---|
| 972 | Re = R0; |
---|
| 973 | Lambda = Lambda0; |
---|
| 974 | } |
---|
[294] | 975 | |
---|
[739] | 976 | void condition ( const vec &cond ); |
---|
| 977 | |
---|
[737] | 978 | void validate() { |
---|
| 979 | bdm_assert ( A.rows() == mu_const.length(), "mlstudent: A vs. mu mismatch" ); |
---|
| 980 | bdm_assert ( _R.rows() == A.rows(), "mlstudent: A vs. R mismatch" ); |
---|
| 981 | |
---|
| 982 | } |
---|
[488] | 983 | }; |
---|
| 984 | /*! |
---|
| 985 | \brief Gamma random walk |
---|
[198] | 986 | |
---|
[488] | 987 | Mean value, \f$\mu\f$, of this density is given by \c rvc . |
---|
| 988 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
---|
| 989 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
[32] | 990 | |
---|
[488] | 991 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
| 992 | */ |
---|
[737] | 993 | class mgamma : public pdf_internal<egamma> { |
---|
| 994 | protected: |
---|
[461] | 995 | |
---|
[737] | 996 | //! Constant \f$k\f$ |
---|
| 997 | double k; |
---|
[461] | 998 | |
---|
[737] | 999 | //! cache of iepdf.beta |
---|
| 1000 | vec &_beta; |
---|
[32] | 1001 | |
---|
[737] | 1002 | public: |
---|
| 1003 | //! Constructor |
---|
| 1004 | mgamma() : pdf_internal<egamma>(), k ( 0 ), |
---|
| 1005 | _beta ( iepdf._beta() ) { |
---|
| 1006 | } |
---|
[461] | 1007 | |
---|
[737] | 1008 | //! Set value of \c k |
---|
| 1009 | void set_parameters ( double k, const vec &beta0 ); |
---|
[461] | 1010 | |
---|
[737] | 1011 | void condition ( const vec &val ) { |
---|
| 1012 | _beta = k / val; |
---|
| 1013 | }; |
---|
| 1014 | /*! Create Gamma density with conditional mean value |
---|
| 1015 | \f[ f(rv|rvc) = \Gamma(k, k/rvc) \f] |
---|
| 1016 | from structure |
---|
| 1017 | \code |
---|
| 1018 | class = 'mgamma'; |
---|
| 1019 | beta = [...]; // vector of initial alpha |
---|
| 1020 | k = 1.1; // multiplicative constant k |
---|
| 1021 | rv = RV({'name'}) // description of RV |
---|
| 1022 | rvc = RV({'name'}) // description of RV in condition |
---|
| 1023 | \endcode |
---|
| 1024 | */ |
---|
| 1025 | void from_setting ( const Setting &set ) { |
---|
| 1026 | pdf::from_setting ( set ); // reads rv and rvc |
---|
| 1027 | vec betatmp; // ugly but necessary |
---|
| 1028 | UI::get ( betatmp, set, "beta", UI::compulsory ); |
---|
| 1029 | UI::get ( k, set, "k", UI::compulsory ); |
---|
| 1030 | set_parameters ( k, betatmp ); |
---|
| 1031 | validate(); |
---|
| 1032 | } |
---|
| 1033 | void validate() { |
---|
| 1034 | pdf_internal<egamma>::validate(); |
---|
| 1035 | |
---|
| 1036 | dim = _beta.length(); |
---|
| 1037 | dimc = _beta.length(); |
---|
| 1038 | } |
---|
[488] | 1039 | }; |
---|
[737] | 1040 | UIREGISTER ( mgamma ); |
---|
| 1041 | SHAREDPTR ( mgamma ); |
---|
[32] | 1042 | |
---|
[488] | 1043 | /*! |
---|
| 1044 | \brief Inverse-Gamma random walk |
---|
[225] | 1045 | |
---|
[488] | 1046 | Mean value, \f$ \mu \f$, of this density is given by \c rvc . |
---|
| 1047 | Standard deviation of the random walk is proportional to one \f$ k \f$-th the mean. |
---|
| 1048 | This is achieved by setting \f$ \alpha=\mu/k^2+2 \f$ and \f$ \beta=\mu(\alpha-1)\f$. |
---|
[461] | 1049 | |
---|
[488] | 1050 | The standard deviation of the walk is then: \f$ \mu/\sqrt(k)\f$. |
---|
| 1051 | */ |
---|
[737] | 1052 | class migamma : public pdf_internal<eigamma> { |
---|
| 1053 | protected: |
---|
| 1054 | //! Constant \f$k\f$ |
---|
| 1055 | double k; |
---|
[461] | 1056 | |
---|
[737] | 1057 | //! cache of iepdf.alpha |
---|
| 1058 | vec &_alpha; |
---|
[225] | 1059 | |
---|
[737] | 1060 | //! cache of iepdf.beta |
---|
| 1061 | vec &_beta; |
---|
[461] | 1062 | |
---|
[737] | 1063 | public: |
---|
| 1064 | //! \name Constructors |
---|
| 1065 | //!@{ |
---|
| 1066 | migamma() : pdf_internal<eigamma>(), |
---|
| 1067 | k ( 0 ), |
---|
| 1068 | _alpha ( iepdf._alpha() ), |
---|
| 1069 | _beta ( iepdf._beta() ) { |
---|
| 1070 | } |
---|
[225] | 1071 | |
---|
[737] | 1072 | migamma ( const migamma &m ) : pdf_internal<eigamma>(), |
---|
| 1073 | k ( 0 ), |
---|
| 1074 | _alpha ( iepdf._alpha() ), |
---|
| 1075 | _beta ( iepdf._beta() ) { |
---|
| 1076 | } |
---|
| 1077 | //!@} |
---|
[225] | 1078 | |
---|
[737] | 1079 | //! Set value of \c k |
---|
| 1080 | void set_parameters ( int len, double k0 ) { |
---|
| 1081 | k = k0; |
---|
| 1082 | iepdf.set_parameters ( ( 1.0 / ( k*k ) + 2.0 ) *ones ( len ) /*alpha*/, ones ( len ) /*beta*/ ); |
---|
| 1083 | dimc = dimension(); |
---|
| 1084 | }; |
---|
| 1085 | void condition ( const vec &val ) { |
---|
| 1086 | _beta = elem_mult ( val, ( _alpha - 1.0 ) ); |
---|
| 1087 | }; |
---|
[488] | 1088 | }; |
---|
[357] | 1089 | |
---|
[60] | 1090 | |
---|
[488] | 1091 | /*! |
---|
| 1092 | \brief Gamma random walk around a fixed point |
---|
[60] | 1093 | |
---|
[488] | 1094 | Mean value, \f$\mu\f$, of this density is given by a geometric combination of \c rvc and given fixed point, \f$p\f$. \f$l\f$ is the coefficient of the geometric combimation |
---|
| 1095 | \f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f] |
---|
[60] | 1096 | |
---|
[488] | 1097 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
---|
| 1098 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
[294] | 1099 | |
---|
[488] | 1100 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
| 1101 | */ |
---|
[737] | 1102 | class mgamma_fix : public mgamma { |
---|
| 1103 | protected: |
---|
| 1104 | //! parameter l |
---|
| 1105 | double l; |
---|
| 1106 | //! reference vector |
---|
| 1107 | vec refl; |
---|
| 1108 | public: |
---|
| 1109 | //! Constructor |
---|
| 1110 | mgamma_fix () : mgamma (), refl () {}; |
---|
| 1111 | //! Set value of \c k |
---|
| 1112 | void set_parameters ( double k0 , vec ref0, double l0 ) { |
---|
| 1113 | mgamma::set_parameters ( k0, ref0 ); |
---|
| 1114 | refl = pow ( ref0, 1.0 - l0 ); |
---|
| 1115 | l = l0; |
---|
| 1116 | dimc = dimension(); |
---|
| 1117 | }; |
---|
[60] | 1118 | |
---|
[737] | 1119 | void condition ( const vec &val ) { |
---|
| 1120 | vec mean = elem_mult ( refl, pow ( val, l ) ); |
---|
| 1121 | _beta = k / mean; |
---|
| 1122 | }; |
---|
[488] | 1123 | }; |
---|
[60] | 1124 | |
---|
[225] | 1125 | |
---|
[488] | 1126 | /*! |
---|
| 1127 | \brief Inverse-Gamma random walk around a fixed point |
---|
[225] | 1128 | |
---|
[488] | 1129 | Mean value, \f$\mu\f$, of this density is given by a geometric combination of \c rvc and given fixed point, \f$p\f$. \f$l\f$ is the coefficient of the geometric combimation |
---|
| 1130 | \f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f] |
---|
[225] | 1131 | |
---|
[488] | 1132 | ==== Check == vv = |
---|
| 1133 | Standard deviation of the random walk is proportional to one \f$k\f$-th the mean. |
---|
| 1134 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
[225] | 1135 | |
---|
[488] | 1136 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
| 1137 | */ |
---|
[737] | 1138 | class migamma_ref : public migamma { |
---|
| 1139 | protected: |
---|
| 1140 | //! parameter l |
---|
| 1141 | double l; |
---|
| 1142 | //! reference vector |
---|
| 1143 | vec refl; |
---|
| 1144 | public: |
---|
| 1145 | //! Constructor |
---|
| 1146 | migamma_ref () : migamma (), refl () {}; |
---|
[766] | 1147 | |
---|
[737] | 1148 | //! Set value of \c k |
---|
| 1149 | void set_parameters ( double k0 , vec ref0, double l0 ) { |
---|
| 1150 | migamma::set_parameters ( ref0.length(), k0 ); |
---|
| 1151 | refl = pow ( ref0, 1.0 - l0 ); |
---|
| 1152 | l = l0; |
---|
| 1153 | dimc = dimension(); |
---|
| 1154 | }; |
---|
[357] | 1155 | |
---|
[737] | 1156 | void condition ( const vec &val ) { |
---|
| 1157 | vec mean = elem_mult ( refl, pow ( val, l ) ); |
---|
| 1158 | migamma::condition ( mean ); |
---|
| 1159 | }; |
---|
[357] | 1160 | |
---|
| 1161 | |
---|
[737] | 1162 | /*! Create inverse-Gamma density with conditional mean value |
---|
| 1163 | \f[ f(rv|rvc) = i\Gamma(k, k/(rvc^l \circ ref^{(1-l)}) \f] |
---|
| 1164 | from structure |
---|
| 1165 | \code |
---|
| 1166 | class = 'migamma_ref'; |
---|
| 1167 | ref = [1e-5; 1e-5; 1e-2 1e-3]; // reference vector |
---|
| 1168 | l = 0.999; // constant l |
---|
| 1169 | k = 0.1; // constant k |
---|
| 1170 | rv = RV({'name'}) // description of RV |
---|
| 1171 | rvc = RV({'name'}) // description of RV in condition |
---|
| 1172 | \endcode |
---|
| 1173 | */ |
---|
| 1174 | void from_setting ( const Setting &set ); |
---|
[225] | 1175 | |
---|
[737] | 1176 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
[488] | 1177 | }; |
---|
[357] | 1178 | |
---|
| 1179 | |
---|
[737] | 1180 | UIREGISTER ( migamma_ref ); |
---|
| 1181 | SHAREDPTR ( migamma_ref ); |
---|
[294] | 1182 | |
---|
[488] | 1183 | /*! Log-Normal probability density |
---|
| 1184 | only allow diagonal covariances! |
---|
[294] | 1185 | |
---|
[488] | 1186 | Density of the form \f$ \log(x)\sim \mathcal{N}(\mu,\sigma^2) \f$ , i.e. |
---|
| 1187 | \f[ |
---|
| 1188 | x \sim \frac{1}{x\sigma\sqrt{2\pi}}\exp{-\frac{1}{2\sigma^2}(\log(x)-\mu)} |
---|
| 1189 | \f] |
---|
[294] | 1190 | |
---|
[621] | 1191 | Function from_setting loads mu and R in the same way as it does for enorm<>! |
---|
[488] | 1192 | */ |
---|
[737] | 1193 | class elognorm: public enorm<ldmat> { |
---|
| 1194 | public: |
---|
| 1195 | vec sample() const { |
---|
| 1196 | return exp ( enorm<ldmat>::sample() ); |
---|
| 1197 | }; |
---|
| 1198 | vec mean() const { |
---|
| 1199 | vec var = enorm<ldmat>::variance(); |
---|
| 1200 | return exp ( mu - 0.5*var ); |
---|
| 1201 | }; |
---|
[285] | 1202 | |
---|
[488] | 1203 | }; |
---|
[285] | 1204 | |
---|
[488] | 1205 | /*! |
---|
| 1206 | \brief Log-Normal random walk |
---|
[285] | 1207 | |
---|
[488] | 1208 | Mean value, \f$\mu\f$, is... |
---|
[285] | 1209 | |
---|
[488] | 1210 | */ |
---|
[737] | 1211 | class mlognorm : public pdf_internal<elognorm> { |
---|
| 1212 | protected: |
---|
| 1213 | //! parameter 1/2*sigma^2 |
---|
| 1214 | double sig2; |
---|
[461] | 1215 | |
---|
[737] | 1216 | //! access |
---|
| 1217 | vec μ |
---|
| 1218 | public: |
---|
| 1219 | //! Constructor |
---|
| 1220 | mlognorm() : pdf_internal<elognorm>(), |
---|
| 1221 | sig2 ( 0 ), |
---|
| 1222 | mu ( iepdf._mu() ) { |
---|
| 1223 | } |
---|
[285] | 1224 | |
---|
[737] | 1225 | //! Set value of \c k |
---|
| 1226 | void set_parameters ( int size, double k ) { |
---|
| 1227 | sig2 = 0.5 * log ( k * k + 1 ); |
---|
| 1228 | iepdf.set_parameters ( zeros ( size ), 2*sig2*eye ( size ) ); |
---|
[285] | 1229 | |
---|
[737] | 1230 | dimc = size; |
---|
| 1231 | }; |
---|
[357] | 1232 | |
---|
[737] | 1233 | void condition ( const vec &val ) { |
---|
| 1234 | mu = log ( val ) - sig2;//elem_mult ( refl,pow ( val,l ) ); |
---|
| 1235 | }; |
---|
[357] | 1236 | |
---|
[737] | 1237 | /*! Create logNormal random Walk |
---|
| 1238 | \f[ f(rv|rvc) = log\mathcal{N}( \log(rvc)-0.5\log(k^2+1), k I) \f] |
---|
| 1239 | from structure |
---|
| 1240 | \code |
---|
| 1241 | class = 'mlognorm'; |
---|
| 1242 | k = 0.1; // "variance" k |
---|
| 1243 | mu0 = 0.1; // Initial value of mean |
---|
| 1244 | rv = RV({'name'}) // description of RV |
---|
| 1245 | rvc = RV({'name'}) // description of RV in condition |
---|
| 1246 | \endcode |
---|
| 1247 | */ |
---|
| 1248 | void from_setting ( const Setting &set ); |
---|
[357] | 1249 | |
---|
[737] | 1250 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
[285] | 1251 | |
---|
[488] | 1252 | }; |
---|
[294] | 1253 | |
---|
[737] | 1254 | UIREGISTER ( mlognorm ); |
---|
| 1255 | SHAREDPTR ( mlognorm ); |
---|
[294] | 1256 | |
---|
[488] | 1257 | /*! inverse Wishart density defined on Choleski decomposition |
---|
| 1258 | |
---|
| 1259 | */ |
---|
[737] | 1260 | class eWishartCh : public epdf { |
---|
| 1261 | protected: |
---|
| 1262 | //! Upper-Triagle of Choleski decomposition of \f$ \Psi \f$ |
---|
| 1263 | chmat Y; |
---|
| 1264 | //! dimension of matrix \f$ \Psi \f$ |
---|
| 1265 | int p; |
---|
| 1266 | //! degrees of freedom \f$ \nu \f$ |
---|
| 1267 | double delta; |
---|
| 1268 | public: |
---|
| 1269 | //! Set internal structures |
---|
| 1270 | void set_parameters ( const mat &Y0, const double delta0 ) { |
---|
| 1271 | Y = chmat ( Y0 ); |
---|
| 1272 | delta = delta0; |
---|
| 1273 | p = Y.rows(); |
---|
| 1274 | dim = p * p; |
---|
| 1275 | } |
---|
| 1276 | //! Set internal structures |
---|
| 1277 | void set_parameters ( const chmat &Y0, const double delta0 ) { |
---|
| 1278 | Y = Y0; |
---|
| 1279 | delta = delta0; |
---|
| 1280 | p = Y.rows(); |
---|
| 1281 | dim = p * p; |
---|
| 1282 | } |
---|
| 1283 | //! Sample matrix argument |
---|
| 1284 | mat sample_mat() const { |
---|
| 1285 | mat X = zeros ( p, p ); |
---|
[488] | 1286 | |
---|
[737] | 1287 | //sample diagonal |
---|
| 1288 | for ( int i = 0; i < p; i++ ) { |
---|
| 1289 | GamRNG.setup ( 0.5* ( delta - i ) , 0.5 ); // no +1 !! index if from 0 |
---|
[294] | 1290 | #pragma omp critical |
---|
[737] | 1291 | X ( i, i ) = sqrt ( GamRNG() ); |
---|
| 1292 | } |
---|
| 1293 | //do the rest |
---|
| 1294 | for ( int i = 0; i < p; i++ ) { |
---|
| 1295 | for ( int j = i + 1; j < p; j++ ) { |
---|
[294] | 1296 | #pragma omp critical |
---|
[737] | 1297 | X ( i, j ) = NorRNG.sample(); |
---|
[294] | 1298 | } |
---|
[488] | 1299 | } |
---|
[737] | 1300 | return X*Y._Ch();// return upper triangular part of the decomposition |
---|
| 1301 | } |
---|
[766] | 1302 | |
---|
[737] | 1303 | vec sample () const { |
---|
| 1304 | return vec ( sample_mat()._data(), p*p ); |
---|
| 1305 | } |
---|
[766] | 1306 | |
---|
| 1307 | virtual vec mean() const NOT_IMPLEMENTED(0); |
---|
| 1308 | |
---|
| 1309 | //! return expected variance (not covariance!) |
---|
| 1310 | virtual vec variance() const NOT_IMPLEMENTED(0); |
---|
| 1311 | |
---|
| 1312 | virtual double evallog ( const vec &val ) const NOT_IMPLEMENTED(0); |
---|
| 1313 | |
---|
[737] | 1314 | //! fast access function y0 will be copied into Y.Ch. |
---|
| 1315 | void setY ( const mat &Ch0 ) { |
---|
| 1316 | copy_vector ( dim, Ch0._data(), Y._Ch()._data() ); |
---|
| 1317 | } |
---|
[766] | 1318 | |
---|
[737] | 1319 | //! fast access function y0 will be copied into Y.Ch. |
---|
| 1320 | void _setY ( const vec &ch0 ) { |
---|
| 1321 | copy_vector ( dim, ch0._data(), Y._Ch()._data() ); |
---|
| 1322 | } |
---|
[766] | 1323 | |
---|
[737] | 1324 | //! access function |
---|
| 1325 | const chmat& getY() const { |
---|
| 1326 | return Y; |
---|
| 1327 | } |
---|
[488] | 1328 | }; |
---|
[294] | 1329 | |
---|
[536] | 1330 | //! Inverse Wishart on Choleski decomposition |
---|
| 1331 | /*! Being computed by conversion from `standard' Wishart |
---|
| 1332 | */ |
---|
[737] | 1333 | class eiWishartCh: public epdf { |
---|
| 1334 | protected: |
---|
| 1335 | //! Internal instance of Wishart density |
---|
| 1336 | eWishartCh W; |
---|
| 1337 | //! size of Ch |
---|
| 1338 | int p; |
---|
| 1339 | //! parameter delta |
---|
| 1340 | double delta; |
---|
| 1341 | public: |
---|
| 1342 | //! constructor function |
---|
| 1343 | void set_parameters ( const mat &Y0, const double delta0 ) { |
---|
| 1344 | delta = delta0; |
---|
| 1345 | W.set_parameters ( inv ( Y0 ), delta0 ); |
---|
| 1346 | p = Y0.rows(); |
---|
| 1347 | } |
---|
[750] | 1348 | |
---|
| 1349 | virtual void validate (){ |
---|
| 1350 | dim = W.dimension(); |
---|
| 1351 | } |
---|
| 1352 | |
---|
| 1353 | |
---|
[737] | 1354 | vec sample() const { |
---|
| 1355 | mat iCh; |
---|
| 1356 | iCh = inv ( W.sample_mat() ); |
---|
| 1357 | return vec ( iCh._data(), dim ); |
---|
| 1358 | } |
---|
| 1359 | //! access function |
---|
| 1360 | void _setY ( const vec &y0 ) { |
---|
| 1361 | mat Ch ( p, p ); |
---|
| 1362 | mat iCh ( p, p ); |
---|
| 1363 | copy_vector ( dim, y0._data(), Ch._data() ); |
---|
[488] | 1364 | |
---|
[737] | 1365 | iCh = inv ( Ch ); |
---|
| 1366 | W.setY ( iCh ); |
---|
| 1367 | } |
---|
[766] | 1368 | |
---|
[737] | 1369 | virtual double evallog ( const vec &val ) const { |
---|
| 1370 | chmat X ( p ); |
---|
| 1371 | const chmat& Y = W.getY(); |
---|
[488] | 1372 | |
---|
[737] | 1373 | copy_vector ( p*p, val._data(), X._Ch()._data() ); |
---|
| 1374 | chmat iX ( p ); |
---|
| 1375 | X.inv ( iX ); |
---|
| 1376 | // compute |
---|
[294] | 1377 | // \frac{ |\Psi|^{m/2}|X|^{-(m+p+1)/2}e^{-tr(\Psi X^{-1})/2} }{ 2^{mp/2}\Gamma_p(m/2)}, |
---|
[737] | 1378 | mat M = Y.to_mat() * iX.to_mat(); |
---|
[285] | 1379 | |
---|
[737] | 1380 | double log1 = 0.5 * p * ( 2 * Y.logdet() ) - 0.5 * ( delta + p + 1 ) * ( 2 * X.logdet() ) - 0.5 * trace ( M ); |
---|
| 1381 | //Fixme! Multivariate gamma omitted!! it is ok for sampling, but not otherwise!! |
---|
[461] | 1382 | |
---|
[737] | 1383 | /* if (0) { |
---|
| 1384 | mat XX=X.to_mat(); |
---|
| 1385 | mat YY=Y.to_mat(); |
---|
[461] | 1386 | |
---|
[737] | 1387 | double log2 = 0.5*p*log(det(YY))-0.5*(delta+p+1)*log(det(XX))-0.5*trace(YY*inv(XX)); |
---|
| 1388 | cout << log1 << "," << log2 << endl; |
---|
| 1389 | }*/ |
---|
| 1390 | return log1; |
---|
| 1391 | }; |
---|
[285] | 1392 | |
---|
[766] | 1393 | virtual vec mean() const NOT_IMPLEMENTED(0); |
---|
| 1394 | |
---|
| 1395 | //! return expected variance (not covariance!) |
---|
| 1396 | virtual vec variance() const NOT_IMPLEMENTED(0); |
---|
[488] | 1397 | }; |
---|
| 1398 | |
---|
[536] | 1399 | //! Random Walk on inverse Wishart |
---|
[737] | 1400 | class rwiWishartCh : public pdf_internal<eiWishartCh> { |
---|
| 1401 | protected: |
---|
| 1402 | //!square root of \f$ \nu-p-1 \f$ - needed for computation of \f$ \Psi \f$ from conditions |
---|
| 1403 | double sqd; |
---|
| 1404 | //!reference point for diagonal |
---|
| 1405 | vec refl; |
---|
| 1406 | //! power of the reference |
---|
| 1407 | double l; |
---|
| 1408 | //! dimension |
---|
| 1409 | int p; |
---|
[488] | 1410 | |
---|
[737] | 1411 | public: |
---|
| 1412 | rwiWishartCh() : sqd ( 0 ), l ( 0 ), p ( 0 ) {} |
---|
| 1413 | //! constructor function |
---|
| 1414 | void set_parameters ( int p0, double k, vec ref0, double l0 ) { |
---|
| 1415 | p = p0; |
---|
| 1416 | double delta = 2 / ( k * k ) + p + 3; |
---|
| 1417 | sqd = sqrt ( delta - p - 1 ); |
---|
| 1418 | l = l0; |
---|
| 1419 | refl = pow ( ref0, 1 - l ); |
---|
[488] | 1420 | |
---|
[737] | 1421 | iepdf.set_parameters ( eye ( p ), delta ); |
---|
| 1422 | dimc = iepdf.dimension(); |
---|
| 1423 | } |
---|
| 1424 | void condition ( const vec &c ) { |
---|
| 1425 | vec z = c; |
---|
| 1426 | int ri = 0; |
---|
| 1427 | for ( int i = 0; i < p*p; i += ( p + 1 ) ) {//trace diagonal element |
---|
| 1428 | z ( i ) = pow ( z ( i ), l ) * refl ( ri ); |
---|
| 1429 | ri++; |
---|
[488] | 1430 | } |
---|
[285] | 1431 | |
---|
[737] | 1432 | iepdf._setY ( sqd*z ); |
---|
| 1433 | } |
---|
[488] | 1434 | }; |
---|
| 1435 | |
---|
[32] | 1436 | //! Switch between various resampling methods. |
---|
[488] | 1437 | enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 }; |
---|
| 1438 | /*! |
---|
| 1439 | \brief Weighted empirical density |
---|
[32] | 1440 | |
---|
[488] | 1441 | Used e.g. in particle filters. |
---|
| 1442 | */ |
---|
[737] | 1443 | class eEmp: public epdf { |
---|
| 1444 | protected : |
---|
| 1445 | //! Number of particles |
---|
| 1446 | int n; |
---|
| 1447 | //! Sample weights \f$w\f$ |
---|
| 1448 | vec w; |
---|
| 1449 | //! Samples \f$x^{(i)}, i=1..n\f$ |
---|
| 1450 | Array<vec> samples; |
---|
| 1451 | public: |
---|
| 1452 | //! \name Constructors |
---|
| 1453 | //!@{ |
---|
| 1454 | eEmp () : epdf (), w (), samples () {}; |
---|
| 1455 | //! copy constructor |
---|
| 1456 | eEmp ( const eEmp &e ) : epdf ( e ), w ( e.w ), samples ( e.samples ) {}; |
---|
| 1457 | //!@} |
---|
[280] | 1458 | |
---|
[737] | 1459 | //! Set samples and weights |
---|
| 1460 | void set_statistics ( const vec &w0, const epdf &pdf0 ); |
---|
| 1461 | //! Set samples and weights |
---|
| 1462 | void set_statistics ( const epdf &pdf0 , int n ) { |
---|
| 1463 | set_statistics ( ones ( n ) / n, pdf0 ); |
---|
| 1464 | }; |
---|
| 1465 | //! Set sample |
---|
| 1466 | void set_samples ( const epdf* pdf0 ); |
---|
| 1467 | //! Set sample |
---|
| 1468 | void set_parameters ( int n0, bool copy = true ) { |
---|
| 1469 | n = n0; |
---|
| 1470 | w.set_size ( n0, copy ); |
---|
| 1471 | samples.set_size ( n0, copy ); |
---|
| 1472 | }; |
---|
| 1473 | //! Set samples |
---|
| 1474 | void set_parameters ( const Array<vec> &Av ) { |
---|
| 1475 | n = Av.size(); |
---|
| 1476 | w = 1 / n * ones ( n ); |
---|
| 1477 | samples = Av; |
---|
| 1478 | }; |
---|
[750] | 1479 | virtual void validate (){ |
---|
[760] | 1480 | bdm_assert (samples.length()==w.length(),"samples and weigths are of different lengths"); |
---|
| 1481 | n = w.length(); |
---|
| 1482 | if (n>0) |
---|
[795] | 1483 | pdf::dim = samples ( 0 ).length(); |
---|
[750] | 1484 | } |
---|
[737] | 1485 | //! Potentially dangerous, use with care. |
---|
| 1486 | vec& _w() { |
---|
| 1487 | return w; |
---|
| 1488 | }; |
---|
| 1489 | //! Potentially dangerous, use with care. |
---|
| 1490 | const vec& _w() const { |
---|
| 1491 | return w; |
---|
| 1492 | }; |
---|
| 1493 | //! access function |
---|
| 1494 | Array<vec>& _samples() { |
---|
| 1495 | return samples; |
---|
| 1496 | }; |
---|
| 1497 | //! access function |
---|
| 1498 | const vec& _sample ( int i ) const { |
---|
| 1499 | return samples ( i ); |
---|
| 1500 | }; |
---|
| 1501 | //! access function |
---|
| 1502 | const Array<vec>& _samples() const { |
---|
| 1503 | return samples; |
---|
| 1504 | }; |
---|
| 1505 | //! Function performs resampling, i.e. removal of low-weight samples and duplication of high-weight samples such that the new samples represent the same density. |
---|
| 1506 | //! The vector with indeces of new samples is returned in variable \c index. |
---|
| 1507 | void resample ( ivec &index, RESAMPLING_METHOD method = SYSTEMATIC ); |
---|
[565] | 1508 | |
---|
[737] | 1509 | //! Resampling without returning index of new particles. |
---|
| 1510 | void resample ( RESAMPLING_METHOD method = SYSTEMATIC ) { |
---|
| 1511 | ivec ind; |
---|
| 1512 | resample ( ind, method ); |
---|
| 1513 | }; |
---|
[565] | 1514 | |
---|
[737] | 1515 | //! inherited operation : NOT implemented |
---|
[766] | 1516 | vec sample() const NOT_IMPLEMENTED(0); |
---|
[565] | 1517 | |
---|
[737] | 1518 | //! inherited operation : NOT implemented |
---|
[766] | 1519 | double evallog ( const vec &val ) const NOT_IMPLEMENTED(0); |
---|
[737] | 1520 | |
---|
| 1521 | vec mean() const { |
---|
| 1522 | vec pom = zeros ( dim ); |
---|
| 1523 | for ( int i = 0; i < n; i++ ) { |
---|
| 1524 | pom += samples ( i ) * w ( i ); |
---|
[488] | 1525 | } |
---|
[737] | 1526 | return pom; |
---|
| 1527 | } |
---|
| 1528 | vec variance() const { |
---|
| 1529 | vec pom = zeros ( dim ); |
---|
| 1530 | for ( int i = 0; i < n; i++ ) { |
---|
| 1531 | pom += pow ( samples ( i ), 2 ) * w ( i ); |
---|
[488] | 1532 | } |
---|
[737] | 1533 | return pom - pow ( mean(), 2 ); |
---|
| 1534 | } |
---|
| 1535 | //! For this class, qbounds are minimum and maximum value of the population! |
---|
[739] | 1536 | void qbounds ( vec &lb, vec &ub, double perc = 0.95 ) const; |
---|
[760] | 1537 | |
---|
| 1538 | void to_setting ( Setting &set ) const { |
---|
| 1539 | epdf::to_setting( set ); |
---|
| 1540 | UI::save ( samples, set, "samples" ); |
---|
| 1541 | UI::save ( w, set, "w" ); |
---|
| 1542 | } |
---|
| 1543 | |
---|
| 1544 | void from_setting ( const Setting &set ) { |
---|
| 1545 | epdf::from_setting( set ); |
---|
| 1546 | |
---|
| 1547 | UI::get( samples, set, "samples", UI::compulsory ); |
---|
| 1548 | UI::get ( w, set, "w", UI::compulsory ); |
---|
| 1549 | validate(); |
---|
| 1550 | } |
---|
| 1551 | |
---|
[488] | 1552 | }; |
---|
[760] | 1553 | UIREGISTER(eEmp); |
---|
[32] | 1554 | |
---|
| 1555 | |
---|
[8] | 1556 | //////////////////////// |
---|
| 1557 | |
---|
[488] | 1558 | template<class sq_T> |
---|
[737] | 1559 | void enorm<sq_T>::set_parameters ( const vec &mu0, const sq_T &R0 ) { |
---|
[28] | 1560 | //Fixme test dimensions of mu0 and R0; |
---|
[488] | 1561 | mu = mu0; |
---|
| 1562 | R = R0; |
---|
| 1563 | validate(); |
---|
| 1564 | }; |
---|
[8] | 1565 | |
---|
[488] | 1566 | template<class sq_T> |
---|
[737] | 1567 | void enorm<sq_T>::from_setting ( const Setting &set ) { |
---|
| 1568 | epdf::from_setting ( set ); //reads rv |
---|
[384] | 1569 | |
---|
[737] | 1570 | UI::get ( mu, set, "mu", UI::compulsory ); |
---|
[488] | 1571 | mat Rtmp;// necessary for conversion |
---|
[737] | 1572 | UI::get ( Rtmp, set, "R", UI::compulsory ); |
---|
[488] | 1573 | R = Rtmp; // conversion |
---|
| 1574 | validate(); |
---|
| 1575 | } |
---|
[8] | 1576 | |
---|
[488] | 1577 | template<class sq_T> |
---|
[773] | 1578 | void enorm<sq_T>::to_setting ( Setting &set ) const { |
---|
| 1579 | epdf::to_setting ( set ); //reads rv |
---|
| 1580 | UI::save ( mu, set, "mu"); |
---|
| 1581 | UI::save ( R.to_mat(), set, "R"); |
---|
| 1582 | } |
---|
| 1583 | |
---|
| 1584 | template<class sq_T> |
---|
[737] | 1585 | void enorm<sq_T>::dupdate ( mat &v, double nu ) { |
---|
[488] | 1586 | // |
---|
| 1587 | }; |
---|
| 1588 | |
---|
[178] | 1589 | // template<class sq_T> |
---|
| 1590 | // void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) { |
---|
| 1591 | // // |
---|
| 1592 | // }; |
---|
[8] | 1593 | |
---|
[488] | 1594 | template<class sq_T> |
---|
[737] | 1595 | vec enorm<sq_T>::sample() const { |
---|
| 1596 | vec x ( dim ); |
---|
[270] | 1597 | #pragma omp critical |
---|
[737] | 1598 | NorRNG.sample_vector ( dim, x ); |
---|
| 1599 | vec smp = R.sqrt_mult ( x ); |
---|
[12] | 1600 | |
---|
[488] | 1601 | smp += mu; |
---|
| 1602 | return smp; |
---|
| 1603 | }; |
---|
[8] | 1604 | |
---|
[214] | 1605 | // template<class sq_T> |
---|
| 1606 | // double enorm<sq_T>::eval ( const vec &val ) const { |
---|
| 1607 | // double pdfl,e; |
---|
| 1608 | // pdfl = evallog ( val ); |
---|
| 1609 | // e = exp ( pdfl ); |
---|
| 1610 | // return e; |
---|
| 1611 | // }; |
---|
[8] | 1612 | |
---|
[488] | 1613 | template<class sq_T> |
---|
[737] | 1614 | double enorm<sq_T>::evallog_nn ( const vec &val ) const { |
---|
[488] | 1615 | // 1.83787706640935 = log(2pi) |
---|
[737] | 1616 | double tmp = -0.5 * ( R.invqform ( mu - val ) );// - lognc(); |
---|
[488] | 1617 | return tmp; |
---|
| 1618 | }; |
---|
[28] | 1619 | |
---|
[488] | 1620 | template<class sq_T> |
---|
[737] | 1621 | inline double enorm<sq_T>::lognc () const { |
---|
[488] | 1622 | // 1.83787706640935 = log(2pi) |
---|
[737] | 1623 | double tmp = 0.5 * ( R.cols() * 1.83787706640935 + R.logdet() ); |
---|
[488] | 1624 | return tmp; |
---|
| 1625 | }; |
---|
[28] | 1626 | |
---|
[8] | 1627 | |
---|
[192] | 1628 | // template<class sq_T> |
---|
| 1629 | // vec mlnorm<sq_T>::samplecond (const vec &cond, double &lik ) { |
---|
| 1630 | // this->condition ( cond ); |
---|
| 1631 | // vec smp = epdf.sample(); |
---|
| 1632 | // lik = epdf.eval ( smp ); |
---|
| 1633 | // return smp; |
---|
| 1634 | // } |
---|
[8] | 1635 | |
---|
[192] | 1636 | // template<class sq_T> |
---|
| 1637 | // mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) { |
---|
| 1638 | // int i; |
---|
| 1639 | // int dim = rv.count(); |
---|
| 1640 | // mat Smp ( dim,n ); |
---|
| 1641 | // vec smp ( dim ); |
---|
| 1642 | // this->condition ( cond ); |
---|
[198] | 1643 | // |
---|
[192] | 1644 | // for ( i=0; i<n; i++ ) { |
---|
| 1645 | // smp = epdf.sample(); |
---|
| 1646 | // lik ( i ) = epdf.eval ( smp ); |
---|
| 1647 | // Smp.set_col ( i ,smp ); |
---|
| 1648 | // } |
---|
[198] | 1649 | // |
---|
[192] | 1650 | // return Smp; |
---|
| 1651 | // } |
---|
[28] | 1652 | |
---|
[8] | 1653 | |
---|
[488] | 1654 | template<class sq_T> |
---|
[737] | 1655 | shared_ptr<epdf> enorm<sq_T>::marginal ( const RV &rvn ) const { |
---|
[504] | 1656 | enorm<sq_T> *tmp = new enorm<sq_T> (); |
---|
[737] | 1657 | shared_ptr<epdf> narrow ( tmp ); |
---|
[504] | 1658 | marginal ( rvn, *tmp ); |
---|
| 1659 | return narrow; |
---|
| 1660 | } |
---|
| 1661 | |
---|
| 1662 | template<class sq_T> |
---|
[737] | 1663 | void enorm<sq_T>::marginal ( const RV &rvn, enorm<sq_T> &target ) const { |
---|
| 1664 | bdm_assert ( isnamed(), "rv description is not assigned" ); |
---|
| 1665 | ivec irvn = rvn.dataind ( rv ); |
---|
[178] | 1666 | |
---|
[737] | 1667 | sq_T Rn ( R, irvn ); // select rows and columns of R |
---|
[280] | 1668 | |
---|
[504] | 1669 | target.set_rv ( rvn ); |
---|
[737] | 1670 | target.set_parameters ( mu ( irvn ), Rn ); |
---|
[488] | 1671 | } |
---|
[178] | 1672 | |
---|
[488] | 1673 | template<class sq_T> |
---|
[737] | 1674 | shared_ptr<pdf> enorm<sq_T>::condition ( const RV &rvn ) const { |
---|
[504] | 1675 | mlnorm<sq_T> *tmp = new mlnorm<sq_T> (); |
---|
[737] | 1676 | shared_ptr<pdf> narrow ( tmp ); |
---|
[504] | 1677 | condition ( rvn, *tmp ); |
---|
| 1678 | return narrow; |
---|
| 1679 | } |
---|
[178] | 1680 | |
---|
[504] | 1681 | template<class sq_T> |
---|
[737] | 1682 | void enorm<sq_T>::condition ( const RV &rvn, pdf &target ) const { |
---|
[504] | 1683 | typedef mlnorm<sq_T> TMlnorm; |
---|
| 1684 | |
---|
[737] | 1685 | bdm_assert ( isnamed(), "rvs are not assigned" ); |
---|
| 1686 | TMlnorm &uptarget = dynamic_cast<TMlnorm &> ( target ); |
---|
[270] | 1687 | |
---|
[737] | 1688 | RV rvc = rv.subt ( rvn ); |
---|
| 1689 | bdm_assert ( ( rvc._dsize() + rvn._dsize() == rv._dsize() ), "wrong rvn" ); |
---|
[488] | 1690 | //Permutation vector of the new R |
---|
[737] | 1691 | ivec irvn = rvn.dataind ( rv ); |
---|
| 1692 | ivec irvc = rvc.dataind ( rv ); |
---|
| 1693 | ivec perm = concat ( irvn , irvc ); |
---|
| 1694 | sq_T Rn ( R, perm ); |
---|
[178] | 1695 | |
---|
[488] | 1696 | //fixme - could this be done in general for all sq_T? |
---|
| 1697 | mat S = Rn.to_mat(); |
---|
| 1698 | //fixme |
---|
| 1699 | int n = rvn._dsize() - 1; |
---|
| 1700 | int end = R.rows() - 1; |
---|
[737] | 1701 | mat S11 = S.get ( 0, n, 0, n ); |
---|
| 1702 | mat S12 = S.get ( 0, n , rvn._dsize(), end ); |
---|
| 1703 | mat S22 = S.get ( rvn._dsize(), end, rvn._dsize(), end ); |
---|
[178] | 1704 | |
---|
[737] | 1705 | vec mu1 = mu ( irvn ); |
---|
| 1706 | vec mu2 = mu ( irvc ); |
---|
| 1707 | mat A = S12 * inv ( S22 ); |
---|
| 1708 | sq_T R_n ( S11 - A *S12.T() ); |
---|
[178] | 1709 | |
---|
[737] | 1710 | uptarget.set_rv ( rvn ); |
---|
| 1711 | uptarget.set_rvc ( rvc ); |
---|
| 1712 | uptarget.set_parameters ( A, mu1 - A*mu2, R_n ); |
---|
[799] | 1713 | uptarget.validate(); |
---|
[488] | 1714 | } |
---|
[178] | 1715 | |
---|
[488] | 1716 | //// |
---|
| 1717 | /////// |
---|
| 1718 | template<class sq_T> |
---|
[737] | 1719 | void mgnorm<sq_T >::set_parameters ( const shared_ptr<fnc> &g0, const sq_T &R0 ) { |
---|
[527] | 1720 | g = g0; |
---|
[737] | 1721 | this->iepdf.set_parameters ( zeros ( g->dimension() ), R0 ); |
---|
[527] | 1722 | } |
---|
| 1723 | |
---|
[488] | 1724 | template<class sq_T> |
---|
[737] | 1725 | void mgnorm<sq_T >::condition ( const vec &cond ) { |
---|
| 1726 | this->iepdf._mu() = g->eval ( cond ); |
---|
| 1727 | }; |
---|
[28] | 1728 | |
---|
[536] | 1729 | //! \todo unify this stuff with to_string() |
---|
[488] | 1730 | template<class sq_T> |
---|
[737] | 1731 | std::ostream &operator<< ( std::ostream &os, mlnorm<sq_T> &ml ) { |
---|
[488] | 1732 | os << "A:" << ml.A << endl; |
---|
| 1733 | os << "mu:" << ml.mu_const << endl; |
---|
| 1734 | os << "R:" << ml._R() << endl; |
---|
| 1735 | return os; |
---|
| 1736 | }; |
---|
[28] | 1737 | |
---|
[254] | 1738 | } |
---|
[8] | 1739 | #endif //EF_H |
---|