Aleph-w 3.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, doublepredict (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 squared residuals.
 
double r2 = 0
 Coefficient of determination.
 
double sigma = 0
 Standard deviation of errors.
 
double mse = 0
 Mean squared error.
 

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. Uses GSL for numerical computation.

Computed Statistics

Field Description
c Y-intercept
m Slope
r2 Coefficient of determination (0-1)
sumsq Sum of squared residuals
sigma Standard deviation of errors
mse Mean squared error

Definition at line 98 of file lfit.H.

Constructor & Destructor Documentation

◆ LFit() [1/2]

LFit::LFit ( )
inlinenoexcept

Default constructor creates an identity fit (y = x)

Definition at line 128 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. Computes R², standard deviation, and MSE.

Parameters
xArray of independent variable values
yArray of dependent variable values
Exceptions
SizeMismatchif x and y have different sizes
SizeMismatchif arrays have only 1 element
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:138
static mpfr_t y
Definition mpfr_mul_d.c:3
DynList< T > maps(const C &c, Op op)
Classic map operation.
Linear regression calculator using least squares method.
Definition lfit.H:99

Definition at line 149 of file lfit.H.

References ALEPHTHROW, Aleph::Array< T >::base(), c, cov00, cov01, cov11, Aleph::diff(), Aleph::error(), Aleph::is_normal_number(), m, Aleph::maps(), mse, Aleph::pow2(), r2, sigma, Aleph::Array< T >::size(), sqrt(), sumsq, y, and Aleph::zip_it().

Member Function Documentation

◆ predict()

std::pair< double, double > LFit::predict ( 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
auto [y, err] = fit.predict(10.0);
// y ± err is the predicted range

Definition at line 202 of file lfit.H.

References c, cov00, cov01, cov11, m, Aleph::maps(), and y.

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 117 of file lfit.H.

Member Data Documentation

◆ c

double LFit::c = 0
mutable

Y-intercept of fitted line.

Definition at line 100 of file lfit.H.

Referenced by LFit(), and predict().

◆ cov00

double LFit::cov00 = 0
mutable

Covariance matrix element (0,0)

Definition at line 102 of file lfit.H.

Referenced by LFit(), and predict().

◆ cov01

double LFit::cov01 = 0
mutable

Covariance matrix element (0,1)

Definition at line 103 of file lfit.H.

Referenced by LFit(), and predict().

◆ cov11

double LFit::cov11 = 0
mutable

Covariance matrix element (1,1)

Definition at line 104 of file lfit.H.

Referenced by LFit(), and predict().

◆ m

double LFit::m = 1
mutable

Slope of fitted line.

Definition at line 101 of file lfit.H.

Referenced by LFit(), and predict().

◆ mse

double LFit::mse = 0
mutable

Mean squared error.

Definition at line 108 of file lfit.H.

Referenced by LFit().

◆ r2

double LFit::r2 = 0
mutable

Coefficient of determination.

Definition at line 106 of file lfit.H.

Referenced by LFit().

◆ sigma

double LFit::sigma = 0
mutable

Standard deviation of errors.

Definition at line 107 of file lfit.H.

Referenced by LFit().

◆ sumsq

double LFit::sumsq = 0
mutable

Sum of squared residuals.

Definition at line 105 of file lfit.H.

Referenced by LFit().


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