mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[apps/regression] Use a new helper to compute linear regression coefficients
This commit is contained in:
committed by
Léa Saviot
parent
13c63f495c
commit
ea4dd33826
@@ -13,6 +13,7 @@ app_src += $(addprefix apps/regression/,\
|
||||
graph_options_controller.cpp \
|
||||
graph_view.cpp \
|
||||
initialisation_parameter_controller.cpp \
|
||||
linear_model_helper.cpp \
|
||||
regression_context.cpp \
|
||||
regression_controller.cpp \
|
||||
store.cpp \
|
||||
|
||||
17
apps/regression/linear_model_helper.cpp
Normal file
17
apps/regression/linear_model_helper.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "linear_model_helper.h"
|
||||
|
||||
namespace Regression {
|
||||
|
||||
namespace LinearModelHelper {
|
||||
|
||||
double Slope(double covariance, double variance) {
|
||||
return covariance / variance;
|
||||
}
|
||||
|
||||
double YIntercept(double meanOfY, double meanOfX, double slope) {
|
||||
return meanOfY - slope * meanOfX;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
15
apps/regression/linear_model_helper.h
Normal file
15
apps/regression/linear_model_helper.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef REGRESSION_LINEAR_MODEL_HELPER
|
||||
#define REGRESSION_LINEAR_MODEL_HELPER
|
||||
|
||||
namespace Regression {
|
||||
|
||||
namespace LinearModelHelper {
|
||||
|
||||
double Slope(double covariance, double variance);
|
||||
double YIntercept(double meanOfY, double meanOfX, double slope);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "exponential_model.h"
|
||||
#include "../store.h"
|
||||
#include "../linear_model_helper.h"
|
||||
#include <math.h>
|
||||
#include <assert.h>
|
||||
#include <poincare/code_point_layout.h>
|
||||
@@ -81,8 +82,9 @@ void ExponentialModel::fit(Store * store, int series, double * modelCoefficients
|
||||
const double meanOfXY = sumOfXY / numberOfPoints;
|
||||
const double variance = meanOfXX - meanOfX * meanOfX;
|
||||
const double covariance = meanOfXY - meanOfX * meanOfY;
|
||||
modelCoefficients[1] = covariance / variance;
|
||||
modelCoefficients[0] = sign * exp(meanOfY - modelCoefficients[1] * meanOfX);
|
||||
modelCoefficients[1] = LinearModelHelper::Slope(covariance, variance);
|
||||
modelCoefficients[0] =
|
||||
sign * exp(LinearModelHelper::YIntercept(meanOfY, meanOfX, modelCoefficients[1]));
|
||||
}
|
||||
|
||||
double ExponentialModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "store.h"
|
||||
#include "linear_model_helper.h"
|
||||
#include "apps/apps_container.h"
|
||||
#include <poincare/preferences.h>
|
||||
#include <assert.h>
|
||||
@@ -238,11 +239,11 @@ double Store::covariance(int series) const {
|
||||
}
|
||||
|
||||
double Store::slope(int series) const {
|
||||
return covariance(series)/varianceOfColumn(series, 0);
|
||||
return LinearModelHelper::Slope(covariance(series), varianceOfColumn(series, 0));
|
||||
}
|
||||
|
||||
double Store::yIntercept(int series) const {
|
||||
return meanOfColumn(series, 1) - slope(series)*meanOfColumn(series, 0);
|
||||
return LinearModelHelper::YIntercept(meanOfColumn(series, 1), meanOfColumn(series, 0), slope(series));
|
||||
}
|
||||
|
||||
double Store::yValueForXValue(int series, double x, Poincare::Context * globalContext) {
|
||||
|
||||
Reference in New Issue
Block a user