Skip to content

Commit be78552

Browse files
authored
[sysid] Fix SSTO calculation (wpilibsuite#6301)
1 parent 3acae55 commit be78552

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

sysid/src/main/native/cpp/analysis/OLS.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ OLSResult OLS(const Eigen::MatrixXd& X, const Eigen::VectorXd& y) {
6060
//
6161
// SSTO = yᵀy - 1/n yᵀJy
6262
//
63-
// Let J = I.
64-
//
65-
// SSTO = yᵀy - 1/n yᵀy
66-
// SSTO = (n − 1)/n yᵀy
67-
double SSTO = (n - 1.0) / n * (y.transpose() * y).value();
63+
// where J is a matrix of ones.
64+
double SSTO =
65+
(y.transpose() * y - 1.0 / y.rows() * y.transpose() *
66+
Eigen::MatrixXd::Ones(y.rows(), y.rows()) * y)
67+
.value();
6868

6969
// R² or the coefficient of determination, which represents how much of the
7070
// total variation (variation in y) can be explained by the regression model

sysid/src/test/native/cpp/analysis/OLSTest.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ TEST(OLSTest, TwoVariablesTwoPoints) {
1414
auto [coeffs, rSquared, rmse] = sysid::OLS(X, y);
1515
EXPECT_EQ(coeffs.size(), 2u);
1616

17-
EXPECT_NEAR(coeffs[0], 1.0, 0.05);
18-
EXPECT_NEAR(coeffs[1], 2.0, 0.05);
19-
EXPECT_NEAR(rSquared, 1.0, 1e-4);
17+
EXPECT_DOUBLE_EQ(coeffs[0], 1.0);
18+
EXPECT_DOUBLE_EQ(coeffs[1], 2.0);
19+
EXPECT_DOUBLE_EQ(rSquared, 1.0);
2020
}
2121

2222
TEST(OLSTest, TwoVariablesFivePoints) {
@@ -28,9 +28,9 @@ TEST(OLSTest, TwoVariablesFivePoints) {
2828
auto [coeffs, rSquared, rmse] = sysid::OLS(X, y);
2929
EXPECT_EQ(coeffs.size(), 2u);
3030

31-
EXPECT_NEAR(coeffs[0], 0.305, 0.05);
32-
EXPECT_NEAR(coeffs[1], 1.518, 0.05);
33-
EXPECT_NEAR(rSquared, 0.985, 0.05);
31+
EXPECT_DOUBLE_EQ(coeffs[0], 0.30487804878048774);
32+
EXPECT_DOUBLE_EQ(coeffs[1], 1.5182926829268293);
33+
EXPECT_DOUBLE_EQ(rSquared, 0.91906029466386019);
3434
}
3535

3636
#ifndef NDEBUG

0 commit comments

Comments
 (0)