Bayes Filters Library
GaussianLikelihood.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2019 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD 3-Clause license. See the accompanying LICENSE file for details.
6  */
7 
9 #include <BayesFilters/utils.h>
10 
11 using namespace bfl;
12 using namespace Eigen;
13 
14 
17 { }
18 
19 
20 GaussianLikelihood::GaussianLikelihood(const double scale_factor) noexcept :
21  scale_factor_(scale_factor)
22 { }
23 
24 
25 std::pair<bool, VectorXd> GaussianLikelihood::likelihood
26 (
27  const MeasurementModel& measurement_model,
28  const Ref<const MatrixXd>& pred_states
29 )
30 {
31  bool valid_measurements;
32  Data data_measurements;
33  std::tie(valid_measurements, data_measurements) = measurement_model.measure();
34 
35  MatrixXd measurements;
36  if (valid_measurements)
37  measurements = any::any_cast<MatrixXd&&>(std::move(data_measurements));
38  else
39  return std::make_pair(false, VectorXd::Zero(1));
40 
41 
42  bool valid_predicted_measurements;
43  Data data_predicted_measurements;
44  std::tie(valid_predicted_measurements, data_predicted_measurements) = measurement_model.predictedMeasure(pred_states);
45 
46  MatrixXd predicted_measurements;
47  if (valid_predicted_measurements)
48  predicted_measurements = any::any_cast<MatrixXd&&>(std::move(data_predicted_measurements));
49  else
50  return std::make_pair(false, VectorXd::Zero(1));
51 
52 
53  bool valid_innovation;
54  Data data_innovations;
55  std::tie(valid_innovation, data_innovations) = measurement_model.innovation(predicted_measurements, measurements);
56 
57  MatrixXd innovations;
58  if (valid_innovation)
59  innovations = any::any_cast<MatrixXd&&>(std::move(data_innovations));
60  else
61  return std::make_pair(false, VectorXd::Zero(1));
62 
63 
64  VectorXd likelihood(innovations.cols());
65  bool valid_covariance_matrix;
66  MatrixXd covariance_matrix;
67  std::tie(valid_covariance_matrix, covariance_matrix) = measurement_model.getNoiseCovarianceMatrix();
68 
69  if (!valid_covariance_matrix)
70  return std::make_pair(false, VectorXd::Zero(1));
71 
72  likelihood = scale_factor_ * utils::multivariate_gaussian_density(innovations, VectorXd::Zero(innovations.rows()), covariance_matrix);
73 
74  return std::make_pair(true, likelihood);
75 }
bfl::MeasurementModel::getNoiseCovarianceMatrix
virtual std::pair< bool, Eigen::MatrixXd > getNoiseCovarianceMatrix() const
Definition: MeasurementModel.cpp:16
GaussianLikelihood.h
bfl::MeasurementModel::predictedMeasure
virtual std::pair< bool, Data > predictedMeasure(const Eigen::Ref< const Eigen::MatrixXd > &cur_states) const =0
bfl::GaussianLikelihood::likelihood
std::pair< bool, Eigen::VectorXd > likelihood(const MeasurementModel &measurement_model, const Eigen::Ref< const Eigen::MatrixXd > &pred_states) override
Definition: GaussianLikelihood.cpp:26
bfl
Port of boost::any for C++11 compilers.
Definition: AdditiveMeasurementModel.h:13
bfl::MeasurementModel::innovation
virtual std::pair< bool, Data > innovation(const Data &predicted_measurements, const Data &measurements) const =0
utils.h
bfl::GaussianLikelihood::GaussianLikelihood
GaussianLikelihood() noexcept
Definition: GaussianLikelihood.cpp:15
bfl::utils::multivariate_gaussian_density
Eigen::VectorXd multivariate_gaussian_density(const Eigen::MatrixBase< Derived > &input, const Eigen::Ref< const Eigen::VectorXd > &mean, const Eigen::Ref< const Eigen::MatrixXd > &covariance)
Evaluate a multivariate Gaussian probability density function.
Definition: utils.h:419
bfl::GaussianLikelihood
Definition: GaussianLikelihood.h:18
bfl::MeasurementModel
This class represent a generic measurement model f(x, w), where x is a state vector and w is random n...
Definition: MeasurementModel.h:29
bfl::any::any
The class any describes a type-safe container for single values of any type.
Definition: any.h:77
bfl::MeasurementModel::measure
virtual std::pair< bool, Data > measure(const Data &data=Data()) const =0