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

Revision 476, 34.8 kB (checked in by vbarta, 15 years ago)

removed duplicate call to lookupValue

  • 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
25//! Global Uniform_RNG
26        extern Uniform_RNG UniRNG;
27//! Global Normal_RNG
28        extern Normal_RNG NorRNG;
29//! Global Gamma_RNG
30        extern Gamma_RNG GamRNG;
31
32        /*!
33        * \brief General conjugate exponential family posterior density.
34
35        * More?...
36        */
37
38        class eEF : public epdf
39        {
40                public:
41//      eEF() :epdf() {};
42                        //! default constructor
43                        eEF ( ) :epdf ( ) {};
44                        //! logarithm of the normalizing constant, \f$\mathcal{I}\f$
45                        virtual double lognc() const =0;
46                        //!TODO decide if it is really needed
47                        virtual void dupdate ( mat &v ) {it_error ( "Not implemented" );};
48                        //!Evaluate normalized log-probability
49                        virtual double evallog_nn ( const vec &val ) const{it_error ( "Not implemented" );return 0.0;};
50                        //!Evaluate normalized log-probability
51                        virtual double evallog ( const vec &val ) const {
52                                double tmp;
53                                tmp= evallog_nn ( val )-lognc();
54//                              it_assert_debug ( std::isfinite ( tmp ),"Infinite value" );
55                                return tmp;}
56                        //!Evaluate normalized log-probability for many samples
57                        virtual vec evallog ( const mat &Val ) const
58                        {
59                                vec x ( Val.cols() );
60                                for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evallog_nn ( Val.get_col ( i ) ) ;}
61                                return x-lognc();
62                        }
63                        //!Power of the density, used e.g. to flatten the density
64                        virtual void pow ( double p ) {it_error ( "Not implemented" );};
65        };
66
67        /*!
68        * \brief Exponential family model.
69
70        * More?...
71        */
72
73        class mEF : public mpdf
74        {
75
76                public:
77                        //! Default constructor
78                        mEF ( ) :mpdf ( ) {};
79        };
80
81//! Estimator for Exponential family
82        class BMEF : public BM
83        {
84                protected:
85                        //! forgetting factor
86                        double frg;
87                        //! cached value of lognc() in the previous step (used in evaluation of \c ll )
88                        double last_lognc;
89                public:
90                        //! Default constructor (=empty constructor)
91                        BMEF ( double frg0=1.0 ) :BM ( ), frg ( frg0 ) {}
92                        //! Copy constructor
93                        BMEF ( const BMEF &B ) :BM ( B ), frg ( B.frg ), last_lognc ( B.last_lognc ) {}
94                        //!get statistics from another model
95                        virtual void set_statistics ( const BMEF* BM0 ) {it_error ( "Not implemented" );};
96                        //! Weighted update of sufficient statistics (Bayes rule)
97                        virtual void bayes ( const vec &data, const double w ) {};
98                        //original Bayes
99                        void bayes ( const vec &dt );
100                        //!Flatten the posterior according to the given BMEF (of the same type!)
101                        virtual void flatten ( const BMEF * B ) {it_error ( "Not implemented" );}
102                        //!Flatten the posterior as if to keep nu0 data
103//      virtual void flatten ( double nu0 ) {it_error ( "Not implemented" );}
104
105                        BMEF* _copy_ () const {it_error ( "function _copy_ not implemented for this BM" ); return NULL;};
106        };
107
108        template<class sq_T>
109        class mlnorm;
110
111        /*!
112        * \brief Gaussian density with positive definite (decomposed) covariance matrix.
113
114        * More?...
115        */
116        template<class sq_T>
117        class enorm : public eEF
118        {
119                protected:
120                        //! mean value
121                        vec mu;
122                        //! Covariance matrix in decomposed form
123                        sq_T R;
124                public:
125                        //!\name Constructors
126                        //!@{
127
128                        enorm ( ) :eEF ( ), mu ( ),R ( ) {};
129                        enorm ( const vec &mu,const sq_T &R ) {set_parameters ( mu,R );}
130                        void set_parameters ( const vec &mu,const sq_T &R );
131                        void from_setting(const Setting &root);
132                        void validate() {
133                                it_assert(mu.length()==R.rows(),"parameters mismatch");
134                                dim = mu.length();
135                        }
136                        //!@}
137
138                        //! \name Mathematical operations
139                        //!@{
140
141                        //! dupdate in exponential form (not really handy)
142                        void dupdate ( mat &v,double nu=1.0 );
143
144                        vec sample() const;
145
146                        double evallog_nn ( const vec &val ) const;
147                        double lognc () const;
148                        vec mean() const {return mu;}
149                        vec variance() const {return diag ( R.to_mat() );}
150//      mlnorm<sq_T>* condition ( const RV &rvn ) const ; <=========== fails to cmpile. Why?
151                        mpdf* condition ( const RV &rvn ) const ;
152                        enorm<sq_T>* marginal ( const RV &rv ) const;
153//                      epdf* marginal ( const RV &rv ) const;
154                        //!@}
155
156                        //! \name Access to attributes
157                        //!@{
158
159                        vec& _mu() {return mu;}
160                        void set_mu ( const vec mu0 ) { mu=mu0;}
161                        sq_T& _R() {return R;}
162                        const sq_T& _R() const {return R;}
163                        //!@}
164
165        };
166        UIREGISTER(enorm<chmat>);
167        UIREGISTER(enorm<ldmat>);
168        UIREGISTER(enorm<fsqmat>);
169
170
171        /*!
172        * \brief Gauss-inverse-Wishart density stored in LD form
173
174        * For \f$p\f$-variate densities, given rv.count() should be \f$p\times\f$ V.rows().
175        *
176        */
177        class egiw : public eEF
178        {
179                protected:
180                        //! Extended information matrix of sufficient statistics
181                        ldmat V;
182                        //! Number of data records (degrees of freedom) of sufficient statistics
183                        double nu;
184                        //! Dimension of the output
185                        int dimx;
186                        //! Dimension of the regressor
187                        int nPsi;
188                public:
189                        //!\name Constructors
190                        //!@{
191                        egiw() :eEF() {};
192                        egiw ( int dimx0, ldmat V0, double nu0=-1.0 ) :eEF() {set_parameters ( dimx0,V0, nu0 );};
193
194                        void set_parameters ( int dimx0, ldmat V0, double nu0=-1.0 )
195                        {
196                                dimx=dimx0;
197                                nPsi = V0.rows()-dimx;
198                                dim = dimx* ( dimx+nPsi ); // size(R) + size(Theta)
199
200                                V=V0;
201                                if ( nu0<0 )
202                                {
203                                        nu = 0.1 +nPsi +2*dimx +2; // +2 assures finite expected value of R
204                                        // terms before that are sufficient for finite normalization
205                                }
206                                else
207                                {
208                                        nu=nu0;
209                                }
210                        }
211                        //!@}
212
213                        vec sample() const;
214                        vec mean() const;
215                        vec variance() const;
216
217                        //! LS estimate of \f$\theta\f$
218                        vec est_theta() const;
219
220                        //! Covariance of the LS estimate
221                        ldmat est_theta_cov() const;
222
223                        void mean_mat ( mat &M, mat&R ) const;
224                        //! In this instance, val= [theta, r]. For multivariate instances, it is stored columnwise val = [theta_1 theta_2 ... r_1 r_2 ]
225                        double evallog_nn ( const vec &val ) const;
226                        double lognc () const;
227                        void pow ( double p ) {V*=p;nu*=p;};
228
229                        //! \name Access attributes
230                        //!@{
231
232                        ldmat& _V() {return V;}
233                        const ldmat& _V() const {return V;}
234                        double& _nu()  {return nu;}
235                        const double& _nu() const {return nu;}
236                        void from_setting(const Setting &set){
237                                UI::get(nu, set, "nu", UI::compulsory);
238                                UI::get(dimx, set, "dimx", UI::compulsory);
239                                mat V;
240                                UI::get(V,set,"V", UI::compulsory);
241                                set_parameters(dimx, V, nu);
242                                RV* rv=UI::build<RV>(set,"rv", UI::compulsory);
243                                set_rv(*rv);
244                                delete rv;
245                        }
246                        //!@}
247        };
248        UIREGISTER(egiw);
249
250        /*! \brief Dirichlet posterior density
251
252        Continuous Dirichlet density of \f$n\f$-dimensional variable \f$x\f$
253        \f[
254        f(x|\beta) = \frac{\Gamma[\gamma]}{\prod_{i=1}^{n}\Gamma(\beta_i)} \prod_{i=1}^{n}x_i^{\beta_i-1}
255        \f]
256        where \f$\gamma=\sum_i \beta_i\f$.
257        */
258        class eDirich: public eEF
259        {
260                protected:
261                        //!sufficient statistics
262                        vec beta;
263                public:
264                        //!\name Constructors
265                        //!@{
266
267                        eDirich () : eEF ( ) {};
268                        eDirich ( const eDirich &D0 ) : eEF () {set_parameters ( D0.beta );};
269                        eDirich ( const vec &beta0 ) {set_parameters ( beta0 );};
270                        void set_parameters ( const vec &beta0 )
271                        {
272                                beta= beta0;
273                                dim = beta.length();
274                        }
275                        //!@}
276
277                        vec sample() const {it_error ( "Not implemented" );return vec_1 ( 0.0 );};
278                        vec mean() const {return beta/sum(beta);};
279                        vec variance() const {double gamma =sum(beta); return elem_mult ( beta, ( beta+1 ) ) / ( gamma* ( gamma+1 ) );}
280                        //! In this instance, val is ...
281                        double evallog_nn ( const vec &val ) const
282                        {
283                                double tmp; tmp= ( beta-1 ) *log ( val );
284//                              it_assert_debug ( std::isfinite ( tmp ),"Infinite value" );
285                                return tmp;
286                        };
287                        double lognc () const
288                        {
289                                double tmp;
290                                double gam=sum ( beta );
291                                double lgb=0.0;
292                                for ( int i=0;i<beta.length();i++ ) {lgb+=lgamma ( beta ( i ) );}
293                                tmp= lgb-lgamma ( gam );
294//                              it_assert_debug ( std::isfinite ( tmp ),"Infinite value" );
295                                return tmp;
296                        };
297                        //!access function
298                        vec& _beta()  {return beta;}
299                        //!Set internal parameters
300        };
301
302//! \brief Estimator for Multinomial density
303        class multiBM : public BMEF
304        {
305                protected:
306                        //! Conjugate prior and posterior
307                        eDirich est;
308                        //! Pointer inside est to sufficient statistics
309                        vec &beta;
310                public:
311                        //!Default constructor
312                        multiBM ( ) : BMEF ( ),est ( ),beta ( est._beta() )
313                        {
314                                if ( beta.length() >0 ) {last_lognc=est.lognc();}
315                                else{last_lognc=0.0;}
316                        }
317                        //!Copy constructor
318                        multiBM ( const multiBM &B ) : BMEF ( B ),est ( B.est ),beta ( est._beta() ) {}
319                        //! Sets sufficient statistics to match that of givefrom mB0
320                        void set_statistics ( const BM* mB0 ) {const multiBM* mB=dynamic_cast<const multiBM*> ( mB0 ); beta=mB->beta;}
321                        void bayes ( const vec &dt )
322                        {
323                                if ( frg<1.0 ) {beta*=frg;last_lognc=est.lognc();}
324                                beta+=dt;
325                                if ( evalll ) {ll=est.lognc()-last_lognc;}
326                        }
327                        double logpred ( const vec &dt ) const
328                        {
329                                eDirich pred ( est );
330                                vec &beta = pred._beta();
331
332                                double lll;
333                                if ( frg<1.0 )
334                                        {beta*=frg;lll=pred.lognc();}
335                                else
336                                        if ( evalll ) {lll=last_lognc;}
337                                        else{lll=pred.lognc();}
338
339                                beta+=dt;
340                                return pred.lognc()-lll;
341                        }
342                        void flatten ( const BMEF* B )
343                        {
344                                const multiBM* E=dynamic_cast<const multiBM*> ( B );
345                                // sum(beta) should be equal to sum(B.beta)
346                                const vec &Eb=E->beta;//const_cast<multiBM*> ( E )->_beta();
347                                beta*= ( sum ( Eb ) /sum ( beta ) );
348                                if ( evalll ) {last_lognc=est.lognc();}
349                        }
350                        const epdf& posterior() const {return est;};
351                        const eDirich* _e() const {return &est;};
352                        void set_parameters ( const vec &beta0 )
353                        {
354                                est.set_parameters ( beta0 );
355                                if ( evalll ) {last_lognc=est.lognc();}
356                        }
357        };
358
359        /*!
360         \brief Gamma posterior density
361
362         Multivariate Gamma density as product of independent univariate densities.
363         \f[
364         f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i)
365         \f]
366        */
367
368        class egamma : public eEF
369        {
370                protected:
371                        //! Vector \f$\alpha\f$
372                        vec alpha;
373                        //! Vector \f$\beta\f$
374                        vec beta;
375                public :
376                        //! \name Constructors
377                        //!@{
378                        egamma ( ) :eEF ( ), alpha ( 0 ), beta ( 0 ) {};
379                        egamma ( const vec &a, const vec &b ) {set_parameters ( a, b );};
380                        void set_parameters ( const vec &a, const vec &b ) {alpha=a,beta=b;dim = alpha.length();};
381                        //!@}
382
383                        vec sample() const;
384                        //! TODO: is it used anywhere?
385//      mat sample ( int N ) const;
386                        double evallog ( const vec &val ) const;
387                        double lognc () const;
388                        //! Returns poiter to alpha and beta. Potentially dengerous: use with care!
389                        vec& _alpha() {return alpha;}
390                        vec& _beta() {return beta;}
391                        vec mean() const {return elem_div ( alpha,beta );}
392                        vec variance() const {return elem_div ( alpha,elem_mult ( beta,beta ) ); }
393                       
394                        //! Load from structure with elements:
395                        //!  \code
396                        //! { alpha = [...];         // vector of alpha
397                        //!   beta = [...];          // vector of beta
398                        //!   rv = {class="RV",...}  // description
399                        //! }
400                        //! \endcode
401                        //!@}
402                        void from_setting(const Setting &set){
403                                epdf::from_setting(set); // reads rv
404                                UI::get(alpha,set,"alpha", UI::compulsory);
405                                UI::get(beta,set,"beta", UI::compulsory);
406                                validate();
407                        }
408                        void validate(){
409                                it_assert(alpha.length() ==beta.length(), "parameters do not match");
410                                dim =alpha.length();
411                        }
412        };
413UIREGISTER(egamma);
414        /*!
415         \brief Inverse-Gamma posterior density
416
417         Multivariate inverse-Gamma density as product of independent univariate densities.
418         \f[
419         f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i)
420         \f]
421
422        Vector \f$\beta\f$ has different meaning (in fact it is 1/beta as used in definition of iG)
423
424         Inverse Gamma can be converted to Gamma using \f[
425         x\sim iG(a,b) => 1/x\sim G(a,1/b)
426        \f]
427        This relation is used in sampling.
428         */
429
430        class eigamma : public egamma
431        {
432                protected:
433                public :
434                        //! \name Constructors
435                        //! All constructors are inherited
436                        //!@{
437                        //!@}
438
439                        vec sample() const {return 1.0/egamma::sample();};
440                        //! Returns poiter to alpha and beta. Potentially dangerous: use with care!
441                        vec mean() const {return elem_div ( beta,alpha-1 );}
442                        vec variance() const {vec mea=mean(); return elem_div ( elem_mult ( mea,mea ),alpha-2 );}
443        };
444        /*
445        //! Weighted mixture of epdfs with external owned components.
446        class emix : public epdf {
447        protected:
448                int n;
449                vec &w;
450                Array<epdf*> Coms;
451        public:
452        //! Default constructor
453                emix ( const RV &rv, vec &w0): epdf(rv), n(w0.length()), w(w0), Coms(n) {};
454                void set_parameters( int &i, double wi, epdf* ep){w(i)=wi;Coms(i)=ep;}
455                vec mean(){vec pom; for(int i=0;i<n;i++){pom+=Coms(i)->mean()*w(i);} return pom;};
456                vec sample() {it_error ( "Not implemented" );return 0;}
457        };
458        */
459
460//!  Uniform distributed density on a rectangular support
461
462        class euni: public epdf
463        {
464                protected:
465//! lower bound on support
466                        vec low;
467//! upper bound on support
468                        vec high;
469//! internal
470                        vec distance;
471//! normalizing coefficients
472                        double nk;
473//! cache of log( \c nk )
474                        double lnk;
475                public:
476                        //! \name Constructors
477                        //!@{
478                        euni ( ) :epdf ( ) {}
479                        euni ( const vec &low0, const vec &high0 ) {set_parameters ( low0,high0 );}
480                        void set_parameters ( const vec &low0, const vec &high0 )
481                        {
482                                distance = high0-low0;
483                                it_assert_debug ( min ( distance ) >0.0,"bad support" );
484                                low = low0;
485                                high = high0;
486                                nk = prod ( 1.0/distance );
487                                lnk = log ( nk );
488                                dim = low.length();
489                        }
490                        //!@}
491
492                        double eval ( const vec &val ) const  {return nk;}
493                        double evallog ( const vec &val ) const  {
494                                if (any(val<low) && any(val>high)) {return inf;}
495                                else return lnk;
496                        }
497                        vec sample() const
498                        {
499                                vec smp ( dim );
500#pragma omp critical
501                                UniRNG.sample_vector ( dim ,smp );
502                                return low+elem_mult ( distance,smp );
503                        }
504                        //! set values of \c low and \c high
505                        vec mean() const {return ( high-low ) /2.0;}
506                        vec variance() const {return ( pow ( high,2 ) +pow ( low,2 ) +elem_mult ( high,low ) ) /3.0;}
507                        //! Load from structure with elements:
508                        //!  \code
509                        //! { high = [...];          // vector of upper bounds
510                        //!   low = [...];           // vector of lower bounds
511                        //!   rv = {class="RV",...}  // description of RV
512                        //! }
513                        //! \endcode
514                        //!@}
515                        void from_setting(const Setting &set){
516                                epdf::from_setting(set); // reads rv and rvc
517
518                                UI::get(high,set,"high", UI::compulsory);
519                                UI::get(low,set,"low", UI::compulsory);
520                        }
521        };
522
523
524        /*!
525         \brief Normal distributed linear function with linear function of mean value;
526
527         Mean value \f$mu=A*rvc+mu_0\f$.
528        */
529        template<class sq_T>
530        class mlnorm : public mEF
531        {
532                protected:
533                        //! Internal epdf that arise by conditioning on \c rvc
534                        shared_ptr<enorm<sq_T> > iepdf;
535                        mat A;
536                        vec mu_const;
537                        vec& _mu; //cached epdf.mu;
538                public:
539                        //! \name Constructors
540                        //!@{
541                        mlnorm():iepdf(new enorm<sq_T>()), _mu(iepdf->_mu()) { set_ep(iepdf); };
542                        mlnorm (const mat &A, const vec &mu0, const sq_T &R ) :iepdf(new enorm<sq_T>()), _mu(iepdf->_mu())
543                        {
544                                set_ep(iepdf); set_parameters ( A,mu0,R );
545                        }
546
547                        //! Set \c A and \c R
548                        void set_parameters ( const  mat &A, const vec &mu0, const sq_T &R );
549                        //!@}
550                        //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it.
551                        void condition ( const vec &cond );
552
553                        //!access function
554                        vec& _mu_const() {return mu_const;}
555                        //!access function
556                        mat& _A() {return A;}
557                        //!access function
558                        mat _R() { return iepdf->_R().to_mat(); }
559
560                        template<class sq_M>
561                        friend std::ostream &operator<< ( std::ostream &os,  mlnorm<sq_M> &ml );
562                       
563                        void from_setting(const Setting &set){
564                                mpdf::from_setting(set);       
565
566                                UI::get(A,set,"A", UI::compulsory);
567                                UI::get(mu_const,set,"const", UI::compulsory);
568                                mat R0;
569                                UI::get(R0,set,"R", UI::compulsory);
570                                set_parameters(A,mu_const,R0);
571                        };
572        };
573        UIREGISTER(mlnorm<ldmat>);
574        UIREGISTER(mlnorm<fsqmat>);
575        UIREGISTER(mlnorm<chmat>);
576
577//! Mpdf with general function for mean value
578        template<class sq_T>
579        class mgnorm : public mEF
580        {
581                protected:
582                        //! Internal epdf that arise by conditioning on \c rvc
583                        shared_ptr<enorm<sq_T> > iepdf;
584                        vec &mu;
585                        fnc* g;
586                public:
587                        //!default constructor
588                        mgnorm():iepdf(new enorm<sq_T>()), mu(iepdf->_mu()) { set_ep(iepdf); }
589                        //!set mean function
590                        void set_parameters ( fnc* g0, const sq_T &R0 ) {g=g0; iepdf->set_parameters ( zeros ( g->dimension() ), R0 );}
591                        void condition ( const vec &cond ) {mu=g->eval ( cond );};
592
593
594                        /*! UI for mgnorm
595
596                        The mgnorm is constructed from a structure with fields:
597                        \code
598                        system = {
599                                type = "mgnorm";
600                                // function for mean value evolution
601                                g = {type="fnc"; ... }
602
603                                // variance
604                                R = [1, 0,
605                                         0, 1];
606                                // --OR --
607                                dR = [1, 1];
608
609                                // == OPTIONAL ==
610
611                                // description of y variables
612                                y = {type="rv"; names=["y", "u"];};
613                                // description of u variable
614                                u = {type="rv"; names=[];}
615                        };
616                        \endcode
617
618                        Result if
619                        */
620
621                        void from_setting( const Setting &set ) 
622                        {       
623                                fnc* g = UI::build<fnc>( set, "g", UI::compulsory );
624
625                                mat R;                                 
626                                vec dR;                                 
627                                if ( UI::get( dR, set, "dR" ) )
628                                        R=diag(dR);
629                                else 
630                                        UI::get( R, set, "R", UI::compulsory); 
631               
632                                set_parameters(g,R);
633                        }
634        };
635
636        UIREGISTER(mgnorm<chmat>);
637
638
639        /*! (Approximate) Student t density with linear function of mean value
640
641        The internal epdf of this class is of the type of a Gaussian (enorm).
642        However, each conditioning is trying to assure the best possible approximation by taking into account the zeta function. See [] for reference.
643
644        Perhaps a moment-matching technique?
645        */
646        class mlstudent : public mlnorm<ldmat>
647        {
648                protected:
649                        ldmat Lambda;
650                        ldmat &_R;
651                        ldmat Re;
652                public:
653                        mlstudent ( ) :mlnorm<ldmat> (),
654                                        Lambda (),      _R ( iepdf->_R() ) {}
655                        void set_parameters ( const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0 )
656                        {
657                                it_assert_debug ( A0.rows() ==mu0.length(),"" );
658                                it_assert_debug ( R0.rows() ==A0.rows(),"" );
659
660                                iepdf->set_parameters ( mu0,Lambda ); //
661                                A = A0;
662                                mu_const = mu0;
663                                Re=R0;
664                                Lambda = Lambda0;
665                        }
666                        void condition ( const vec &cond )
667                        {
668                                _mu = A*cond + mu_const;
669                                double zeta;
670                                //ugly hack!
671                                if ( ( cond.length() +1 ) ==Lambda.rows() )
672                                {
673                                        zeta = Lambda.invqform ( concat ( cond, vec_1 ( 1.0 ) ) );
674                                }
675                                else
676                                {
677                                        zeta = Lambda.invqform ( cond );
678                                }
679                                _R = Re;
680                                _R*= ( 1+zeta );// / ( nu ); << nu is in Re!!!!!!
681                        };
682
683        };
684        /*!
685        \brief  Gamma random walk
686
687        Mean value, \f$\mu\f$, of this density is given by \c rvc .
688        Standard deviation of the random walk is proportional to one \f$k\f$-th the mean.
689        This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$.
690
691        The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$.
692        */
693        class mgamma : public mEF
694        {
695                protected:
696                        //! Internal epdf that arise by conditioning on \c rvc
697                        shared_ptr<egamma> iepdf;
698
699                        //! Constant \f$k\f$
700                        double k;
701
702                        //! cache of iepdf.beta
703                        vec &_beta;
704
705                public:
706                        //! Constructor
707                        mgamma():iepdf(new egamma()), k(0),
708                            _beta(iepdf->_beta()) {
709                            set_ep(iepdf);
710                        }
711
712                        //! Set value of \c k
713                        void set_parameters(double k, const vec &beta0);
714
715                        void condition ( const vec &val ) {_beta=k/val;};
716                        //! Load from structure with elements:
717                        //!  \code
718                        //! { alpha = [...];         // vector of alpha
719                        //!   k = 1.1;               // multiplicative constant k
720                        //!   rv = {class="RV",...}  // description of RV
721                        //!   rvc = {class="RV",...} // description of RV in condition
722                        //! }
723                        //! \endcode
724                        //!@}
725                        void from_setting(const Setting &set){
726                                mpdf::from_setting(set); // reads rv and rvc
727                                vec betatmp; // ugly but necessary
728                                UI::get(betatmp,set,"beta", UI::compulsory);
729                                UI::get(k,set,"k", UI::compulsory);
730                                set_parameters(k,betatmp);
731                        }
732        };
733        UIREGISTER(mgamma);
734       
735        /*!
736        \brief  Inverse-Gamma random walk
737
738        Mean value, \f$ \mu \f$, of this density is given by \c rvc .
739        Standard deviation of the random walk is proportional to one \f$ k \f$-th the mean.
740        This is achieved by setting \f$ \alpha=\mu/k^2+2 \f$ and \f$ \beta=\mu(\alpha-1)\f$.
741
742        The standard deviation of the walk is then: \f$ \mu/\sqrt(k)\f$.
743         */
744        class migamma : public mEF
745        {
746                protected:
747                        //! Internal epdf that arise by conditioning on \c rvc
748                        shared_ptr<eigamma> iepdf;
749
750                        //! Constant \f$k\f$
751                        double k;
752
753                        //! cache of iepdf.alpha
754                        vec &_alpha;
755
756                        //! cache of iepdf.beta
757                        vec &_beta;
758
759                public:
760                        //! \name Constructors
761                        //!@{
762                        migamma():iepdf(new eigamma()),
763                            k(0),
764                            _alpha(iepdf->_alpha()),
765                            _beta(iepdf->_beta()) {
766                            set_ep(iepdf);
767                        }
768
769                        migamma(const migamma &m):iepdf(m.iepdf),
770                            k(0),
771                            _alpha(iepdf->_alpha()),
772                            _beta(iepdf->_beta()) {
773                            set_ep(iepdf);
774                        }
775                        //!@}
776
777                        //! Set value of \c k
778                        void set_parameters ( int len, double k0 )
779                        {
780                                k=k0;
781                                iepdf->set_parameters ( ( 1.0/ ( k*k ) +2.0 ) *ones ( len ) /*alpha*/, ones ( len ) /*beta*/ );
782                                dimc = dimension();
783                        };
784                        void condition ( const vec &val )
785                        {
786                                _beta=elem_mult ( val, ( _alpha-1.0 ) );
787                        };
788        };
789
790
791        /*!
792        \brief  Gamma random walk around a fixed point
793
794        Mean 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
795        \f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f]
796
797        Standard deviation of the random walk is proportional to one \f$k\f$-th the mean.
798        This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$.
799
800        The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$.
801        */
802        class mgamma_fix : public mgamma
803        {
804                protected:
805                        //! parameter l
806                        double l;
807                        //! reference vector
808                        vec refl;
809                public:
810                        //! Constructor
811                        mgamma_fix ( ) : mgamma ( ),refl () {};
812                        //! Set value of \c k
813                        void set_parameters ( double k0 , vec ref0, double l0 )
814                        {
815                                mgamma::set_parameters ( k0, ref0 );
816                                refl=pow ( ref0,1.0-l0 );l=l0;
817                                dimc=dimension();
818                        };
819
820                        void condition ( const vec &val ) {vec mean=elem_mult ( refl,pow ( val,l ) ); _beta=k/mean;};
821        };
822
823
824        /*!
825        \brief  Inverse-Gamma random walk around a fixed point
826
827        Mean 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
828        \f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f]
829
830        ==== Check == vv =
831        Standard deviation of the random walk is proportional to one \f$k\f$-th the mean.
832        This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$.
833
834        The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$.
835         */
836        class migamma_ref : public migamma
837        {
838                protected:
839                        //! parameter l
840                        double l;
841                        //! reference vector
842                        vec refl;
843                public:
844                        //! Constructor
845                        migamma_ref ( ) : migamma (),refl ( ) {};
846                        //! Set value of \c k
847                        void set_parameters ( double k0 , vec ref0, double l0 )
848                        {
849                                migamma::set_parameters ( ref0.length(), k0 );
850                                refl=pow ( ref0,1.0-l0 );
851                                l=l0;
852                                dimc = dimension();
853                        };
854
855                        void condition ( const vec &val )
856                        {
857                                vec mean=elem_mult ( refl,pow ( val,l ) );
858                                migamma::condition ( mean );
859                        };
860
861                        /*! UI for migamma_ref
862
863                        The migamma_ref is constructed from a structure with fields:
864                        \code
865                        system = {
866                                type = "migamma_ref";
867                                ref = [1e-5; 1e-5; 1e-2 1e-3];            // reference vector
868                                l = 0.999;                                // constant l
869                                k = 0.1;                                  // constant k
870                               
871                                // == OPTIONAL ==
872                                // description of y variables
873                                y = {type="rv"; names=["y", "u"];};
874                                // description of u variable
875                                u = {type="rv"; names=[];}
876                        };
877                        \endcode
878
879                        Result if
880                         */
881                        void from_setting( const Setting &set );
882
883                        // TODO dodelat void to_setting( Setting &set ) const;
884        };
885
886
887        UIREGISTER(migamma_ref);
888
889        /*! Log-Normal probability density
890         only allow diagonal covariances!
891
892        Density of the form \f$ \log(x)\sim \mathcal{N}(\mu,\sigma^2) \f$ , i.e.
893        \f[
894        x \sim \frac{1}{x\sigma\sqrt{2\pi}}\exp{-\frac{1}{2\sigma^2}(\log(x)-\mu)}
895        \f]
896
897        */
898        class elognorm: public enorm<ldmat>
899        {
900                public:
901                        vec sample() const {return exp ( enorm<ldmat>::sample() );};
902                        vec mean() const {vec var=enorm<ldmat>::variance();return exp ( mu - 0.5*var );};
903
904        };
905
906        /*!
907        \brief  Log-Normal random walk
908
909        Mean value, \f$\mu\f$, is...
910
911        ==== Check == vv =
912        Standard deviation of the random walk is proportional to one \f$k\f$-th the mean.
913        This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$.
914
915        The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$.
916         */
917        class mlognorm : public mpdf
918        {
919                protected:
920                        shared_ptr<elognorm> eno;
921
922                        //! parameter 1/2*sigma^2
923                        double sig2;
924
925                        //! access
926                        vec &mu;
927                public:
928                        //! Constructor
929                        mlognorm():eno(new elognorm()),
930                            sig2(0),
931                            mu(eno->_mu()) {
932                            set_ep(eno);
933                        }
934
935                        //! Set value of \c k
936                        void set_parameters ( int size, double k )
937                        {
938                                sig2 = 0.5*log ( k*k+1 );
939                                eno->set_parameters ( zeros ( size ),2*sig2*eye ( size ) );
940
941                                dimc = size;
942                        };
943
944                        void condition ( const vec &val )
945                        {
946                                mu=log ( val )-sig2;//elem_mult ( refl,pow ( val,l ) );
947                        };
948
949                        /*! UI for mlognorm
950
951                        The mlognorm is constructed from a structure with fields:
952                        \code
953                        system = {
954                                type = "mlognorm";
955                                k = 0.1;                                  // constant k
956                                mu0 = [1., 1.];
957                               
958                                // == OPTIONAL ==
959                                // description of y variables
960                                y = {type="rv"; names=["y", "u"];};
961                                // description of u variable
962                                u = {type="rv"; names=[];}
963                        };
964                        \endcode
965
966                         */
967                        void from_setting( const Setting &set );
968
969                        // TODO dodelat void to_setting( Setting &set ) const;
970
971        };
972       
973        UIREGISTER(mlognorm);
974
975        /*! inverse Wishart density defined on Choleski decomposition
976
977        */
978        class eWishartCh : public epdf
979        {
980                protected:
981                        //! Upper-Triagle of Choleski decomposition of \f$ \Psi \f$
982                        chmat Y;
983                        //! dimension of matrix  \f$ \Psi \f$
984                        int p;
985                        //! degrees of freedom  \f$ \nu \f$
986                        double delta;
987                public:
988                        void set_parameters ( const mat &Y0, const double delta0 ) {Y=chmat ( Y0 );delta=delta0; p=Y.rows(); dim = p*p; }
989                        mat sample_mat() const
990                        {
991                                mat X=zeros ( p,p );
992
993                                //sample diagonal
994                                for ( int i=0;i<p;i++ )
995                                {
996                                        GamRNG.setup ( 0.5* ( delta-i ) , 0.5 ); // no +1 !! index if from 0
997#pragma omp critical
998                                        X ( i,i ) =sqrt ( GamRNG() );
999                                }
1000                                //do the rest
1001                                for ( int i=0;i<p;i++ )
1002                                {
1003                                        for ( int j=i+1;j<p;j++ )
1004                                        {
1005#pragma omp critical
1006                                                X ( i,j ) =NorRNG.sample();
1007                                        }
1008                                }
1009                                return X*Y._Ch();// return upper triangular part of the decomposition
1010                        }
1011                        vec sample () const
1012                        {
1013                                return vec ( sample_mat()._data(),p*p );
1014                        }
1015                        //! fast access function y0 will be copied into Y.Ch.
1016                        void setY ( const mat &Ch0 ) {copy_vector ( dim,Ch0._data(), Y._Ch()._data() );}
1017                        //! fast access function y0 will be copied into Y.Ch.
1018                        void _setY ( const vec &ch0 ) {copy_vector ( dim, ch0._data(), Y._Ch()._data() ); }
1019                        //! access function
1020                        const chmat& getY()const {return Y;}
1021        };
1022
1023        class eiWishartCh: public epdf
1024        {
1025                protected:
1026                        eWishartCh W;
1027                        int p;
1028                        double delta;
1029                public:
1030                        void set_parameters ( const mat &Y0, const double delta0) {
1031                                delta = delta0;
1032                                W.set_parameters ( inv ( Y0 ),delta0 ); 
1033                                dim = W.dimension(); p=Y0.rows();
1034                        }
1035                        vec sample() const {mat iCh; iCh=inv ( W.sample_mat() ); return vec ( iCh._data(),dim );}
1036                        void _setY ( const vec &y0 )
1037                        {
1038                                mat Ch ( p,p );
1039                                mat iCh ( p,p );
1040                                copy_vector ( dim, y0._data(), Ch._data() );
1041                               
1042                                iCh=inv ( Ch );
1043                                W.setY ( iCh );
1044                        }
1045                        virtual double evallog ( const vec &val ) const {
1046                                chmat X(p);
1047                                const chmat& Y=W.getY();
1048                                 
1049                                copy_vector(p*p,val._data(),X._Ch()._data());
1050                                chmat iX(p);X.inv(iX);
1051                                // compute 
1052//                              \frac{ |\Psi|^{m/2}|X|^{-(m+p+1)/2}e^{-tr(\Psi X^{-1})/2} }{ 2^{mp/2}\Gamma_p(m/2)},
1053                                mat M=Y.to_mat()*iX.to_mat();
1054                               
1055                                double log1 = 0.5*p*(2*Y.logdet())-0.5*(delta+p+1)*(2*X.logdet())-0.5*trace(M); 
1056                                //Fixme! Multivariate gamma omitted!! it is ok for sampling, but not otherwise!!
1057                               
1058/*                              if (0) {
1059                                        mat XX=X.to_mat();
1060                                        mat YY=Y.to_mat();
1061                                       
1062                                        double log2 = 0.5*p*log(det(YY))-0.5*(delta+p+1)*log(det(XX))-0.5*trace(YY*inv(XX));
1063                                        cout << log1 << "," << log2 << endl;
1064                                }*/
1065                                return log1;                           
1066                        };
1067                       
1068        };
1069
1070        class rwiWishartCh : public mpdf
1071        {
1072                protected:
1073                        shared_ptr<eiWishartCh> eiW;
1074                        //!square root of \f$ \nu-p-1 \f$ - needed for computation of \f$ \Psi \f$ from conditions
1075                        double sqd;
1076                        //reference point for diagonal
1077                        vec refl;
1078                        double l;
1079                        int p;
1080
1081                public:
1082                        rwiWishartCh():eiW(new eiWishartCh()),
1083                            sqd(0), l(0), p(0) {
1084                            set_ep(eiW);
1085                        }
1086
1087                        void set_parameters ( int p0, double k, vec ref0, double l0  )
1088                        {
1089                                p=p0;
1090                                double delta = 2/(k*k)+p+3;
1091                                sqd=sqrt ( delta-p-1 );
1092                                l=l0;
1093                                refl=pow(ref0,1-l);
1094                               
1095                                eiW->set_parameters ( eye ( p ),delta );
1096                                dimc=eiW->dimension();
1097                        }
1098                        void condition ( const vec &c ) {
1099                                vec z=c;
1100                                int ri=0;
1101                                for(int i=0;i<p*p;i+=(p+1)){//trace diagonal element
1102                                        z(i) = pow(z(i),l)*refl(ri);
1103                                        ri++;
1104                                }
1105
1106                                eiW->_setY ( sqd*z );
1107                        }
1108        };
1109
1110//! Switch between various resampling methods.
1111        enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 };
1112        /*!
1113        \brief Weighted empirical density
1114
1115        Used e.g. in particle filters.
1116        */
1117        class eEmp: public epdf
1118        {
1119                protected :
1120                        //! Number of particles
1121                        int n;
1122                        //! Sample weights \f$w\f$
1123                        vec w;
1124                        //! Samples \f$x^{(i)}, i=1..n\f$
1125                        Array<vec> samples;
1126                public:
1127                        //! \name Constructors
1128                        //!@{
1129                        eEmp ( ) :epdf ( ),w ( ),samples ( ) {};
1130                        //! copy constructor
1131                        eEmp ( const eEmp &e ) : epdf ( e ), w ( e.w ), samples ( e.samples ) {};
1132                        //!@}
1133
1134                        //! Set samples and weights
1135                        void set_statistics ( const vec &w0, const epdf* pdf0 );
1136                        //! Set samples and weights
1137                        void set_statistics ( const epdf* pdf0 , int n ) {set_statistics ( ones ( n ) /n,pdf0 );};
1138                        //! Set sample
1139                        void set_samples ( const epdf* pdf0 );
1140                        //! Set sample
1141                        void set_parameters ( int n0, bool copy=true ) {n=n0; w.set_size ( n0,copy );samples.set_size ( n0,copy );};
1142                        //! Potentially dangerous, use with care.
1143                        vec& _w()  {return w;};
1144                        //! Potentially dangerous, use with care.
1145                        const vec& _w() const {return w;};
1146                        //! access function
1147                        Array<vec>& _samples() {return samples;};
1148                        //! access function
1149                        const Array<vec>& _samples() const {return samples;};
1150                        //! 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.
1151                        ivec resample ( RESAMPLING_METHOD method=SYSTEMATIC );
1152                        //! inherited operation : NOT implemneted
1153                        vec sample() const {it_error ( "Not implemented" );return 0;}
1154                        //! inherited operation : NOT implemneted
1155                        double evallog ( const vec &val ) const {it_error ( "Not implemented" );return 0.0;}
1156                        vec mean() const
1157                        {
1158                                vec pom=zeros ( dim );
1159                                for ( int i=0;i<n;i++ ) {pom+=samples ( i ) *w ( i );}
1160                                return pom;
1161                        }
1162                        vec variance() const
1163                        {
1164                                vec pom=zeros ( dim );
1165                                for ( int i=0;i<n;i++ ) {pom+=pow ( samples ( i ),2 ) *w ( i );}
1166                                return pom-pow ( mean(),2 );
1167                        }
1168                        //! For this class, qbounds are minimum and maximum value of the population!
1169                        void qbounds ( vec &lb, vec &ub, double perc=0.95 ) const
1170                        {
1171                                // lb in inf so than it will be pushed below;
1172                                lb.set_size ( dim );
1173                                ub.set_size ( dim );
1174                                lb = std::numeric_limits<double>::infinity();
1175                                ub = -std::numeric_limits<double>::infinity();
1176                                int j;
1177                                for ( int i=0;i<n;i++ )
1178                                {
1179                                        for ( j=0;j<dim; j++ )
1180                                        {
1181                                                if ( samples ( i ) ( j ) <lb ( j ) ) {lb ( j ) =samples ( i ) ( j );}
1182                                                if ( samples ( i ) ( j ) >ub ( j ) ) {ub ( j ) =samples ( i ) ( j );}
1183                                        }
1184                                }
1185                        }
1186        };
1187
1188
1189////////////////////////
1190
1191        template<class sq_T>
1192        void enorm<sq_T>::set_parameters ( const vec &mu0, const sq_T &R0 )
1193        {
1194//Fixme test dimensions of mu0 and R0;
1195                mu = mu0;
1196                R = R0;
1197                validate();
1198        };
1199
1200        template<class sq_T>
1201        void enorm<sq_T>::from_setting(const Setting &set){
1202                epdf::from_setting(set); //reads rv
1203               
1204                UI::get(mu,set,"mu", UI::compulsory);
1205                mat Rtmp;// necessary for conversion
1206                UI::get(Rtmp,set,"R", UI::compulsory);
1207                R=Rtmp; // conversion
1208                validate();
1209        }
1210
1211        template<class sq_T>
1212        void enorm<sq_T>::dupdate ( mat &v, double nu )
1213        {
1214                //
1215        };
1216
1217// template<class sq_T>
1218// void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) {
1219//      //
1220// };
1221
1222        template<class sq_T>
1223        vec enorm<sq_T>::sample() const
1224        {
1225                vec x ( dim );
1226#pragma omp critical
1227                NorRNG.sample_vector ( dim,x );
1228                vec smp = R.sqrt_mult ( x );
1229
1230                smp += mu;
1231                return smp;
1232        };
1233
1234// template<class sq_T>
1235// double enorm<sq_T>::eval ( const vec &val ) const {
1236//      double pdfl,e;
1237//      pdfl = evallog ( val );
1238//      e = exp ( pdfl );
1239//      return e;
1240// };
1241
1242        template<class sq_T>
1243        double enorm<sq_T>::evallog_nn ( const vec &val ) const
1244        {
1245                // 1.83787706640935 = log(2pi)
1246                double tmp=-0.5* ( R.invqform ( mu-val ) );// - lognc();
1247                return  tmp;
1248        };
1249
1250        template<class sq_T>
1251        inline double enorm<sq_T>::lognc () const
1252        {
1253                // 1.83787706640935 = log(2pi)
1254                double tmp=0.5* ( R.cols() * 1.83787706640935 +R.logdet() );
1255                return tmp;
1256        };
1257
1258        template<class sq_T>
1259        void mlnorm<sq_T>::set_parameters ( const mat &A0, const vec &mu0, const sq_T &R0 )
1260        {
1261                it_assert_debug ( A0.rows() ==mu0.length(),"" );
1262                it_assert_debug ( A0.rows() ==R0.rows(),"" );
1263
1264                iepdf->set_parameters(zeros(A0.rows()), R0);
1265                A = A0;
1266                mu_const = mu0;
1267                dimc = A0.cols();
1268        }
1269
1270// template<class sq_T>
1271// vec mlnorm<sq_T>::samplecond (const  vec &cond, double &lik ) {
1272//      this->condition ( cond );
1273//      vec smp = epdf.sample();
1274//      lik = epdf.eval ( smp );
1275//      return smp;
1276// }
1277
1278// template<class sq_T>
1279// mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) {
1280//      int i;
1281//      int dim = rv.count();
1282//      mat Smp ( dim,n );
1283//      vec smp ( dim );
1284//      this->condition ( cond );
1285//
1286//      for ( i=0; i<n; i++ ) {
1287//              smp = epdf.sample();
1288//              lik ( i ) = epdf.eval ( smp );
1289//              Smp.set_col ( i ,smp );
1290//      }
1291//
1292//      return Smp;
1293// }
1294
1295        template<class sq_T>
1296        void mlnorm<sq_T>::condition ( const vec &cond )
1297        {
1298                _mu = A*cond + mu_const;
1299//R is already assigned;
1300        }
1301
1302        template<class sq_T>
1303        enorm<sq_T>* enorm<sq_T>::marginal ( const RV &rvn ) const
1304        {
1305                it_assert_debug ( isnamed(), "rv description is not assigned" );
1306                ivec irvn = rvn.dataind ( rv );
1307
1308                sq_T Rn ( R,irvn ); //select rows and columns of R
1309
1310                enorm<sq_T>* tmp = new enorm<sq_T>;
1311                tmp->set_rv ( rvn );
1312                tmp->set_parameters ( mu ( irvn ), Rn );
1313                return tmp;
1314        }
1315
1316        template<class sq_T>
1317        mpdf* enorm<sq_T>::condition ( const RV &rvn ) const
1318        {
1319
1320                it_assert_debug ( isnamed(),"rvs are not assigned" );
1321
1322                RV rvc = rv.subt ( rvn );
1323                it_assert_debug ( ( rvc._dsize() +rvn._dsize() ==rv._dsize() ),"wrong rvn" );
1324                //Permutation vector of the new R
1325                ivec irvn = rvn.dataind ( rv );
1326                ivec irvc = rvc.dataind ( rv );
1327                ivec perm=concat ( irvn , irvc );
1328                sq_T Rn ( R,perm );
1329
1330                //fixme - could this be done in general for all sq_T?
1331                mat S=Rn.to_mat();
1332                //fixme
1333                int n=rvn._dsize()-1;
1334                int end=R.rows()-1;
1335                mat S11 = S.get ( 0,n, 0, n );
1336                mat S12 = S.get ( 0, n , rvn._dsize(), end );
1337                mat S22 = S.get ( rvn._dsize(), end, rvn._dsize(), end );
1338
1339                vec mu1 = mu ( irvn );
1340                vec mu2 = mu ( irvc );
1341                mat A=S12*inv ( S22 );
1342                sq_T R_n ( S11 - A *S12.T() );
1343
1344                mlnorm<sq_T>* tmp=new mlnorm<sq_T> ( );
1345                tmp->set_rv ( rvn ); tmp->set_rvc ( rvc );
1346                tmp->set_parameters ( A,mu1-A*mu2,R_n );
1347                return tmp;
1348        }
1349
1350///////////
1351
1352        template<class sq_T>
1353        std::ostream &operator<< ( std::ostream &os,  mlnorm<sq_T> &ml )
1354        {
1355                os << "A:"<< ml.A<<endl;
1356                os << "mu:"<< ml.mu_const<<endl;
1357                os << "R:" << ml.iepdf->_R().to_mat() <<endl;
1358                return os;
1359        };
1360
1361}
1362#endif //EF_H
Note: See TracBrowser for help on using the browser.