Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
LFit Struct Reference

Linear regression calculator using least squares method. More...

#include <lfit.H>

Public Member Functions

 LFit () noexcept
 Default constructor creates an identity fit (y = x)
 
 LFit (const Array< double > &x, const Array< double > &y)
 Construct and compute linear regression.
 
std::pair< double, double > predict (const double x) const noexcept
 Predict y-value and error for given x.
 

Public Attributes

double c = 0
 Y-intercept of fitted line.
 
double m = 1
 Slope of fitted line.
 
double cov00 = 0
 Covariance matrix element (0,0)
 
double cov01 = 0
 Covariance matrix element (0,1)
 
double cov11 = 0
 Covariance matrix element (1,1)
 
double sumsq = 0
 Sum of the squares of the residuals, y - (c + m x)
 
double r2 = 0
 Coefficient of determination.
 
double sigma = 0
 Standard deviation of the errors: the root of sumsq / (n - 2), the estimate from which the covariances come; NaN for two points.
 
double mse = 0
 Mean squared error: sumsq / n.
 

Private Member Functions

void fit (const double *x, const double *y, const size_t n) noexcept
 

Static Private Member Functions

static double correlation (const double *x, const double *y, const size_t n) noexcept
 

Friends

std::ostream & operator<< (std::ostream &out, const LFit &f)
 Output stream operator for fit results.
 

Detailed Description

Linear regression calculator using least squares method.

Computes the best-fit line y = c + m*x that minimizes the sum of squared residuals, with the algorithms of GSL (see the file).

Computed Statistics

Field Description
c Y-intercept
m Slope
cov00, cov01, cov11 Covariances of c and m
r2 Coefficient of determination (0-1)
sumsq Sum of the squared residuals, y - (c + m x)
sigma Standard deviation of the errors: the root of sumsq / (n - 2)
mse Mean squared error: sumsq / n

Definition at line 116 of file lfit.H.

Constructor & Destructor Documentation

◆ LFit() [1/2]

LFit::LFit ( )
inlinenoexcept

Default constructor creates an identity fit (y = x)

Definition at line 148 of file lfit.H.

◆ LFit() [2/2]

LFit::LFit ( const Array< double > &  x,
const Array< double > &  y 
)
inline

Construct and compute linear regression.

Fits the model y = c + m*x to minimize sum of squared errors, as gsl_fit_linear() does, and computes R², as the square of gsl_stats_correlation(), the mean squared error and the standard deviation of the errors.

Parameters
xArray of independent variable values
yArray of dependent variable values
Exceptions
SizeMismatchif x and y have different sizes
SizeMismatchif the arrays are empty or have only 1 element

SizeMismatch, of the deprecated aleph-exceptions.H, is a std::runtime_error; from 8.0.0, LFit throws std::invalid_argument instead. Catching std::exception catches both.

Example
Array<double> x = {1, 2, 3, 4};
Array<double> y = {2, 4, 6, 8};
LFit fit(x, y); // Perfect fit: y = 2x, R² = 1.0
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
static mpfr_t y
Definition mpfr_mul_d.c:3
Linear regression calculator using least squares method.
Definition lfit.H:117
void fit(const double *x, const double *y, const size_t n) noexcept
Definition lfit.H:228

Definition at line 175 of file lfit.H.

References ALEPHTHROW, Aleph::Array< T >::base(), correlation(), fit(), Aleph::is_normal_number(), mse, Aleph::pow2(), r2, sigma, Aleph::Array< T >::size(), sumsq, and y.

Member Function Documentation

◆ correlation()

static double LFit::correlation ( const double *  x,
const double *  y,
const size_t  n 
)
inlinestaticprivatenoexcept

Definition at line 273 of file lfit.H.

References r, and y.

Referenced by LFit().

◆ fit()

void LFit::fit ( const double *  x,
const double *  y,
const size_t  n 
)
inlineprivatenoexcept

Definition at line 228 of file lfit.H.

References c, cov00, cov01, cov11, m, sumsq, and y.

Referenced by LFit().

◆ predict()

std::pair< double, double > LFit::predict ( const double  x) const
inlinenoexcept

Predict y-value and error for given x.

Uses the fitted model to predict y at the given x value, along with an error estimate based on the covariance matrix.

Parameters
xIndependent variable value
Returns
Pair (predicted_y, error_estimate)
Example
LFit fit(x_data, y_data);
auto [y, err] = fit.predict(10.0);
// y ± err is the predicted range

Definition at line 213 of file lfit.H.

References c, cov00, cov01, cov11, m, and y.

Referenced by TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  out,
const LFit &  f 
)
friend

Output stream operator for fit results.

Parameters
outOutput stream
fLFit object to output
Returns
Reference to output stream

Definition at line 137 of file lfit.H.

Member Data Documentation

◆ c

double LFit::c = 0
mutable

Y-intercept of fitted line.

Definition at line 118 of file lfit.H.

Referenced by fit(), predict(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ cov00

double LFit::cov00 = 0
mutable

Covariance matrix element (0,0)

Definition at line 120 of file lfit.H.

Referenced by fit(), and predict().

◆ cov01

double LFit::cov01 = 0
mutable

Covariance matrix element (0,1)

Definition at line 121 of file lfit.H.

Referenced by fit(), and predict().

◆ cov11

double LFit::cov11 = 0
mutable

Covariance matrix element (1,1)

Definition at line 122 of file lfit.H.

Referenced by fit(), predict(), and TEST_F().

◆ m

double LFit::m = 1
mutable

Slope of fitted line.

Definition at line 119 of file lfit.H.

Referenced by fit(), predict(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ mse

double LFit::mse = 0
mutable

Mean squared error: sumsq / n.

Definition at line 128 of file lfit.H.

Referenced by LFit(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ r2

double LFit::r2 = 0
mutable

Coefficient of determination.

Definition at line 124 of file lfit.H.

Referenced by LFit(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ sigma

double LFit::sigma = 0
mutable

Standard deviation of the errors: the root of sumsq / (n - 2), the estimate from which the covariances come; NaN for two points.

Definition at line 127 of file lfit.H.

Referenced by LFit(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ sumsq

double LFit::sumsq = 0
mutable

Sum of the squares of the residuals, y - (c + m x)

Definition at line 123 of file lfit.H.

Referenced by LFit(), fit(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().


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