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

Revision 802, 45.1 kB (checked in by smidl, 14 years ago)

new class estudent + tests

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