ssmkit  master-68aed98
gaussian.hpp
Go to the documentation of this file.
1 
7 #ifndef SSMPACK_DISTRIBUTION_GAUSSIAN_HPP
8 #define SSMPACK_DISTRIBUTION_GAUSSIAN_HPP
9 
11 
12 #include <armadillo>
13 
14 #include <cmath>
15 
16 namespace ssmkit {
17 namespace distribution {
18 
24 class Gaussian {
25 
26  public:
28  using TParameterVAR = std::tuple<arma::vec, arma::mat>;
29 
30  private:
32  arma::vec mean_;
34  arma::mat covariance_;
36  std::normal_distribution<double> normal_;
38  static constexpr double pi = 3.1415926535897;
40  arma::mat inv_cov_;
42  double part_;
44  arma::mat chol_dec_;
46  int dim_;
47 
48  private:
53  void calcDistConstants() {
54  dim_ = mean_.n_rows;
55  inv_cov_ = arma::inv(covariance_);
56  // calculate partition function
57  double det_cov = arma::det(covariance_);
58  double den_pi = 1 / std::sqrt(std::pow(2 * pi, dim_));
59  part_ = den_pi * (1 / std::sqrt(det_cov));
60  // Cholesky decomposition
61  chol_dec_ = arma::chol(covariance_, "lower");
62  }
63 
64  public:
65  Gaussian() = delete;
70  Gaussian(int dim) : Gaussian(arma::zeros(dim), arma::eye(dim, dim)) {}
71 
79  Gaussian(arma::vec mean, arma::mat covariance)
80  : mean_(std::move(mean)), covariance_(std::move(covariance)) {
81  calcDistConstants();
82  }
83 
88  arma::vec random() {
89  arma::vec rnd(dim_); // how much overload the vector construction has?
90  rnd.imbue(
91  [&]() { return normal_(random::Generator::get().getGenerator()); });
92  return mean_ + chol_dec_ * rnd;
93  }
94 
101  double likelihood(const arma::vec &rv) const {
102  const auto diff = rv - mean_;
103  const arma::vec tmp = diff.t() * inv_cov_ * diff;
104  const double expt = -tmp(0) / 2;
105  return part_ * std::exp(expt);
106  }
107 
113  Gaussian &parameterize(const TParameterVAR &parameters) {
114  return parameterize(std::get<0>(parameters), std::get<1>(parameters));
115  }
116 
122  Gaussian &parameterize(const arma::vec &mean,
123  const arma::mat &covariance) {
124  mean_ = mean;
125  covariance_ = covariance;
126  calcDistConstants();
127  return (*this);
128  }
130  const arma::vec& getMean() const { return mean_; }
132  const arma::mat& getCovariance() const { return covariance_; }
133 };
134 
135 } // namespace distribution
136 } // namespace ssmkit
137 
138 #endif // SSMPACK_DISTRIBUTION_GAUSSIAN_HPP
const arma::mat & getCovariance() const
Returns the covariance matrix.
Definition: gaussian.hpp:132
arma::vec random()
Returns a random variable from the distribution.
Definition: gaussian.hpp:88
static Generator & get()
Returns a reference to singleton instance.
Definition: generator.hpp:38
double likelihood(const arma::vec &rv) const
Returns the likelihood of a given random variable.
Definition: gaussian.hpp:101
Gaussian & parameterize(const arma::vec &mean, const arma::mat &covariance)
Changes the mean and covariance of the distribution with the given values.
Definition: gaussian.hpp:122
std::tuple< arma::vec, arma::mat > TParameterVAR
Data type of the parameter variable .
Definition: gaussian.hpp:28
const arma::vec & getMean() const
Returns the mean vector.
Definition: gaussian.hpp:130
A D-dimensional multivariate Gaussian distribution.
Definition: gaussian.hpp:24
Gaussian(int dim)
Default constructor.
Definition: gaussian.hpp:70
Gaussian(arma::vec mean, arma::mat covariance)
Returns D dimensional Gaussian with given mean and covariance.
Definition: gaussian.hpp:79
Gaussian & parameterize(const TParameterVAR &parameters)
Changes the mean and covariance of the distribution with the given parameters.
Definition: gaussian.hpp:113