88# if defined(_MSC_VER) && !defined(__clang__)
90# pragma warning(disable : 4996)
92# pragma GCC diagnostic push
93# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
118 mutable double c = 0;
119 mutable double m = 1;
124 mutable double r2 = 0;
139 return out <<
"r2 = " << f.
r2 << std::endl
140 <<
"sumsq = " << f.
sumsq << std::endl
141 <<
"sigma = " << f.
sigma << std::endl
142 <<
"mse = " << f.
mse << std::endl
143 <<
"c = " << f.
c << std::endl
144 <<
"m = " << f.
m << std::endl;
177 const size_t n =
y.size();
194 sigma = n > 2 ? std::sqrt(
sumsq / (n - 2.0)) : std::numeric_limits<double>::quiet_NaN();
213 std::pair<double, double>
predict(
const double x)
const noexcept
216 const double y =
c +
m * x;
218 return std::make_pair(
y, y_err);
228 void fit(
const double * x,
const double *
y,
const size_t n)
noexcept
230 double m_x = 0, m_y = 0, m_dx2 = 0, m_dxdy = 0;
232 for (
size_t i = 0; i < n; ++i, ip1 += 1)
234 m_x += (x[i] - m_x) / ip1;
235 m_y += (
y[i] - m_y) / ip1;
239 for (
size_t i = 0; i < n; ++i, ip1 += 1)
241 const double dx = x[i] - m_x;
242 const double dy =
y[i] - m_y;
243 m_dx2 += (dx * dx - m_dx2) / ip1;
244 m_dxdy += (dx * dy - m_dxdy) / ip1;
247 const double b = m_dxdy / m_dx2;
248 const double a = m_y - m_x * b;
251 for (
size_t i = 0; i < n; ++i)
253 const double dx = x[i] - m_x;
254 const double dy =
y[i] - m_y;
255 const double d = dy - b * dx;
259 const double s2 = d2 / (n - 2.0);
262 cov00 = s2 * (1.0 / n) * (1 + m_x * m_x / m_dx2);
263 cov11 = s2 * 1.0 / (n * m_dx2);
264 cov01 = s2 * (-m_x) / (n * m_dx2);
273 static double correlation(
const double * x,
const double *
y,
const size_t n)
noexcept
275 long double sum_xsq = 0, sum_ysq = 0, sum_cross = 0;
276 long double mean_x = x[0], mean_y =
y[0];
278 for (
size_t i = 1; i < n; ++i, di += 1)
280 const double ip1 = di + 1;
281 const long double delta_x = x[i] - mean_x;
282 mean_x += delta_x / ip1;
283 const long double delta_y =
y[i] - mean_y;
284 mean_y += delta_y / ip1;
285 const long double ratio = di / ip1;
286 sum_xsq += delta_x * delta_x * ratio;
287 sum_ysq += delta_y * delta_y * ratio;
288 sum_cross += delta_x * delta_y * ratio;
291 const long double r = sum_cross / (std::sqrt(
static_cast<double>(sum_xsq))
292 * std::sqrt(
static_cast<double>(sum_ysq)));
293 return static_cast<double>(
r);
298# if defined(_MSC_VER) && !defined(__clang__)
301# pragma GCC diagnostic pop
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.
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & base()
Return a reference to the first element of array.
double pow2(const double x)
Return x^2.
bool is_normal_number(const double n)
Return true if a floating-point number is normal or zero.
Linear regression calculator using least squares method.
LFit(const Array< double > &x, const Array< double > &y)
Construct and compute linear regression.
LFit() noexcept
Default constructor creates an identity fit (y = x)
double cov00
Covariance matrix element (0,0)
double sumsq
Sum of the squares of the residuals, y - (c + m x)
double cov01
Covariance matrix element (0,1)
std::pair< double, double > predict(const double x) const noexcept
Predict y-value and error for given x.
double r2
Coefficient of determination.
double m
Slope of fitted line.
friend std::ostream & operator<<(std::ostream &out, const LFit &f)
Output stream operator for fit results.
void fit(const double *x, const double *y, const size_t n) noexcept
double cov11
Covariance matrix element (1,1)
double c
Y-intercept of fitted line.
double mse
Mean squared error: sumsq / n.
double sigma
Standard deviation of the errors: the root of sumsq / (n - 2), the estimate from which the covariance...
static double correlation(const double *x, const double *y, const size_t n) noexcept
Dynamic array container with automatic resizing.