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

Revision 977, 44.2 kB (checked in by smidl, 14 years ago)

make egiw work for empty Gauss

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