|
| 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.
|
| |
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.
| 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
-
| x | Array of independent variable values |
| y | Array of dependent variable values |
- Exceptions
-
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>
y = {2, 4, 6, 8};
Simple dynamic array with automatic resizing and functional operations.
Linear regression calculator using least squares method.
void fit(const double *x, const double *y, const size_t n) noexcept
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.
| 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
-
| x | Independent variable value |
- Returns
- Pair (predicted_y, error_estimate)
- Example
auto [
y, err] =
fit.predict(10.0);
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().