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

Revision 583, 35.2 kB (checked in by smidl, 15 years ago)

redesign of Kalman to use BM, new object StateSpace? created

  • 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                        bdm_assert_debug (min (distance) > 0.0, "bad support");
493                        low = low0;
494                        high = high0;
495                        nk = prod (1.0 / distance);
496                        lnk = log (nk);
497                        dim = low.length();
498                }
499                //!@}
500
501                double evallog (const vec &val) const  {
502                        if (any (val < low) && any (val > high)) {return inf;}
503                        else return lnk;
504                }
505                vec sample() const {
506                        vec smp (dim);
507#pragma omp critical
508                        UniRNG.sample_vector (dim , smp);
509                        return low + elem_mult (distance, smp);
510                }
511                //! set values of \c low and \c high
512                vec mean() const {return (high -low) / 2.0;}
513                vec variance() const {return (pow (high, 2) + pow (low, 2) + elem_mult (high, low)) / 3.0;}
514                //! Load from structure with elements:
515                //!  \code
516                //! { high = [...];          // vector of upper bounds
517                //!   low = [...];           // vector of lower bounds
518                //!   rv = {class="RV",...}  // description of RV
519                //! }
520                //! \endcode
521                //!@}
522                void from_setting (const Setting &set) {
523                        epdf::from_setting (set); // reads rv and rvc
524
525                        UI::get (high, set, "high", UI::compulsory);
526                        UI::get (low, set, "low", UI::compulsory);
527                }
528};
529
530
531/*!
532 \brief Normal distributed linear function with linear function of mean value;
533
534 Mean value \f$ \mu=A*\mbox{rvc}+\mu_0 \f$.
535*/
536template < class sq_T, template <typename> class TEpdf = enorm >
537class mlnorm : public mpdf_internal< TEpdf<sq_T> >
538{
539        protected:
540                //! Internal epdf that arise by conditioning on \c rvc
541                mat A;
542                //! Constant additive term
543                vec mu_const;
544//                      vec& _mu; //cached epdf.mu; !!!!!! WHY NOT?
545        public:
546                //! \name Constructors
547                //!@{
548                mlnorm() : mpdf_internal< TEpdf<sq_T> >() {};
549                mlnorm (const mat &A, const vec &mu0, const sq_T &R) : mpdf_internal< TEpdf<sq_T> >() {
550                        set_parameters (A, mu0, R);
551                }
552
553                //! Set \c A and \c R
554                void set_parameters (const  mat &A0, const vec &mu0, const sq_T &R0) {
555                        bdm_assert_debug (A0.rows() == mu0.length(), "mlnorm: A vs. mu mismatch");
556                        bdm_assert_debug (A0.rows() == R0.rows(), "mlnorm: A vs. R mismatch");
557
558                        this->iepdf.set_parameters (zeros (A0.rows()), R0);
559                        A = A0;
560                        mu_const = mu0;
561                        this->dimc = A0.cols();
562                }
563                //!@}
564                //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it.
565                void condition (const vec &cond) {
566                        this->iepdf._mu() = A * cond + mu_const;
567//R is already assigned;
568                }
569
570                //!access function
571                vec& _mu_const() {return mu_const;}
572                //!access function
573                mat& _A() {return A;}
574                //!access function
575                mat _R() { return this->iepdf._R().to_mat(); }
576
577                //! Debug stream
578                template<typename sq_M>
579                friend std::ostream &operator<< (std::ostream &os,  mlnorm<sq_M, enorm> &ml);
580
581                void from_setting (const Setting &set) {
582                        mpdf::from_setting (set);
583
584                        UI::get (A, set, "A", UI::compulsory);
585                        UI::get (mu_const, set, "const", UI::compulsory);
586                        mat R0;
587                        UI::get (R0, set, "R", UI::compulsory);
588                        set_parameters (A, mu_const, R0);
589                };
590};
591UIREGISTER2 (mlnorm,ldmat);
592SHAREDPTR2 ( mlnorm, ldmat );
593UIREGISTER2 (mlnorm,fsqmat);
594SHAREDPTR2 ( mlnorm, fsqmat );
595UIREGISTER2 (mlnorm, chmat);
596SHAREDPTR2 ( mlnorm, chmat );
597
598//! Mpdf with general function for mean value
599template<class sq_T>
600class mgnorm : public mpdf_internal< enorm< sq_T > >
601{
602        private:
603//                      vec &mu; WHY NOT?
604                shared_ptr<fnc> g;
605
606        public:
607                //!default constructor
608                mgnorm() : mpdf_internal<enorm<sq_T> >() { }
609                //!set mean function
610                inline void set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0);
611                inline void condition (const vec &cond);
612
613
614                /*! UI for mgnorm
615
616                The mgnorm is constructed from a structure with fields:
617                \code
618                system = {
619                        type = "mgnorm";
620                        // function for mean value evolution
621                        g = {type="fnc"; ... }
622
623                        // variance
624                        R = [1, 0,
625                                 0, 1];
626                        // --OR --
627                        dR = [1, 1];
628
629                        // == OPTIONAL ==
630
631                        // description of y variables
632                        y = {type="rv"; names=["y", "u"];};
633                        // description of u variable
634                        u = {type="rv"; names=[];}
635                };
636                \endcode
637
638                Result if
639                */
640
641                void from_setting (const Setting &set) {
642                        shared_ptr<fnc> g = UI::build<fnc> (set, "g", UI::compulsory);
643
644                        mat R;
645                        vec dR;
646                        if (UI::get (dR, set, "dR"))
647                                R = diag (dR);
648                        else
649                                UI::get (R, set, "R", UI::compulsory);
650
651                        set_parameters (g, R);
652                }
653};
654
655UIREGISTER2 (mgnorm, chmat);
656SHAREDPTR2 ( mgnorm, chmat );
657
658
659/*! (Approximate) Student t density with linear function of mean value
660
661The internal epdf of this class is of the type of a Gaussian (enorm).
662However, each conditioning is trying to assure the best possible approximation by taking into account the zeta function. See [] for reference.
663
664Perhaps a moment-matching technique?
665*/
666class mlstudent : public mlnorm<ldmat, enorm>
667{
668        protected:
669                //! Variable \f$ \Lambda \f$ from theory
670                ldmat Lambda;
671                //! Reference to variable \f$ R \f$
672                ldmat &_R;
673                //! Variable \f$ R_e \f$
674                ldmat Re;
675        public:
676                mlstudent () : mlnorm<ldmat, enorm> (),
677                                Lambda (),      _R (iepdf._R()) {}
678                //! constructor function
679                void set_parameters (const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0) {
680                        bdm_assert_debug (A0.rows() == mu0.length(), "mlstudent: A vs. mu mismatch");
681                        bdm_assert_debug (R0.rows() == A0.rows(), "mlstudent: A vs. R mismatch");
682
683                        iepdf.set_parameters (mu0, R0);// was Lambda, why?
684                        A = A0;
685                        mu_const = mu0;
686                        Re = R0;
687                        Lambda = Lambda0;
688                }
689                void condition (const vec &cond) {
690                        iepdf._mu() = A * cond + mu_const;
691                        double zeta;
692                        //ugly hack!
693                        if ( (cond.length() + 1) == Lambda.rows()) {
694                                zeta = Lambda.invqform (concat (cond, vec_1 (1.0)));
695                        } else {
696                                zeta = Lambda.invqform (cond);
697                        }
698                        _R = Re;
699                        _R *= (1 + zeta);// / ( nu ); << nu is in Re!!!!!!
700                };
701
702};
703/*!
704\brief  Gamma random walk
705
706Mean value, \f$\mu\f$, of this density is given by \c rvc .
707Standard deviation of the random walk is proportional to one \f$k\f$-th the mean.
708This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$.
709
710The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$.
711*/
712class mgamma : public mpdf_internal<egamma>
713{
714        protected:
715
716                //! Constant \f$k\f$
717                double k;
718
719                //! cache of iepdf.beta
720                vec &_beta;
721
722        public:
723                //! Constructor
724                mgamma() : mpdf_internal<egamma>(), k (0),
725                                _beta (iepdf._beta()) {
726                }
727
728                //! Set value of \c k
729                void set_parameters (double k, const vec &beta0);
730
731                void condition (const vec &val) {_beta = k / val;};
732                //! Load from structure with elements:
733                //!  \code
734                //! { alpha = [...];         // vector of alpha
735                //!   k = 1.1;               // multiplicative constant k
736                //!   rv = {class="RV",...}  // description of RV
737                //!   rvc = {class="RV",...} // description of RV in condition
738                //! }
739                //! \endcode
740                //!@}
741                void from_setting (const Setting &set) {
742                        mpdf::from_setting (set); // reads rv and rvc
743                        vec betatmp; // ugly but necessary
744                        UI::get (betatmp, set, "beta", UI::compulsory);
745                        UI::get (k, set, "k", UI::compulsory);
746                        set_parameters (k, betatmp);
747                }
748};
749UIREGISTER (mgamma);
750SHAREDPTR (mgamma);
751
752/*!
753\brief  Inverse-Gamma random walk
754
755Mean value, \f$ \mu \f$, of this density is given by \c rvc .
756Standard deviation of the random walk is proportional to one \f$ k \f$-th the mean.
757This is achieved by setting \f$ \alpha=\mu/k^2+2 \f$ and \f$ \beta=\mu(\alpha-1)\f$.
758
759The standard deviation of the walk is then: \f$ \mu/\sqrt(k)\f$.
760 */
761class migamma : public mpdf_internal<eigamma>
762{
763        protected:
764                //! Constant \f$k\f$
765                double k;
766
767                //! cache of iepdf.alpha
768                vec &_alpha;
769
770                //! cache of iepdf.beta
771                vec &_beta;
772
773        public:
774                //! \name Constructors
775                //!@{
776                migamma() : mpdf_internal<eigamma>(),
777                                k (0),
778                                _alpha (iepdf._alpha()),
779                                _beta (iepdf._beta()) {
780                }
781
782                migamma (const migamma &m) : mpdf_internal<eigamma>(),
783                                k (0),
784                                _alpha (iepdf._alpha()),
785                                _beta (iepdf._beta()) {
786                }
787                //!@}
788
789                //! Set value of \c k
790                void set_parameters (int len, double k0) {
791                        k = k0;
792                        iepdf.set_parameters ( (1.0 / (k*k) + 2.0) *ones (len) /*alpha*/, ones (len) /*beta*/);
793                        dimc = dimension();
794                };
795                void condition (const vec &val) {
796                        _beta = elem_mult (val, (_alpha - 1.0));
797                };
798};
799
800
801/*!
802\brief  Gamma random walk around a fixed point
803
804Mean 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
805\f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f]
806
807Standard deviation of the random walk is proportional to one \f$k\f$-th the mean.
808This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$.
809
810The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$.
811*/
812class mgamma_fix : public mgamma
813{
814        protected:
815                //! parameter l
816                double l;
817                //! reference vector
818                vec refl;
819        public:
820                //! Constructor
821                mgamma_fix () : mgamma (), refl () {};
822                //! Set value of \c k
823                void set_parameters (double k0 , vec ref0, double l0) {
824                        mgamma::set_parameters (k0, ref0);
825                        refl = pow (ref0, 1.0 - l0);l = l0;
826                        dimc = dimension();
827                };
828
829                void condition (const vec &val) {vec mean = elem_mult (refl, pow (val, l)); _beta = k / mean;};
830};
831
832
833/*!
834\brief  Inverse-Gamma random walk around a fixed point
835
836Mean 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
837\f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f]
838
839==== Check == vv =
840Standard deviation of the random walk is proportional to one \f$k\f$-th the mean.
841This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$.
842
843The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$.
844 */
845class migamma_ref : public migamma
846{
847        protected:
848                //! parameter l
849                double l;
850                //! reference vector
851                vec refl;
852        public:
853                //! Constructor
854                migamma_ref () : migamma (), refl () {};
855                //! Set value of \c k
856                void set_parameters (double k0 , vec ref0, double l0) {
857                        migamma::set_parameters (ref0.length(), k0);
858                        refl = pow (ref0, 1.0 - l0);
859                        l = l0;
860                        dimc = dimension();
861                };
862
863                void condition (const vec &val) {
864                        vec mean = elem_mult (refl, pow (val, l));
865                        migamma::condition (mean);
866                };
867
868                /*! UI for migamma_ref
869
870                The migamma_ref is constructed from a structure with fields:
871                \code
872                system = {
873                        type = "migamma_ref";
874                        ref = [1e-5; 1e-5; 1e-2 1e-3];            // reference vector
875                        l = 0.999;                                // constant l
876                        k = 0.1;                                  // constant k
877                       
878                        // == OPTIONAL ==
879                        // description of y variables
880                        y = {type="rv"; names=["y", "u"];};
881                        // description of u variable
882                        u = {type="rv"; names=[];}
883                };
884                \endcode
885
886                Result if
887                 */
888                void from_setting (const Setting &set);
889
890                // TODO dodelat void to_setting( Setting &set ) const;
891};
892
893
894UIREGISTER (migamma_ref);
895SHAREDPTR (migamma_ref);
896
897/*! Log-Normal probability density
898 only allow diagonal covariances!
899
900Density of the form \f$ \log(x)\sim \mathcal{N}(\mu,\sigma^2) \f$ , i.e.
901\f[
902x \sim \frac{1}{x\sigma\sqrt{2\pi}}\exp{-\frac{1}{2\sigma^2}(\log(x)-\mu)}
903\f]
904
905*/
906class elognorm: public enorm<ldmat>
907{
908        public:
909                vec sample() const {return exp (enorm<ldmat>::sample());};
910                vec mean() const {vec var = enorm<ldmat>::variance();return exp (mu - 0.5*var);};
911
912};
913
914/*!
915\brief  Log-Normal random walk
916
917Mean value, \f$\mu\f$, is...
918
919==== Check == vv =
920Standard deviation of the random walk is proportional to one \f$k\f$-th the mean.
921This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$.
922
923The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$.
924 */
925class mlognorm : public mpdf_internal<elognorm>
926{
927        protected:
928                //! parameter 1/2*sigma^2
929                double sig2;
930
931                //! access
932                vec &mu;
933        public:
934                //! Constructor
935                mlognorm() : mpdf_internal<elognorm>(),
936                                sig2 (0),
937                                mu (iepdf._mu()) {
938                }
939
940                //! Set value of \c k
941                void set_parameters (int size, double k) {
942                        sig2 = 0.5 * log (k * k + 1);
943                        iepdf.set_parameters (zeros (size), 2*sig2*eye (size));
944
945                        dimc = size;
946                };
947
948                void condition (const vec &val) {
949                        mu = log (val) - sig2;//elem_mult ( refl,pow ( val,l ) );
950                };
951
952                /*! UI for mlognorm
953
954                The mlognorm is constructed from a structure with fields:
955                \code
956                system = {
957                        type = "mlognorm";
958                        k = 0.1;                                  // constant k
959                        mu0 = [1., 1.];
960                       
961                        // == OPTIONAL ==
962                        // description of y variables
963                        y = {type="rv"; names=["y", "u"];};
964                        // description of u variable
965                        u = {type="rv"; names=[];}
966                };
967                \endcode
968
969                 */
970                void from_setting (const Setting &set);
971
972                // TODO dodelat void to_setting( Setting &set ) const;
973
974};
975
976UIREGISTER (mlognorm);
977SHAREDPTR (mlognorm);
978
979/*! inverse Wishart density defined on Choleski decomposition
980
981*/
982class eWishartCh : public epdf
983{
984        protected:
985                //! Upper-Triagle of Choleski decomposition of \f$ \Psi \f$
986                chmat Y;
987                //! dimension of matrix  \f$ \Psi \f$
988                int p;
989                //! degrees of freedom  \f$ \nu \f$
990                double delta;
991        public:
992                //! Set internal structures
993                void set_parameters (const mat &Y0, const double delta0) {Y = chmat (Y0);delta = delta0; p = Y.rows(); dim = p * p; }
994                //! Sample matrix argument
995                mat sample_mat() const {
996                        mat X = zeros (p, p);
997
998                        //sample diagonal
999                        for (int i = 0;i < p;i++) {
1000                                GamRNG.setup (0.5* (delta - i) , 0.5);   // no +1 !! index if from 0
1001#pragma omp critical
1002                                X (i, i) = sqrt (GamRNG());
1003                        }
1004                        //do the rest
1005                        for (int i = 0;i < p;i++) {
1006                                for (int j = i + 1;j < p;j++) {
1007#pragma omp critical
1008                                        X (i, j) = NorRNG.sample();
1009                                }
1010                        }
1011                        return X*Y._Ch();// return upper triangular part of the decomposition
1012                }
1013                vec sample () const {
1014                        return vec (sample_mat()._data(), p*p);
1015                }
1016                //! fast access function y0 will be copied into Y.Ch.
1017                void setY (const mat &Ch0) {copy_vector (dim, Ch0._data(), Y._Ch()._data());}
1018                //! fast access function y0 will be copied into Y.Ch.
1019                void _setY (const vec &ch0) {copy_vector (dim, ch0._data(), Y._Ch()._data()); }
1020                //! access function
1021                const chmat& getY() const {return Y;}
1022};
1023
1024//! Inverse Wishart on Choleski decomposition
1025/*! Being computed by conversion from `standard' Wishart
1026*/
1027class eiWishartCh: public epdf
1028{
1029        protected:
1030                //! Internal instance of Wishart density
1031                eWishartCh W;
1032                //! size of Ch
1033                int p;
1034                //! parameter delta
1035                double delta;
1036        public:
1037                //! constructor function
1038                void set_parameters (const mat &Y0, const double delta0) {
1039                        delta = delta0;
1040                        W.set_parameters (inv (Y0), delta0);
1041                        dim = W.dimension(); p = Y0.rows();
1042                }
1043                vec sample() const {mat iCh; iCh = inv (W.sample_mat()); return vec (iCh._data(), dim);}
1044                //! access function
1045                void _setY (const vec &y0) {
1046                        mat Ch (p, p);
1047                        mat iCh (p, p);
1048                        copy_vector (dim, y0._data(), Ch._data());
1049
1050                        iCh = inv (Ch);
1051                        W.setY (iCh);
1052                }
1053                virtual double evallog (const vec &val) const {
1054                        chmat X (p);
1055                        const chmat& Y = W.getY();
1056
1057                        copy_vector (p*p, val._data(), X._Ch()._data());
1058                        chmat iX (p);X.inv (iX);
1059                        // compute
1060//                              \frac{ |\Psi|^{m/2}|X|^{-(m+p+1)/2}e^{-tr(\Psi X^{-1})/2} }{ 2^{mp/2}\Gamma_p(m/2)},
1061                        mat M = Y.to_mat() * iX.to_mat();
1062
1063                        double log1 = 0.5 * p * (2 * Y.logdet()) - 0.5 * (delta + p + 1) * (2 * X.logdet()) - 0.5 * trace (M);
1064                        //Fixme! Multivariate gamma omitted!! it is ok for sampling, but not otherwise!!
1065
1066                        /*                              if (0) {
1067                                                                mat XX=X.to_mat();
1068                                                                mat YY=Y.to_mat();
1069
1070                                                                double log2 = 0.5*p*log(det(YY))-0.5*(delta+p+1)*log(det(XX))-0.5*trace(YY*inv(XX));
1071                                                                cout << log1 << "," << log2 << endl;
1072                                                        }*/
1073                        return log1;
1074                };
1075
1076};
1077
1078//! Random Walk on inverse Wishart
1079class rwiWishartCh : public mpdf_internal<eiWishartCh>
1080{
1081        protected:
1082                //!square root of \f$ \nu-p-1 \f$ - needed for computation of \f$ \Psi \f$ from conditions
1083                double sqd;
1084                //!reference point for diagonal
1085                vec refl;
1086                //! power of the reference
1087                double l;
1088                //! dimension
1089                int p;
1090
1091        public:
1092                rwiWishartCh() : sqd (0), l (0), p (0) {}
1093                //! constructor function
1094                void set_parameters (int p0, double k, vec ref0, double l0) {
1095                        p = p0;
1096                        double delta = 2 / (k * k) + p + 3;
1097                        sqd = sqrt (delta - p - 1);
1098                        l = l0;
1099                        refl = pow (ref0, 1 - l);
1100
1101                        iepdf.set_parameters (eye (p), delta);
1102                        dimc = iepdf.dimension();
1103                }
1104                void condition (const vec &c) {
1105                        vec z = c;
1106                        int ri = 0;
1107                        for (int i = 0;i < p*p;i += (p + 1)) {//trace diagonal element
1108                                z (i) = pow (z (i), l) * refl (ri);
1109                                ri++;
1110                        }
1111
1112                        iepdf._setY (sqd*z);
1113                }
1114};
1115
1116//! Switch between various resampling methods.
1117enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 };
1118/*!
1119\brief Weighted empirical density
1120
1121Used e.g. in particle filters.
1122*/
1123class eEmp: public epdf
1124{
1125        protected :
1126                //! Number of particles
1127                int n;
1128                //! Sample weights \f$w\f$
1129                vec w;
1130                //! Samples \f$x^{(i)}, i=1..n\f$
1131                Array<vec> samples;
1132        public:
1133                //! \name Constructors
1134                //!@{
1135                eEmp () : epdf (), w (), samples () {};
1136                //! copy constructor
1137                eEmp (const eEmp &e) : epdf (e), w (e.w), samples (e.samples) {};
1138                //!@}
1139
1140                //! Set samples and weights
1141                void set_statistics (const vec &w0, const epdf &pdf0);
1142                //! Set samples and weights
1143                void set_statistics (const epdf &pdf0 , int n) {set_statistics (ones (n) / n, pdf0);};
1144                //! Set sample
1145                void set_samples (const epdf* pdf0);
1146                //! Set sample
1147                void set_parameters (int n0, bool copy = true) {n = n0; w.set_size (n0, copy);samples.set_size (n0, copy);};
1148                //! Set samples
1149                void set_parameters (const Array<vec> &Av) {
1150                        bdm_assert_debug(Av.size()>0,"Empty samples"); 
1151                        n = Av.size(); 
1152                        epdf::set_parameters(Av(0).length());
1153                        w=1/n*ones(n);
1154                        samples=Av;
1155                };
1156                //! Potentially dangerous, use with care.
1157                vec& _w()  {return w;};
1158                //! Potentially dangerous, use with care.
1159                const vec& _w() const {return w;};
1160                //! access function
1161                Array<vec>& _samples() {return samples;};
1162                //! access function
1163                const Array<vec>& _samples() const {return samples;};
1164                //! 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.
1165                ivec resample (RESAMPLING_METHOD method = SYSTEMATIC);
1166
1167                //! inherited operation : NOT implemented
1168                vec sample() const {
1169                        bdm_error ("Not implemented");
1170                        return vec();
1171                }
1172
1173                //! inherited operation : NOT implemented
1174                double evallog (const vec &val) const {
1175                        bdm_error ("Not implemented");
1176                        return 0.0;
1177                }
1178
1179                vec mean() const {
1180                        vec pom = zeros (dim);
1181                        for (int i = 0;i < n;i++) {pom += samples (i) * w (i);}
1182                        return pom;
1183                }
1184                vec variance() const {
1185                        vec pom = zeros (dim);
1186                        for (int i = 0;i < n;i++) {pom += pow (samples (i), 2) * w (i);}
1187                        return pom -pow (mean(), 2);
1188                }
1189                //! For this class, qbounds are minimum and maximum value of the population!
1190                void qbounds (vec &lb, vec &ub, double perc = 0.95) const {
1191                        // lb in inf so than it will be pushed below;
1192                        lb.set_size (dim);
1193                        ub.set_size (dim);
1194                        lb = std::numeric_limits<double>::infinity();
1195                        ub = -std::numeric_limits<double>::infinity();
1196                        int j;
1197                        for (int i = 0;i < n;i++) {
1198                                for (j = 0;j < dim; j++) {
1199                                        if (samples (i) (j) < lb (j)) {lb (j) = samples (i) (j);}
1200                                        if (samples (i) (j) > ub (j)) {ub (j) = samples (i) (j);}
1201                                }
1202                        }
1203                }
1204};
1205
1206
1207////////////////////////
1208
1209template<class sq_T>
1210void enorm<sq_T>::set_parameters (const vec &mu0, const sq_T &R0)
1211{
1212//Fixme test dimensions of mu0 and R0;
1213        mu = mu0;
1214        R = R0;
1215        validate();
1216};
1217
1218template<class sq_T>
1219void enorm<sq_T>::from_setting (const Setting &set)
1220{
1221        epdf::from_setting (set); //reads rv
1222
1223        UI::get (mu, set, "mu", UI::compulsory);
1224        mat Rtmp;// necessary for conversion
1225        UI::get (Rtmp, set, "R", UI::compulsory);
1226        R = Rtmp; // conversion
1227        validate();
1228}
1229
1230template<class sq_T>
1231void enorm<sq_T>::dupdate (mat &v, double nu)
1232{
1233        //
1234};
1235
1236// template<class sq_T>
1237// void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) {
1238//      //
1239// };
1240
1241template<class sq_T>
1242vec enorm<sq_T>::sample() const
1243{
1244        vec x (dim);
1245#pragma omp critical
1246        NorRNG.sample_vector (dim, x);
1247        vec smp = R.sqrt_mult (x);
1248
1249        smp += mu;
1250        return smp;
1251};
1252
1253// template<class sq_T>
1254// double enorm<sq_T>::eval ( const vec &val ) const {
1255//      double pdfl,e;
1256//      pdfl = evallog ( val );
1257//      e = exp ( pdfl );
1258//      return e;
1259// };
1260
1261template<class sq_T>
1262double enorm<sq_T>::evallog_nn (const vec &val) const
1263{
1264        // 1.83787706640935 = log(2pi)
1265        double tmp = -0.5 * (R.invqform (mu - val));// - lognc();
1266        return  tmp;
1267};
1268
1269template<class sq_T>
1270inline double enorm<sq_T>::lognc () const
1271{
1272        // 1.83787706640935 = log(2pi)
1273        double tmp = 0.5 * (R.cols() * 1.83787706640935 + R.logdet());
1274        return tmp;
1275};
1276
1277
1278// template<class sq_T>
1279// vec mlnorm<sq_T>::samplecond (const  vec &cond, double &lik ) {
1280//      this->condition ( cond );
1281//      vec smp = epdf.sample();
1282//      lik = epdf.eval ( smp );
1283//      return smp;
1284// }
1285
1286// template<class sq_T>
1287// mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) {
1288//      int i;
1289//      int dim = rv.count();
1290//      mat Smp ( dim,n );
1291//      vec smp ( dim );
1292//      this->condition ( cond );
1293//
1294//      for ( i=0; i<n; i++ ) {
1295//              smp = epdf.sample();
1296//              lik ( i ) = epdf.eval ( smp );
1297//              Smp.set_col ( i ,smp );
1298//      }
1299//
1300//      return Smp;
1301// }
1302
1303
1304template<class sq_T>
1305shared_ptr<epdf> enorm<sq_T>::marginal ( const RV &rvn ) const
1306{
1307        enorm<sq_T> *tmp = new enorm<sq_T> ();
1308        shared_ptr<epdf> narrow(tmp);
1309        marginal ( rvn, *tmp );
1310        return narrow;
1311}
1312
1313template<class sq_T>
1314void enorm<sq_T>::marginal ( const RV &rvn, enorm<sq_T> &target ) const
1315{
1316        bdm_assert_debug (isnamed(), "rv description is not assigned");
1317        ivec irvn = rvn.dataind (rv);
1318
1319        sq_T Rn (R, irvn);  // select rows and columns of R
1320
1321        target.set_rv ( rvn );
1322        target.set_parameters (mu (irvn), Rn);
1323}
1324
1325template<class sq_T>
1326shared_ptr<mpdf> enorm<sq_T>::condition ( const RV &rvn ) const
1327{
1328        mlnorm<sq_T> *tmp = new mlnorm<sq_T> ();
1329        shared_ptr<mpdf> narrow(tmp);
1330        condition ( rvn, *tmp );
1331        return narrow;
1332}
1333
1334template<class sq_T>
1335void enorm<sq_T>::condition ( const RV &rvn, mpdf &target ) const
1336{
1337        typedef mlnorm<sq_T> TMlnorm;
1338
1339        bdm_assert_debug (isnamed(), "rvs are not assigned");
1340        TMlnorm &uptarget = dynamic_cast<TMlnorm &>(target);
1341
1342        RV rvc = rv.subt (rvn);
1343        bdm_assert_debug ( (rvc._dsize() + rvn._dsize() == rv._dsize()), "wrong rvn");
1344        //Permutation vector of the new R
1345        ivec irvn = rvn.dataind (rv);
1346        ivec irvc = rvc.dataind (rv);
1347        ivec perm = concat (irvn , irvc);
1348        sq_T Rn (R, perm);
1349
1350        //fixme - could this be done in general for all sq_T?
1351        mat S = Rn.to_mat();
1352        //fixme
1353        int n = rvn._dsize() - 1;
1354        int end = R.rows() - 1;
1355        mat S11 = S.get (0, n, 0, n);
1356        mat S12 = S.get (0, n , rvn._dsize(), end);
1357        mat S22 = S.get (rvn._dsize(), end, rvn._dsize(), end);
1358
1359        vec mu1 = mu (irvn);
1360        vec mu2 = mu (irvc);
1361        mat A = S12 * inv (S22);
1362        sq_T R_n (S11 - A *S12.T());
1363
1364        uptarget.set_rv (rvn);
1365        uptarget.set_rvc (rvc);
1366        uptarget.set_parameters (A, mu1 - A*mu2, R_n);
1367}
1368
1369////
1370///////
1371template<class sq_T>
1372void mgnorm<sq_T >::set_parameters (const shared_ptr<fnc> &g0, const sq_T &R0) {
1373        g = g0;
1374        this->iepdf.set_parameters (zeros (g->dimension()), R0);
1375}
1376
1377template<class sq_T>
1378void mgnorm<sq_T >::condition (const vec &cond) {this->iepdf._mu() = g->eval (cond);};
1379
1380//! \todo unify this stuff with to_string()
1381template<class sq_T>
1382std::ostream &operator<< (std::ostream &os,  mlnorm<sq_T> &ml)
1383{
1384        os << "A:" << ml.A << endl;
1385        os << "mu:" << ml.mu_const << endl;
1386        os << "R:" << ml._R() << endl;
1387        return os;
1388};
1389
1390}
1391#endif //EF_H
Note: See TracBrowser for help on using the browser.