ssmkit  master-68aed98
kalman.hpp
Go to the documentation of this file.
1 
7 #ifndef SSMPACK_FILTER_KALMAN_HPP
8 #define SSMPACK_FILTER_KALMAN_HPP
9 
16 #include <armadillo>
17 
18 namespace ssmkit {
19 namespace filter {
20 
22 using process::Markov;
26 
29 template <class STA_MAP, class OBS_MAP>
30 class Kalman
31  : public RecursiveBayesianBase<Kalman<STA_MAP, OBS_MAP>> {
32 
33  public:
35  using TProcess =
36  Hierarchical<Markov<Gaussian, STA_MAP, Gaussian>,
37  Memoryless<Gaussian, OBS_MAP>>;
39  using TCompeleteState =
40  std::tuple<arma::vec, arma::mat>;
41 
42  private:
44  TProcess process_;
46  const arma::mat &dyn_mat_;
48  const arma::mat &mes_mat_;
50  const arma::mat &dyn_cov_;
52  const arma::mat &mes_cov_;
54  arma::vec state_vec_;
56  arma::mat state_cov_;
58  arma::vec p_state_vec_;
60  arma::mat p_state_cov_;
61 
62  public:
67  Kalman(const TProcess &process)
68  : process_(process),
69  dyn_mat_(
70  process_.template getProcess<0>().getCPDF().getParamMap().transfer),
71  mes_mat_(
72  process_.template getProcess<1>().getCPDF().getParamMap().transfer),
73  dyn_cov_(process_.template getProcess<0>()
74  .getCPDF()
75  .getParamMap()
76  .covariance),
77  mes_cov_(process_.template getProcess<1>()
78  .getCPDF()
79  .getParamMap()
80  .covariance) {}
81 
82 
93  template <class... TArgs>
94  void predict(const TArgs &... args) {
95  // use the map function to pass controls, avoiding control definition
96  // is move used here??
97  p_state_vec_ =
98  std::get<0>(process_.template getProcess<0>().getCPDF().getParamMap()(
99  state_vec_, args...));
100  p_state_cov_ = dyn_mat_ * state_cov_ * dyn_mat_.t() + dyn_cov_;
101  }
102 
117  template <class... TArgs>
118  TCompeleteState correct(const arma::vec &measurement,
119  const TArgs &... args) {
120  arma::vec inovation =
121  measurement -
122  std::get<0>(process_.template getProcess<1>().getCPDF().getParamMap()(
123  p_state_vec_, args...));
124 
125  arma::mat inovation_cov = mes_mat_ * p_state_cov_ * mes_mat_.t() + mes_cov_;
126  arma::mat kalman_gain =
127  p_state_cov_ * mes_mat_.t() * arma::inv_sympd(inovation_cov);
128 
129  state_vec_ = p_state_vec_ + kalman_gain * inovation;
130  state_cov_ = p_state_cov_ - kalman_gain * mes_mat_ * p_state_cov_;
131  return std::make_tuple(state_vec_, state_cov_);
132  }
138  state_vec_ = process_.template getProcess<0>().getInitialPDF().getMean();
139  state_cov_ =
140  process_.template getProcess<0>().getInitialPDF().getCovariance();
141  return std::make_tuple(state_vec_, state_cov_);
142  }
143 };
144 
145 template <class STA_MAP, class OBS_MAP>
147  Hierarchical<Markov<Gaussian, STA_MAP, Gaussian>,
148  Memoryless<Gaussian, OBS_MAP>> process) {
149  return Kalman<STA_MAP, OBS_MAP>(process);
150 }
151 
152 } // namespace filter
153 } // namespace ssmkit
154 
155 #endif // SSMPACK_FILTER_KALMAN_HPP
A stochastic process constructed as hierarchy of stochastic processes.
Kalman(const TProcess &process)
Construct a Kalman filter.
Definition: kalman.hpp:67
Conditional distribution function.
Definition: conditional.hpp:34
Kalman filter.
Definition: kalman.hpp:30
Kalman< STA_MAP, OBS_MAP > makeKalman(Hierarchical< Markov< Gaussian, STA_MAP, Gaussian >, Memoryless< Gaussian, OBS_MAP >> process)
Definition: kalman.hpp:146
A first-order Markov process.
Definition: markov.hpp:28
void predict(const TArgs &...args)
Prediction.
Definition: kalman.hpp:94
TCompeleteState initialize()
Initialization.
Definition: kalman.hpp:137
Hierarchical< Markov< Gaussian, STA_MAP, Gaussian >, Memoryless< Gaussian, OBS_MAP >> TProcess
Type of process object.
Definition: kalman.hpp:37
A Memoryless (independent/white) random process.
Definition: memoryless.hpp:28
A D-dimensional multivariate Gaussian distribution.
Definition: gaussian.hpp:24
TCompeleteState correct(const arma::vec &measurement, const TArgs &...args)
Correction.
Definition: kalman.hpp:118
std::tuple< arma::vec, arma::mat > TCompeleteState
Type of the posterior state .
Definition: kalman.hpp:40