ssmkit  master-68aed98
particle.hpp
Go to the documentation of this file.
1 
7 #ifndef SSMPACK_FILTER_PARTICLE_HPP
8 #define SSMPACK_FILTER_PARTICLE_HPP
9 
15 #include <armadillo>
16 
17 #include <tuple>
18 
19 namespace ssmkit {
20 namespace filter {
21 
22 using process::Hierarchical;
23 using process::Markov;
24 using process::Memoryless;
25 using distribution::Conditional;
26 
29 template <class Process, class Resampler>
30 class Particle : public RecursiveBayesianBase<Particle<Process, Resampler>> {
31  public:
36  using CompeleteState = std::tuple<arma::mat, arma::vec>;
37 
38  private:
40  arma::vec w_;
42  arma::mat state_par_;
44  Process process_;
46  Resampler resampler_;
48  unsigned long num_;
49 
50  private:
52  void normalizeWeights(void) { w_ = w_ / arma::sum(w_); }
53 
54  public:
63  Particle(Process process, Resampler resampler, unsigned long particles_num)
64  : process_{process}, resampler_{resampler}, num_{particles_num} {
65  // initialized w_ and state_par_
66  w_.resize(num_);
67  // take one sample to find out dimension
68  auto tmp = process_.template getProcess<0>().getInitialPDF().random();
69  state_par_.resize(tmp.size(), num_);
70  }
79  template <class... Args>
80  void predict(const Args &... args) {
81  state_par_.each_col([this, &args...](arma::vec &v, const Args &... args) {
82  v = process_.template getProcess<0>().getCPDF().random(v, args...);
83  });
84  }
98  template <class Measurement, class... TArgs>
99  CompeleteState correct(const Measurement &measurement,
100  const TArgs &... args) {
101  unsigned long cnt = 0;
102  w_.for_each([this, &cnt, &measurement, &args...](double &e) {
103  e *= process_.template getProcess<1>().getCPDF().likelihood(
104  measurement, state_par_.col(cnt++), args...);
105  });
106 
107  normalizeWeights();
108 
109  resampler_(state_par_, w_);
110 
111  return std::make_tuple(state_par_, w_);
112  }
118  state_par_.each_col([this](arma::vec &v) {
119  v = process_.template getProcess<0>().getInitialPDF().random();
120  });
121 
122  unsigned long cnt = 0;
123  w_.for_each([this, &cnt](double &e) {
124  e = process_.template getProcess<0>().getInitialPDF().likelihood(
125  state_par_.col(cnt++));
126  });
127 
128  normalizeWeights();
129 
130  return std::make_tuple(state_par_, w_);
131  }
133  const arma::vec &getWeights(void) const { return w_; }
135  const arma::mat &getStateParticles(void) const { return state_par_; }
136 };
137 
140 template <class StatePDF, class StateParamMap, class InitialPDF,
141  class MeasurementPDF, class MeasurementParamMap, class Resampler>
143  Hierarchical<Markov<StatePDF, StateParamMap, InitialPDF>,
144  Memoryless<MeasurementPDF, MeasurementParamMap>> process,
145  Resampler resampler, unsigned long particle_num) {
147  Memoryless<MeasurementPDF, MeasurementParamMap>>,
148  Resampler>(process, resampler, particle_num);
149 }
150 
151 } // namespace filter
152 } // namespace ssmkit
153 
154 #endif // SSMPACK_FILTER_PARTICLE_HPP
const arma::vec & getWeights(void) const
Definition: particle.hpp:133
std::tuple< arma::mat, arma::vec > CompeleteState
Type of the state posterior.
Definition: particle.hpp:36
Particle Filter.
Definition: particle.hpp:30
CompeleteState correct(const Measurement &measurement, const TArgs &...args)
Correction.
Definition: particle.hpp:99
void predict(const Args &...args)
Prediction.
Definition: particle.hpp:80
Particle(Process process, Resampler resampler, unsigned long particles_num)
Constructor.
Definition: particle.hpp:63
const arma::mat & getStateParticles(void) const
Definition: particle.hpp:135
CompeleteState initialize()
Initialization.
Definition: particle.hpp:117
auto makeParticle(Hierarchical< Markov< StatePDF, StateParamMap, InitialPDF >, Memoryless< MeasurementPDF, MeasurementParamMap >> process, Resampler resampler, unsigned long particle_num)
Definition: particle.hpp:142