Skip to content

Commit

Permalink
[sysid] Fix SSTO calculation (#6301)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Jan 24, 2024
1 parent 3acae55 commit be78552
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions sysid/src/main/native/cpp/analysis/OLS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ OLSResult OLS(const Eigen::MatrixXd& X, const Eigen::VectorXd& y) {
//
// SSTO = yᵀy - 1/n yᵀJy
//
// Let J = I.
//
// SSTO = yᵀy - 1/n yᵀy
// SSTO = (n − 1)/n yᵀy
double SSTO = (n - 1.0) / n * (y.transpose() * y).value();
// where J is a matrix of ones.
double SSTO =
(y.transpose() * y - 1.0 / y.rows() * y.transpose() *
Eigen::MatrixXd::Ones(y.rows(), y.rows()) * y)
.value();

// R² or the coefficient of determination, which represents how much of the
// total variation (variation in y) can be explained by the regression model
Expand Down
12 changes: 6 additions & 6 deletions sysid/src/test/native/cpp/analysis/OLSTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ TEST(OLSTest, TwoVariablesTwoPoints) {
auto [coeffs, rSquared, rmse] = sysid::OLS(X, y);
EXPECT_EQ(coeffs.size(), 2u);

EXPECT_NEAR(coeffs[0], 1.0, 0.05);
EXPECT_NEAR(coeffs[1], 2.0, 0.05);
EXPECT_NEAR(rSquared, 1.0, 1e-4);
EXPECT_DOUBLE_EQ(coeffs[0], 1.0);
EXPECT_DOUBLE_EQ(coeffs[1], 2.0);
EXPECT_DOUBLE_EQ(rSquared, 1.0);
}

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

EXPECT_NEAR(coeffs[0], 0.305, 0.05);
EXPECT_NEAR(coeffs[1], 1.518, 0.05);
EXPECT_NEAR(rSquared, 0.985, 0.05);
EXPECT_DOUBLE_EQ(coeffs[0], 0.30487804878048774);
EXPECT_DOUBLE_EQ(coeffs[1], 1.5182926829268293);
EXPECT_DOUBLE_EQ(rSquared, 0.91906029466386019);
}

#ifndef NDEBUG
Expand Down

0 comments on commit be78552

Please sign in to comment.