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

Revision 739, 40.1 kB (checked in by mido, 14 years ago)

the rest of h to cpp movements, with exception of from_setting and validate to avoid conflicts with Sarka

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