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

Revision 887, 46.2 kB (checked in by smidl, 14 years ago)

new base for particle filtering

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