00001
00013 #ifndef EF_H
00014 #define EF_H
00015
00016
00017 #include "../shared_ptr.h"
00018 #include "../base/bdmbase.h"
00019 #include "../math/chmat.h"
00020
00021 namespace bdm
00022 {
00023
00024
00026 extern Uniform_RNG UniRNG;
00028 extern Normal_RNG NorRNG;
00030 extern Gamma_RNG GamRNG;
00031
00038 class eEF : public epdf
00039 {
00040 public:
00041
00043 eEF () : epdf () {};
00045 virtual double lognc() const = 0;
00046
00048 virtual double evallog_nn (const vec &val) const {
00049 bdm_error ("Not implemented");
00050 return 0.0;
00051 }
00052
00054 virtual double evallog (const vec &val) const {
00055 double tmp;
00056 tmp = evallog_nn (val) - lognc();
00057 return tmp;
00058 }
00060 virtual vec evallog_m (const mat &Val) const {
00061 vec x (Val.cols());
00062 for (int i = 0;i < Val.cols();i++) {x (i) = evallog_nn (Val.get_col (i)) ;}
00063 return x -lognc();
00064 }
00066 virtual vec evallog_m (const Array<vec> &Val) const {
00067 vec x (Val.length());
00068 for (int i = 0;i < Val.length();i++) {x (i) = evallog_nn (Val (i)) ;}
00069 return x -lognc();
00070 }
00071
00073 virtual void pow (double p) {
00074 bdm_error ("Not implemented");
00075 }
00076 };
00077
00078
00080 class BMEF : public BM
00081 {
00082 protected:
00084 double frg;
00086 double last_lognc;
00087 public:
00089 BMEF (double frg0 = 1.0) : BM (), frg (frg0) {}
00091 BMEF (const BMEF &B) : BM (B), frg (B.frg), last_lognc (B.last_lognc) {}
00093 virtual void set_statistics (const BMEF* BM0) {
00094 bdm_error ("Not implemented");
00095 }
00096
00098 virtual void bayes (const vec &data, const double w) {};
00099
00100 void bayes (const vec &dt);
00101
00103 virtual void flatten (const BMEF * B) {
00104 bdm_error ("Not implemented");
00105 }
00106
00107 BMEF* _copy_ () const {
00108 bdm_error ("function _copy_ not implemented for this BM");
00109 return NULL;
00110 }
00111 };
00112
00113 template<class sq_T, template <typename> class TEpdf>
00114 class mlnorm;
00115
00121 template<class sq_T>
00122 class enorm : public eEF
00123 {
00124 protected:
00126 vec mu;
00128 sq_T R;
00129 public:
00132
00133 enorm () : eEF (), mu (), R () {};
00134 enorm (const vec &mu, const sq_T &R) {set_parameters (mu, R);}
00135 void set_parameters (const vec &mu, const sq_T &R);
00145 void from_setting (const Setting &root);
00146 void validate() {
00147 bdm_assert (mu.length() == R.rows(), "mu and R parameters do not match");
00148 dim = mu.length();
00149 }
00151
00154
00156 void dupdate (mat &v, double nu = 1.0);
00157
00158 vec sample() const;
00159
00160 double evallog_nn (const vec &val) const;
00161 double lognc () const;
00162 vec mean() const {return mu;}
00163 vec variance() const {return diag (R.to_mat());}
00164
00165 shared_ptr<mpdf> condition ( const RV &rvn ) const;
00166
00167
00168
00169
00170
00171 void condition ( const RV &rvn, mpdf &target ) const;
00172
00173 shared_ptr<epdf> marginal (const RV &rvn ) const;
00174 void marginal ( const RV &rvn, enorm<sq_T> &target ) const;
00176
00179
00180 vec& _mu() {return mu;}
00181 const vec& _mu() const {return mu;}
00182 void set_mu (const vec mu0) { mu = mu0;}
00183 sq_T& _R() {return R;}
00184 const sq_T& _R() const {return R;}
00186
00187 };
00188 UIREGISTER2 (enorm, chmat);
00189 SHAREDPTR2 ( enorm, chmat );
00190 UIREGISTER2 (enorm, ldmat);
00191 SHAREDPTR2 ( enorm, ldmat );
00192 UIREGISTER2 (enorm, fsqmat);
00193 SHAREDPTR2 ( enorm, fsqmat );
00194
00195
00202 class egiw : public eEF
00203 {
00204 protected:
00206 ldmat V;
00208 double nu;
00210 int dimx;
00212 int nPsi;
00213 public:
00216 egiw() : eEF() {};
00217 egiw (int dimx0, ldmat V0, double nu0 = -1.0) : eEF() {set_parameters (dimx0, V0, nu0);};
00218
00219 void set_parameters (int dimx0, ldmat V0, double nu0 = -1.0) {
00220 dimx = dimx0;
00221 nPsi = V0.rows() - dimx;
00222 dim = dimx * (dimx + nPsi);
00223
00224 V = V0;
00225 if (nu0 < 0) {
00226 nu = 0.1 + nPsi + 2 * dimx + 2;
00227
00228 } else {
00229 nu = nu0;
00230 }
00231 }
00233
00234 vec sample() const;
00235 vec mean() const;
00236 vec variance() const;
00237
00239 vec est_theta() const;
00240
00242 ldmat est_theta_cov() const;
00243
00245 void mean_mat (mat &M, mat&R) const;
00247 double evallog_nn (const vec &val) const;
00248 double lognc () const;
00249 void pow (double p) {V *= p;nu *= p;};
00250
00253
00254 ldmat& _V() {return V;}
00255 const ldmat& _V() const {return V;}
00256 double& _nu() {return nu;}
00257 const double& _nu() const {return nu;}
00272 void from_setting (const Setting &set) {
00273 epdf::from_setting(set);
00274 if (!UI::get (nu, set, "nu", UI::compulsory)) {nu=-1;}
00275 UI::get (dimx, set, "dimx", UI::compulsory);
00276 mat V;
00277 UI::get (V, set, "V", UI::compulsory);
00278 set_parameters (dimx, V, nu);
00279 }
00281 };
00282 UIREGISTER ( egiw );
00283 SHAREDPTR ( egiw );
00284
00293 class eDirich: public eEF
00294 {
00295 protected:
00297 vec beta;
00298 public:
00301
00302 eDirich () : eEF () {};
00303 eDirich (const eDirich &D0) : eEF () {set_parameters (D0.beta);};
00304 eDirich (const vec &beta0) {set_parameters (beta0);};
00305 void set_parameters (const vec &beta0) {
00306 beta = beta0;
00307 dim = beta.length();
00308 }
00310
00311 vec sample() const {
00312 bdm_error ("Not implemented");
00313 return vec();
00314 }
00315
00316 vec mean() const {return beta / sum (beta);};
00317 vec variance() const {double gamma = sum (beta); return elem_mult (beta, (beta + 1)) / (gamma* (gamma + 1));}
00319 double evallog_nn (const vec &val) const {
00320 double tmp; tmp = (beta - 1) * log (val);
00321 return tmp;
00322 }
00323
00324 double lognc () const {
00325 double tmp;
00326 double gam = sum (beta);
00327 double lgb = 0.0;
00328 for (int i = 0;i < beta.length();i++) {lgb += lgamma (beta (i));}
00329 tmp = lgb - lgamma (gam);
00330 return tmp;
00331 }
00332
00334 vec& _beta() {return beta;}
00336 };
00337
00339 class multiBM : public BMEF
00340 {
00341 protected:
00343 eDirich est;
00345 vec β
00346 public:
00348 multiBM () : BMEF (), est (), beta (est._beta()) {
00349 if (beta.length() > 0) {last_lognc = est.lognc();}
00350 else{last_lognc = 0.0;}
00351 }
00353 multiBM (const multiBM &B) : BMEF (B), est (B.est), beta (est._beta()) {}
00355 void set_statistics (const BM* mB0) {const multiBM* mB = dynamic_cast<const multiBM*> (mB0); beta = mB->beta;}
00356 void bayes (const vec &dt) {
00357 if (frg < 1.0) {beta *= frg;last_lognc = est.lognc();}
00358 beta += dt;
00359 if (evalll) {ll = est.lognc() - last_lognc;}
00360 }
00361 double logpred (const vec &dt) const {
00362 eDirich pred (est);
00363 vec &beta = pred._beta();
00364
00365 double lll;
00366 if (frg < 1.0)
00367 {beta *= frg;lll = pred.lognc();}
00368 else
00369 if (evalll) {lll = last_lognc;}
00370 else{lll = pred.lognc();}
00371
00372 beta += dt;
00373 return pred.lognc() - lll;
00374 }
00375 void flatten (const BMEF* B) {
00376 const multiBM* E = dynamic_cast<const multiBM*> (B);
00377
00378 const vec &Eb = E->beta;
00379 beta *= (sum (Eb) / sum (beta));
00380 if (evalll) {last_lognc = est.lognc();}
00381 }
00383 const eDirich& posterior() const {return est;};
00385 void set_parameters (const vec &beta0) {
00386 est.set_parameters (beta0);
00387 if (evalll) {last_lognc = est.lognc();}
00388 }
00389 };
00390
00400 class egamma : public eEF
00401 {
00402 protected:
00404 vec alpha;
00406 vec beta;
00407 public :
00410 egamma () : eEF (), alpha (0), beta (0) {};
00411 egamma (const vec &a, const vec &b) {set_parameters (a, b);};
00412 void set_parameters (const vec &a, const vec &b) {alpha = a, beta = b;dim = alpha.length();};
00414
00415 vec sample() const;
00416 double evallog (const vec &val) const;
00417 double lognc () const;
00419 vec& _alpha() {return alpha;}
00421 vec& _beta() {return beta;}
00422 vec mean() const {return elem_div (alpha, beta);}
00423 vec variance() const {return elem_div (alpha, elem_mult (beta, beta)); }
00424
00435 void from_setting (const Setting &set) {
00436 epdf::from_setting (set);
00437 UI::get (alpha, set, "alpha", UI::compulsory);
00438 UI::get (beta, set, "beta", UI::compulsory);
00439 validate();
00440 }
00441 void validate() {
00442 bdm_assert (alpha.length() == beta.length(), "parameters do not match");
00443 dim = alpha.length();
00444 }
00445 };
00446 UIREGISTER (egamma);
00447 SHAREDPTR ( egamma );
00448
00465 class eigamma : public egamma
00466 {
00467 protected:
00468 public :
00473
00474 vec sample() const {return 1.0 / egamma::sample();};
00476 vec mean() const {return elem_div (beta, alpha - 1);}
00477 vec variance() const {vec mea = mean(); return elem_div (elem_mult (mea, mea), alpha - 2);}
00478 };
00479
00481
00482
00483
00484
00485
00486
00488
00489
00490
00491
00492
00493
00495
00496 class euni: public epdf
00497 {
00498 protected:
00500 vec low;
00502 vec high;
00504 vec distance;
00506 double nk;
00508 double lnk;
00509 public:
00512 euni () : epdf () {}
00513 euni (const vec &low0, const vec &high0) {set_parameters (low0, high0);}
00514 void set_parameters (const vec &low0, const vec &high0) {
00515 distance = high0 - low0;
00516 low = low0;
00517 high = high0;
00518 nk = prod (1.0 / distance);
00519 lnk = log (nk);
00520 dim = low.length();
00521 }
00523
00524 double evallog (const vec &val) const {
00525 if (any (val < low) && any (val > high)) {return inf;}
00526 else return lnk;
00527 }
00528 vec sample() const {
00529 vec smp (dim);
00530 #pragma omp critical
00531 UniRNG.sample_vector (dim , smp);
00532 return low + elem_mult (distance, smp);
00533 }
00535 vec mean() const {return (high -low) / 2.0;}
00536 vec variance() const {return (pow (high, 2) + pow (low, 2) + elem_mult (high, low)) / 3.0;}
00547 void from_setting (const Setting &set) {
00548 epdf::from_setting (set);
00549
00550 UI::get (high, set, "high", UI::compulsory);
00551 UI::get (low, set, "low", UI::compulsory);
00552 set_parameters(low,high);
00553 validate();
00554 }
00555 void validate() {
00556 bdm_assert(high.length()==low.length(), "Incompatible high and low vectors");
00557 dim = high.length();
00558 bdm_assert (min (distance) > 0.0, "bad support");
00559 }
00560 };
00561 UIREGISTER(euni);
00562
00568 template < class sq_T, template <typename> class TEpdf = enorm >
00569 class mlnorm : public mpdf_internal< TEpdf<sq_T> >
00570 {
00571 protected:
00573 mat A;
00575 vec mu_const;
00576
00577 public:
00580 mlnorm() : mpdf_internal< TEpdf<sq_T> >() {};
00581 mlnorm (const mat &A, const vec &mu0, const sq_T &R) : mpdf_internal< TEpdf<sq_T> >() {
00582 set_parameters (A, mu0, R);
00583 }
00584
00586 void set_parameters (const mat &A0, const vec &mu0, const sq_T &R0) {
00587 this->iepdf.set_parameters (zeros (A0.rows()), R0);
00588 A = A0;
00589 mu_const = mu0;
00590 this->dimc = A0.cols();
00591 }
00594 void condition (const vec &cond) {
00595 this->iepdf._mu() = A * cond + mu_const;
00596
00597 }
00598
00600 const vec& _mu_const() const {return mu_const;}
00602 const mat& _A() const {return A;}
00604 mat _R() const { return this->iepdf._R().to_mat(); }
00605
00607 template<typename sq_M>
00608 friend std::ostream &operator<< (std::ostream &os, mlnorm<sq_M, enorm> &ml);
00609
00620 void from_setting (const Setting &set) {
00621 mpdf::from_setting (set);
00622
00623 UI::get (A, set, "A", UI::compulsory);
00624 UI::get (mu_const, set, "const", UI::compulsory);
00625 mat R0;
00626 UI::get (R0, set, "R", UI::compulsory);
00627 set_parameters (A, mu_const, R0);
00628 validate();
00629 };
00630 void validate() {
00631 bdm_assert (A.rows() == mu_const.length(), "mlnorm: A vs. mu mismatch");
00632 bdm_assert (A.rows() == _R().rows(), "mlnorm: A vs. R mismatch");
00633
00634 }
00635 };
00636 UIREGISTER2 (mlnorm,ldmat);
00637 SHAREDPTR2 ( mlnorm, ldmat );
00638 UIREGISTER2 (mlnorm,fsqmat);
00639 SHAREDPTR2 ( mlnorm, fsqmat );
00640 UIREGISTER2 (mlnorm, chmat);
00641 SHAREDPTR2 ( mlnorm, chmat );
00642
00644 template<class sq_T>
00645 class mgnorm : public mpdf_internal< enorm< sq_T > >
00646 {
00647 private:
00648
00649 shared_ptr<fnc> g;
00650
00651 public:
00653 mgnorm() : mpdf_internal<enorm<sq_T> >() { }
00655 inline void set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0);
00656 inline void condition (const vec &cond);
00657
00658
00677 void from_setting (const Setting &set) {
00678 mpdf::from_setting(set);
00679 shared_ptr<fnc> g = UI::build<fnc> (set, "g", UI::compulsory);
00680
00681 mat R;
00682 vec dR;
00683 if (UI::get (dR, set, "dR"))
00684 R = diag (dR);
00685 else
00686 UI::get (R, set, "R", UI::compulsory);
00687
00688 set_parameters (g, R);
00689 validate();
00690 }
00691 void validate() {
00692 bdm_assert(g->dimension()==this->dimension(),"incompatible function");
00693 }
00694 };
00695
00696 UIREGISTER2 (mgnorm, chmat);
00697 SHAREDPTR2 ( mgnorm, chmat );
00698
00699
00707 class mlstudent : public mlnorm<ldmat, enorm>
00708 {
00709 protected:
00711 ldmat Lambda;
00713 ldmat &_R;
00715 ldmat Re;
00716 public:
00717 mlstudent () : mlnorm<ldmat, enorm> (),
00718 Lambda (), _R (iepdf._R()) {}
00720 void set_parameters (const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0) {
00721 iepdf.set_parameters (mu0, R0);
00722 A = A0;
00723 mu_const = mu0;
00724 Re = R0;
00725 Lambda = Lambda0;
00726 }
00727 void condition (const vec &cond) {
00728 iepdf._mu() = A * cond + mu_const;
00729 double zeta;
00730
00731 if ( (cond.length() + 1) == Lambda.rows()) {
00732 zeta = Lambda.invqform (concat (cond, vec_1 (1.0)));
00733 } else {
00734 zeta = Lambda.invqform (cond);
00735 }
00736 _R = Re;
00737 _R *= (1 + zeta);
00738 };
00739
00740 void validate() {
00741 bdm_assert (A.rows() == mu_const.length(), "mlstudent: A vs. mu mismatch");
00742 bdm_assert (_R.rows() == A.rows(), "mlstudent: A vs. R mismatch");
00743
00744 }
00745 };
00755 class mgamma : public mpdf_internal<egamma>
00756 {
00757 protected:
00758
00760 double k;
00761
00763 vec &_beta;
00764
00765 public:
00767 mgamma() : mpdf_internal<egamma>(), k (0),
00768 _beta (iepdf._beta()) {
00769 }
00770
00772 void set_parameters (double k, const vec &beta0);
00773
00774 void condition (const vec &val) {_beta = k / val;};
00786 void from_setting (const Setting &set) {
00787 mpdf::from_setting (set);
00788 vec betatmp;
00789 UI::get (betatmp, set, "beta", UI::compulsory);
00790 UI::get (k, set, "k", UI::compulsory);
00791 set_parameters (k, betatmp);
00792 }
00793 };
00794 UIREGISTER (mgamma);
00795 SHAREDPTR (mgamma);
00796
00806 class migamma : public mpdf_internal<eigamma>
00807 {
00808 protected:
00810 double k;
00811
00813 vec &_alpha;
00814
00816 vec &_beta;
00817
00818 public:
00821 migamma() : mpdf_internal<eigamma>(),
00822 k (0),
00823 _alpha (iepdf._alpha()),
00824 _beta (iepdf._beta()) {
00825 }
00826
00827 migamma (const migamma &m) : mpdf_internal<eigamma>(),
00828 k (0),
00829 _alpha (iepdf._alpha()),
00830 _beta (iepdf._beta()) {
00831 }
00833
00835 void set_parameters (int len, double k0) {
00836 k = k0;
00837 iepdf.set_parameters ( (1.0 / (k*k) + 2.0) *ones (len) , ones (len) );
00838 dimc = dimension();
00839 };
00840 void condition (const vec &val) {
00841 _beta = elem_mult (val, (_alpha - 1.0));
00842 };
00843 };
00844
00845
00857 class mgamma_fix : public mgamma
00858 {
00859 protected:
00861 double l;
00863 vec refl;
00864 public:
00866 mgamma_fix () : mgamma (), refl () {};
00868 void set_parameters (double k0 , vec ref0, double l0) {
00869 mgamma::set_parameters (k0, ref0);
00870 refl = pow (ref0, 1.0 - l0);l = l0;
00871 dimc = dimension();
00872 };
00873
00874 void condition (const vec &val) {vec mean = elem_mult (refl, pow (val, l)); _beta = k / mean;};
00875 };
00876
00877
00890 class migamma_ref : public migamma
00891 {
00892 protected:
00894 double l;
00896 vec refl;
00897 public:
00899 migamma_ref () : migamma (), refl () {};
00901 void set_parameters (double k0 , vec ref0, double l0) {
00902 migamma::set_parameters (ref0.length(), k0);
00903 refl = pow (ref0, 1.0 - l0);
00904 l = l0;
00905 dimc = dimension();
00906 };
00907
00908 void condition (const vec &val) {
00909 vec mean = elem_mult (refl, pow (val, l));
00910 migamma::condition (mean);
00911 };
00912
00913
00926 void from_setting (const Setting &set);
00927
00928
00929 };
00930
00931
00932 UIREGISTER (migamma_ref);
00933 SHAREDPTR (migamma_ref);
00934
00945 class elognorm: public enorm<ldmat>
00946 {
00947 public:
00948 vec sample() const {return exp (enorm<ldmat>::sample());};
00949 vec mean() const {vec var = enorm<ldmat>::variance();return exp (mu - 0.5*var);};
00950
00951 };
00952
00959 class mlognorm : public mpdf_internal<elognorm>
00960 {
00961 protected:
00963 double sig2;
00964
00966 vec μ
00967 public:
00969 mlognorm() : mpdf_internal<elognorm>(),
00970 sig2 (0),
00971 mu (iepdf._mu()) {
00972 }
00973
00975 void set_parameters (int size, double k) {
00976 sig2 = 0.5 * log (k * k + 1);
00977 iepdf.set_parameters (zeros (size), 2*sig2*eye (size));
00978
00979 dimc = size;
00980 };
00981
00982 void condition (const vec &val) {
00983 mu = log (val) - sig2;
00984 };
00985
00997 void from_setting (const Setting &set);
00998
00999
01000
01001 };
01002
01003 UIREGISTER (mlognorm);
01004 SHAREDPTR (mlognorm);
01005
01009 class eWishartCh : public epdf
01010 {
01011 protected:
01013 chmat Y;
01015 int p;
01017 double delta;
01018 public:
01020 void set_parameters (const mat &Y0, const double delta0) {Y = chmat (Y0);delta = delta0; p = Y.rows(); dim = p * p; }
01022 mat sample_mat() const {
01023 mat X = zeros (p, p);
01024
01025
01026 for (int i = 0;i < p;i++) {
01027 GamRNG.setup (0.5* (delta - i) , 0.5);
01028 #pragma omp critical
01029 X (i, i) = sqrt (GamRNG());
01030 }
01031
01032 for (int i = 0;i < p;i++) {
01033 for (int j = i + 1;j < p;j++) {
01034 #pragma omp critical
01035 X (i, j) = NorRNG.sample();
01036 }
01037 }
01038 return X*Y._Ch();
01039 }
01040 vec sample () const {
01041 return vec (sample_mat()._data(), p*p);
01042 }
01044 void setY (const mat &Ch0) {copy_vector (dim, Ch0._data(), Y._Ch()._data());}
01046 void _setY (const vec &ch0) {copy_vector (dim, ch0._data(), Y._Ch()._data()); }
01048 const chmat& getY() const {return Y;}
01049 };
01050
01052
01054 class eiWishartCh: public epdf
01055 {
01056 protected:
01058 eWishartCh W;
01060 int p;
01062 double delta;
01063 public:
01065 void set_parameters (const mat &Y0, const double delta0) {
01066 delta = delta0;
01067 W.set_parameters (inv (Y0), delta0);
01068 dim = W.dimension(); p = Y0.rows();
01069 }
01070 vec sample() const {mat iCh; iCh = inv (W.sample_mat()); return vec (iCh._data(), dim);}
01072 void _setY (const vec &y0) {
01073 mat Ch (p, p);
01074 mat iCh (p, p);
01075 copy_vector (dim, y0._data(), Ch._data());
01076
01077 iCh = inv (Ch);
01078 W.setY (iCh);
01079 }
01080 virtual double evallog (const vec &val) const {
01081 chmat X (p);
01082 const chmat& Y = W.getY();
01083
01084 copy_vector (p*p, val._data(), X._Ch()._data());
01085 chmat iX (p);X.inv (iX);
01086
01087
01088 mat M = Y.to_mat() * iX.to_mat();
01089
01090 double log1 = 0.5 * p * (2 * Y.logdet()) - 0.5 * (delta + p + 1) * (2 * X.logdet()) - 0.5 * trace (M);
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100 return log1;
01101 };
01102
01103 };
01104
01106 class rwiWishartCh : public mpdf_internal<eiWishartCh>
01107 {
01108 protected:
01110 double sqd;
01112 vec refl;
01114 double l;
01116 int p;
01117
01118 public:
01119 rwiWishartCh() : sqd (0), l (0), p (0) {}
01121 void set_parameters (int p0, double k, vec ref0, double l0) {
01122 p = p0;
01123 double delta = 2 / (k * k) + p + 3;
01124 sqd = sqrt (delta - p - 1);
01125 l = l0;
01126 refl = pow (ref0, 1 - l);
01127
01128 iepdf.set_parameters (eye (p), delta);
01129 dimc = iepdf.dimension();
01130 }
01131 void condition (const vec &c) {
01132 vec z = c;
01133 int ri = 0;
01134 for (int i = 0;i < p*p;i += (p + 1)) {
01135 z (i) = pow (z (i), l) * refl (ri);
01136 ri++;
01137 }
01138
01139 iepdf._setY (sqd*z);
01140 }
01141 };
01142
01144 enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 };
01150 class eEmp: public epdf
01151 {
01152 protected :
01154 int n;
01156 vec w;
01158 Array<vec> samples;
01159 public:
01162 eEmp () : epdf (), w (), samples () {};
01164 eEmp (const eEmp &e) : epdf (e), w (e.w), samples (e.samples) {};
01166
01168 void set_statistics (const vec &w0, const epdf &pdf0);
01170 void set_statistics (const epdf &pdf0 , int n) {set_statistics (ones (n) / n, pdf0);};
01172 void set_samples (const epdf* pdf0);
01174 void set_parameters (int n0, bool copy = true) {n = n0; w.set_size (n0, copy);samples.set_size (n0, copy);};
01176 void set_parameters (const Array<vec> &Av) {
01177 bdm_assert(Av.size()>0,"Empty samples");
01178 n = Av.size();
01179 epdf::set_parameters(Av(0).length());
01180 w=1/n*ones(n);
01181 samples=Av;
01182 };
01184 vec& _w() {return w;};
01186 const vec& _w() const {return w;};
01188 Array<vec>& _samples() {return samples;};
01190 const Array<vec>& _samples() const {return samples;};
01192 ivec resample (RESAMPLING_METHOD method = SYSTEMATIC);
01193
01195 vec sample() const {
01196 bdm_error ("Not implemented");
01197 return vec();
01198 }
01199
01201 double evallog (const vec &val) const {
01202 bdm_error ("Not implemented");
01203 return 0.0;
01204 }
01205
01206 vec mean() const {
01207 vec pom = zeros (dim);
01208 for (int i = 0;i < n;i++) {pom += samples (i) * w (i);}
01209 return pom;
01210 }
01211 vec variance() const {
01212 vec pom = zeros (dim);
01213 for (int i = 0;i < n;i++) {pom += pow (samples (i), 2) * w (i);}
01214 return pom -pow (mean(), 2);
01215 }
01217 void qbounds (vec &lb, vec &ub, double perc = 0.95) const {
01218
01219 lb.set_size (dim);
01220 ub.set_size (dim);
01221 lb = std::numeric_limits<double>::infinity();
01222 ub = -std::numeric_limits<double>::infinity();
01223 int j;
01224 for (int i = 0;i < n;i++) {
01225 for (j = 0;j < dim; j++) {
01226 if (samples (i) (j) < lb (j)) {lb (j) = samples (i) (j);}
01227 if (samples (i) (j) > ub (j)) {ub (j) = samples (i) (j);}
01228 }
01229 }
01230 }
01231 };
01232
01233
01235
01236 template<class sq_T>
01237 void enorm<sq_T>::set_parameters (const vec &mu0, const sq_T &R0)
01238 {
01239
01240 mu = mu0;
01241 R = R0;
01242 validate();
01243 };
01244
01245 template<class sq_T>
01246 void enorm<sq_T>::from_setting (const Setting &set)
01247 {
01248 epdf::from_setting (set);
01249
01250 UI::get (mu, set, "mu", UI::compulsory);
01251 mat Rtmp;
01252 UI::get (Rtmp, set, "R", UI::compulsory);
01253 R = Rtmp;
01254 validate();
01255 }
01256
01257 template<class sq_T>
01258 void enorm<sq_T>::dupdate (mat &v, double nu)
01259 {
01260
01261 };
01262
01263
01264
01265
01266
01267
01268 template<class sq_T>
01269 vec enorm<sq_T>::sample() const
01270 {
01271 vec x (dim);
01272 #pragma omp critical
01273 NorRNG.sample_vector (dim, x);
01274 vec smp = R.sqrt_mult (x);
01275
01276 smp += mu;
01277 return smp;
01278 };
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288 template<class sq_T>
01289 double enorm<sq_T>::evallog_nn (const vec &val) const
01290 {
01291
01292 double tmp = -0.5 * (R.invqform (mu - val));
01293 return tmp;
01294 };
01295
01296 template<class sq_T>
01297 inline double enorm<sq_T>::lognc () const
01298 {
01299
01300 double tmp = 0.5 * (R.cols() * 1.83787706640935 + R.logdet());
01301 return tmp;
01302 };
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331 template<class sq_T>
01332 shared_ptr<epdf> enorm<sq_T>::marginal ( const RV &rvn ) const
01333 {
01334 enorm<sq_T> *tmp = new enorm<sq_T> ();
01335 shared_ptr<epdf> narrow(tmp);
01336 marginal ( rvn, *tmp );
01337 return narrow;
01338 }
01339
01340 template<class sq_T>
01341 void enorm<sq_T>::marginal ( const RV &rvn, enorm<sq_T> &target ) const
01342 {
01343 bdm_assert (isnamed(), "rv description is not assigned");
01344 ivec irvn = rvn.dataind (rv);
01345
01346 sq_T Rn (R, irvn);
01347
01348 target.set_rv ( rvn );
01349 target.set_parameters (mu (irvn), Rn);
01350 }
01351
01352 template<class sq_T>
01353 shared_ptr<mpdf> enorm<sq_T>::condition ( const RV &rvn ) const
01354 {
01355 mlnorm<sq_T> *tmp = new mlnorm<sq_T> ();
01356 shared_ptr<mpdf> narrow(tmp);
01357 condition ( rvn, *tmp );
01358 return narrow;
01359 }
01360
01361 template<class sq_T>
01362 void enorm<sq_T>::condition ( const RV &rvn, mpdf &target ) const
01363 {
01364 typedef mlnorm<sq_T> TMlnorm;
01365
01366 bdm_assert (isnamed(), "rvs are not assigned");
01367 TMlnorm &uptarget = dynamic_cast<TMlnorm &>(target);
01368
01369 RV rvc = rv.subt (rvn);
01370 bdm_assert ( (rvc._dsize() + rvn._dsize() == rv._dsize()), "wrong rvn");
01371
01372 ivec irvn = rvn.dataind (rv);
01373 ivec irvc = rvc.dataind (rv);
01374 ivec perm = concat (irvn , irvc);
01375 sq_T Rn (R, perm);
01376
01377
01378 mat S = Rn.to_mat();
01379
01380 int n = rvn._dsize() - 1;
01381 int end = R.rows() - 1;
01382 mat S11 = S.get (0, n, 0, n);
01383 mat S12 = S.get (0, n , rvn._dsize(), end);
01384 mat S22 = S.get (rvn._dsize(), end, rvn._dsize(), end);
01385
01386 vec mu1 = mu (irvn);
01387 vec mu2 = mu (irvc);
01388 mat A = S12 * inv (S22);
01389 sq_T R_n (S11 - A *S12.T());
01390
01391 uptarget.set_rv (rvn);
01392 uptarget.set_rvc (rvc);
01393 uptarget.set_parameters (A, mu1 - A*mu2, R_n);
01394 }
01395
01398 template<class sq_T>
01399 void mgnorm<sq_T >::set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0) {
01400 g = g0;
01401 this->iepdf.set_parameters (zeros (g->dimension()), R0);
01402 }
01403
01404 template<class sq_T>
01405 void mgnorm<sq_T >::condition (const vec &cond) {this->iepdf._mu() = g->eval (cond);};
01406
01408 template<class sq_T>
01409 std::ostream &operator<< (std::ostream &os, mlnorm<sq_T> &ml)
01410 {
01411 os << "A:" << ml.A << endl;
01412 os << "mu:" << ml.mu_const << endl;
01413 os << "R:" << ml._R() << endl;
01414 return os;
01415 };
01416
01417 }
01418 #endif //EF_H