Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Commit

Permalink
add acceleration RMSE
Browse files Browse the repository at this point in the history
  • Loading branch information
Oblarg committed Feb 12, 2022
1 parent d35e7cd commit 2e899e8
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ static void PopulateOLSVector(const std::vector<PreparedData>& d,
}
}

std::tuple<std::vector<double>, double> sysid::CalculateFeedforwardGains(
const Storage& data, const AnalysisType& type) {
std::tuple<std::vector<double>, double, double>
sysid::CalculateFeedforwardGains(const Storage& data,
const AnalysisType& type) {
// Create a raw vector of doubles with our data in it.
std::vector<double> olsData;

Expand Down Expand Up @@ -88,5 +89,5 @@ std::tuple<std::vector<double>, double> sysid::CalculateFeedforwardGains(
}

// Gains are Ks, Kv, Ka, Kg (elevator only)
return std::tuple{gains, std::get<1>(ols)};
return std::tuple{gains, std::get<1>(ols), std::get<2>(ols)};
}
5 changes: 3 additions & 2 deletions sysid-application/src/main/native/cpp/analysis/OLS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

using namespace sysid;

std::tuple<std::vector<double>, double> sysid::OLS(
std::tuple<std::vector<double>, double, double> sysid::OLS(
const std::vector<double>& data, size_t independentVariables) {
// Perform some quick sanity checks regarding the size of the vector.
assert(data.size() % (independentVariables + 1) == 0);
Expand Down Expand Up @@ -56,6 +56,7 @@ std::tuple<std::vector<double>, double> sysid::OLS(

double rSquared = (SSTO - SSE) / SSTO;
double adjRSquared = 1 - (1 - rSquared) * ((n - 1.0) / (n - 3));
double RMSE = std::sqrt(SSE / n);

return {{b.data(), b.data() + b.rows()}, adjRSquared};
return {{b.data(), b.data() + b.rows()}, adjRSquared, RMSE};
}
23 changes: 17 additions & 6 deletions sysid-application/src/main/native/cpp/view/Analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ void Analyzer::Display() {
try {
const auto& [ff, trackWidth] = m_manager->CalculateFeedforward();
m_ff = std::get<0>(ff);
m_rSquared = std::get<1>(ff);
m_accelRSquared = std::get<1>(ff);
m_accelRMSE = std::get<2>(ff);
m_timescale = m_ff[2] / m_ff[1];
m_trackWidth = trackWidth;
m_state = AnalyzerState::kGraphPrep;
Expand Down Expand Up @@ -370,23 +371,33 @@ void Analyzer::DisplayGraphs() {

// If a JSON is selected
if (m_state == AnalyzerState::kNominalDisplay) {
DisplayGain("Acceleration R²", &m_rSquared);
DisplayGain("Acceleration R²", &m_accelRSquared);
CreateTooltip(
"The coefficient of determination of the OLS fit of acceleration "
"versus velocity and voltage. Acceleration is extremely noisy, "
"so this is generally quite small.");

ImGui::SameLine();
DisplayGain("Acceleration RMSE", &m_accelRMSE);
CreateTooltip(
"The standard deviation of the residuals from the predicted "
"acceleration."
"This can be interpreted loosely as the mean measured disturbance "
"from the \"ideal\" system equation.");

DisplayGain("Sim velocity R²", m_plot.GetSimRSquared());
CreateTooltip(
"The coefficient of determination the simulated velocity. "
"Velocity is much less-noisy than acceleration, so this "
"is pretty close to 1 for a decent fit.");

DisplayGain("Sim RMSE", m_plot.GetRMSE());
ImGui::SameLine();
DisplayGain("Sim velocity RMSE", m_plot.GetSimRMSE());
CreateTooltip(
"The Root Mean Squared Error (RMSE) of the simulation "
"predictions compared to the recorded data. It is essentially the "
"mean error of the simulated model in the recorded velocity units.");
"The standard deviation of the residuals from the simulated velocity "
"predictions - essentially the size of the mean error of the "
"simulated model "
"in the recorded velocity units.");
}
}
m_prevPlotsLoaded = plotsLoaded;
Expand Down
7 changes: 4 additions & 3 deletions sysid-application/src/main/native/cpp/view/AnalyzerPlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ void AnalyzerPlot::SetData(const Storage& rawData, const Storage& filteredData,
// represents the prediction at the timestep, and N represents the number of
// points
m_RMSE = std::sqrt(simSquaredErrorSum / timeSeriesPoints);
m_RSquared = 1 - m_RMSE / std::sqrt(squaredVariationSum / timeSeriesPoints);
m_accelRSquared =
1 - m_RMSE / std::sqrt(squaredVariationSum / timeSeriesPoints);
FitPlots();
}

Expand All @@ -378,12 +379,12 @@ void AnalyzerPlot::FitPlots() {
m_timestepData.fitNextPlot = true;
}

double* AnalyzerPlot::GetRMSE() {
double* AnalyzerPlot::GetSimRMSE() {
return &m_RMSE;
}

double* AnalyzerPlot::GetSimRSquared() {
return &m_RSquared;
return &m_accelRSquared;
}

static void PlotSimData(std::vector<std::vector<ImPlotPoint>>& data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class AnalysisManager {
/**
* Stores the Feedforward gains.
*/
std::tuple<std::vector<double>, double> ffGains;
std::tuple<std::vector<double>, double, double> ffGains;

/**
* Stores the trackwidth for angular drivetrain tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ namespace sysid {
* perform.
*
* @return Tuple containing the coefficients of the analysis along with the
* r-squared (coefficient of determination) of the fit.
* r-squared (coefficient of determination) and RMSE (standard deviation
* of the residuals) of the fit.
*/
std::tuple<std::vector<double>, double> CalculateFeedforwardGains(
std::tuple<std::vector<double>, double, double> CalculateFeedforwardGains(
const Storage& data, const AnalysisType& type);
} // namespace sysid
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace sysid {
/**
* Performs ordinary least squares multiple regression on the provided data and
* returns a vector of coefficients along with the r-squared (coefficient of
* determination) of the fit.
* determination) and RMSE (stardard deviation of the residuals) of the fit.
*
* @param data The data to perform the regression on. The data
* must be formatted as y, x₀, x₁, x₂, ..., y, ...
* in the vector.
* @param independentVariables The number of independent variables (x values).
*/
std::tuple<std::vector<double>, double> OLS(const std::vector<double>& data,
size_t independentVariables);
std::tuple<std::vector<double>, double, double> OLS(
const std::vector<double>& data, size_t independentVariables);
} // namespace sysid
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ class Analyzer : public glass::View {

// Feedforward and feedback gains.
std::vector<double> m_ff;
double m_rSquared;
double m_accelRSquared;
double m_accelRMSE;
double m_Kp;
double m_Kd;
double m_timescale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class AnalyzerPlot {
*
* @return A pointer to the RMSE
*/
double* GetRMSE();
double* GetSimRMSE();

/**
* Gets the pointer to the stored simulated velocity R-squared for display
Expand Down Expand Up @@ -198,6 +198,6 @@ class AnalyzerPlot {
DataWithFitLinePlot m_timestepData;

double m_RMSE;
double m_RSquared;
double m_accelRSquared;
};
} // namespace sysid
4 changes: 2 additions & 2 deletions sysid-application/src/test/native/cpp/analysis/OLSTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TEST(OLSTest, TwoVariablesTwoPoints) {
// (1, 3) and (2, 5). Should produce y = 2x + 1.
std::vector<double> data{3.0, 1.0, 1.0, 5.0, 1.0, 2.0};

auto [coefficients, cod] = sysid::OLS(data, 2);
auto [coefficients, cod, rmse] = sysid::OLS(data, 2);
EXPECT_EQ(coefficients.size(), 2u);

EXPECT_NEAR(coefficients[0], 1.0, 0.05);
Expand All @@ -24,7 +24,7 @@ TEST(OLSTest, TwoVariablesFivePoints) {
// Should produce 1.518x + 0.305.
std::vector<double> data{4, 1, 2, 5, 1, 3, 7, 1, 5, 10, 1, 7, 15, 1, 9};

auto [coefficients, cod] = sysid::OLS(data, 2);
auto [coefficients, cod, rmse] = sysid::OLS(data, 2);
EXPECT_EQ(coefficients.size(), 2u);

EXPECT_NEAR(coefficients[0], 0.305, 0.05);
Expand Down

0 comments on commit 2e899e8

Please # to comment.