Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
lfit.H File Reference

Linear regression (least squares fitting). More...

#include <cmath>
#include <cstddef>
#include <limits>
#include <string>
#include <utility>
#include <ah-zip.H>
#include <tpl_array.H>
#include <ah-string-utils.H>
#include <aleph-exceptions.H>
Include dependency graph for lfit.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

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

Detailed Description

Linear regression (least squares fitting).

This file provides the LFit struct for performing simple linear regression on paired (x, y) data. It makes the computations of gsl_fit_linear(), gsl_stats_correlation() and gsl_fit_linear_est() of the GNU Scientific Library, in their order of operations, without GSL: built without fused multiply-adds, as GSL is for x86-64, its results are those of GSL, bit by bit; where the compiler fuses multiply-adds, as on arm64, the fit and the prediction may differ in their last bits, as the builds of GSL there do.

Features

  • Least squares linear fitting: y = c + m*x
  • Coefficient of determination (R²)
  • Mean squared error and standard deviation of the errors
  • Prediction with error estimates

Usage Example

Array<double> x = {1, 2, 3, 4, 5};
Array<double> y = {2.1, 3.9, 6.1, 8.0, 10.2};
LFit fit(x, y);
std::cout << "y = " << fit.c << " + " << fit.m << " * x\n";
std::cout << "R² = " << fit.r2 << "\n";
auto [predicted, error] = fit.predict(6.0);
static mpfr_t y
Definition mpfr_mul_d.c:3
void error(const char *file, int line, const char *format,...)
Print an error message with file and line info.
Definition ahDefs.C:100
Linear regression calculator using least squares method.
Definition lfit.H:117
See also
line.H Line equation class
Author
Leandro Rabindranath León

Definition in file lfit.H.