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);
00221
00222 vec sample() const;
00223 vec mean() const;
00224 vec variance() const;
00225
00227 vec est_theta() const;
00228
00230 ldmat est_theta_cov() const;
00231
00233 void mean_mat (mat &M, mat&R) const;
00235 double evallog_nn (const vec &val) const;
00236 double lognc () const;
00237 void pow (double p) {V *= p;nu *= p;};
00238
00241
00242 ldmat& _V() {return V;}
00243 const ldmat& _V() const {return V;}
00244 double& _nu() {return nu;}
00245 const double& _nu() const {return nu;}
00260 void from_setting (const Setting &set) {
00261 epdf::from_setting(set);
00262 if (!UI::get (nu, set, "nu", UI::compulsory)) {nu=-1;}
00263 UI::get (dimx, set, "dimx", UI::compulsory);
00264 mat V;
00265 UI::get (V, set, "V", UI::compulsory);
00266 set_parameters (dimx, V, nu);
00267 }
00269 };
00270 UIREGISTER ( egiw );
00271 SHAREDPTR ( egiw );
00272
00281 class eDirich: public eEF
00282 {
00283 protected:
00285 vec beta;
00286 public:
00289
00290 eDirich () : eEF () {};
00291 eDirich (const eDirich &D0) : eEF () {set_parameters (D0.beta);};
00292 eDirich (const vec &beta0) {set_parameters (beta0);};
00293 void set_parameters (const vec &beta0) {
00294 beta = beta0;
00295 dim = beta.length();
00296 }
00298
00299 vec sample() const {
00300 bdm_error ("Not implemented");
00301 return vec();
00302 }
00303
00304 vec mean() const {return beta / sum (beta);};
00305 vec variance() const {double gamma = sum (beta); return elem_mult (beta, (beta + 1)) / (gamma* (gamma + 1));}
00307 double evallog_nn (const vec &val) const {
00308 double tmp; tmp = (beta - 1) * log (val);
00309 return tmp;
00310 }
00311
00312 double lognc () const {
00313 double tmp;
00314 double gam = sum (beta);
00315 double lgb = 0.0;
00316 for (int i = 0;i < beta.length();i++) {lgb += lgamma (beta (i));}
00317 tmp = lgb - lgamma (gam);
00318 return tmp;
00319 }
00320
00322 vec& _beta() {return beta;}
00324 };
00325
00327 class multiBM : public BMEF
00328 {
00329 protected:
00331 eDirich est;
00333 vec β
00334 public:
00336 multiBM () : BMEF (), est (), beta (est._beta()) {
00337 if (beta.length() > 0) {last_lognc = est.lognc();}
00338 else{last_lognc = 0.0;}
00339 }
00341 multiBM (const multiBM &B) : BMEF (B), est (B.est), beta (est._beta()) {}
00343 void set_statistics (const BM* mB0) {const multiBM* mB = dynamic_cast<const multiBM*> (mB0); beta = mB->beta;}
00344 void bayes (const vec &dt) {
00345 if (frg < 1.0) {beta *= frg;last_lognc = est.lognc();}
00346 beta += dt;
00347 if (evalll) {ll = est.lognc() - last_lognc;}
00348 }
00349 double logpred (const vec &dt) const {
00350 eDirich pred (est);
00351 vec &beta = pred._beta();
00352
00353 double lll;
00354 if (frg < 1.0)
00355 {beta *= frg;lll = pred.lognc();}
00356 else
00357 if (evalll) {lll = last_lognc;}
00358 else{lll = pred.lognc();}
00359
00360 beta += dt;
00361 return pred.lognc() - lll;
00362 }
00363 void flatten (const BMEF* B) {
00364 const multiBM* E = dynamic_cast<const multiBM*> (B);
00365
00366 const vec &Eb = E->beta;
00367 beta *= (sum (Eb) / sum (beta));
00368 if (evalll) {last_lognc = est.lognc();}
00369 }
00371 const eDirich& posterior() const {return est;};
00373 void set_parameters (const vec &beta0) {
00374 est.set_parameters (beta0);
00375 if (evalll) {last_lognc = est.lognc();}
00376 }
00377 };
00378
00388 class egamma : public eEF
00389 {
00390 protected:
00392 vec alpha;
00394 vec beta;
00395 public :
00398 egamma () : eEF (), alpha (0), beta (0) {};
00399 egamma (const vec &a, const vec &b) {set_parameters (a, b);};
00400 void set_parameters (const vec &a, const vec &b) {alpha = a, beta = b;dim = alpha.length();};
00402
00403 vec sample() const;
00404 double evallog (const vec &val) const;
00405 double lognc () const;
00407 vec& _alpha() {return alpha;}
00409 vec& _beta() {return beta;}
00410 vec mean() const {return elem_div (alpha, beta);}
00411 vec variance() const {return elem_div (alpha, elem_mult (beta, beta)); }
00412
00423 void from_setting (const Setting &set) {
00424 epdf::from_setting (set);
00425 UI::get (alpha, set, "alpha", UI::compulsory);
00426 UI::get (beta, set, "beta", UI::compulsory);
00427 validate();
00428 }
00429 void validate() {
00430 bdm_assert (alpha.length() == beta.length(), "parameters do not match");
00431 dim = alpha.length();
00432 }
00433 };
00434 UIREGISTER (egamma);
00435 SHAREDPTR ( egamma );
00436
00453 class eigamma : public egamma
00454 {
00455 protected:
00456 public :
00461
00462 vec sample() const {return 1.0 / egamma::sample();};
00464 vec mean() const {return elem_div (beta, alpha - 1);}
00465 vec variance() const {vec mea = mean(); return elem_div (elem_mult (mea, mea), alpha - 2);}
00466 };
00467
00469
00470
00471
00472
00473
00474
00476
00477
00478
00479
00480
00481
00483
00484 class euni: public epdf
00485 {
00486 protected:
00488 vec low;
00490 vec high;
00492 vec distance;
00494 double nk;
00496 double lnk;
00497 public:
00500 euni () : epdf () {}
00501 euni (const vec &low0, const vec &high0) {set_parameters (low0, high0);}
00502 void set_parameters (const vec &low0, const vec &high0) {
00503 distance = high0 - low0;
00504 low = low0;
00505 high = high0;
00506 nk = prod (1.0 / distance);
00507 lnk = log (nk);
00508 dim = low.length();
00509 }
00511
00512 double evallog (const vec &val) const {
00513 if (any (val < low) && any (val > high)) {return inf;}
00514 else return lnk;
00515 }
00516 vec sample() const {
00517 vec smp (dim);
00518 #pragma omp critical
00519 UniRNG.sample_vector (dim , smp);
00520 return low + elem_mult (distance, smp);
00521 }
00523 vec mean() const {return (high -low) / 2.0;}
00524 vec variance() const {return (pow (high, 2) + pow (low, 2) + elem_mult (high, low)) / 3.0;}
00535 void from_setting (const Setting &set) {
00536 epdf::from_setting (set);
00537
00538 UI::get (high, set, "high", UI::compulsory);
00539 UI::get (low, set, "low", UI::compulsory);
00540 set_parameters(low,high);
00541 validate();
00542 }
00543 void validate() {
00544 bdm_assert(high.length()==low.length(), "Incompatible high and low vectors");
00545 dim = high.length();
00546 bdm_assert (min (distance) > 0.0, "bad support");
00547 }
00548 };
00549 UIREGISTER(euni);
00550
00556 template < class sq_T, template <typename> class TEpdf = enorm >
00557 class mlnorm : public mpdf_internal< TEpdf<sq_T> >
00558 {
00559 protected:
00561 mat A;
00563 vec mu_const;
00564
00565 public:
00568 mlnorm() : mpdf_internal< TEpdf<sq_T> >() {};
00569 mlnorm (const mat &A, const vec &mu0, const sq_T &R) : mpdf_internal< TEpdf<sq_T> >() {
00570 set_parameters (A, mu0, R);
00571 }
00572
00574 void set_parameters (const mat &A0, const vec &mu0, const sq_T &R0) {
00575 this->iepdf.set_parameters (zeros (A0.rows()), R0);
00576 A = A0;
00577 mu_const = mu0;
00578 this->dimc = A0.cols();
00579 }
00582 void condition (const vec &cond) {
00583 this->iepdf._mu() = A * cond + mu_const;
00584
00585 }
00586
00588 const vec& _mu_const() const {return mu_const;}
00590 const mat& _A() const {return A;}
00592 mat _R() const { return this->iepdf._R().to_mat(); }
00593
00595 template<typename sq_M>
00596 friend std::ostream &operator<< (std::ostream &os, mlnorm<sq_M, enorm> &ml);
00597
00608 void from_setting (const Setting &set) {
00609 mpdf::from_setting (set);
00610
00611 UI::get (A, set, "A", UI::compulsory);
00612 UI::get (mu_const, set, "const", UI::compulsory);
00613 mat R0;
00614 UI::get (R0, set, "R", UI::compulsory);
00615 set_parameters (A, mu_const, R0);
00616 validate();
00617 };
00618 void validate() {
00619 bdm_assert (A.rows() == mu_const.length(), "mlnorm: A vs. mu mismatch");
00620 bdm_assert (A.rows() == _R().rows(), "mlnorm: A vs. R mismatch");
00621
00622 }
00623 };
00624 UIREGISTER2 (mlnorm,ldmat);
00625 SHAREDPTR2 ( mlnorm, ldmat );
00626 UIREGISTER2 (mlnorm,fsqmat);
00627 SHAREDPTR2 ( mlnorm, fsqmat );
00628 UIREGISTER2 (mlnorm, chmat);
00629 SHAREDPTR2 ( mlnorm, chmat );
00630
00632 template<class sq_T>
00633 class mgnorm : public mpdf_internal< enorm< sq_T > >
00634 {
00635 private:
00636
00637 shared_ptr<fnc> g;
00638
00639 public:
00641 mgnorm() : mpdf_internal<enorm<sq_T> >() { }
00643 inline void set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0);
00644 inline void condition (const vec &cond);
00645
00646
00665 void from_setting (const Setting &set) {
00666 mpdf::from_setting(set);
00667 shared_ptr<fnc> g = UI::build<fnc> (set, "g", UI::compulsory);
00668
00669 mat R;
00670 vec dR;
00671 if (UI::get (dR, set, "dR"))
00672 R = diag (dR);
00673 else
00674 UI::get (R, set, "R", UI::compulsory);
00675
00676 set_parameters (g, R);
00677 validate();
00678 }
00679 void validate() {
00680 bdm_assert(g->dimension()==this->dimension(),"incompatible function");
00681 }
00682 };
00683
00684 UIREGISTER2 (mgnorm, chmat);
00685 SHAREDPTR2 ( mgnorm, chmat );
00686
00687
00695 class mlstudent : public mlnorm<ldmat, enorm>
00696 {
00697 protected:
00699 ldmat Lambda;
00701 ldmat &_R;
00703 ldmat Re;
00704 public:
00705 mlstudent () : mlnorm<ldmat, enorm> (),
00706 Lambda (), _R (iepdf._R()) {}
00708 void set_parameters (const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0) {
00709 iepdf.set_parameters (mu0, R0);
00710 A = A0;
00711 mu_const = mu0;
00712 Re = R0;
00713 Lambda = Lambda0;
00714 }
00715 void condition (const vec &cond) {
00716 iepdf._mu() = A * cond + mu_const;
00717 double zeta;
00718
00719 if ( (cond.length() + 1) == Lambda.rows()) {
00720 zeta = Lambda.invqform (concat (cond, vec_1 (1.0)));
00721 } else {
00722 zeta = Lambda.invqform (cond);
00723 }
00724 _R = Re;
00725 _R *= (1 + zeta);
00726 };
00727
00728 void validate() {
00729 bdm_assert (A.rows() == mu_const.length(), "mlstudent: A vs. mu mismatch");
00730 bdm_assert (_R.rows() == A.rows(), "mlstudent: A vs. R mismatch");
00731
00732 }
00733 };
00743 class mgamma : public mpdf_internal<egamma>
00744 {
00745 protected:
00746
00748 double k;
00749
00751 vec &_beta;
00752
00753 public:
00755 mgamma() : mpdf_internal<egamma>(), k (0),
00756 _beta (iepdf._beta()) {
00757 }
00758
00760 void set_parameters (double k, const vec &beta0);
00761
00762 void condition (const vec &val) {_beta = k / val;};
00774 void from_setting (const Setting &set) {
00775 mpdf::from_setting (set);
00776 vec betatmp;
00777 UI::get (betatmp, set, "beta", UI::compulsory);
00778 UI::get (k, set, "k", UI::compulsory);
00779 set_parameters (k, betatmp);
00780 }
00781 };
00782 UIREGISTER (mgamma);
00783 SHAREDPTR (mgamma);
00784
00794 class migamma : public mpdf_internal<eigamma>
00795 {
00796 protected:
00798 double k;
00799
00801 vec &_alpha;
00802
00804 vec &_beta;
00805
00806 public:
00809 migamma() : mpdf_internal<eigamma>(),
00810 k (0),
00811 _alpha (iepdf._alpha()),
00812 _beta (iepdf._beta()) {
00813 }
00814
00815 migamma (const migamma &m) : mpdf_internal<eigamma>(),
00816 k (0),
00817 _alpha (iepdf._alpha()),
00818 _beta (iepdf._beta()) {
00819 }
00821
00823 void set_parameters (int len, double k0) {
00824 k = k0;
00825 iepdf.set_parameters ( (1.0 / (k*k) + 2.0) *ones (len) , ones (len) );
00826 dimc = dimension();
00827 };
00828 void condition (const vec &val) {
00829 _beta = elem_mult (val, (_alpha - 1.0));
00830 };
00831 };
00832
00833
00845 class mgamma_fix : public mgamma
00846 {
00847 protected:
00849 double l;
00851 vec refl;
00852 public:
00854 mgamma_fix () : mgamma (), refl () {};
00856 void set_parameters (double k0 , vec ref0, double l0) {
00857 mgamma::set_parameters (k0, ref0);
00858 refl = pow (ref0, 1.0 - l0);l = l0;
00859 dimc = dimension();
00860 };
00861
00862 void condition (const vec &val) {vec mean = elem_mult (refl, pow (val, l)); _beta = k / mean;};
00863 };
00864
00865
00878 class migamma_ref : public migamma
00879 {
00880 protected:
00882 double l;
00884 vec refl;
00885 public:
00887 migamma_ref () : migamma (), refl () {};
00889 void set_parameters (double k0 , vec ref0, double l0) {
00890 migamma::set_parameters (ref0.length(), k0);
00891 refl = pow (ref0, 1.0 - l0);
00892 l = l0;
00893 dimc = dimension();
00894 };
00895
00896 void condition (const vec &val) {
00897 vec mean = elem_mult (refl, pow (val, l));
00898 migamma::condition (mean);
00899 };
00900
00901
00914 void from_setting (const Setting &set);
00915
00916
00917 };
00918
00919
00920 UIREGISTER (migamma_ref);
00921 SHAREDPTR (migamma_ref);
00922
00933 class elognorm: public enorm<ldmat>
00934 {
00935 public:
00936 vec sample() const {return exp (enorm<ldmat>::sample());};
00937 vec mean() const {vec var = enorm<ldmat>::variance();return exp (mu - 0.5*var);};
00938
00939 };
00940
00947 class mlognorm : public mpdf_internal<elognorm>
00948 {
00949 protected:
00951 double sig2;
00952
00954 vec μ
00955 public:
00957 mlognorm() : mpdf_internal<elognorm>(),
00958 sig2 (0),
00959 mu (iepdf._mu()) {
00960 }
00961
00963 void set_parameters (int size, double k) {
00964 sig2 = 0.5 * log (k * k + 1);
00965 iepdf.set_parameters (zeros (size), 2*sig2*eye (size));
00966
00967 dimc = size;
00968 };
00969
00970 void condition (const vec &val) {
00971 mu = log (val) - sig2;
00972 };
00973
00985 void from_setting (const Setting &set);
00986
00987
00988
00989 };
00990
00991 UIREGISTER (mlognorm);
00992 SHAREDPTR (mlognorm);
00993
00997 class eWishartCh : public epdf
00998 {
00999 protected:
01001 chmat Y;
01003 int p;
01005 double delta;
01006 public:
01008 void set_parameters (const mat &Y0, const double delta0) {Y = chmat (Y0);delta = delta0; p = Y.rows(); dim = p * p; }
01010 mat sample_mat() const {
01011 mat X = zeros (p, p);
01012
01013
01014 for (int i = 0;i < p;i++) {
01015 GamRNG.setup (0.5* (delta - i) , 0.5);
01016 #pragma omp critical
01017 X (i, i) = sqrt (GamRNG());
01018 }
01019
01020 for (int i = 0;i < p;i++) {
01021 for (int j = i + 1;j < p;j++) {
01022 #pragma omp critical
01023 X (i, j) = NorRNG.sample();
01024 }
01025 }
01026 return X*Y._Ch();
01027 }
01028 vec sample () const {
01029 return vec (sample_mat()._data(), p*p);
01030 }
01032 void setY (const mat &Ch0) {copy_vector (dim, Ch0._data(), Y._Ch()._data());}
01034 void _setY (const vec &ch0) {copy_vector (dim, ch0._data(), Y._Ch()._data()); }
01036 const chmat& getY() const {return Y;}
01037 };
01038
01040
01042 class eiWishartCh: public epdf
01043 {
01044 protected:
01046 eWishartCh W;
01048 int p;
01050 double delta;
01051 public:
01053 void set_parameters (const mat &Y0, const double delta0) {
01054 delta = delta0;
01055 W.set_parameters (inv (Y0), delta0);
01056 dim = W.dimension(); p = Y0.rows();
01057 }
01058 vec sample() const {mat iCh; iCh = inv (W.sample_mat()); return vec (iCh._data(), dim);}
01060 void _setY (const vec &y0) {
01061 mat Ch (p, p);
01062 mat iCh (p, p);
01063 copy_vector (dim, y0._data(), Ch._data());
01064
01065 iCh = inv (Ch);
01066 W.setY (iCh);
01067 }
01068 virtual double evallog (const vec &val) const {
01069 chmat X (p);
01070 const chmat& Y = W.getY();
01071
01072 copy_vector (p*p, val._data(), X._Ch()._data());
01073 chmat iX (p);X.inv (iX);
01074
01075
01076 mat M = Y.to_mat() * iX.to_mat();
01077
01078 double log1 = 0.5 * p * (2 * Y.logdet()) - 0.5 * (delta + p + 1) * (2 * X.logdet()) - 0.5 * trace (M);
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088 return log1;
01089 };
01090
01091 };
01092
01094 class rwiWishartCh : public mpdf_internal<eiWishartCh>
01095 {
01096 protected:
01098 double sqd;
01100 vec refl;
01102 double l;
01104 int p;
01105
01106 public:
01107 rwiWishartCh() : sqd (0), l (0), p (0) {}
01109 void set_parameters (int p0, double k, vec ref0, double l0) {
01110 p = p0;
01111 double delta = 2 / (k * k) + p + 3;
01112 sqd = sqrt (delta - p - 1);
01113 l = l0;
01114 refl = pow (ref0, 1 - l);
01115
01116 iepdf.set_parameters (eye (p), delta);
01117 dimc = iepdf.dimension();
01118 }
01119 void condition (const vec &c) {
01120 vec z = c;
01121 int ri = 0;
01122 for (int i = 0;i < p*p;i += (p + 1)) {
01123 z (i) = pow (z (i), l) * refl (ri);
01124 ri++;
01125 }
01126
01127 iepdf._setY (sqd*z);
01128 }
01129 };
01130
01132 enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 };
01138 class eEmp: public epdf
01139 {
01140 protected :
01142 int n;
01144 vec w;
01146 Array<vec> samples;
01147 public:
01150 eEmp () : epdf (), w (), samples () {};
01152 eEmp (const eEmp &e) : epdf (e), w (e.w), samples (e.samples) {};
01154
01156 void set_statistics (const vec &w0, const epdf &pdf0);
01158 void set_statistics (const epdf &pdf0 , int n) {set_statistics (ones (n) / n, pdf0);};
01160 void set_samples (const epdf* pdf0);
01162 void set_parameters (int n0, bool copy = true) {n = n0; w.set_size (n0, copy);samples.set_size (n0, copy);};
01164 void set_parameters (const Array<vec> &Av) {
01165 bdm_assert(Av.size()>0,"Empty samples");
01166 n = Av.size();
01167 epdf::set_parameters(Av(0).length());
01168 w=1/n*ones(n);
01169 samples=Av;
01170 };
01172 vec& _w() {return w;};
01174 const vec& _w() const {return w;};
01176 Array<vec>& _samples() {return samples;};
01178 const Array<vec>& _samples() const {return samples;};
01180 ivec resample (RESAMPLING_METHOD method = SYSTEMATIC);
01181
01183 vec sample() const {
01184 bdm_error ("Not implemented");
01185 return vec();
01186 }
01187
01189 double evallog (const vec &val) const {
01190 bdm_error ("Not implemented");
01191 return 0.0;
01192 }
01193
01194 vec mean() const {
01195 vec pom = zeros (dim);
01196 for (int i = 0;i < n;i++) {pom += samples (i) * w (i);}
01197 return pom;
01198 }
01199 vec variance() const {
01200 vec pom = zeros (dim);
01201 for (int i = 0;i < n;i++) {pom += pow (samples (i), 2) * w (i);}
01202 return pom -pow (mean(), 2);
01203 }
01205 void qbounds (vec &lb, vec &ub, double perc = 0.95) const {
01206
01207 lb.set_size (dim);
01208 ub.set_size (dim);
01209 lb = std::numeric_limits<double>::infinity();
01210 ub = -std::numeric_limits<double>::infinity();
01211 int j;
01212 for (int i = 0;i < n;i++) {
01213 for (j = 0;j < dim; j++) {
01214 if (samples (i) (j) < lb (j)) {lb (j) = samples (i) (j);}
01215 if (samples (i) (j) > ub (j)) {ub (j) = samples (i) (j);}
01216 }
01217 }
01218 }
01219 };
01220
01221
01223
01224 template<class sq_T>
01225 void enorm<sq_T>::set_parameters (const vec &mu0, const sq_T &R0)
01226 {
01227
01228 mu = mu0;
01229 R = R0;
01230 validate();
01231 };
01232
01233 template<class sq_T>
01234 void enorm<sq_T>::from_setting (const Setting &set)
01235 {
01236 epdf::from_setting (set);
01237
01238 UI::get (mu, set, "mu", UI::compulsory);
01239 mat Rtmp;
01240 UI::get (Rtmp, set, "R", UI::compulsory);
01241 R = Rtmp;
01242 validate();
01243 }
01244
01245 template<class sq_T>
01246 void enorm<sq_T>::dupdate (mat &v, double nu)
01247 {
01248
01249 };
01250
01251
01252
01253
01254
01255
01256 template<class sq_T>
01257 vec enorm<sq_T>::sample() const
01258 {
01259 vec x (dim);
01260 #pragma omp critical
01261 NorRNG.sample_vector (dim, x);
01262 vec smp = R.sqrt_mult (x);
01263
01264 smp += mu;
01265 return smp;
01266 };
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276 template<class sq_T>
01277 double enorm<sq_T>::evallog_nn (const vec &val) const
01278 {
01279
01280 double tmp = -0.5 * (R.invqform (mu - val));
01281 return tmp;
01282 };
01283
01284 template<class sq_T>
01285 inline double enorm<sq_T>::lognc () const
01286 {
01287
01288 double tmp = 0.5 * (R.cols() * 1.83787706640935 + R.logdet());
01289 return tmp;
01290 };
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319 template<class sq_T>
01320 shared_ptr<epdf> enorm<sq_T>::marginal ( const RV &rvn ) const
01321 {
01322 enorm<sq_T> *tmp = new enorm<sq_T> ();
01323 shared_ptr<epdf> narrow(tmp);
01324 marginal ( rvn, *tmp );
01325 return narrow;
01326 }
01327
01328 template<class sq_T>
01329 void enorm<sq_T>::marginal ( const RV &rvn, enorm<sq_T> &target ) const
01330 {
01331 bdm_assert (isnamed(), "rv description is not assigned");
01332 ivec irvn = rvn.dataind (rv);
01333
01334 sq_T Rn (R, irvn);
01335
01336 target.set_rv ( rvn );
01337 target.set_parameters (mu (irvn), Rn);
01338 }
01339
01340 template<class sq_T>
01341 shared_ptr<mpdf> enorm<sq_T>::condition ( const RV &rvn ) const
01342 {
01343 mlnorm<sq_T> *tmp = new mlnorm<sq_T> ();
01344 shared_ptr<mpdf> narrow(tmp);
01345 condition ( rvn, *tmp );
01346 return narrow;
01347 }
01348
01349 template<class sq_T>
01350 void enorm<sq_T>::condition ( const RV &rvn, mpdf &target ) const
01351 {
01352 typedef mlnorm<sq_T> TMlnorm;
01353
01354 bdm_assert (isnamed(), "rvs are not assigned");
01355 TMlnorm &uptarget = dynamic_cast<TMlnorm &>(target);
01356
01357 RV rvc = rv.subt (rvn);
01358 bdm_assert ( (rvc._dsize() + rvn._dsize() == rv._dsize()), "wrong rvn");
01359
01360 ivec irvn = rvn.dataind (rv);
01361 ivec irvc = rvc.dataind (rv);
01362 ivec perm = concat (irvn , irvc);
01363 sq_T Rn (R, perm);
01364
01365
01366 mat S = Rn.to_mat();
01367
01368 int n = rvn._dsize() - 1;
01369 int end = R.rows() - 1;
01370 mat S11 = S.get (0, n, 0, n);
01371 mat S12 = S.get (0, n , rvn._dsize(), end);
01372 mat S22 = S.get (rvn._dsize(), end, rvn._dsize(), end);
01373
01374 vec mu1 = mu (irvn);
01375 vec mu2 = mu (irvc);
01376 mat A = S12 * inv (S22);
01377 sq_T R_n (S11 - A *S12.T());
01378
01379 uptarget.set_rv (rvn);
01380 uptarget.set_rvc (rvc);
01381 uptarget.set_parameters (A, mu1 - A*mu2, R_n);
01382 }
01383
01386 template<class sq_T>
01387 void mgnorm<sq_T >::set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0) {
01388 g = g0;
01389 this->iepdf.set_parameters (zeros (g->dimension()), R0);
01390 }
01391
01392 template<class sq_T>
01393 void mgnorm<sq_T >::condition (const vec &cond) {this->iepdf._mu() = g->eval (cond);};
01394
01396 template<class sq_T>
01397 std::ostream &operator<< (std::ostream &os, mlnorm<sq_T> &ml)
01398 {
01399 os << "A:" << ml.A << endl;
01400 os << "mu:" << ml.mu_const << endl;
01401 os << "R:" << ml._R() << endl;
01402 return os;
01403 };
01404
01405 }
01406 #endif //EF_H