1 | /*! |
---|
2 | \file |
---|
3 | \brief Bayesian Filtering using stochastic sampling (Particle Filters) |
---|
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 PARTICLES_H |
---|
14 | #define PARTICLES_H |
---|
15 | |
---|
16 | |
---|
17 | #include "../stat/exp_family.h" |
---|
18 | |
---|
19 | namespace bdm { |
---|
20 | |
---|
21 | /*! |
---|
22 | * \brief Trivial particle filter with proposal density equal to parameter evolution model. |
---|
23 | |
---|
24 | Posterior density is represented by a weighted empirical density (\c eEmp ). |
---|
25 | */ |
---|
26 | |
---|
27 | class PF : public BM { |
---|
28 | protected: |
---|
29 | //!number of particles; |
---|
30 | int n; |
---|
31 | //!posterior density |
---|
32 | eEmp est; |
---|
33 | //! pointer into \c eEmp |
---|
34 | vec &_w; |
---|
35 | //! pointer into \c eEmp |
---|
36 | Array<vec> &_samples; |
---|
37 | //! Parameter evolution model |
---|
38 | mpdf *par; |
---|
39 | //! Observation model |
---|
40 | mpdf *obs; |
---|
41 | |
---|
42 | //! which resampling method will be used |
---|
43 | RESAMPLING_METHOD resmethod; |
---|
44 | |
---|
45 | //! \name Options |
---|
46 | //!@{ |
---|
47 | |
---|
48 | //! Log all samples |
---|
49 | bool opt_L_smp; |
---|
50 | //! Log all samples |
---|
51 | bool opt_L_wei; |
---|
52 | //!@} |
---|
53 | |
---|
54 | public: |
---|
55 | //! \name Constructors |
---|
56 | //!@{ |
---|
57 | PF ( ) : est(), _w ( est._w() ), _samples ( est._samples() ), opt_L_smp ( false ), opt_L_wei ( false ) { |
---|
58 | LIDs.set_size ( 5 ); |
---|
59 | }; |
---|
60 | /* PF ( mpdf *par0, mpdf *obs0, epdf *epdf0, int n0 ) : |
---|
61 | est ( ),_w ( est._w() ),_samples ( est._samples() ),opt_L_smp(false), opt_L_wei(false) |
---|
62 | { set_parameters ( par0,obs0,n0 ); set_statistics ( ones ( n0 ),epdf0 ); };*/ |
---|
63 | void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm = SYSTEMATIC ) { |
---|
64 | par = par0; |
---|
65 | obs = obs0; |
---|
66 | n = n0; |
---|
67 | resmethod = rm; |
---|
68 | }; |
---|
69 | void set_statistics ( const vec w0, const epdf &epdf0 ) { |
---|
70 | est.set_statistics ( w0, epdf0 ); |
---|
71 | }; |
---|
72 | //!@} |
---|
73 | //! Set posterior density by sampling from epdf0 |
---|
74 | // void set_est ( const epdf &epdf0 ); |
---|
75 | void set_options ( const string &opt ) { |
---|
76 | BM::set_options ( opt ); |
---|
77 | opt_L_wei = ( opt.find ( "logweights" ) != string::npos ); |
---|
78 | opt_L_smp = ( opt.find ( "logsamples" ) != string::npos ); |
---|
79 | } |
---|
80 | void bayes ( const vec &dt ); |
---|
81 | //!access function |
---|
82 | vec* __w() { |
---|
83 | return &_w; |
---|
84 | } |
---|
85 | }; |
---|
86 | |
---|
87 | /*! |
---|
88 | \brief Marginalized Particle filter |
---|
89 | |
---|
90 | Trivial version: proposal = parameter evolution, observation model is not used. (it is assumed to be part of BM). |
---|
91 | */ |
---|
92 | |
---|
93 | template<class BM_T> |
---|
94 | class MPF : public PF { |
---|
95 | Array<BM_T*> BMs; |
---|
96 | |
---|
97 | //! internal class for MPDF providing composition of eEmp with external components |
---|
98 | |
---|
99 | class mpfepdf : public epdf { |
---|
100 | protected: |
---|
101 | eEmp &E; |
---|
102 | vec &_w; |
---|
103 | Array<const epdf*> Coms; |
---|
104 | public: |
---|
105 | mpfepdf ( eEmp &E0 ) : |
---|
106 | epdf ( ), E ( E0 ), _w ( E._w() ), |
---|
107 | Coms ( _w.length() ) { |
---|
108 | }; |
---|
109 | //! read statistics from MPF |
---|
110 | void read_statistics ( Array<BM_T*> &A ) { |
---|
111 | dim = E.dimension() + A ( 0 )->posterior().dimension(); |
---|
112 | for ( int i = 0; i < _w.length() ; i++ ) { |
---|
113 | Coms ( i ) = &(A ( i )->posterior()); |
---|
114 | } |
---|
115 | } |
---|
116 | //! needed in resampling |
---|
117 | void set_elements ( int &i, double wi, const epdf* ep ) { |
---|
118 | _w ( i ) = wi; |
---|
119 | Coms ( i ) = ep; |
---|
120 | }; |
---|
121 | |
---|
122 | void set_parameters ( int n ) { |
---|
123 | E.set_parameters ( n, false ); |
---|
124 | Coms.set_length ( n ); |
---|
125 | } |
---|
126 | vec mean() const { |
---|
127 | // ugly |
---|
128 | vec pom = zeros ( Coms ( 0 )->dimension() ); |
---|
129 | for ( int i = 0; i < _w.length(); i++ ) { |
---|
130 | pom += Coms ( i )->mean() * _w ( i ); |
---|
131 | } |
---|
132 | return concat ( E.mean(), pom ); |
---|
133 | } |
---|
134 | vec variance() const { |
---|
135 | // ugly |
---|
136 | vec pom = zeros ( Coms ( 0 )->dimension() ); |
---|
137 | vec pom2 = zeros ( Coms ( 0 )->dimension() ); |
---|
138 | for ( int i = 0; i < _w.length(); i++ ) { |
---|
139 | pom += Coms ( i )->mean() * _w ( i ); |
---|
140 | pom2 += ( Coms ( i )->variance() + pow ( Coms ( i )->mean(), 2 ) ) * _w ( i ); |
---|
141 | } |
---|
142 | return concat ( E.variance(), pom2 - pow ( pom, 2 ) ); |
---|
143 | } |
---|
144 | void qbounds ( vec &lb, vec &ub, double perc = 0.95 ) const { |
---|
145 | //bounds on particles |
---|
146 | vec lbp; |
---|
147 | vec ubp; |
---|
148 | E.qbounds ( lbp, ubp ); |
---|
149 | |
---|
150 | //bounds on Components |
---|
151 | int dimC = Coms ( 0 )->dimension(); |
---|
152 | int j; |
---|
153 | // temporary |
---|
154 | vec lbc ( dimC ); |
---|
155 | vec ubc ( dimC ); |
---|
156 | // minima and maxima |
---|
157 | vec Lbc ( dimC ); |
---|
158 | vec Ubc ( dimC ); |
---|
159 | Lbc = std::numeric_limits<double>::infinity(); |
---|
160 | Ubc = -std::numeric_limits<double>::infinity(); |
---|
161 | |
---|
162 | for ( int i = 0; i < _w.length(); i++ ) { |
---|
163 | // check Coms |
---|
164 | Coms ( i )->qbounds ( lbc, ubc ); |
---|
165 | for ( j = 0; j < dimC; j++ ) { |
---|
166 | if ( lbc ( j ) < Lbc ( j ) ) { |
---|
167 | Lbc ( j ) = lbc ( j ); |
---|
168 | } |
---|
169 | if ( ubc ( j ) > Ubc ( j ) ) { |
---|
170 | Ubc ( j ) = ubc ( j ); |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|
174 | lb = concat ( lbp, Lbc ); |
---|
175 | ub = concat ( ubp, Ubc ); |
---|
176 | } |
---|
177 | |
---|
178 | vec sample() const { |
---|
179 | bdm_error ( "Not implemented" ); |
---|
180 | return vec(); |
---|
181 | } |
---|
182 | |
---|
183 | double evallog ( const vec &val ) const { |
---|
184 | bdm_error ( "not implemented" ); |
---|
185 | return 0.0; |
---|
186 | } |
---|
187 | }; |
---|
188 | |
---|
189 | //! Density joining PF.est with conditional parts |
---|
190 | mpfepdf jest; |
---|
191 | |
---|
192 | //! Log means of BMs |
---|
193 | bool opt_L_mea; |
---|
194 | |
---|
195 | public: |
---|
196 | //! Default constructor. |
---|
197 | MPF () : PF (), jest ( est ) {}; |
---|
198 | void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm = SYSTEMATIC ) { |
---|
199 | PF::set_parameters ( par0, obs0, n0, rm ); |
---|
200 | jest.set_parameters ( n0 );//duplication of rm |
---|
201 | BMs.set_length ( n0 ); |
---|
202 | } |
---|
203 | void set_statistics ( const epdf &epdf0, const BM_T* BMcond0 ) { |
---|
204 | |
---|
205 | PF::set_statistics ( ones ( n ) / n, epdf0 ); |
---|
206 | // copy |
---|
207 | for ( int i = 0; i < n; i++ ) { |
---|
208 | BMs ( i ) = new BM_T ( *BMcond0 ); |
---|
209 | BMs ( i )->condition ( _samples ( i ) ); |
---|
210 | } |
---|
211 | |
---|
212 | jest.read_statistics ( BMs ); |
---|
213 | //options |
---|
214 | }; |
---|
215 | |
---|
216 | void bayes ( const vec &dt ); |
---|
217 | const epdf& posterior() const { |
---|
218 | return jest; |
---|
219 | } |
---|
220 | //! Set postrior of \c rvc to samples from epdf0. Statistics of BMs are not re-computed! Use only for initialization! |
---|
221 | /* void set_est ( const epdf& epdf0 ) { |
---|
222 | PF::set_est ( epdf0 ); // sample params in condition |
---|
223 | // copy conditions to BMs |
---|
224 | |
---|
225 | for ( int i=0;i<n;i++ ) {BMs(i)->condition ( _samples ( i ) );} |
---|
226 | }*/ |
---|
227 | void set_options ( const string &opt ) { |
---|
228 | PF::set_options ( opt ); |
---|
229 | opt_L_mea = ( opt.find ( "logmeans" ) != string::npos ); |
---|
230 | } |
---|
231 | |
---|
232 | //!Access function |
---|
233 | const BM* _BM ( int i ) { |
---|
234 | return BMs ( i ); |
---|
235 | } |
---|
236 | }; |
---|
237 | |
---|
238 | template<class BM_T> |
---|
239 | void MPF<BM_T>::bayes ( const vec &dt ) { |
---|
240 | int i; |
---|
241 | vec lls ( n ); |
---|
242 | vec llsP ( n ); |
---|
243 | ivec ind; |
---|
244 | double mlls = -std::numeric_limits<double>::infinity(); |
---|
245 | |
---|
246 | #pragma omp parallel for |
---|
247 | for ( i = 0; i < n; i++ ) { |
---|
248 | //generate new samples from paramater evolution model; |
---|
249 | vec old_smp=_samples ( i ); |
---|
250 | _samples ( i ) = par->samplecond ( old_smp ); |
---|
251 | llsP ( i ) = par->evallogcond ( _samples ( i ), old_smp ); |
---|
252 | BMs ( i )->condition ( _samples ( i ) ); |
---|
253 | BMs ( i )->bayes ( dt ); |
---|
254 | lls ( i ) = BMs ( i )->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!! |
---|
255 | if ( lls ( i ) > mlls ) mlls = lls ( i ); //find maximum likelihood (for numerical stability) |
---|
256 | } |
---|
257 | |
---|
258 | double sum_w = 0.0; |
---|
259 | // compute weights |
---|
260 | #pragma omp parallel for |
---|
261 | for ( i = 0; i < n; i++ ) { |
---|
262 | _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood |
---|
263 | sum_w += _w ( i ); |
---|
264 | } |
---|
265 | |
---|
266 | if ( sum_w > 0.0 ) { |
---|
267 | _w /= sum_w; //? |
---|
268 | } else { |
---|
269 | cout << "sum(w)==0" << endl; |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | double eff = 1.0 / ( _w * _w ); |
---|
274 | if ( eff < ( 0.3*n ) ) { |
---|
275 | ind = est.resample ( resmethod ); |
---|
276 | // Resample Bms! |
---|
277 | |
---|
278 | #pragma omp parallel for |
---|
279 | for ( i = 0; i < n; i++ ) { |
---|
280 | if ( ind ( i ) != i ) {//replace the current Bm by a new one |
---|
281 | //fixme this would require new assignment operator |
---|
282 | // *Bms[i] = *Bms[ind ( i ) ]; |
---|
283 | |
---|
284 | // poor-man's solution: replicate constructor here |
---|
285 | // copied from MPF::MPF |
---|
286 | delete BMs ( i ); |
---|
287 | BMs ( i ) = new BM_T ( *BMs ( ind ( i ) ) ); //copy constructor |
---|
288 | const epdf& pom = BMs ( i )->posterior(); |
---|
289 | jest.set_elements ( i, 1.0 / n, &pom ); |
---|
290 | } |
---|
291 | }; |
---|
292 | cout << '.'; |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | } |
---|
297 | #endif // KF_H |
---|
298 | |
---|