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

Revision 612, 35.5 kB (checked in by smidl, 15 years ago)

register euni

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