- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

# exp_family.h

Go to the documentation of this file.

    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 //      eEF() :epdf() {};
    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                 //original Bayes
    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);
    00136                 void from_setting (const Setting &root);
    00137                 void validate() {
    00138                         bdm_assert_debug (mu.length() == R.rows(), "parameters mismatch");
    00139                         dim = mu.length();
    00140                 }
    00142
    00145
    00147                 void dupdate (mat &v, double nu = 1.0);
    00148
    00149                 vec sample() const;
    00150
    00151                 double evallog_nn (const vec &val) const;
    00152                 double lognc () const;
    00153                 vec mean() const {return mu;}
    00154                 vec variance() const {return diag (R.to_mat());}
    00155 //      mlnorm<sq_T>* condition ( const RV &rvn ) const ; <=========== fails to cmpile. Why?
    00156                 shared_ptr<mpdf> condition ( const RV &rvn ) const;
    00157
    00158                 // target not typed to mlnorm<sq_T, enorm<sq_T> > &
    00159                 // because that doesn't compile (perhaps because we
    00160                 // haven't finished defining enorm yet), but the type
    00161                 // is required
    00162                 void condition ( const RV &rvn, mpdf &target ) const;
    00163
    00164                 shared_ptr<epdf> marginal (const RV &rvn ) const;
    00165                 void marginal ( const RV &rvn, enorm<sq_T> &target ) const;
    00167
    00170
    00171                 vec& _mu() {return mu;}
    00172                 const vec& _mu() const {return mu;}
    00173                 void set_mu (const vec mu0) { mu = mu0;}
    00174                 sq_T& _R() {return R;}
    00175                 const sq_T& _R() const {return R;}
    00177
    00178 };
    00179 UIREGISTER2 (enorm, chmat);
    00180 SHAREDPTR2 ( enorm, chmat );
    00181 UIREGISTER2 (enorm, ldmat);
    00182 SHAREDPTR2 ( enorm, ldmat );
    00183 UIREGISTER2 (enorm, fsqmat);
    00184 SHAREDPTR2 ( enorm, fsqmat );
    00185
    00186
    00193 class egiw : public eEF
    00194 {
    00195         protected:
    00197                 ldmat V;
    00199                 double nu;
    00201                 int dimx;
    00203                 int nPsi;
    00204         public:
    00207                 egiw() : eEF() {};
    00208                 egiw (int dimx0, ldmat V0, double nu0 = -1.0) : eEF() {set_parameters (dimx0, V0, nu0);};
    00209
    00210                 void set_parameters (int dimx0, ldmat V0, double nu0 = -1.0) {
    00211                         dimx = dimx0;
    00212                         nPsi = V0.rows() - dimx;
    00213                         dim = dimx * (dimx + nPsi); // size(R) + size(Theta)
    00214
    00215                         V = V0;
    00216                         if (nu0 < 0) {
    00217                                 nu = 0.1 + nPsi + 2 * dimx + 2; // +2 assures finite expected value of R
    00218                                 // terms before that are sufficient for finite normalization
    00219                         } else {
    00220                                 nu = nu0;
    00221                         }
    00222                 }
    00224
    00225                 vec sample() const;
    00226                 vec mean() const;
    00227                 vec variance() const;
    00228
    00230                 vec est_theta() const;
    00231
    00233                 ldmat est_theta_cov() const;
    00234
    00236                 void mean_mat (mat &M, mat&R) const;
    00238                 double evallog_nn (const vec &val) const;
    00239                 double lognc () const;
    00240                 void pow (double p) {V *= p;nu *= p;};
    00241
    00244
    00245                 ldmat& _V() {return V;}
    00246                 const ldmat& _V() const {return V;}
    00247                 double& _nu()  {return nu;}
    00248                 const double& _nu() const {return nu;}
    00249                 void from_setting (const Setting &set) {
    00250                         UI::get (nu, set, "nu", UI::compulsory);
    00251                         UI::get (dimx, set, "dimx", UI::compulsory);
    00252                         mat V;
    00253                         UI::get (V, set, "V", UI::compulsory);
    00254                         set_parameters (dimx, V, nu);
    00255                         shared_ptr<RV> rv = UI::build<RV> (set, "rv", UI::compulsory);
    00256                         set_rv (*rv);
    00257                 }
    00259 };
    00260 UIREGISTER ( egiw );
    00261 SHAREDPTR ( egiw );
    00262
    00271 class eDirich: public eEF
    00272 {
    00273         protected:
    00275                 vec beta;
    00276         public:
    00279
    00280                 eDirich () : eEF () {};
    00281                 eDirich (const eDirich &D0) : eEF () {set_parameters (D0.beta);};
    00282                 eDirich (const vec &beta0) {set_parameters (beta0);};
    00283                 void set_parameters (const vec &beta0) {
    00284                         beta = beta0;
    00285                         dim = beta.length();
    00286                 }
    00288
    00289                 vec sample() const {
    00290                         bdm_error ("Not implemented");
    00291                         return vec();
    00292                 }
    00293
    00294                 vec mean() const {return beta / sum (beta);};
    00295                 vec variance() const {double gamma = sum (beta); return elem_mult (beta, (beta + 1)) / (gamma* (gamma + 1));}
    00297                 double evallog_nn (const vec &val) const {
    00298                         double tmp; tmp = (beta - 1) * log (val);
    00299                         return tmp;
    00300                 }
    00301
    00302                 double lognc () const {
    00303                         double tmp;
    00304                         double gam = sum (beta);
    00305                         double lgb = 0.0;
    00306                         for (int i = 0;i < beta.length();i++) {lgb += lgamma (beta (i));}
    00307                         tmp = lgb - lgamma (gam);
    00308                         return tmp;
    00309                 }
    00310
    00312                 vec& _beta()  {return beta;}
    00314 };
    00315
    00317 class multiBM : public BMEF
    00318 {
    00319         protected:
    00321                 eDirich est;
    00323                 vec &beta;
    00324         public:
    00326                 multiBM () : BMEF (), est (), beta (est._beta()) {
    00327                         if (beta.length() > 0) {last_lognc = est.lognc();}
    00328                         else{last_lognc = 0.0;}
    00329                 }
    00331                 multiBM (const multiBM &B) : BMEF (B), est (B.est), beta (est._beta()) {}
    00333                 void set_statistics (const BM* mB0) {const multiBM* mB = dynamic_cast<const multiBM*> (mB0); beta = mB->beta;}
    00334                 void bayes (const vec &dt) {
    00335                         if (frg < 1.0) {beta *= frg;last_lognc = est.lognc();}
    00336                         beta += dt;
    00337                         if (evalll) {ll = est.lognc() - last_lognc;}
    00338                 }
    00339                 double logpred (const vec &dt) const {
    00340                         eDirich pred (est);
    00341                         vec &beta = pred._beta();
    00342
    00343                         double lll;
    00344                         if (frg < 1.0)
    00345                                 {beta *= frg;lll = pred.lognc();}
    00346                         else
    00347                                 if (evalll) {lll = last_lognc;}
    00348                                 else{lll = pred.lognc();}
    00349
    00350                         beta += dt;
    00351                         return pred.lognc() - lll;
    00352                 }
    00353                 void flatten (const BMEF* B) {
    00354                         const multiBM* E = dynamic_cast<const multiBM*> (B);
    00355                         // sum(beta) should be equal to sum(B.beta)
    00356                         const vec &Eb = E->beta;//const_cast<multiBM*> ( E )->_beta();
    00357                         beta *= (sum (Eb) / sum (beta));
    00358                         if (evalll) {last_lognc = est.lognc();}
    00359                 }
    00361                 const eDirich& posterior() const {return est;};
    00363                 void set_parameters (const vec &beta0) {
    00364                         est.set_parameters (beta0);
    00365                         if (evalll) {last_lognc = est.lognc();}
    00366                 }
    00367 };
    00368
    00378 class egamma : public eEF
    00379 {
    00380         protected:
    00382                 vec alpha;
    00384                 vec beta;
    00385         public :
    00388                 egamma () : eEF (), alpha (0), beta (0) {};
    00389                 egamma (const vec &a, const vec &b) {set_parameters (a, b);};
    00390                 void set_parameters (const vec &a, const vec &b) {alpha = a, beta = b;dim = alpha.length();};
    00392
    00393                 vec sample() const;
    00394                 double evallog (const vec &val) const;
    00395                 double lognc () const;
    00397                 vec& _alpha() {return alpha;}
    00399                 vec& _beta() {return beta;}
    00400                 vec mean() const {return elem_div (alpha, beta);}
    00401                 vec variance() const {return elem_div (alpha, elem_mult (beta, beta)); }
    00402
    00411                 void from_setting (const Setting &set) {
    00412                         epdf::from_setting (set); // reads rv
    00413                         UI::get (alpha, set, "alpha", UI::compulsory);
    00414                         UI::get (beta, set, "beta", UI::compulsory);
    00415                         validate();
    00416                 }
    00417                 void validate() {
    00418                         bdm_assert_debug (alpha.length() == beta.length(), "parameters do not match");
    00419                         dim = alpha.length();
    00420                 }
    00421 };
    00422 UIREGISTER (egamma);
    00423 SHAREDPTR ( egamma );
    00424
    00441 class eigamma : public egamma
    00442 {
    00443         protected:
    00444         public :
    00449
    00450                 vec sample() const {return 1.0 / egamma::sample();};
    00452                 vec mean() const {return elem_div (beta, alpha - 1);}
    00453                 vec variance() const {vec mea = mean(); return elem_div (elem_mult (mea, mea), alpha - 2);}
    00454 };
    00455 /*
    00457 class emix : public epdf {
    00458 protected:
    00459         int n;
    00460         vec &w;
    00461         Array<epdf*> Coms;
    00462 public:
    00464         emix ( const RV &rv, vec &w0): epdf(rv), n(w0.length()), w(w0), Coms(n) {};
    00465         void set_parameters( int &i, double wi, epdf* ep){w(i)=wi;Coms(i)=ep;}
    00466         vec mean(){vec pom; for(int i=0;i<n;i++){pom+=Coms(i)->mean()*w(i);} return pom;};
    00467 };
    00468 */
    00469
    00471
    00472 class euni: public epdf
    00473 {
    00474         protected:
    00476                 vec low;
    00478                 vec high;
    00480                 vec distance;
    00482                 double nk;
    00484                 double lnk;
    00485         public:
    00488                 euni () : epdf () {}
    00489                 euni (const vec &low0, const vec &high0) {set_parameters (low0, high0);}
    00490                 void set_parameters (const vec &low0, const vec &high0) {
    00491                         distance = high0 - low0;
    00492                         bdm_assert_debug (min (distance) > 0.0, "bad support");
    00493                         low = low0;
    00494                         high = high0;
    00495                         nk = prod (1.0 / distance);
    00496                         lnk = log (nk);
    00497                         dim = low.length();
    00498                 }
    00500
    00501                 double evallog (const vec &val) const  {
    00502                         if (any (val < low) && any (val > high)) {return inf;}
    00503                         else return lnk;
    00504                 }
    00505                 vec sample() const {
    00506                         vec smp (dim);
    00507 #pragma omp critical
    00508                         UniRNG.sample_vector (dim , smp);
    00509                         return low + elem_mult (distance, smp);
    00510                 }
    00512                 vec mean() const {return (high -low) / 2.0;}
    00513                 vec variance() const {return (pow (high, 2) + pow (low, 2) + elem_mult (high, low)) / 3.0;}
    00522                 void from_setting (const Setting &set) {
    00523                         epdf::from_setting (set); // reads rv and rvc
    00524
    00525                         UI::get (high, set, "high", UI::compulsory);
    00526                         UI::get (low, set, "low", UI::compulsory);
    00527                 }
    00528 };
    00529
    00530
    00536 template < class sq_T, template <typename> class TEpdf = enorm >
    00537 class mlnorm : public mpdf_internal< TEpdf<sq_T> >
    00538 {
    00539         protected:
    00541                 mat A;
    00543                 vec mu_const;
    00544 //                      vec& _mu; //cached epdf.mu; !!!!!! WHY NOT?
    00545         public:
    00548                 mlnorm() : mpdf_internal< TEpdf<sq_T> >() {};
    00549                 mlnorm (const mat &A, const vec &mu0, const sq_T &R) : mpdf_internal< TEpdf<sq_T> >() {
    00550                         set_parameters (A, mu0, R);
    00551                 }
    00552
    00554                 void set_parameters (const  mat &A0, const vec &mu0, const sq_T &R0) {
    00555                         bdm_assert_debug (A0.rows() == mu0.length(), "mlnorm: A vs. mu mismatch");
    00556                         bdm_assert_debug (A0.rows() == R0.rows(), "mlnorm: A vs. R mismatch");
    00557
    00558                         this->iepdf.set_parameters (zeros (A0.rows()), R0);
    00559                         A = A0;
    00560                         mu_const = mu0;
    00561                         this->dimc = A0.cols();
    00562                 }
    00565                 void condition (const vec &cond) {
    00566                         this->iepdf._mu() = A * cond + mu_const;
    00567 //R is already assigned;
    00568                 }
    00569
    00571                 vec& _mu_const() {return mu_const;}
    00573                 mat& _A() {return A;}
    00575                 mat _R() { return this->iepdf._R().to_mat(); }
    00576
    00578                 template<typename sq_M>
    00579                 friend std::ostream &operator<< (std::ostream &os,  mlnorm<sq_M, enorm> &ml);
    00580
    00581                 void from_setting (const Setting &set) {
    00582                         mpdf::from_setting (set);
    00583
    00584                         UI::get (A, set, "A", UI::compulsory);
    00585                         UI::get (mu_const, set, "const", UI::compulsory);
    00586                         mat R0;
    00587                         UI::get (R0, set, "R", UI::compulsory);
    00588                         set_parameters (A, mu_const, R0);
    00589                 };
    00590 };
    00591 UIREGISTER2 (mlnorm,ldmat);
    00592 SHAREDPTR2 ( mlnorm, ldmat );
    00593 UIREGISTER2 (mlnorm,fsqmat);
    00594 SHAREDPTR2 ( mlnorm, fsqmat );
    00595 UIREGISTER2 (mlnorm, chmat);
    00596 SHAREDPTR2 ( mlnorm, chmat );
    00597
    00599 template<class sq_T>
    00600 class mgnorm : public mpdf_internal< enorm< sq_T > >
    00601 {
    00602         private:
    00603 //                      vec &mu; WHY NOT?
    00604                 shared_ptr<fnc> g;
    00605
    00606         public:
    00608                 mgnorm() : mpdf_internal<enorm<sq_T> >() { }
    00610                 inline void set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0);
    00611                 inline void condition (const vec &cond);
    00612
    00613
    00641                 void from_setting (const Setting &set) {
    00642                         shared_ptr<fnc> g = UI::build<fnc> (set, "g", UI::compulsory);
    00643
    00644                         mat R;
    00645                         vec dR;
    00646                         if (UI::get (dR, set, "dR"))
    00647                                 R = diag (dR);
    00648                         else
    00649                                 UI::get (R, set, "R", UI::compulsory);
    00650
    00651                         set_parameters (g, R);
    00652                 }
    00653 };
    00654
    00655 UIREGISTER2 (mgnorm, chmat);
    00656 SHAREDPTR2 ( mgnorm, chmat );
    00657
    00658
    00666 class mlstudent : public mlnorm<ldmat, enorm>
    00667 {
    00668         protected:
    00670                 ldmat Lambda;
    00672                 ldmat &_R;
    00674                 ldmat Re;
    00675         public:
    00676                 mlstudent () : mlnorm<ldmat, enorm> (),
    00677                                 Lambda (),      _R (iepdf._R()) {}
    00679                 void set_parameters (const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0) {
    00680                         bdm_assert_debug (A0.rows() == mu0.length(), "mlstudent: A vs. mu mismatch");
    00681                         bdm_assert_debug (R0.rows() == A0.rows(), "mlstudent: A vs. R mismatch");
    00682
    00683                         iepdf.set_parameters (mu0, R0);// was Lambda, why?
    00684                         A = A0;
    00685                         mu_const = mu0;
    00686                         Re = R0;
    00687                         Lambda = Lambda0;
    00688                 }
    00689                 void condition (const vec &cond) {
    00690                         iepdf._mu() = A * cond + mu_const;
    00691                         double zeta;
    00692                         //ugly hack!
    00693                         if ( (cond.length() + 1) == Lambda.rows()) {
    00694                                 zeta = Lambda.invqform (concat (cond, vec_1 (1.0)));
    00695                         } else {
    00696                                 zeta = Lambda.invqform (cond);
    00697                         }
    00698                         _R = Re;
    00699                         _R *= (1 + zeta);// / ( nu ); << nu is in Re!!!!!!
    00700                 };
    00701
    00702 };
    00712 class mgamma : public mpdf_internal<egamma>
    00713 {
    00714         protected:
    00715
    00717                 double k;
    00718
    00720                 vec &_beta;
    00721
    00722         public:
    00724                 mgamma() : mpdf_internal<egamma>(), k (0),
    00725                                 _beta (iepdf._beta()) {
    00726                 }
    00727
    00729                 void set_parameters (double k, const vec &beta0);
    00730
    00731                 void condition (const vec &val) {_beta = k / val;};
    00741                 void from_setting (const Setting &set) {
    00742                         mpdf::from_setting (set); // reads rv and rvc
    00743                         vec betatmp; // ugly but necessary
    00744                         UI::get (betatmp, set, "beta", UI::compulsory);
    00745                         UI::get (k, set, "k", UI::compulsory);
    00746                         set_parameters (k, betatmp);
    00747                 }
    00748 };
    00749 UIREGISTER (mgamma);
    00750 SHAREDPTR (mgamma);
    00751
    00761 class migamma : public mpdf_internal<eigamma>
    00762 {
    00763         protected:
    00765                 double k;
    00766
    00768                 vec &_alpha;
    00769
    00771                 vec &_beta;
    00772
    00773         public:
    00776                 migamma() : mpdf_internal<eigamma>(),
    00777                                 k (0),
    00778                                 _alpha (iepdf._alpha()),
    00779                                 _beta (iepdf._beta()) {
    00780                 }
    00781
    00782                 migamma (const migamma &m) : mpdf_internal<eigamma>(),
    00783                                 k (0),
    00784                                 _alpha (iepdf._alpha()),
    00785                                 _beta (iepdf._beta()) {
    00786                 }
    00788
    00790                 void set_parameters (int len, double k0) {
    00791                         k = k0;
    00792                         iepdf.set_parameters ( (1.0 / (k*k) + 2.0) *ones (len) /*alpha*/, ones (len) /*beta*/);
    00793                         dimc = dimension();
    00794                 };
    00795                 void condition (const vec &val) {
    00796                         _beta = elem_mult (val, (_alpha - 1.0));
    00797                 };
    00798 };
    00799
    00800
    00812 class mgamma_fix : public mgamma
    00813 {
    00814         protected:
    00816                 double l;
    00818                 vec refl;
    00819         public:
    00821                 mgamma_fix () : mgamma (), refl () {};
    00823                 void set_parameters (double k0 , vec ref0, double l0) {
    00824                         mgamma::set_parameters (k0, ref0);
    00825                         refl = pow (ref0, 1.0 - l0);l = l0;
    00826                         dimc = dimension();
    00827                 };
    00828
    00829                 void condition (const vec &val) {vec mean = elem_mult (refl, pow (val, l)); _beta = k / mean;};
    00830 };
    00831
    00832
    00845 class migamma_ref : public migamma
    00846 {
    00847         protected:
    00849                 double l;
    00851                 vec refl;
    00852         public:
    00854                 migamma_ref () : migamma (), refl () {};
    00856                 void set_parameters (double k0 , vec ref0, double l0) {
    00857                         migamma::set_parameters (ref0.length(), k0);
    00858                         refl = pow (ref0, 1.0 - l0);
    00859                         l = l0;
    00860                         dimc = dimension();
    00861                 };
    00862
    00863                 void condition (const vec &val) {
    00864                         vec mean = elem_mult (refl, pow (val, l));
    00865                         migamma::condition (mean);
    00866                 };
    00867
    00888                 void from_setting (const Setting &set);
    00889
    00890                 // TODO dodelat void to_setting( Setting &set ) const;
    00891 };
    00892
    00893
    00894 UIREGISTER (migamma_ref);
    00895 SHAREDPTR (migamma_ref);
    00896
    00906 class elognorm: public enorm<ldmat>
    00907 {
    00908         public:
    00909                 vec sample() const {return exp (enorm<ldmat>::sample());};
    00910                 vec mean() const {vec var = enorm<ldmat>::variance();return exp (mu - 0.5*var);};
    00911
    00912 };
    00913
    00925 class mlognorm : public mpdf_internal<elognorm>
    00926 {
    00927         protected:
    00929                 double sig2;
    00930
    00932                 vec &mu;
    00933         public:
    00935                 mlognorm() : mpdf_internal<elognorm>(),
    00936                                 sig2 (0),
    00937                                 mu (iepdf._mu()) {
    00938                 }
    00939
    00941                 void set_parameters (int size, double k) {
    00942                         sig2 = 0.5 * log (k * k + 1);
    00943                         iepdf.set_parameters (zeros (size), 2*sig2*eye (size));
    00944
    00945                         dimc = size;
    00946                 };
    00947
    00948                 void condition (const vec &val) {
    00949                         mu = log (val) - sig2;//elem_mult ( refl,pow ( val,l ) );
    00950                 };
    00951
    00970                 void from_setting (const Setting &set);
    00971
    00972                 // TODO dodelat void to_setting( Setting &set ) const;
    00973
    00974 };
    00975
    00976 UIREGISTER (mlognorm);
    00977 SHAREDPTR (mlognorm);
    00978
    00982 class eWishartCh : public epdf
    00983 {
    00984         protected:
    00986                 chmat Y;
    00988                 int p;
    00990                 double delta;
    00991         public:
    00993                 void set_parameters (const mat &Y0, const double delta0) {Y = chmat (Y0);delta = delta0; p = Y.rows(); dim = p * p; }
    00995                 mat sample_mat() const {
    00996                         mat X = zeros (p, p);
    00997
    00998                         //sample diagonal
    00999                         for (int i = 0;i < p;i++) {
    01000                                 GamRNG.setup (0.5* (delta - i) , 0.5);   // no +1 !! index if from 0
    01001 #pragma omp critical
    01002                                 X (i, i) = sqrt (GamRNG());
    01003                         }
    01004                         //do the rest
    01005                         for (int i = 0;i < p;i++) {
    01006                                 for (int j = i + 1;j < p;j++) {
    01007 #pragma omp critical
    01008                                         X (i, j) = NorRNG.sample();
    01009                                 }
    01010                         }
    01011                         return X*Y._Ch();// return upper triangular part of the decomposition
    01012                 }
    01013                 vec sample () const {
    01014                         return vec (sample_mat()._data(), p*p);
    01015                 }
    01017                 void setY (const mat &Ch0) {copy_vector (dim, Ch0._data(), Y._Ch()._data());}
    01019                 void _setY (const vec &ch0) {copy_vector (dim, ch0._data(), Y._Ch()._data()); }
    01021                 const chmat& getY() const {return Y;}
    01022 };
    01023
    01025
    01027 class eiWishartCh: public epdf
    01028 {
    01029         protected:
    01031                 eWishartCh W;
    01033                 int p;
    01035                 double delta;
    01036         public:
    01038                 void set_parameters (const mat &Y0, const double delta0) {
    01039                         delta = delta0;
    01040                         W.set_parameters (inv (Y0), delta0);
    01041                         dim = W.dimension(); p = Y0.rows();
    01042                 }
    01043                 vec sample() const {mat iCh; iCh = inv (W.sample_mat()); return vec (iCh._data(), dim);}
    01045                 void _setY (const vec &y0) {
    01046                         mat Ch (p, p);
    01047                         mat iCh (p, p);
    01048                         copy_vector (dim, y0._data(), Ch._data());
    01049
    01050                         iCh = inv (Ch);
    01051                         W.setY (iCh);
    01052                 }
    01053                 virtual double evallog (const vec &val) const {
    01054                         chmat X (p);
    01055                         const chmat& Y = W.getY();
    01056
    01057                         copy_vector (p*p, val._data(), X._Ch()._data());
    01058                         chmat iX (p);X.inv (iX);
    01059                         // compute
    01060 //                              \frac{ |\Psi|^{m/2}|X|^{-(m+p+1)/2}e^{-tr(\Psi X^{-1})/2} }{ 2^{mp/2}\Gamma_p(m/2)},
    01061                         mat M = Y.to_mat() * iX.to_mat();
    01062
    01063                         double log1 = 0.5 * p * (2 * Y.logdet()) - 0.5 * (delta + p + 1) * (2 * X.logdet()) - 0.5 * trace (M);
    01064                         //Fixme! Multivariate gamma omitted!! it is ok for sampling, but not otherwise!!
    01065
    01066                         /*                              if (0) {
    01067                                                                 mat XX=X.to_mat();
    01068                                                                 mat YY=Y.to_mat();
    01069
    01070                                                                 double log2 = 0.5*p*log(det(YY))-0.5*(delta+p+1)*log(det(XX))-0.5*trace(YY*inv(XX));
    01071                                                                 cout << log1 << "," << log2 << endl;
    01072                                                         }*/
    01073                         return log1;
    01074                 };
    01075
    01076 };
    01077
    01079 class rwiWishartCh : public mpdf_internal<eiWishartCh>
    01080 {
    01081         protected:
    01083                 double sqd;
    01085                 vec refl;
    01087                 double l;
    01089                 int p;
    01090
    01091         public:
    01092                 rwiWishartCh() : sqd (0), l (0), p (0) {}
    01094                 void set_parameters (int p0, double k, vec ref0, double l0) {
    01095                         p = p0;
    01096                         double delta = 2 / (k * k) + p + 3;
    01097                         sqd = sqrt (delta - p - 1);
    01098                         l = l0;
    01099                         refl = pow (ref0, 1 - l);
    01100
    01101                         iepdf.set_parameters (eye (p), delta);
    01102                         dimc = iepdf.dimension();
    01103                 }
    01104                 void condition (const vec &c) {
    01105                         vec z = c;
    01106                         int ri = 0;
    01107                         for (int i = 0;i < p*p;i += (p + 1)) {//trace diagonal element
    01108                                 z (i) = pow (z (i), l) * refl (ri);
    01109                                 ri++;
    01110                         }
    01111
    01112                         iepdf._setY (sqd*z);
    01113                 }
    01114 };
    01115
    01117 enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 };
    01123 class eEmp: public epdf
    01124 {
    01125         protected :
    01127                 int n;
    01129                 vec w;
    01131                 Array<vec> samples;
    01132         public:
    01135                 eEmp () : epdf (), w (), samples () {};
    01137                 eEmp (const eEmp &e) : epdf (e), w (e.w), samples (e.samples) {};
    01139
    01141                 void set_statistics (const vec &w0, const epdf &pdf0);
    01143                 void set_statistics (const epdf &pdf0 , int n) {set_statistics (ones (n) / n, pdf0);};
    01145                 void set_samples (const epdf* pdf0);
    01147                 void set_parameters (int n0, bool copy = true) {n = n0; w.set_size (n0, copy);samples.set_size (n0, copy);};
    01149                 void set_parameters (const Array<vec> &Av) {
    01150                         bdm_assert_debug(Av.size()>0,"Empty samples");
    01151                         n = Av.size();
    01152                         epdf::set_parameters(Av(0).length());
    01153                         w=1/n*ones(n);
    01154                         samples=Av;
    01155                 };
    01157                 vec& _w()  {return w;};
    01159                 const vec& _w() const {return w;};
    01161                 Array<vec>& _samples() {return samples;};
    01163                 const Array<vec>& _samples() const {return samples;};
    01165                 ivec resample (RESAMPLING_METHOD method = SYSTEMATIC);
    01166
    01168                 vec sample() const {
    01169                         bdm_error ("Not implemented");
    01170                         return vec();
    01171                 }
    01172
    01174                 double evallog (const vec &val) const {
    01175                         bdm_error ("Not implemented");
    01176                         return 0.0;
    01177                 }
    01178
    01179                 vec mean() const {
    01180                         vec pom = zeros (dim);
    01181                         for (int i = 0;i < n;i++) {pom += samples (i) * w (i);}
    01182                         return pom;
    01183                 }
    01184                 vec variance() const {
    01185                         vec pom = zeros (dim);
    01186                         for (int i = 0;i < n;i++) {pom += pow (samples (i), 2) * w (i);}
    01187                         return pom -pow (mean(), 2);
    01188                 }
    01190                 void qbounds (vec &lb, vec &ub, double perc = 0.95) const {
    01191                         // lb in inf so than it will be pushed below;
    01192                         lb.set_size (dim);
    01193                         ub.set_size (dim);
    01194                         lb = std::numeric_limits<double>::infinity();
    01195                         ub = -std::numeric_limits<double>::infinity();
    01196                         int j;
    01197                         for (int i = 0;i < n;i++) {
    01198                                 for (j = 0;j < dim; j++) {
    01199                                         if (samples (i) (j) < lb (j)) {lb (j) = samples (i) (j);}
    01200                                         if (samples (i) (j) > ub (j)) {ub (j) = samples (i) (j);}
    01201                                 }
    01202                         }
    01203                 }
    01204 };
    01205
    01206
    01208
    01209 template<class sq_T>
    01210 void enorm<sq_T>::set_parameters (const vec &mu0, const sq_T &R0)
    01211 {
    01212 //Fixme test dimensions of mu0 and R0;
    01213         mu = mu0;
    01214         R = R0;
    01215         validate();
    01216 };
    01217
    01218 template<class sq_T>
    01219 void enorm<sq_T>::from_setting (const Setting &set)
    01220 {
    01221         epdf::from_setting (set); //reads rv
    01222
    01223         UI::get (mu, set, "mu", UI::compulsory);
    01224         mat Rtmp;// necessary for conversion
    01225         UI::get (Rtmp, set, "R", UI::compulsory);
    01226         R = Rtmp; // conversion
    01227         validate();
    01228 }
    01229
    01230 template<class sq_T>
    01231 void enorm<sq_T>::dupdate (mat &v, double nu)
    01232 {
    01233         //
    01234 };
    01235
    01236 // template<class sq_T>
    01237 // void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) {
    01238 //      //
    01239 // };
    01240
    01241 template<class sq_T>
    01242 vec enorm<sq_T>::sample() const
    01243 {
    01244         vec x (dim);
    01245 #pragma omp critical
    01246         NorRNG.sample_vector (dim, x);
    01247         vec smp = R.sqrt_mult (x);
    01248
    01249         smp += mu;
    01250         return smp;
    01251 };
    01252
    01253 // template<class sq_T>
    01254 // double enorm<sq_T>::eval ( const vec &val ) const {
    01255 //      double pdfl,e;
    01256 //      pdfl = evallog ( val );
    01257 //      e = exp ( pdfl );
    01258 //      return e;
    01259 // };
    01260
    01261 template<class sq_T>
    01262 double enorm<sq_T>::evallog_nn (const vec &val) const
    01263 {
    01264         // 1.83787706640935 = log(2pi)
    01265         double tmp = -0.5 * (R.invqform (mu - val));// - lognc();
    01266         return  tmp;
    01267 };
    01268
    01269 template<class sq_T>
    01270 inline double enorm<sq_T>::lognc () const
    01271 {
    01272         // 1.83787706640935 = log(2pi)
    01273         double tmp = 0.5 * (R.cols() * 1.83787706640935 + R.logdet());
    01274         return tmp;
    01275 };
    01276
    01277
    01278 // template<class sq_T>
    01279 // vec mlnorm<sq_T>::samplecond (const  vec &cond, double &lik ) {
    01280 //      this->condition ( cond );
    01281 //      vec smp = epdf.sample();
    01282 //      lik = epdf.eval ( smp );
    01283 //      return smp;
    01284 // }
    01285
    01286 // template<class sq_T>
    01287 // mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) {
    01288 //      int i;
    01289 //      int dim = rv.count();
    01290 //      mat Smp ( dim,n );
    01291 //      vec smp ( dim );
    01292 //      this->condition ( cond );
    01293 //
    01294 //      for ( i=0; i<n; i++ ) {
    01295 //              smp = epdf.sample();
    01296 //              lik ( i ) = epdf.eval ( smp );
    01297 //              Smp.set_col ( i ,smp );
    01298 //      }
    01299 //
    01300 //      return Smp;
    01301 // }
    01302
    01303
    01304 template<class sq_T>
    01305 shared_ptr<epdf> enorm<sq_T>::marginal ( const RV &rvn ) const
    01306 {
    01307         enorm<sq_T> *tmp = new enorm<sq_T> ();
    01308         shared_ptr<epdf> narrow(tmp);
    01309         marginal ( rvn, *tmp );
    01310         return narrow;
    01311 }
    01312
    01313 template<class sq_T>
    01314 void enorm<sq_T>::marginal ( const RV &rvn, enorm<sq_T> &target ) const
    01315 {
    01316         bdm_assert_debug (isnamed(), "rv description is not assigned");
    01317         ivec irvn = rvn.dataind (rv);
    01318
    01319         sq_T Rn (R, irvn);  // select rows and columns of R
    01320
    01321         target.set_rv ( rvn );
    01322         target.set_parameters (mu (irvn), Rn);
    01323 }
    01324
    01325 template<class sq_T>
    01326 shared_ptr<mpdf> enorm<sq_T>::condition ( const RV &rvn ) const
    01327 {
    01328         mlnorm<sq_T> *tmp = new mlnorm<sq_T> ();
    01329         shared_ptr<mpdf> narrow(tmp);
    01330         condition ( rvn, *tmp );
    01331         return narrow;
    01332 }
    01333
    01334 template<class sq_T>
    01335 void enorm<sq_T>::condition ( const RV &rvn, mpdf &target ) const
    01336 {
    01337         typedef mlnorm<sq_T> TMlnorm;
    01338
    01339         bdm_assert_debug (isnamed(), "rvs are not assigned");
    01340         TMlnorm &uptarget = dynamic_cast<TMlnorm &>(target);
    01341
    01342         RV rvc = rv.subt (rvn);
    01343         bdm_assert_debug ( (rvc._dsize() + rvn._dsize() == rv._dsize()), "wrong rvn");
    01344         //Permutation vector of the new R
    01345         ivec irvn = rvn.dataind (rv);
    01346         ivec irvc = rvc.dataind (rv);
    01347         ivec perm = concat (irvn , irvc);
    01348         sq_T Rn (R, perm);
    01349
    01350         //fixme - could this be done in general for all sq_T?
    01351         mat S = Rn.to_mat();
    01352         //fixme
    01353         int n = rvn._dsize() - 1;
    01354         int end = R.rows() - 1;
    01355         mat S11 = S.get (0, n, 0, n);
    01356         mat S12 = S.get (0, n , rvn._dsize(), end);
    01357         mat S22 = S.get (rvn._dsize(), end, rvn._dsize(), end);
    01358
    01359         vec mu1 = mu (irvn);
    01360         vec mu2 = mu (irvc);
    01361         mat A = S12 * inv (S22);
    01362         sq_T R_n (S11 - A *S12.T());
    01363
    01364         uptarget.set_rv (rvn);
    01365         uptarget.set_rvc (rvc);
    01366         uptarget.set_parameters (A, mu1 - A*mu2, R_n);
    01367 }
    01368
    01371 template<class sq_T>
    01372 void mgnorm<sq_T >::set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0) {
    01373         g = g0;
    01374         this->iepdf.set_parameters (zeros (g->dimension()), R0);
    01375 }
    01376
    01377 template<class sq_T>
    01378 void mgnorm<sq_T >::condition (const vec &cond) {this->iepdf._mu() = g->eval (cond);};
    01379
    01381 template<class sq_T>
    01382 std::ostream &operator<< (std::ostream &os,  mlnorm<sq_T> &ml)
    01383 {
    01384         os << "A:" << ml.A << endl;
    01385         os << "mu:" << ml.mu_const << endl;
    01386         os << "R:" << ml._R() << endl;
    01387         return os;
    01388 };
    01389
    01390 }
    01391 #endif //EF_H