0.2.0
Loading...
Searching...
No Matches
LeastSquares.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
13
14#pragma once
15
16#include <Eigen/Core>
17#include <Eigen/Dense>
18#include <unsupported/Eigen/MatrixFunctions>
19#include <cmath>
20#include "util/Logger.hpp"
21
22namespace NAV
23{
24
26template<typename SolutionVector, typename VarianceMatrix>
28{
29 SolutionVector solution;
30 VarianceMatrix variance;
31};
32
46template<typename DerivedA, typename DerivedB>
47Eigen::Vector<typename DerivedA::Scalar, DerivedA::ColsAtCompileTime> solveLinearLeastSquares(const Eigen::MatrixBase<DerivedA>& H, const Eigen::MatrixBase<DerivedB>& dz)
48{
49 return (H.transpose() * H).inverse() * H.transpose() * dz;
50}
51
61template<typename DerivedA, typename DerivedW, typename DerivedB>
62Eigen::Vector<typename DerivedA::Scalar, DerivedA::ColsAtCompileTime> solveWeightedLinearLeastSquares(const Eigen::MatrixBase<DerivedA>& H, const Eigen::MatrixBase<DerivedW>& W, const Eigen::MatrixBase<DerivedB>& dz)
63{
64 return (H.transpose() * W * H).inverse() * H.transpose() * W * dz;
65}
66
71template<typename DerivedA, typename DerivedB>
72LeastSquaresResult<Eigen::Vector<typename DerivedA::Scalar, DerivedA::ColsAtCompileTime>, Eigen::Matrix<typename DerivedA::Scalar, DerivedA::ColsAtCompileTime, DerivedA::ColsAtCompileTime>>
73 solveLinearLeastSquaresUncertainties(const Eigen::MatrixBase<DerivedA>& H, const Eigen::MatrixBase<DerivedB>& dz)
74{
75 // Cofactor matrix
76 Eigen::Matrix<typename DerivedA::Scalar, DerivedA::ColsAtCompileTime, DerivedA::ColsAtCompileTime> Q = (H.transpose() * H).inverse();
77 LOG_DATA("Q = \n{}", Q);
78 // Least squares solution
79 Eigen::Vector<typename DerivedA::Scalar, DerivedA::ColsAtCompileTime> dx = Q * H.transpose() * dz;
80 LOG_DATA("dx = {}", dx.transpose());
81
82 // Residual sum of squares
83 double RSS = std::pow(dz.norm(), 2);
84 LOG_DATA("RSS = {}", RSS);
85
86 // Amount of equations
87 auto m = H.rows();
88 // Amount of variables
89 auto n = H.cols();
90 // Statistical degrees of freedom
91 auto dof = m - n;
92 LOG_DATA("dof = {}", dof);
93
94 // Estimated error variance (reduced chi-squared statistic)
95 double sigma2 = RSS / static_cast<double>(dof);
96 LOG_DATA("sigma2 = {}", sigma2);
97
98 // Covariance matrix
99 Q *= sigma2;
100 LOG_DATA("variance = \n{}", Q);
101
102 return { .solution = dx, .variance = Q };
103}
104
110template<typename DerivedA, typename DerivedW, typename DerivedB>
111LeastSquaresResult<Eigen::Vector<typename DerivedA::Scalar, DerivedA::ColsAtCompileTime>, Eigen::Matrix<typename DerivedA::Scalar, DerivedA::ColsAtCompileTime, DerivedA::ColsAtCompileTime>>
112 solveWeightedLinearLeastSquaresUncertainties(const Eigen::MatrixBase<DerivedA>& H, const Eigen::MatrixBase<DerivedW>& W, const Eigen::MatrixBase<DerivedB>& dz)
113{
114 // Cofactor matrix
115 Eigen::Matrix<typename DerivedA::Scalar, DerivedA::ColsAtCompileTime, DerivedA::ColsAtCompileTime> Q = (H.transpose() * W * H).inverse();
116 LOG_DATA("Cofactor matrix = \n{}", Q);
117 // Least squares solution
118 Eigen::Vector<typename DerivedA::Scalar, DerivedA::ColsAtCompileTime> dx = Q * H.transpose() * W * dz;
119 LOG_DATA("dx = {}", dx.transpose());
120
121 // Residual sum of squares
122 double RSS = dz.transpose() * W * dz;
123 LOG_DATA("RSS = {}", RSS);
124
125 // Amount of equations
126 auto m = H.rows();
127 // Amount of variables
128 auto n = H.cols();
129 // Statistical degrees of freedom
130 auto dof = m - n;
131 LOG_DATA("dof = {}", dof);
132
133 // Estimated error variance (reduced chi-squared statistic)
134 double sigma2 = RSS / static_cast<double>(dof);
135 LOG_DATA("sigma2 = {}", sigma2);
136
137 // Covariance matrix
138 Q *= sigma2;
139 LOG_DATA("Covariance matrix = \n{}", Q);
140
141 return { .solution = dx, .variance = Q };
142}
143
144} // 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
Utility class for logging to console and file.
#define LOG_DATA
All output which occurs repeatedly every time observations are received.
Definition Logger.hpp:29
Least Squares Uncertainties return value.
Definition LeastSquares.hpp:28
VarianceMatrix variance
Least squares variance.
Definition LeastSquares.hpp:30
SolutionVector solution
Least squares solution.
Definition LeastSquares.hpp:29