0.2.0
Loading...
Searching...
No Matches
KeyedLeastSquares.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
14
15#pragma once
16
18
19namespace NAV
20{
22template<typename Scalar, typename StateKeyType>
28
35template<typename Scalar, typename StateKeyType, typename MeasKeyType>
37{
38 KeyedVectorX<Scalar, StateKeyType> dx = KeyedVectorX<Scalar, StateKeyType>(Eigen::VectorX<Scalar>::Zero(H.cols()), H.colKeys());
39 dx(all) = (H(all, all).transpose() * H(all, all)).inverse() * H(all, all).transpose() * dz(all);
40 return dx;
41}
42
48template<typename Scalar, typename StateKeyType, typename MeasKeyType>
50{
51 KeyedVectorX<Scalar, StateKeyType> dx = KeyedVectorX<Scalar, StateKeyType>(Eigen::VectorX<Scalar>::Zero(H.cols()), H.colKeys());
52 dx(all) = (H(all, all).transpose() * W(all, all) * H(all, all)).inverse() * H(all, all).transpose() * W(all, all) * dz(all);
53 return dx;
54}
55
60template<typename Scalar, typename StateKeyType, typename MeasKeyType>
61KeyedLeastSquaresResult<Scalar, StateKeyType>
63{
64 // Amount of equations
65 auto m = static_cast<int>(H.rows());
66 // Amount of variables
67 auto n = static_cast<int>(H.cols());
68
69 // Cofactor matrix
71 Q(all, all) = (H(all, all).transpose() * H(all, all)).inverse();
72 LOG_DATA("Q = \n{}", Q(all, all));
73 // Least squares solution
74 KeyedVectorX<Scalar, StateKeyType> dx = KeyedVectorX<Scalar, StateKeyType>(Eigen::VectorX<Scalar>::Zero(n), H.colKeys());
75 dx(all) = Q(all, all) * H(all, all).transpose() * dz(all);
76 LOG_DATA("dx = {}", dx(all).transpose());
77
78 // Residual sum of squares
79 double RSS = std::pow(dz(all).norm(), 2);
80 LOG_DATA("RSS = {}", RSS);
81
82 // Statistical degrees of freedom
83 auto dof = m - n;
84 LOG_DATA("dof = {}", dof);
85
86 // Estimated error variance (reduced chi-squared statistic)
87 double sigma2 = RSS / static_cast<double>(dof);
88 LOG_DATA("sigma2 = {}", sigma2);
89
90 // Covariance matrix
91 Q(all, all) *= sigma2;
92 LOG_DATA("variance = \n{}", Q(all, all));
93
94 return { .solution = dx, .variance = Q };
95}
96
102template<typename Scalar, typename StateKeyType, typename MeasKeyType>
103KeyedLeastSquaresResult<Scalar, StateKeyType>
105{
106 // Amount of equations
107 auto m = static_cast<int>(H.rows());
108 // Amount of variables
109 auto n = static_cast<int>(H.cols());
110
111 // Cofactor matrix
113 Q(all, all) = (H(all, all).transpose() * W(all, all) * H(all, all)).inverse();
114 LOG_DATA("Q = \n{}", Q(all, all));
115
116 // Least squares solution
117 KeyedVectorX<Scalar, StateKeyType> dx = KeyedVectorX<Scalar, StateKeyType>(Eigen::VectorX<Scalar>::Zero(n), H.colKeys());
118 dx(all) = Q(all, all) * H(all, all).transpose() * W(all, all) * dz(all);
119 LOG_DATA("dx = {}", dx(all).transpose());
120
121 // Residual sum of squares
122 double RSS = dz(all).transpose() * W(all, all) * dz(all);
123 LOG_DATA("RSS = {}", RSS);
124
125 // Statistical degrees of freedom
126 auto dof = m - n;
127 LOG_DATA("dof = {}", dof);
128
129 // Estimated error variance (reduced chi-squared statistic)
130 double sigma2 = RSS / static_cast<double>(dof);
131 LOG_DATA("sigma2 = {}", sigma2);
132
133 // Covariance matrix
134 Q(all, all) *= sigma2;
135 LOG_DATA("Covariance matrix = \n{}", Q(all, all));
136
137 return { .solution = dx, .variance = Q };
138}
139
140} // namespace NAV
KeyedLeastSquaresResult< Scalar, StateKeyType > solveLinearLeastSquaresUncertainties(const KeyedMatrixX< Scalar, MeasKeyType, StateKeyType > &H, const KeyedVectorX< Scalar, MeasKeyType > &dz)
Finds the "least squares" solution for the equation .
Definition KeyedLeastSquares.hpp:62
KeyedVectorX< Scalar, StateKeyType > solveLinearLeastSquares(const KeyedMatrixX< Scalar, MeasKeyType, StateKeyType > &H, const KeyedVectorX< Scalar, MeasKeyType > &dz)
Finds the "least squares" solution for the equation .
Definition KeyedLeastSquares.hpp:36
KeyedVectorX< Scalar, StateKeyType > solveWeightedLinearLeastSquares(const KeyedMatrixX< Scalar, MeasKeyType, StateKeyType > &H, const KeyedMatrixX< Scalar, MeasKeyType, MeasKeyType > &W, const KeyedVectorX< Scalar, MeasKeyType > &dz)
Finds the "weighted least squares" solution (see LeastSquares.hpp)
Definition KeyedLeastSquares.hpp:49
KeyedLeastSquaresResult< Scalar, StateKeyType > solveWeightedLinearLeastSquaresUncertainties(const KeyedMatrixX< Scalar, MeasKeyType, StateKeyType > &H, const KeyedMatrixX< Scalar, MeasKeyType, MeasKeyType > &W, const KeyedVectorX< Scalar, MeasKeyType > &dz)
Finds the "weighted least squares" solution.
Definition KeyedLeastSquares.hpp:104
#define LOG_DATA
All output which occurs repeatedly every time observations are received.
Definition Logger.hpp:29
Dynamic sized KeyedMatrix.
Definition KeyedMatrix.hpp:2055
Dynamic sized KeyedVector.
Definition KeyedMatrix.hpp:1569
const std::vector< ColKeyType > & colKeys() const
Returns the col keys.
Definition KeyedMatrix.hpp:221
decltype(auto) cols() const
Return the cols of the underlying Eigen matrix.
Definition KeyedMatrix.hpp:218
decltype(auto) rows() const
Return the rows of the underlying Eigen matrix.
Definition KeyedMatrix.hpp:74
Least Squares Uncertainties return value.
Definition KeyedLeastSquares.hpp:24
KeyedMatrixX< Scalar, StateKeyType, StateKeyType > variance
Least squares variance.
Definition KeyedLeastSquares.hpp:26
KeyedVectorX< Scalar, StateKeyType > solution
Least squares solution.
Definition KeyedLeastSquares.hpp:25
Matrix which can be accessed by keys.