00001 #ifndef _BAYES_FILTER_SIR
00002 #define _BAYES_FILTER_SIR
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "bayesFlt.hpp"
00043
00044
00045 namespace Bayesian_filter
00046 {
00047
00048 struct SIR_random
00049
00050
00051
00052
00053 {
00054 virtual void normal(FM::DenseVec& v) = 0;
00055 virtual void uniform_01(FM::DenseVec& v) = 0;
00056 virtual ~SIR_random () {}
00057 };
00058
00059
00060 class Importance_resampler : public Bayes_base
00061
00062
00063
00064
00065
00066 {
00067 public:
00068 typedef std::vector<std::size_t> Resamples_t;
00069
00070 virtual Float resample (Resamples_t& presamples, std::size_t& uresamples, FM::DenseVec& w, SIR_random& r) const = 0;
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 };
00089
00090 class Standard_resampler : public Importance_resampler
00091
00092 {
00093 public:
00094 Float resample (Resamples_t& presamples, std::size_t& uresamples, FM::DenseVec& w, SIR_random& r) const;
00095 };
00096
00097 class Systematic_resampler : public Importance_resampler
00098
00099 {
00100 public:
00101 Float resample (Resamples_t& presamples, std::size_t& uresamples, FM::DenseVec& w, SIR_random& r) const;
00102 };
00103
00104
00105 template <class Predict_model>
00106 class Sampled_general_predict_model: public Predict_model, public Sampled_predict_model
00107
00108
00109
00110
00111 {
00112 public:
00113 Sampled_general_predict_model (std::size_t x_size, std::size_t q_size, SIR_random& random_helper) :
00114 Predict_model(x_size, q_size),
00115 Sampled_predict_model(),
00116 genn(random_helper),
00117 xp(x_size),
00118 n(q_size), rootq(q_size)
00119 {
00120 first_init = true;
00121 }
00122
00123 virtual const FM::Vec& fw(const FM::Vec& x) const
00124
00125
00126
00127
00128
00129 {
00130 if (first_init)
00131 init_GqG();
00132
00133 xp = Predict_model::f(x);
00134
00135 genn.normal(n);
00136
00137 for (FM::DenseVec::iterator ni = n.begin(); ni != n.end(); ++ni) {
00138 *ni *= rootq[ni.index()];
00139 }
00140 FM::noalias(xp) += FM::prod(this->G,n);
00141 return xp;
00142 }
00143
00144 void init_GqG() const
00145
00146
00147
00148 {
00149 first_init = false;
00150 for (FM::Vec::const_iterator qi = this->q.begin(); qi != this->q.end(); ++qi) {
00151 if (*qi < 0)
00152 error (Numeric_exception("Negative q in init_GqG"));
00153 rootq[qi.index()] = std::sqrt(*qi);
00154 }
00155 }
00156 private:
00157 SIR_random& genn;
00158 mutable FM::Vec xp;
00159 mutable FM::DenseVec n;
00160 mutable FM::Vec rootq;
00161 mutable bool first_init;
00162 };
00163
00164 typedef Sampled_general_predict_model<Linear_predict_model> Sampled_LiAd_predict_model;
00165 typedef Sampled_general_predict_model<Linear_invertable_predict_model> Sampled_LiInAd_predict_model;
00166
00167
00168
00169
00170
00171 class SIR_scheme : public Sample_filter
00172
00173
00174
00175
00176
00177
00178
00179 {
00180 friend class SIR_kalman_scheme;
00181 public:
00182 std::size_t stochastic_samples;
00183
00184 SIR_scheme (std::size_t x_size, std::size_t s_size, SIR_random& random_helper);
00185 SIR_scheme& operator= (const SIR_scheme&);
00186
00187
00188
00189 void init_S ();
00190
00191 Float update_resample ()
00192
00193 { return update_resample (Standard_resampler());
00194 }
00195
00196 virtual Float update_resample (const Importance_resampler& resampler);
00197
00198
00199
00200
00201 void predict (Functional_predict_model& f)
00202
00203 { Sample_filter::predict (f);
00204 }
00205 void predict (Sampled_predict_model& f);
00206
00207
00208 void observe (Likelihood_observe_model& h, const FM::Vec& z);
00209
00210
00211 void observe_likelihood (const FM::Vec& lw);
00212
00213
00214 Float rougheningK;
00215 virtual void roughen()
00216
00217 {
00218 if (rougheningK != 0)
00219 roughen_minmax (S, rougheningK);
00220 }
00221
00222 static void copy_resamples (FM::ColMatrix& P, const Importance_resampler::Resamples_t& presamples);
00223
00224
00225 SIR_random& random;
00226
00227 protected:
00228 void roughen_minmax (FM::ColMatrix& P, Float K) const;
00229 Importance_resampler::Resamples_t resamples;
00230 FM::DenseVec wir;
00231 bool wir_update;
00232 private:
00233 static const Float rougheningKinit;
00234 std::size_t x_size;
00235 };
00236
00237
00238 class SIR_kalman_scheme : public SIR_scheme, virtual public Kalman_state_filter
00239
00240
00241
00242
00243
00244 {
00245 public:
00246 SIR_kalman_scheme (std::size_t x_size, std::size_t s_size, SIR_random& random_helper);
00247
00248
00249
00250 void init ();
00251
00252 void update ()
00253
00254 { (void)SIR_scheme::update_resample();
00255 }
00256
00257 Float update_resample ()
00258
00259 { return SIR_scheme::update_resample();
00260 }
00261
00262 Float update_resample (const Importance_resampler& resampler);
00263
00264
00265 void update_statistics ();
00266
00267
00268 void roughen()
00269 {
00270 if (rougheningK != 0)
00271 roughen_correlated (S, rougheningK);
00272 }
00273
00274 protected:
00275 void roughen_correlated (FM::ColMatrix& P, Float K);
00276 Sampled_LiAd_predict_model roughen_model;
00277 private:
00278 static Float scaled_vector_square(const FM::Vec& v, const FM::SymMatrix& S);
00279 void mean();
00280 };
00281
00282
00283 }
00284 #endif