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

Revision 1020, 44.9 kB (checked in by smidl, 14 years ago)

DEBUG macro for conditional output + new class

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