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 | #include <itpp/itbase.h> |
---|
17 | #include "../math/libDC.h" |
---|
18 | #include "libBM.h" |
---|
19 | #include "../itpp_ext.h" |
---|
20 | //#include <std> |
---|
21 | |
---|
22 | using namespace itpp; |
---|
23 | |
---|
24 | |
---|
25 | //! Global Uniform_RNG |
---|
26 | extern Uniform_RNG UniRNG; |
---|
27 | //! Global Normal_RNG |
---|
28 | extern Normal_RNG NorRNG; |
---|
29 | //! Global Gamma_RNG |
---|
30 | extern Gamma_RNG GamRNG; |
---|
31 | |
---|
32 | /*! |
---|
33 | * \brief General conjugate exponential family posterior density. |
---|
34 | |
---|
35 | * More?... |
---|
36 | */ |
---|
37 | |
---|
38 | class eEF : public epdf { |
---|
39 | |
---|
40 | public: |
---|
41 | // eEF() :epdf() {}; |
---|
42 | //! default constructor |
---|
43 | eEF ( const RV &rv ) :epdf ( rv ) {}; |
---|
44 | //!TODO decide if it is really needed |
---|
45 | virtual void tupdate ( double phi, mat &vbar, double nubar ) {}; |
---|
46 | //!TODO decide if it is really needed |
---|
47 | virtual void dupdate ( mat &v,double nu=1.0 ) {}; |
---|
48 | }; |
---|
49 | |
---|
50 | /*! |
---|
51 | * \brief Exponential family model. |
---|
52 | |
---|
53 | * More?... |
---|
54 | */ |
---|
55 | |
---|
56 | class mEF : public mpdf { |
---|
57 | |
---|
58 | public: |
---|
59 | //! Default constructor |
---|
60 | mEF ( const RV &rv0, const RV &rvc0 ) :mpdf ( rv0,rvc0 ) {}; |
---|
61 | }; |
---|
62 | |
---|
63 | /*! |
---|
64 | * \brief Gaussian density with positive definite (decomposed) covariance matrix. |
---|
65 | |
---|
66 | * More?... |
---|
67 | */ |
---|
68 | template<class sq_T> |
---|
69 | |
---|
70 | class enorm : public eEF { |
---|
71 | protected: |
---|
72 | //! mean value |
---|
73 | vec mu; |
---|
74 | //! Covariance matrix in decomposed form |
---|
75 | sq_T R; |
---|
76 | //! Cache: _iR = inv(R); |
---|
77 | sq_T _iR; |
---|
78 | //! indicator if \c _iR is chached |
---|
79 | bool cached; |
---|
80 | //! dimension (redundant from rv.count() for easier coding ) |
---|
81 | int dim; |
---|
82 | public: |
---|
83 | // enorm() :eEF() {}; |
---|
84 | //!Default constructor |
---|
85 | enorm ( RV &rv ); |
---|
86 | //! Set mean value \c mu and covariance \c R |
---|
87 | void set_parameters ( const vec &mu,const sq_T &R ); |
---|
88 | //! tupdate in exponential form (not really handy) |
---|
89 | void tupdate ( double phi, mat &vbar, double nubar ); |
---|
90 | //! dupdate in exponential form (not really handy) |
---|
91 | void dupdate ( mat &v,double nu=1.0 ); |
---|
92 | |
---|
93 | vec sample() const; |
---|
94 | //! TODO is it used? |
---|
95 | mat sample ( int N ) const; |
---|
96 | double eval ( const vec &val ) const ; |
---|
97 | double evalpdflog ( const vec &val ) const; |
---|
98 | vec mean() const {return mu;} |
---|
99 | |
---|
100 | //Access methods |
---|
101 | //! returns a pointer to the internal mean value. Use with Care! |
---|
102 | vec* _mu() {return μ} |
---|
103 | |
---|
104 | //! returns pointers to the internal variance and its inverse. Use with Care! |
---|
105 | void _R ( sq_T* &pR, sq_T* &piR ) { |
---|
106 | pR=&R; |
---|
107 | piR=&_iR; |
---|
108 | } |
---|
109 | |
---|
110 | //! set cache as inconsistent |
---|
111 | void _cached ( bool what ) {cached=what;} |
---|
112 | //! access mthod |
---|
113 | mat getR () {return R.to_mat();} |
---|
114 | }; |
---|
115 | |
---|
116 | /*! |
---|
117 | \brief Gamma posterior density |
---|
118 | |
---|
119 | Multvariate Gamma density as product of independent univariate densities. |
---|
120 | \f[ |
---|
121 | f(x|\alpha,\beta) = \prod f(x_i|\alpha_i,\beta_i) |
---|
122 | \f] |
---|
123 | */ |
---|
124 | |
---|
125 | class egamma : public eEF { |
---|
126 | protected: |
---|
127 | //! Vector \f$\alpha\f$ |
---|
128 | vec alpha; |
---|
129 | //! Vector \f$\beta\f$ |
---|
130 | vec beta; |
---|
131 | public : |
---|
132 | //! Default constructor |
---|
133 | egamma ( const RV &rv ) :eEF ( rv ) {}; |
---|
134 | //! Sets parameters |
---|
135 | void set_parameters ( const vec &a, const vec &b ) {alpha=a,beta=b;}; |
---|
136 | vec sample() const; |
---|
137 | //! TODO: is it used anywhere? |
---|
138 | mat sample ( int N ) const; |
---|
139 | double evalpdflog ( const vec &val ) const; |
---|
140 | //! Returns poiter to alpha and beta. Potentially dengerous: use with care! |
---|
141 | void _param ( vec* &a, vec* &b ) {a=αb=β}; |
---|
142 | vec mean() const {vec pom ( alpha ); pom/=beta; return pom;} |
---|
143 | }; |
---|
144 | /* |
---|
145 | //! Weighted mixture of epdfs with external owned components. |
---|
146 | class emix : public epdf { |
---|
147 | protected: |
---|
148 | int n; |
---|
149 | vec &w; |
---|
150 | Array<epdf*> Coms; |
---|
151 | public: |
---|
152 | //! Default constructor |
---|
153 | emix ( const RV &rv, vec &w0): epdf(rv), n(w0.length()), w(w0), Coms(n) {}; |
---|
154 | void set_parameters( int &i, double wi, epdf* ep){w(i)=wi;Coms(i)=ep;} |
---|
155 | vec mean(){vec pom; for(int i=0;i<n;i++){pom+=Coms(i)->mean()*w(i);} return pom;}; |
---|
156 | vec sample() {it_error ( "Not implemented" );return 0;} |
---|
157 | }; |
---|
158 | */ |
---|
159 | |
---|
160 | //! Uniform distributed density on a rectangular support |
---|
161 | |
---|
162 | class euni: public epdf { |
---|
163 | protected: |
---|
164 | //! lower bound on support |
---|
165 | vec low; |
---|
166 | //! upper bound on support |
---|
167 | vec high; |
---|
168 | //! internal |
---|
169 | vec distance; |
---|
170 | //! normalizing coefficients |
---|
171 | double nk; |
---|
172 | //! cache of log( \c nk ) |
---|
173 | double lnk; |
---|
174 | public: |
---|
175 | //! Defualt constructor |
---|
176 | euni ( const RV rv ) :epdf ( rv ) {} |
---|
177 | double eval ( const vec &val ) const {return nk;} |
---|
178 | double evalpdflog ( const vec &val ) const {return lnk;} |
---|
179 | vec sample() const { |
---|
180 | vec smp ( rv.count() ); UniRNG.sample_vector ( rv.count(),smp ); |
---|
181 | return low+distance*smp; |
---|
182 | } |
---|
183 | //! set values of \c low and \c high |
---|
184 | void set_parameters ( const vec &low0, const vec &high0 ) { |
---|
185 | distance = high0-low0; |
---|
186 | it_assert_debug ( min ( distance ) >0.0,"bad support" ); |
---|
187 | low = low0; |
---|
188 | high = high0; |
---|
189 | nk = prod ( 1.0/distance ); |
---|
190 | lnk = log ( nk ); |
---|
191 | } |
---|
192 | vec mean() const {vec pom=high; pom-=low; pom/=2.0; return pom;} |
---|
193 | }; |
---|
194 | |
---|
195 | |
---|
196 | /*! |
---|
197 | \brief Normal distributed linear function with linear function of mean value; |
---|
198 | |
---|
199 | Mean value $mu=A*rvc$. |
---|
200 | */ |
---|
201 | template<class sq_T> |
---|
202 | class mlnorm : public mEF { |
---|
203 | //! Internal epdf that arise by conditioning on \c rvc |
---|
204 | enorm<sq_T> epdf; |
---|
205 | vec* _mu; //cached epdf.mu; |
---|
206 | mat A; |
---|
207 | public: |
---|
208 | //! Constructor |
---|
209 | mlnorm ( RV &rv,RV &rvc ); |
---|
210 | //! Set \c A and \c R |
---|
211 | void set_parameters ( const mat &A, const sq_T &R ); |
---|
212 | //!Generate one sample of the posterior |
---|
213 | vec samplecond ( vec &cond, double &lik ); |
---|
214 | //!Generate matrix of samples of the posterior |
---|
215 | mat samplecond ( vec &cond, vec &lik, int n ); |
---|
216 | //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it. |
---|
217 | void condition ( vec &cond ); |
---|
218 | }; |
---|
219 | |
---|
220 | /*! |
---|
221 | \brief Gamma random walk |
---|
222 | |
---|
223 | Mean value, \f$\mu\f$, of this density is given by \c rvc . |
---|
224 | Standard deviation of the random walk is proportional to one $k$-th the mean. |
---|
225 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
226 | |
---|
227 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
228 | */ |
---|
229 | class mgamma : public mEF { |
---|
230 | protected: |
---|
231 | //! Internal epdf that arise by conditioning on \c rvc |
---|
232 | egamma epdf; |
---|
233 | //! Constant $k$ |
---|
234 | double k; |
---|
235 | //! cache of epdf.beta |
---|
236 | vec* _beta; |
---|
237 | |
---|
238 | public: |
---|
239 | //! Constructor |
---|
240 | mgamma ( const RV &rv,const RV &rvc ); |
---|
241 | //! Set value of \c k |
---|
242 | void set_parameters ( double k ); |
---|
243 | //!Generate one sample of the posterior |
---|
244 | vec samplecond ( vec &cond, double &lik ); |
---|
245 | //!Generate matrix of samples of the posterior |
---|
246 | mat samplecond ( vec &cond, vec &lik, int n ); |
---|
247 | void condition ( const vec &val ) {*_beta=k/val;}; |
---|
248 | }; |
---|
249 | |
---|
250 | /*! |
---|
251 | \brief Gamma random walk around a fixed point |
---|
252 | |
---|
253 | Mean value, \f$\mu\f$, of this density is given by a geometric combination of \c rvc and given fixed point, $p$. $k$ is the coefficient of the geometric combimation |
---|
254 | \f[ \mu = \mu_{t-1} ^{l} p^{1-l}\f] |
---|
255 | |
---|
256 | Standard deviation of the random walk is proportional to one $k$-th the mean. |
---|
257 | This is achieved by setting \f$\alpha=k\f$ and \f$\beta=k/\mu\f$. |
---|
258 | |
---|
259 | The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. |
---|
260 | */ |
---|
261 | class mgamma_fix : public mgamma { |
---|
262 | protected: |
---|
263 | double l; |
---|
264 | vec refl; |
---|
265 | public: |
---|
266 | //! Constructor |
---|
267 | mgamma_fix ( const RV &rv,const RV &rvc ) : mgamma ( rv,rvc ),refl ( rv.count() ) {}; |
---|
268 | //! Set value of \c k |
---|
269 | void set_parameters ( double k0 , vec ref0, double l0 ) { |
---|
270 | mgamma::set_parameters ( k0 ); |
---|
271 | refl=pow ( ref0,1.0-l0 );l=l0; |
---|
272 | }; |
---|
273 | |
---|
274 | void condition ( const vec &val ) {vec mean=elem_mult ( refl,pow ( val,l ) ); *_beta=k/mean;}; |
---|
275 | }; |
---|
276 | |
---|
277 | //! Switch between various resampling methods. |
---|
278 | enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 }; |
---|
279 | /*! |
---|
280 | \brief Weighted empirical density |
---|
281 | |
---|
282 | Used e.g. in particle filters. |
---|
283 | */ |
---|
284 | class eEmp: public epdf { |
---|
285 | protected : |
---|
286 | //! Number of particles |
---|
287 | int n; |
---|
288 | //! Sample weights $w$ |
---|
289 | vec w; |
---|
290 | //! Samples \f$x^{(i)}, i=1..n\f$ |
---|
291 | Array<vec> samples; |
---|
292 | public: |
---|
293 | //! Default constructor |
---|
294 | eEmp ( const RV &rv0 ,int n0 ) :epdf ( rv0 ),n ( n0 ),w ( n ),samples ( n ) {}; |
---|
295 | //! Set sample |
---|
296 | void set_parameters ( const vec &w0, epdf* pdf0 ); |
---|
297 | //! Potentially dangerous, use with care. |
---|
298 | vec& _w() {return w;}; |
---|
299 | //! access function |
---|
300 | Array<vec>& _samples() {return samples;}; |
---|
301 | //! 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. |
---|
302 | ivec resample ( RESAMPLING_METHOD method = SYSTEMATIC ); |
---|
303 | //! inherited operation : NOT implemneted |
---|
304 | vec sample() const {it_error ( "Not implemented" );return 0;} |
---|
305 | //! inherited operation : NOT implemneted |
---|
306 | double evalpdflog ( const vec &val ) const {it_error ( "Not implemented" );return 0.0;} |
---|
307 | vec mean() const { |
---|
308 | vec pom=zeros ( rv.count() ); |
---|
309 | for ( int i=0;i<n;i++ ) {pom+=samples ( i ) *w ( i );} |
---|
310 | return pom; |
---|
311 | } |
---|
312 | }; |
---|
313 | |
---|
314 | |
---|
315 | //////////////////////// |
---|
316 | |
---|
317 | template<class sq_T> |
---|
318 | enorm<sq_T>::enorm ( RV &rv ) :eEF ( rv ), mu ( rv.count() ),R ( rv.count() ),_iR ( rv.count() ),cached ( false ),dim ( rv.count() ) {}; |
---|
319 | |
---|
320 | template<class sq_T> |
---|
321 | void enorm<sq_T>::set_parameters ( const vec &mu0, const sq_T &R0 ) { |
---|
322 | //Fixme test dimensions of mu0 and R0; |
---|
323 | mu = mu0; |
---|
324 | R = R0; |
---|
325 | if ( _iR.rows() !=R.rows() ) _iR=R; // memory allocation! |
---|
326 | R.inv ( _iR ); //update cache |
---|
327 | cached=true; |
---|
328 | }; |
---|
329 | |
---|
330 | template<class sq_T> |
---|
331 | void enorm<sq_T>::dupdate ( mat &v, double nu ) { |
---|
332 | // |
---|
333 | }; |
---|
334 | |
---|
335 | template<class sq_T> |
---|
336 | void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) { |
---|
337 | // |
---|
338 | }; |
---|
339 | |
---|
340 | template<class sq_T> |
---|
341 | vec enorm<sq_T>::sample() const { |
---|
342 | vec x ( dim ); |
---|
343 | NorRNG.sample_vector ( dim,x ); |
---|
344 | vec smp = R.sqrt_mult ( x ); |
---|
345 | |
---|
346 | smp += mu; |
---|
347 | return smp; |
---|
348 | }; |
---|
349 | |
---|
350 | template<class sq_T> |
---|
351 | mat enorm<sq_T>::sample ( int N ) const { |
---|
352 | mat X ( dim,N ); |
---|
353 | vec x ( dim ); |
---|
354 | vec pom; |
---|
355 | int i; |
---|
356 | |
---|
357 | for ( i=0;i<N;i++ ) { |
---|
358 | NorRNG.sample_vector ( dim,x ); |
---|
359 | pom = R.sqrt_mult ( x ); |
---|
360 | pom +=mu; |
---|
361 | X.set_col ( i, pom ); |
---|
362 | } |
---|
363 | |
---|
364 | return X; |
---|
365 | }; |
---|
366 | |
---|
367 | template<class sq_T> |
---|
368 | double enorm<sq_T>::eval ( const vec &val ) const { |
---|
369 | double pdfl,e; |
---|
370 | pdfl = evalpdflog ( val ); |
---|
371 | e = exp ( pdfl ); |
---|
372 | return e; |
---|
373 | }; |
---|
374 | |
---|
375 | template<class sq_T> |
---|
376 | double enorm<sq_T>::evalpdflog ( const vec &val ) const { |
---|
377 | if ( !cached ) {it_error ( "this should not happen, see cached" );} |
---|
378 | |
---|
379 | // 1.83787706640935 = log(2pi) |
---|
380 | return -0.5* ( R.cols() * 1.83787706640935 +R.logdet() +_iR.qform ( mu-val ) ); |
---|
381 | }; |
---|
382 | |
---|
383 | |
---|
384 | template<class sq_T> |
---|
385 | mlnorm<sq_T>::mlnorm ( RV &rv0,RV &rvc0 ) :mEF ( rv0,rvc0 ),epdf ( rv ),A ( rv0.count(),rv0.count() ) { |
---|
386 | _mu = epdf._mu(); |
---|
387 | } |
---|
388 | |
---|
389 | template<class sq_T> |
---|
390 | void mlnorm<sq_T>::set_parameters ( const mat &A0, const sq_T &R0 ) { |
---|
391 | epdf.set_parameters ( zeros ( rv.count() ),R0 ); |
---|
392 | A = A0; |
---|
393 | } |
---|
394 | |
---|
395 | template<class sq_T> |
---|
396 | vec mlnorm<sq_T>::samplecond ( vec &cond, double &lik ) { |
---|
397 | this->condition ( cond ); |
---|
398 | vec smp = epdf.sample(); |
---|
399 | lik = epdf.eval ( smp ); |
---|
400 | return smp; |
---|
401 | } |
---|
402 | |
---|
403 | template<class sq_T> |
---|
404 | mat mlnorm<sq_T>::samplecond ( vec &cond, vec &lik, int n ) { |
---|
405 | int i; |
---|
406 | int dim = rv.count(); |
---|
407 | mat Smp ( dim,n ); |
---|
408 | vec smp ( dim ); |
---|
409 | this->condition ( cond ); |
---|
410 | |
---|
411 | for ( i=0; i<n; i++ ) { |
---|
412 | smp = epdf.sample(); |
---|
413 | lik ( i ) = epdf.eval ( smp ); |
---|
414 | Smp.set_col ( i ,smp ); |
---|
415 | } |
---|
416 | |
---|
417 | return Smp; |
---|
418 | } |
---|
419 | |
---|
420 | template<class sq_T> |
---|
421 | void mlnorm<sq_T>::condition ( vec &cond ) { |
---|
422 | *_mu = A*cond; |
---|
423 | //R is already assigned; |
---|
424 | } |
---|
425 | |
---|
426 | /////////// |
---|
427 | |
---|
428 | |
---|
429 | #endif //EF_H |
---|