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

Revision 800, 42.6 kB (checked in by smidl, 14 years ago)

new ARX model (allowing non-linear transformation)

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