|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "numerical/estimators/Estimator.hpp" |
| 4 | + |
| 5 | +namespace estimators |
| 6 | +{ |
| 7 | + enum class State |
| 8 | + { |
| 9 | + unstable, |
| 10 | + converged, |
| 11 | + }; |
| 12 | + |
| 13 | + template<typename T, std::size_t Features> |
| 14 | + class RecursiveLeastSquares |
| 15 | + : public OnlineEstimator<T, Features> |
| 16 | + { |
| 17 | + public: |
| 18 | + using CoefficientsMatrix = typename OnlineEstimator<T, Features>::CoefficientsMatrix; |
| 19 | + using DesignMatrix = typename OnlineEstimator<T, Features>::DesignMatrix; |
| 20 | + using InputMatrix = typename OnlineEstimator<T, Features>::InputMatrix; |
| 21 | + using EstimationMetrics = typename OnlineEstimator<T, Features>::EstimationMetrics; |
| 22 | + |
| 23 | + RecursiveLeastSquares(std::optional<T> initialCovariance, T forgettingFactor); |
| 24 | + |
| 25 | + EstimationMetrics Update(const InputMatrix& X, const math::Matrix<T, 1, 1>& y) override; |
| 26 | + const CoefficientsMatrix& Coefficients() const override; |
| 27 | + |
| 28 | + template<typename... Args> |
| 29 | + static InputMatrix& MakeRegressor(InputMatrix& regressor, Args&&... args); |
| 30 | + |
| 31 | + static State EvaluateConvergence(const EstimationMetrics& metrics, T innovationThreshold, T uncertaintyThreshold); |
| 32 | + |
| 33 | + private: |
| 34 | + CoefficientsMatrix theta; |
| 35 | + DesignMatrix covariance; |
| 36 | + T lambda; |
| 37 | + T lambdaInverse; |
| 38 | + EstimationMetrics metrics; |
| 39 | + }; |
| 40 | + |
| 41 | + // Implementation |
| 42 | + |
| 43 | + template<typename T, std::size_t Features> |
| 44 | + RecursiveLeastSquares<T, Features>::RecursiveLeastSquares(std::optional<T> initialCovariance, T forgettingFactor) |
| 45 | + : theta{} |
| 46 | + , covariance(initialCovariance.has_value() |
| 47 | + ? DesignMatrix::Identity() * initialCovariance.value() |
| 48 | + : DesignMatrix::Identity()) |
| 49 | + , lambda(forgettingFactor) |
| 50 | + , lambdaInverse(T(1) / lambda) |
| 51 | + { |
| 52 | + } |
| 53 | + |
| 54 | + template<typename T, std::size_t Features> |
| 55 | + typename RecursiveLeastSquares<T, Features>::EstimationMetrics RecursiveLeastSquares<T, Features>::Update(const InputMatrix& x, const math::Matrix<T, 1, 1>& y) |
| 56 | + { |
| 57 | + auto Px = covariance * x; |
| 58 | + auto denominatorInv = T(1) / (lambda + (x.Transpose() * Px).at(0, 0)); |
| 59 | + auto error = y.at(0, 0) - (x.Transpose() * theta).at(0, 0); |
| 60 | + theta += Px * (error * denominatorInv); |
| 61 | + covariance = (covariance - Px * Px.Transpose() * denominatorInv) * lambdaInverse; |
| 62 | + |
| 63 | + metrics.innovation = error; |
| 64 | + metrics.residual = y.at(0, 0) - (x.Transpose() * theta).at(0, 0); |
| 65 | + metrics.uncertainty = covariance.Trace(); |
| 66 | + |
| 67 | + return metrics; |
| 68 | + } |
| 69 | + |
| 70 | + template<typename T, std::size_t Features> |
| 71 | + const typename RecursiveLeastSquares<T, Features>::CoefficientsMatrix& RecursiveLeastSquares<T, Features>::Coefficients() const |
| 72 | + { |
| 73 | + return theta; |
| 74 | + } |
| 75 | + |
| 76 | + template<typename T, std::size_t Features> |
| 77 | + template<typename... Args> |
| 78 | + typename RecursiveLeastSquares<T, Features>::InputMatrix& RecursiveLeastSquares<T, Features>::MakeRegressor(InputMatrix& regressor, Args&&... args) |
| 79 | + { |
| 80 | + static_assert(sizeof...(Args) == Features - 1, "Number of arguments must match Features - 1"); |
| 81 | + |
| 82 | + regressor.at(0, 0) = T(1); |
| 83 | + std::size_t i = 1; |
| 84 | + ((regressor.at(i++, 0) = std::forward<Args>(args)), ...); |
| 85 | + return regressor; |
| 86 | + } |
| 87 | + |
| 88 | + template<typename T, std::size_t Features> |
| 89 | + State RecursiveLeastSquares<T, Features>::EvaluateConvergence(const EstimationMetrics& metrics, T innovationThreshold, T uncertaintyThreshold) |
| 90 | + { |
| 91 | + if (std::abs(metrics.innovation) < innovationThreshold && metrics.uncertainty < uncertaintyThreshold) |
| 92 | + return State::converged; |
| 93 | + else |
| 94 | + return State::unstable; |
| 95 | + } |
| 96 | +} |
0 commit comments