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

Revision 948, 46.7 kB (checked in by smidl, 14 years ago)

doc

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