1#include <gtest/gtest.h>
12# include <gsl/gsl_fit.h>
13# include <gsl/gsl_statistics_double.h>
23 return std::abs(a - b) < eps;
96 EXPECT_GT(fit.
r2, 0.95);
102 Array<double> y = {2.5, 4.2, 6.1, 7.8, 10.3, 12.1, 13.9, 16.2, 17.8, 20.1};
106 EXPECT_GT(fit.
m, 1.5);
107 EXPECT_LT(fit.
m, 2.5);
108 EXPECT_GT(fit.
r2, 0.9);
144 for (
double xval = 0; xval <= 4; xval += 0.5)
208 EXPECT_GE(fit.
r2, 0.0);
209 EXPECT_LE(fit.
r2, 1.0);
219 EXPECT_GE(fit.
sumsq, 0.0);
230 EXPECT_GE(fit.
mse, 0.0);
231 EXPECT_GT(fit.
sigma, 0.0);
243 for (
size_t i = 0; i < x.
size(); ++i)
281 for (
int i = 0; i < 1000; ++i)
284 y.append(2.5 * i + 10.0);
301 for (
int i = -10; i <= 20; ++i)
313 for (
int i = 0; i < 100; ++i)
337 EXPECT_GT(fit2.
m, fit1.
m);
352 EXPECT_GT(fit2.
c, fit1.
c);
368 EXPECT_EQ(fit.
sumsq, 0.0);
369 EXPECT_EQ(fit.
mse, 0.0);
370 EXPECT_EQ(fit.
sigma, 0.0);
380 const size_t n = x.
size();
382 for (
size_t i = 0; i < n; ++i)
384 const double e =
y[i] - (fit.
c + fit.
m * x[i]);
392 EXPECT_EQ(fit.
sigma, std::sqrt(fit.
sumsq / (n - 2.0)));
393 EXPECT_GT(fit.
sigma, 0.0);
394 EXPECT_NEAR(fit.
mse,
sum / n, 1e-12 *
sum);
409 EXPECT_EQ(fit.
sumsq, 0.0);
410 EXPECT_EQ(fit.
mse, 0.0);
411 EXPECT_TRUE(std::isnan(fit.
sigma));
415#if defined(_MSC_VER) && !defined(__clang__)
416# pragma warning(push)
417# pragma warning(disable : 4996)
419# pragma GCC diagnostic push
420# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
441#if defined(_MSC_VER) && !defined(__clang__)
444# pragma GCC diagnostic pop
454 EXPECT_THROW((
void)
LFit(x,
y), std::runtime_error);
455 EXPECT_THROW((
void)
LFit(e, e), std::exception);
467 constexpr bool exact =
468# if (defined(__x86_64__) || defined(_M_X64)) && !defined(__FMA__) && !defined(__FP_FAST_FMA)
474 ::testing::AssertionResult same_as_gsl(
const double ours,
const double gsl,
475 const double tolerance,
const double scale = 0)
477 if (std::memcmp(&ours, &gsl,
sizeof ours) == 0 or (std::isnan(ours)
and std::isnan(gsl)))
478 return ::testing::AssertionSuccess();
479 if (not exact
and std::abs(ours - gsl) <= tolerance * std::max(std::abs(gsl), scale))
480 return ::testing::AssertionSuccess();
481 return ::testing::AssertionFailure() << std::setprecision(17) << ours <<
" instead of "
493 std::mt19937_64 g(2026);
494 std::uniform_real_distribution<double> u(-1, 1);
496 for (
int scale = -12; scale <= 12; scale += 3)
497 for (
const size_t n : {2, 3, 4, 5, 7, 10, 31, 100, 1000})
500 const double sx = std::ldexp(1.0, scale), sy = std::ldexp(1.0, -scale / 2);
501 const double slope = u(g) * 10,
intercept = u(g) * 1000;
502 const bool degenerate =
rep < 3 or n == 2;
504 for (
size_t i = 0; i < n; ++i)
506 const double xi = (
rep % 4 == 3 ?
static_cast<double>(i) : u(g)) * sx
507 + (
rep >= 8 ? 1e6 : 0);
509 y.append(
rep == 1 ? 3.25 : slope * x[i] +
intercept + (
rep == 0 ? 0 : u(g) * sy));
511 if (degenerate
and not exact)
516 double c,
m, cov00, cov01, cov11, sumsq;
517 gsl_fit_linear(&x.
base(), 1, &
y.base(), 1, n, &c, &
m, &cov00, &cov01, &cov11, &sumsq);
518 double r2 =
pow2(gsl_stats_correlation(&x.
base(), 1, &
y.base(), 1, n));
523 for (
size_t i = 0; i < n; ++i)
524 ymax = std::max(ymax, std::abs(
y[i]));
526 const auto where = [&] {
return "scale " + std::to_string(scale) +
", n "
527 + std::to_string(n) +
", case " + std::to_string(
rep); };
528 EXPECT_TRUE(same_as_gsl(fit.c, c, 1e-10, ymax)) << where();
529 EXPECT_TRUE(same_as_gsl(fit.m,
m, 1e-10)) << where();
530 EXPECT_TRUE(same_as_gsl(fit.cov00, cov00, 1e-6)) << where();
531 EXPECT_TRUE(same_as_gsl(fit.cov01, cov01, 1e-6)) << where();
532 EXPECT_TRUE(same_as_gsl(fit.cov11, cov11, 1e-6)) << where();
533 EXPECT_TRUE(same_as_gsl(fit.sumsq, sumsq, 1e-6)) << where();
534 EXPECT_TRUE(same_as_gsl(fit.r2, r2, 1e-10)) << where();
536 for (
const double xp : {0.0, u(g) * sx, 3 * sx})
539 gsl_fit_linear_est(xp, c,
m, cov00, cov01, cov11, &yp, &yp_err);
540 const auto [ours, ours_err] = fit.predict(xp);
541 EXPECT_TRUE(same_as_gsl(ours, yp, 1e-10, ymax)) << where() <<
", x " << xp;
542 EXPECT_TRUE(same_as_gsl(ours_err, yp_err, 1e-6)) << where() <<
", x " << xp;
546 EXPECT_GT(compared, exact ? 800u : 500u);
555 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
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.
T & append(const T &data)
Append a copy of data
Minimal std::expected-style result type for C++20.
bool approx_equal(double a, double b, double eps=1e-6)
bool approx_equal(double a, double b, double tol=EPSILON)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_y1_function > > y1(const __gmp_expr< T, U > &expr)
Freq_Node * pred
Predecessor node in level-order traversal.
Linear regression (least squares fitting).
TEST_F(LFitTest, PerfectLineFit)
DynList< T > intercept(const Container< T > &c1, const Container< T > &c2)
Return intersection of two containers as a DynList.
DynList< T > rep(const size_t n, const T &item)
Create a sequence of repeated items.
and
Check uniqueness with explicit hash + equality functors.
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.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Linear regression calculator using least squares method.
double sumsq
Sum of the squares of the residuals, y - (c + m x)
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.
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...
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)