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