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