Gin
Loading...
Searching...
No Matches
Public Member Functions | List of all members
LeastSquaresRegression Class Reference

Performs least squares regression to fit a quadratic curve to data points. More...

#include <gin_leastsquaresregression.h>

Public Member Functions

 LeastSquaresRegression ()=default
 Creates an empty regression object.
 
void addPoint (double x, double y)
 Adds a data point to the regression.
 
void addPoint (Point< double > point)
 Adds a data point to the regression.
 
void addPoints (juce::Array< Point< double > > points)
 Adds multiple data points to the regression.
 
void clear ()
 Removes all data points.
 
bool enoughPoints ()
 Checks if enough points have been added for valid regression.
 
juce::Array< doublegetTerms ()
 Returns all three coefficients as an array.
 
double aTerm ()
 Returns the coefficient of the x² term.
 
double bTerm ()
 Returns the coefficient of the x term.
 
double cTerm ()
 Returns the constant term.
 
double rSquare ()
 Returns the coefficient of determination (R²).
 

Detailed Description

Performs least squares regression to fit a quadratic curve to data points.

This class fits a quadratic equation y = ax² + bx + c to a set of data points using the least squares method. It calculates the coefficients (a, b, c) that minimize the sum of squared differences between the data points and the curve.

The class also computes R² (the coefficient of determination), which measures how well the curve fits the data (values closer to 1.0 indicate better fit).

Example usage:

regression.addPoint (1.0, 2.5);
regression.addPoint (2.0, 5.2);
regression.addPoint (3.0, 8.8);
// ... add more points
if (regression.enoughPoints())
{
double a = regression.aTerm(); // Coefficient of x²
double b = regression.bTerm(); // Coefficient of x
double c = regression.cTerm(); // Constant term
double r2 = regression.rSquare(); // Goodness of fit
}
Performs least squares regression to fit a quadratic curve to data points.
Definition gin_leastsquaresregression.h:46
A lightweight 2D point class for projects that don't use juce_graphics.
Definition gin_point.h:25

Based on code from: https://www.codeproject.com/Articles/63170/Least-Squares-Regression-for-Quadratic-Curve-Fitti

See also
aTerm, bTerm, cTerm, rSquare

Constructor & Destructor Documentation

◆ LeastSquaresRegression()

LeastSquaresRegression::LeastSquaresRegression ( )
default

Creates an empty regression object.

Member Function Documentation

◆ addPoint() [1/2]

void LeastSquaresRegression::addPoint ( double  x,
double  y 
)

Adds a data point to the regression.

Parameters
xThe x coordinate
yThe y coordinate

◆ addPoint() [2/2]

void LeastSquaresRegression::addPoint ( Point< double point)

Adds a data point to the regression.

Parameters
pointThe point to add

◆ addPoints()

void LeastSquaresRegression::addPoints ( juce::Array< Point< double > >  points)

Adds multiple data points to the regression.

Parameters
pointsArray of points to add

◆ clear()

void LeastSquaresRegression::clear ( )

Removes all data points.

◆ enoughPoints()

bool LeastSquaresRegression::enoughPoints ( )

Checks if enough points have been added for valid regression.

Returns
true if there are enough points to calculate the curve

◆ getTerms()

juce::Array< double > LeastSquaresRegression::getTerms ( )

Returns all three coefficients as an array.

Returns
Array containing [a, b, c] where y = ax² + bx + c

◆ aTerm()

double LeastSquaresRegression::aTerm ( )

Returns the coefficient of the x² term.

Returns
The 'a' coefficient in y = ax² + bx + c

◆ bTerm()

double LeastSquaresRegression::bTerm ( )

Returns the coefficient of the x term.

Returns
The 'b' coefficient in y = ax² + bx + c

◆ cTerm()

double LeastSquaresRegression::cTerm ( )

Returns the constant term.

Returns
The 'c' coefficient in y = ax² + bx + c

◆ rSquare()

double LeastSquaresRegression::rSquare ( )

Returns the coefficient of determination (R²).

R² measures how well the curve fits the data, ranging from 0 to 1. Values closer to 1 indicate a better fit.

Returns
The R² value

The documentation for this class was generated from the following file: