Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
lfit.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
65# ifndef LFIT_H
66# define LFIT_H
67
68# include <gsl/gsl_statistics_double.h>
69# include <gsl/gsl_fit.h>
70
71# include <string>
72
73# include <ah-zip.H>
74# include <tpl_array.H>
75# include <ah-string-utils.H>
76
77# include <aleph-exceptions.H>
78
98struct LFit
99{
100 mutable double c = 0;
101 mutable double m = 1;
102 mutable double cov00 = 0;
103 mutable double cov01 = 0;
104 mutable double cov11 = 0;
105 mutable double sumsq = 0;
106 mutable double r2 = 0;
107 mutable double sigma = 0;
108 mutable double mse = 0;
109
117 friend std::ostream &operator <<(std::ostream & out, const LFit & f)
118 {
119 return out << "r2 = " << f.r2 << std::endl
120 << "sumsq = " << f.sumsq << std::endl
121 << "sigma = " << f.sigma << std::endl
122 << "mse = " << f.mse << std::endl
123 << "c = " << f.c << std::endl
124 << "m = " << f.m << std::endl;
125 }
126
129
149 LFit(const Array<double> & x, const Array<double> & y)
150 {
151 const size_t & n = y.size();
152 if (x.size() != n)
153 ALEPHTHROW(SizeMismatch, "x and y have different sizes");
154
155 if (n == 1)
156 ALEPHTHROW(SizeMismatch, "arrays have length = 1");
157
158 gsl_fit_linear(&x.base(), 1, &y.base(), 1, y.size(), &c, &m,
159 &cov00, &cov01, &cov11, &sumsq);
160
161 const auto pearson_corr = gsl_stats_correlation(&x.base(), 1, &y.base(), 1, n);
164 r2 = 1;
165
166 double sum_sigma = 0, sum_mse = 0;
168 for (auto it = zip_it(x, y); it.has_curr(); it.next_ne())
169 {
170 auto t = it.get_curr();
171 const auto diff = get<0>(t) - get<1>(t);
172 auto e = fabs(diff);
173 error.append(e);
174 sum_sigma += e;
175 sum_mse += pow2(diff);
176 }
177
178 sigma = sqrt(error.foldl<double>(0.0, [ym = sum_sigma / n](auto acu, auto y)
179 {
180 return acu + pow2(y - ym);
181 }) / n);
182
183 mse = sqrt(sum_mse / n);
184 }
185
202 std::pair<double, double> predict(double x) const noexcept
203 {
204 double y = 0, y_err = 0;
206 return std::make_pair(y, y_err);
207 }
208};
209
210
211# endif
String manipulation utilities.
Zip iterators and functional operations for multiple containers.
Custom exception classes with source location tracking.
#define ALEPHTHROW(type, msg)
Macro for throwing an aleph exception.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:333
T & base()
Return a reference to the first element of array.
Definition tpl_array.H:314
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sqrt_function > > sqrt(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4058
static mpfr_t y
Definition mpfr_mul_d.c:3
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
double pow2(const double x)
Return x^2.
Definition ahUtils.H:382
void error(const char *file, int line, const char *format,...)
Print an error message with file and line info.
Definition ahDefs.C:105
bool is_normal_number(const double n)
Return true if a floating-point number is normal or zero.
Definition ahUtils.H:469
ZipIterator< Cs... > zip_it(const Cs &... cs)
Alias for get_zip_it.
Definition ah-zip.H:275
DynList< T > maps(const C &c, Op op)
Classic map operation.
Linear regression calculator using least squares method.
Definition lfit.H:99
LFit(const Array< double > &x, const Array< double > &y)
Construct and compute linear regression.
Definition lfit.H:149
LFit() noexcept
Default constructor creates an identity fit (y = x)
Definition lfit.H:128
double cov00
Covariance matrix element (0,0)
Definition lfit.H:102
double sumsq
Sum of squared residuals.
Definition lfit.H:105
double cov01
Covariance matrix element (0,1)
Definition lfit.H:103
std::pair< double, double > predict(double x) const noexcept
Predict y-value and error for given x.
Definition lfit.H:202
double r2
Coefficient of determination.
Definition lfit.H:106
double m
Slope of fitted line.
Definition lfit.H:101
friend std::ostream & operator<<(std::ostream &out, const LFit &f)
Output stream operator for fit results.
Definition lfit.H:117
double cov11
Covariance matrix element (1,1)
Definition lfit.H:104
double c
Y-intercept of fitted line.
Definition lfit.H:100
double mse
Mean squared error.
Definition lfit.H:108
double sigma
Standard deviation of errors.
Definition lfit.H:107
Dynamic array container with automatic resizing.