root/library/bdm/stat/exp_family.h @ 811

Revision 811, 45.4 kB (checked in by smidl, 14 years ago)

extensions and stuff for MPF

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