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

Revision 878, 45.9 kB (checked in by sarka, 14 years ago)

dim ze set_parameters do validate

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