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

Revision 944, 46.4 kB (checked in by smidl, 14 years ago)

Doc + new examples

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