hops
MultivariateGaussian.hpp
Go to the documentation of this file.
1 #ifndef HOPS_MULTIVARIATEGAUSSIAN_HPP
2 #define HOPS_MULTIVARIATEGAUSSIAN_HPP
3 
4 #define _USE_MATH_DEFINES
5 #include <math.h> // Using deprecated math for windows
6 #include <Eigen/Cholesky>
7 #include <Eigen/Core>
8 #include <Eigen/LU>
9 #include <utility>
10 #include <hops/Model/Model.hpp>
11 
12 namespace hops {
13  class MultivariateGaussian : public Model {
14  public:
15  MultivariateGaussian(VectorType mean, MatrixType covariance);
16 
22  [[nodiscard]] MatrixType::Scalar computeNegativeLogLikelihood(const VectorType &x) const override;
23 
24  [[nodiscard]] std::optional<VectorType> computeLogLikelihoodGradient(const VectorType &x) const override;
25 
26  [[nodiscard]] std::optional<MatrixType> computeExpectedFisherInformation(const VectorType &) const override;
27 
28  [[nodiscard]] const VectorType &getMean() const;
29 
30  [[nodiscard]] const MatrixType &getCovariance() const;
31 
32  private:
33  VectorType mean;
34  MatrixType covariance;
35  MatrixType inverseCovariance;
36  typename MatrixType::Scalar logNormalizationConstant;
37  };
38 
40  mean(std::move(mean)),
41  covariance(std::move(covariance)) {
42  Eigen::LLT<MatrixType> solver(this->covariance);
43  if (!this->covariance.isApprox(this->covariance.transpose()) || solver.info() == Eigen::NumericalIssue) {
44  throw std::domain_error(
45  "Possibly non semi-positive definite covariance in initialization for MultivariateGaussian.");
46  }
47  Eigen::Matrix<typename MatrixType::Scalar, Eigen::Dynamic, Eigen::Dynamic> matrixL = solver.matrixL();
48  Eigen::Matrix<typename MatrixType::Scalar, Eigen::Dynamic, Eigen::Dynamic> inverseMatrixL = matrixL.inverse();
49  inverseCovariance = inverseMatrixL.transpose() * inverseMatrixL;
50 
51  logNormalizationConstant = -static_cast<typename MatrixType::Scalar>(this->mean.rows()) / 2 *
52  std::log(2 * M_PI)
53  - matrixL.diagonal().array().log().sum();
54  }
55 
56  typename MatrixType::Scalar
58  return -logNormalizationConstant +
59  0.5 * static_cast<typename MatrixType::Scalar>((x - mean).transpose() * inverseCovariance * (x - mean));
60  }
61 
62  std::optional<VectorType> MultivariateGaussian::computeLogLikelihoodGradient(const VectorType &x) const {
63  return -inverseCovariance * (x - mean);
64  }
65 
66  std::optional<MatrixType> MultivariateGaussian::computeExpectedFisherInformation(const VectorType &) const {
67  return inverseCovariance;
68  }
69 
71  return mean;
72  }
73 
75  return covariance;
76  }
77 }
78 
79 #endif //HOPS_MULTIVARIATEGAUSSIAN_HPP
hops::MultivariateGaussian::computeNegativeLogLikelihood
MatrixType::Scalar computeNegativeLogLikelihood(const VectorType &x) const override
Evaluates the negative log likelihood for input x.
Definition: MultivariateGaussian.hpp:57
hops::MatrixType
Eigen::MatrixXd MatrixType
Definition: MatrixType.hpp:7
hops::MultivariateGaussian
Definition: MultivariateGaussian.hpp:13
Model.hpp
hops
Definition: CsvReader.hpp:8
hops::MultivariateGaussian::getCovariance
const MatrixType & getCovariance() const
Definition: MultivariateGaussian.hpp:74
hops::MultivariateGaussian::computeLogLikelihoodGradient
std::optional< VectorType > computeLogLikelihoodGradient(const VectorType &x) const override
Definition: MultivariateGaussian.hpp:62
hops::MultivariateGaussian::getMean
const VectorType & getMean() const
Definition: MultivariateGaussian.hpp:70
hops::MultivariateGaussian::MultivariateGaussian
MultivariateGaussian(VectorType mean, MatrixType covariance)
Definition: MultivariateGaussian.hpp:39
hops::Model
Definition: Model.hpp:12
hops::VectorType
Eigen::VectorXd VectorType
Definition: VectorType.hpp:7
hops::MultivariateGaussian::computeExpectedFisherInformation
std::optional< MatrixType > computeExpectedFisherInformation(const VectorType &) const override
Definition: MultivariateGaussian.hpp:66