Skip to content

Commit 0ed488b

Browse files
feat: add rls (#80)
* add rls * merge * remove yule walker example * add convergence test
1 parent 1996f83 commit 0ed488b

15 files changed

Lines changed: 444 additions & 473 deletions

‎examples/estimators/CMakeLists.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
add_subdirectory(LinearRegression)
2-
add_subdirectory(YuleWalker)

‎examples/estimators/YuleWalker/CMakeLists.txt‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎examples/estimators/YuleWalker/YuleWalker.cpp‎

Lines changed: 0 additions & 174 deletions
This file was deleted.

‎numerical/estimators/CMakeLists.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ target_sources(numerical.estimators PRIVATE
1414
Estimator.hpp
1515
LinearRegression.cpp
1616
LinearRegression.hpp
17-
YuleWalker.hpp
17+
RecursiveLeastSquares.cpp
18+
RecursiveLeastSquares.hpp
1819
)
1920

2021
add_subdirectory(test)

‎numerical/estimators/Estimator.hpp‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
#ifndef ESTIMATORS_ESTIMATOR_HPP
2-
#define ESTIMATORS_ESTIMATOR_HPP
1+
#pragma once
32

43
#include "numerical/math/Matrix.hpp"
54

65
namespace estimators
76
{
87
template<typename T, std::size_t Samples, std::size_t Features>
9-
class Estimator
8+
class OfflineEstimator
109
{
1110
static_assert(math::detail::is_supported_type_v<T>,
12-
"Estimator only supports float or QNumber types");
11+
"OfflineEstimator only supports float or QNumber types");
1312

1413
public:
1514
using CoefficientsMatrix = math::Matrix<T, Features + 1, 1>;
@@ -20,6 +19,26 @@ namespace estimators
2019
virtual T Predict(const InputMatrix& X) const = 0;
2120
virtual const CoefficientsMatrix& Coefficients() const = 0;
2221
};
23-
}
2422

25-
#endif
23+
template<typename T, std::size_t Features>
24+
class OnlineEstimator
25+
{
26+
static_assert(math::detail::is_supported_type_v<T>,
27+
"OnlineEstimator only supports float or QNumber types");
28+
29+
public:
30+
using CoefficientsMatrix = math::Matrix<T, Features, 1>;
31+
using DesignMatrix = math::Matrix<T, Features, Features>;
32+
using InputMatrix = math::Matrix<T, Features, 1>;
33+
34+
struct EstimationMetrics
35+
{
36+
T innovation; // The error BEFORE the update (how surprised we were)
37+
T residual; // The error AFTER the update (how well we matched it)
38+
T uncertainty; // The 'Trace' or 'Norm' of P (how confident we are)
39+
};
40+
41+
virtual EstimationMetrics Update(const InputMatrix& x, const math::Matrix<T, 1, 1>& y) = 0;
42+
virtual const CoefficientsMatrix& Coefficients() const = 0;
43+
};
44+
}

‎numerical/estimators/LinearRegression.hpp‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
#ifndef ESTIMATORS_LINEAR_REGRESSION_HPP
2-
#define ESTIMATORS_LINEAR_REGRESSION_HPP
1+
#pragma once
32

43
#include "numerical/estimators/Estimator.hpp"
54

65
namespace estimators
76
{
87
template<typename T, std::size_t Samples, std::size_t Features>
98
class LinearRegression
10-
: public Estimator<T, Samples, Features>
9+
: public OfflineEstimator<T, Samples, Features>
1110
{
1211
public:
13-
using CoefficientsMatrix = typename Estimator<T, Samples, Features>::CoefficientsMatrix;
14-
using DesignMatrix = typename Estimator<T, Samples, Features>::DesignMatrix;
15-
using InputMatrix = typename Estimator<T, Samples, Features>::InputMatrix;
12+
using CoefficientsMatrix = typename OfflineEstimator<T, Samples, Features>::CoefficientsMatrix;
13+
using DesignMatrix = typename OfflineEstimator<T, Samples, Features>::DesignMatrix;
14+
using InputMatrix = typename OfflineEstimator<T, Samples, Features>::InputMatrix;
1615

1716
LinearRegression() = default;
1817

@@ -112,5 +111,3 @@ namespace estimators
112111
return x;
113112
}
114113
}
115-
116-
#endif

‎numerical/estimators/RecursiveLeastSquares.cpp‎

Whitespace-only changes.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)