Theoretica
A C++ numerical and automatic mathematical library
regression.h
Go to the documentation of this file.
1 
5 
6 #ifndef THEORETICA_REGRESSION_H
7 #define THEORETICA_REGRESSION_H
8 
9 #include "./statistics.h"
10 #include "../algebra/mat.h"
11 
12 
13 namespace theoretica {
14 
16  namespace regression {
17 
18 
21  template<typename Dataset1, typename Dataset2>
22  inline void ols_linear(
23  const Dataset1& X, const Dataset2& Y,
24  real& intercept, real& slope) {
25 
26  // Check that the two data sets have the same size
27  if(X.size() != Y.size()) {
28  TH_MATH_ERROR("regression::ols_linear", X.size(), INVALID_ARGUMENT);
29  intercept = nan(); slope = nan();
30  return;
31  }
32 
33  // Pre-compute values
34  const real sum_sqr_x = sum_squares(X);
35  const real sqr_sum_x = square(sum(X));
36  const real Delta = X.size() * sum_sqr_x - sqr_sum_x;
37  const real prod_sum_xy = product_sum(X, Y);
38  const real sum_x = sum(X);
39  const real sum_y = sum(Y);
40 
41  if(abs(Delta) < MACH_EPSILON) {
42  TH_MATH_ERROR("ols_linear", Delta, DIV_BY_ZERO);
43  intercept = nan(); slope = nan();
44  return;
45  }
46 
47  // Ordinary Least Squares formula for the linear model
48  // without weights.
49  intercept = (sum_sqr_x * sum_y - sum_x * prod_sum_xy) / Delta;
50  slope = (X.size() * prod_sum_xy - sum_x * sum_y) / Delta;
51  }
52 
53 
56  template<typename Dataset1, typename Dataset2>
57  inline void ols_linear(
58  const Dataset1& X, const Dataset2& Y, real sigma_Y,
59  real& intercept, real& slope,
60  real& sigma_A, real& sigma_B) {
61 
62  // Check that the two data sets have the same size
63  if(X.size() != Y.size()) {
64  TH_MATH_ERROR("regression::ols_linear", X.size(), INVALID_ARGUMENT);
65  intercept = nan(); slope = nan();
66  return;
67  }
68 
69  // Pre-compute values
70  const real sum_sqr_x = sum_squares(X);
71  const real sqr_sum_x = square(sum(X));
72  const real Delta = X.size() * sum_sqr_x - sqr_sum_x;
73  const real sum_xy = product_sum(X, Y);
74  const real sum_x = sum(X);
75  const real sum_y = sum(Y);
76 
77  if(abs(Delta) < MACH_EPSILON) {
78  TH_MATH_ERROR("ols_linear", Delta, DIV_BY_ZERO);
79  intercept = nan(); slope = nan();
80  return;
81  }
82 
83  // Ordinary Least Squares formula for the linear model
84  // without weights.
85  intercept = (sum_sqr_x * sum_y - sum_x * sum_xy) / Delta;
86  slope = (X.size() * sum_xy - sum_x * sum_y) / Delta;
87 
88  // Compute the uncertainties on the coefficients
89  sigma_A = sqrt(square(sigma_Y) * sum_sqr_x / Delta);
90  sigma_B = sqrt(square(sigma_Y) * X.size() / Delta);
91  }
92 
93 
96  template<typename Dataset1, typename Dataset2>
97  inline void ols_linear(
98  const Dataset1& X, const Dataset2& Y, real sigma_Y,
99  real& intercept, real& slope, mat2& covar_mat) {
100 
101  // Check that the two data sets have the same size
102  if(X.size() != Y.size()) {
103  TH_MATH_ERROR("regression::ols_linear", X.size(), INVALID_ARGUMENT);
104  intercept = nan();
105  slope = nan();
106  return;
107  }
108 
109  // Pre-compute values
110  const real sum_sqr_x = sum_squares(X);
111  const real sqr_sum_x = square(sum(X));
112  const real Delta = X.size() * sum_sqr_x - sqr_sum_x;
113  const real sum_xy = product_sum(X, Y);
114  const real sum_x = sum(X);
115  const real sum_y = sum(Y);
116 
117  if(abs(Delta) < MACH_EPSILON) {
118  TH_MATH_ERROR("ols_linear", Delta, DIV_BY_ZERO);
119  intercept = nan(); slope = nan();
120  return;
121  }
122 
123  // Ordinary Least Squares formula for the linear model
124  // without weights.
125  intercept = (sum_sqr_x * sum_y - sum_x * sum_xy) / Delta;
126  slope = (X.size() * sum_xy - sum_x * sum_y) / Delta;
127 
128  // Compute the Covariance Matrix for the coefficients
129  covar_mat(0, 0) = square(sigma_Y) * sum_sqr_x / Delta;
130  covar_mat(1, 1) = square(sigma_Y) * X.size() / Delta;
131  covar_mat(0, 1) = -square(sigma_Y) * sum_x / Delta;
132  covar_mat(1, 0) = covar_mat(0, 1);
133  }
134 
135 
138  template<typename Dataset1, typename Dataset2, typename Dataset3>
139  inline void wls_linear(
140  const Dataset1& X, const Dataset2& Y, const Dataset3& W,
141  real& intercept, real& slope, mat2& covar_mat) {
142 
143  if(X.size() != Y.size() || X.size() != W.size()) {
144  TH_MATH_ERROR("wls_linear", X.size(), INVALID_ARGUMENT);
145  intercept = nan(); slope = nan();
146  return;
147  }
148 
149  // Pre-compute values
150  const real sum_xw = product_sum(X, W);
151  const real sum_xxw = product_sum(X, X, W);
152  const real sum_xyw = product_sum(X, Y, W);
153  const real sum_yw = product_sum(Y, W);
154  const real sum_w = sum(W);
155  const real Delta = sum_w * sum_xxw - square(sum_xw);
156 
157  if(abs(Delta) < MACH_EPSILON) {
158  TH_MATH_ERROR("wls_linear", Delta, DIV_BY_ZERO);
159  intercept = nan(); slope = nan();
160  return;
161  }
162 
163  intercept = (sum_xxw * sum_yw - sum_xw * sum_xyw) / Delta;
164  slope = (sum_w * sum_xyw - sum_xw * sum_yw) / Delta;
165 
166  // Compute the Covariance Matrix for the coefficients
167  covar_mat(0, 0) = sum_xxw / Delta;
168  covar_mat(1, 1) = sum_w / Delta;
169  covar_mat(0, 1) = -sum_xw / Delta;
170  covar_mat(1, 0) = covar_mat(0, 1);
171  }
172 
173 
176  template<typename Dataset1, typename Dataset2>
177  inline void wls_linear(
178  const Dataset1& X, const Dataset2& Y,
179  real sigma_X, real sigma_Y,
180  real& intercept, real& slope,
181  mat2& covar_mat) {
182 
183  if(X.size() != Y.size()) {
184  TH_MATH_ERROR("wls_linear", X.size(), INVALID_ARGUMENT);
185  intercept = nan();
186  slope = nan();
187  return;
188  }
189 
190  // Pre-compute values
191  const real mean_x = stats::mean(X);
192  const real mean_y = stats::mean(Y);
193  const real delta_x = (sum_squares(X) / X.size()) - square(mean_x);
194  const real delta_y = (sum_squares(Y) / Y.size()) - square(mean_y);
195  const real delta_xy = (product_sum(X, Y) / X.size()) - mean_x * mean_y;
196  const real Delta = square(sigma_X) * delta_y - square(sigma_Y) * delta_x;
197 
198  slope = (Delta + sqrt(
199  square(Delta) + 4 * square(sigma_X * sigma_Y * delta_xy)
200  )) / (2 * square(sigma_X) * delta_xy);
201 
202  intercept = mean_y - slope * mean_x;
203 
204  // TO-DO Compute Covariance Matrix for this case
205  covar_mat = mat2(nan());
206  }
207 
210  template<typename Dataset1, typename Dataset2>
211  inline void ols_linear_orig(
212  const Dataset1& X, const Dataset2& Y, real sigma_Y,
213  real& B, real& sigma_B) {
214 
215  if(X.size() != Y.size()) {
216  TH_MATH_ERROR("ols_linear_orig", X.size(), INVALID_ARGUMENT);
217  B = nan();
218  return;
219  }
220 
221  const real sum_x = sum(X);
222  const real sum_y = sum(Y);
223 
224  if(abs(sum_y) < MACH_EPSILON) {
225  TH_MATH_ERROR("ols_linear_orig", sum_y, DIV_BY_ZERO);
226  B = nan();
227  return;
228  }
229 
230  B = sum_x / sum_y;
231  sigma_B = sqrt(X.size() * square(sigma_Y / sum_x));
232  }
233 
236  template<typename Dataset1, typename Dataset2, typename Dataset3>
237  inline void wls_linear_orig(
238  const Dataset1& X, const Dataset2& Y,
239  const Dataset3& W, real& B, real& sigma_B) {
240 
241  if(X.size() != Y.size() || X.size() != W.size()) {
242  TH_MATH_ERROR("wls_linear_orig", X.size(), INVALID_ARGUMENT);
243  B = nan();
244  return;
245  }
246 
247  const real sum_xw = product_sum(X, W);
248  const real sum_yw = product_sum(Y, W);
249 
250  if(abs(sum_yw) < MACH_EPSILON) {
251  TH_MATH_ERROR("ols_linear_orig", sum_yw, DIV_BY_ZERO);
252  B = nan();
253  return;
254  }
255 
256  B = sum_xw / sum_yw;
257  sigma_B = sqrt(sum(W) / sum_xw);
258  }
259 
260 
263  template<typename Dataset1, typename Dataset2>
265  const Dataset1& X, const Dataset2& Y,
266  real intercept, real slope) {
267 
268  if(X.size() != Y.size()) {
269  TH_MATH_ERROR("ols_linear_error", X.size(), INVALID_ARGUMENT);
270  return nan();
271  }
272 
273  real err = 0;
274  for (unsigned int i = 0; i < X.size(); ++i) {
275  err += square(Y[i] - intercept - slope * X[i]);
276  }
277 
278  // Correction by degrees of freedom (N - 2)
279  return sqrt(err / (real) (X.size() - 2));
280  }
281 
282 
286  struct linear_model {
287 
290 
293 
296 
299 
302 
305 
308 
311  unsigned int ndf;
312 
316 
317 
319  linear_model() : A(nan()), B(nan()) {}
320 
321 
324  template<typename Dataset1, typename Dataset2>
325  inline linear_model(const Dataset1& X, const Dataset2& Y) {
326  fit(X, Y);
327  }
328 
329 
332  template<typename Dataset1, typename Dataset2>
333  inline linear_model(const Dataset1& X, const Dataset2& Y, real sigma_Y) {
334  fit(X, Y, sigma_Y);
335  }
336 
337 
340  template<typename Dataset1, typename Dataset2, typename Dataset3>
341  inline linear_model(
342  const Dataset1& X, const Dataset2& Y, const Dataset3& sigma_Y) {
343  fit(X, Y, sigma_Y);
344  }
345 
346 
349  template<typename Dataset1, typename Dataset2>
350  inline linear_model(
351  const Dataset1& X, const Dataset2& Y, real sigma_X, real sigma_Y) {
352  fit(X, Y, sigma_X, sigma_Y);
353  }
354 
355 
363  template<typename Dataset1, typename Dataset2>
364  inline void fit(const Dataset1& X, const Dataset2& Y) {
365 
366  if(X.size() != Y.size()) {
367  TH_MATH_ERROR("linear_model::fit", X.size(), INVALID_ARGUMENT);
368  A = nan(); B = nan();
369  return;
370  }
371 
372  if(Y.size() <= 2) {
373  TH_MATH_ERROR("linear_model::fit", Y.size(), INVALID_ARGUMENT);
374  A = nan(); B = nan();
375  return;
376  }
377 
378  ols_linear(X, Y, A, B);
379 
380  if(is_nan(A))
381  return;
382 
383  err = ols_linear_error(X, Y, A, B);
384  covar_mat = mat2(nan());
385  sigma_A = nan();
386  sigma_B = nan();
387  chi_squared = nan();
388  p_value = nan();
389  ndf = Y.size() - 2;
390  }
391 
392 
399  template<typename Dataset1, typename Dataset2>
400  inline void fit(
401  const Dataset1& X, const Dataset2& Y, real sigma_Y) {
402 
403  if(X.size() != Y.size()) {
404  TH_MATH_ERROR("linear_model::fit", X.size(), INVALID_ARGUMENT);
405  A = nan(); B = nan();
406  return;
407  }
408 
409  if(Y.size() <= 2) {
410  TH_MATH_ERROR("linear_model::fit", Y.size(), INVALID_ARGUMENT);
411  A = nan(); B = nan();
412  return;
413  }
414 
415  if(abs(sigma_Y) <= MACH_EPSILON) {
416  TH_MATH_ERROR("linear_model::fit", sigma_Y, DIV_BY_ZERO);
417  A = nan(); B = nan();
418  return;
419  }
420 
421  ols_linear(X, Y, sigma_Y, A, B, this->covar_mat);
422 
423  if(is_nan(A))
424  return;
425 
426  sigma_A = sqrt(this->covar_mat(0, 0));
427  sigma_B = sqrt(this->covar_mat(1, 1));
428 
429  err = ols_linear_error(X, Y, A, B);
430  chi_squared = err / sigma_Y;
431  ndf = Y.size() - 2;
433  }
434 
435 
442  template<typename Dataset1, typename Dataset2, typename Dataset3>
443  inline void fit(
444  const Dataset1& X, const Dataset2& Y, const Dataset3& sigma) {
445 
446  if(X.size() != Y.size()) {
447  TH_MATH_ERROR("linear_model::fit", X.size(), INVALID_ARGUMENT);
448  A = nan(); B = nan();
449  return;
450  }
451 
452  if(Y.size() < 2) {
453  TH_MATH_ERROR("linear_model::fit", Y.size(), INVALID_ARGUMENT);
454  A = nan(); B = nan();
455  return;
456  }
457 
458  // The weights are given by the squared inverse of the sigma
459  auto W = sigma;
460  apply(W, [](real x) {
461  return 1.0 / (x * x);
462  });
463 
464  wls_linear(X, Y, W, A, B, covar_mat);
465 
466  if(is_nan(A))
467  return;
468 
469  sigma_A = sqrt(this->covar_mat(0, 0));
470  sigma_B = sqrt(this->covar_mat(1, 1));
471 
472  err = ols_linear_error(X, Y, A, B);
473  chi_squared = stats::chi_square_linear(X, Y, sigma, A, B);
474  ndf = Y.size() - 2;
476  }
477 
478 
485  template<typename Dataset1, typename Dataset2>
486  inline void fit(
487  const Dataset1& X, const Dataset2& Y, real sigma_X, real sigma_Y) {
488 
489  if(X.size() != Y.size()) {
490  TH_MATH_ERROR("linear_model::fit", X.size(), INVALID_ARGUMENT);
491  A = nan(); B = nan();
492  return;
493  }
494 
495  if(Y.size() < 2) {
496  TH_MATH_ERROR("linear_model::fit", Y.size(), INVALID_ARGUMENT);
497  A = nan(); B = nan();
498  return;
499  }
500 
501  wls_linear(X, Y, sigma_X, sigma_Y, A, B, covar_mat);
502 
503  if(is_nan(A))
504  return;
505 
506  sigma_A = sqrt(this->covar_mat(0, 0));
507  sigma_B = sqrt(this->covar_mat(1, 1));
508 
509  err = ols_linear_error(X, Y, A, B);
510  chi_squared = err / (square(sigma_Y) + square(B * sigma_X));
511  ndf = Y.size() - 2;
513  }
514 
515 
518  inline real operator()(real x) {
519  return A + B * x;
520  }
521 
522 
523 #ifndef THEORETICA_NO_PRINT
524 
526  inline std::string to_string() const {
527 
528  std::stringstream res;
529 
530  res << "Model: y = A + B * x" << std::endl;
531 
532  res << "A = " << A;
533  if(!is_nan(sigma_A))
534  res << " +- " << sigma_A;
535  res << std::endl;
536 
537  res << "B = " << B;
538  if(!is_nan(sigma_B))
539  res << " +- " << sigma_B;
540  res << std::endl;
541 
542  res << "Error = " << err << std::endl;
543  res << "ndf = " << ndf << std::endl;
544 
545  if(!is_nan(chi_squared))
546  res << "Chi-squared = " << chi_squared << std::endl;
547 
548  if(!is_nan(p_value))
549  res << "p-value = " << p_value << std::endl;
550 
551  if(!is_nan(covar_mat(0, 0)))
552  res << "Covariance Matrix:\n" << covar_mat;
553 
554  return res.str();
555  }
556 
557 
559  inline operator std::string() {
560  return to_string();
561  }
562 
563 
565  inline friend std::ostream& operator<<(
566  std::ostream& out, const linear_model& obj) {
567  return out << obj.to_string();
568  }
569 
570 #endif
571 
572  };
573 
574 
577  template<typename Dataset1, typename Dataset2>
579  const Dataset1& X, const Dataset2& Y) {
580 
581  if(X.size() != Y.size()) {
582  TH_MATH_ERROR("ols_linear_intercept", X.size(), INVALID_ARGUMENT);
583  return nan();
584  }
585 
586  const real Delta = X.size() * sum_squares(X) - square(sum(X));
587  const real A = (sum_squares(X) * sum(Y) - sum(X)
588  * product_sum(X, Y)) / Delta;
589 
590  return A;
591  }
592 
593 
595  template<typename Dataset1, typename Dataset2>
597  const Dataset1& X, const Dataset2& Y, real sigma_y) {
598 
599  const real Delta = X.size() * sum_squares(X) - square(sum(X));
600  return sqrt(sum_squares(X) / Delta) * abs(sigma_y);
601  }
602 
603 
606  template<typename Dataset1, typename Dataset2>
607  inline real ols_linear_slope(const Dataset1& X, const Dataset2& Y) {
608 
609  if(X.size() != Y.size()) {
610  TH_MATH_ERROR("ols_linear_slope", X.size(), INVALID_ARGUMENT);
611  return nan();
612  }
613 
614  const real Delta = X.size() * sum_squares(X) - square(sum(X));
615  const real B = (X.size() * product_sum(X, Y) - sum(X) * sum(Y))
616  / Delta;
617 
618  return B;
619  }
620 
621 
623  template<typename Dataset1, typename Dataset2>
625  const Dataset1& X, const Dataset2& Y, real sigma_y) {
626 
627  const real Delta = X.size() * sum_squares(X) - square(sum(X));
628  return sqrt(X.size() / Delta) * abs(sigma_y);
629  }
630 
631 
634  template<typename Dataset1, typename Dataset2, typename Dataset3>
636  const Dataset1& X, const Dataset2& Y, const Dataset3& W) {
637 
638  if(X.size() != Y.size() || X.size() != W.size()) {
640  "wls_linear_intercept",
641  X.size(), INVALID_ARGUMENT);
642  return nan();
643  }
644 
645  const real Delta = sum(W) * product_sum(X, X, W)
646  - square(product_sum(X, W));
647 
648  const real A = (product_sum(X, X, W) * product_sum(Y, W) -
649  product_sum(X, W) * product_sum(X, Y, W)) / Delta;
650 
651  return A;
652  }
653 
654 
657  template<typename Dataset1, typename Dataset2, typename Dataset3>
659  const Dataset1& X, const Dataset2& Y, const Dataset3& W) {
660 
661  if(X.size() != Y.size() || X.size() != W.size()) {
663  "wls_linear_slope",
664  X.size(), INVALID_ARGUMENT);
665  return nan();
666  }
667 
668  const real Delta = sum(W) * product_sum(X, X, W)
669  - square(product_sum(X, W));
670 
671  const real B = (sum(W) * product_sum(X, Y, W) -
672  product_sum(X, W) * product_sum(Y, W)) / Delta;
673 
674  return B;
675  }
676  }
677 }
678 
679 
680 #endif
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition: random.h:102
void wls_linear(const Dataset1 &X, const Dataset2 &Y, const Dataset3 &W, real &intercept, real &slope, mat2 &covar_mat)
Compute the coefficients of the linear regression using Weighted Least Squares.
Definition: regression.h:139
void ols_linear_orig(const Dataset1 &X, const Dataset2 &Y, real sigma_Y, real &B, real &sigma_B)
Compute the Ordinary Least Squares regression to a line passing through the origin.
Definition: regression.h:211
real ols_linear_slope(const Dataset1 &X, const Dataset2 &Y)
Compute the slope of the least squares linear regression from X and Y.
Definition: regression.h:607
real ols_linear_sigma_B(const Dataset1 &X, const Dataset2 &Y, real sigma_y)
Compute the error on the slope coefficient (B)
Definition: regression.h:624
void wls_linear_orig(const Dataset1 &X, const Dataset2 &Y, const Dataset3 &W, real &B, real &sigma_B)
Compute the Weight Least Squares regression to a line passing through the origin.
Definition: regression.h:237
real ols_linear_error(const Dataset1 &X, const Dataset2 &Y, real intercept, real slope)
Compute the error of the least squares linear regression from the X and Y datasets.
Definition: regression.h:264
real ols_linear_intercept(const Dataset1 &X, const Dataset2 &Y)
Compute the intercept of the least squares linear regression from X and Y.
Definition: regression.h:578
void ols_linear(const Dataset1 &X, const Dataset2 &Y, real &intercept, real &slope)
Compute the coefficients of the linear regression using Ordinary Least Squares.
Definition: regression.h:22
real wls_linear_slope(const Dataset1 &X, const Dataset2 &Y, const Dataset3 &W)
Compute the slope of the weighted least squares linear regression from X and Y.
Definition: regression.h:658
real ols_linear_sigma_A(const Dataset1 &X, const Dataset2 &Y, real sigma_y)
Compute the error on the intercept (A)
Definition: regression.h:596
real wls_linear_intercept(const Dataset1 &X, const Dataset2 &Y, const Dataset3 &W)
Compute the intercept of the weighted least squares linear regression from X and Y.
Definition: regression.h:635
real pvalue_chi_squared(real chi_sqr, unsigned int ndf)
Compute the (right-tailed) p-value associated to a computed Chi-square value as the integral of the C...
Definition: statistics.h:489
real chi_square_linear(const Dataset1 &X, const Dataset2 &Y, const Dataset3 &sigma, real intercept, real slope)
Compute the chi-square on a linear regression, as the sum of the squares of the residuals divided by ...
Definition: statistics.h:566
real mean(const histogram &h)
Compute the mean of the values of a histogram.
Definition: histogram.h:296
Matrix covar_mat(const std::vector< Dataset > &v)
Build the covariance matrix given a vector of datasets by computing the covariance between all couple...
Definition: errorprop.h:32
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition: dual2_functions.h:54
bool is_nan(const T &x)
Check whether a generic variable is (equivalent to) a NaN number.
Definition: error.h:70
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition: dual2_functions.h:183
auto sum(const Vector &X)
Compute the sum of a vector of real values using pairwise summation to reduce round-off error.
Definition: dataset.h:219
Vector & apply(Function f, Vector &X)
Apply a function to a set of values element-wise.
Definition: dataset.h:250
mat< real, 2, 2 > mat2
A 2x2 matrix with real entries.
Definition: algebra_types.h:17
auto product_sum(const Vector &X, const Vector &Y)
Sum the products of two sets of values.
Definition: dataset.h:46
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition: dual2_functions.h:23
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54
auto sum_squares(const Vector &X)
Sum the squares of a set of values.
Definition: dataset.h:127
Statistical functions.
structure for computation and storage of least squares linear regression results with model .
Definition: regression.h:286
void fit(const Dataset1 &X, const Dataset2 &Y, real sigma_X, real sigma_Y)
Compute the linear regression of two sets of data of the same size using least squares linear regress...
Definition: regression.h:486
linear_model()
Default constructor.
Definition: regression.h:319
void fit(const Dataset1 &X, const Dataset2 &Y, const Dataset3 &sigma)
Compute the linear regression of two sets of data of the same size using least squares linear regress...
Definition: regression.h:443
linear_model(const Dataset1 &X, const Dataset2 &Y, const Dataset3 &sigma_Y)
Construct a linear model from data and compute the fit.
Definition: regression.h:341
real B
Slope.
Definition: regression.h:295
mat2 covar_mat
Covariance matrix of the coefficients A and B.
Definition: regression.h:301
unsigned int ndf
Number of degrees of freedom of the linear regression.
Definition: regression.h:311
real chi_squared
Chi-squared on linearization.
Definition: regression.h:307
linear_model(const Dataset1 &X, const Dataset2 &Y, real sigma_X, real sigma_Y)
Construct a linear model from data and compute the fit.
Definition: regression.h:350
friend std::ostream & operator<<(std::ostream &out, const linear_model &obj)
Stream the vector in string representation to an output stream (std::ostream)
Definition: regression.h:565
real sigma_A
Estimated error on A.
Definition: regression.h:292
real sigma_B
Estimated error on B.
Definition: regression.h:298
linear_model(const Dataset1 &X, const Dataset2 &Y, real sigma_Y)
Construct a linear model from data and compute the fit.
Definition: regression.h:333
real p_value
The p-value associated to the computed Chi-squared.
Definition: regression.h:315
void fit(const Dataset1 &X, const Dataset2 &Y, real sigma_Y)
Compute the linear regression of two sets of data of the same size using ordinary least squares linea...
Definition: regression.h:400
real operator()(real x)
Compute the expected Y value for the given X value, following the computed model.
Definition: regression.h:518
linear_model(const Dataset1 &X, const Dataset2 &Y)
Construct a linear model from data and compute the fit.
Definition: regression.h:325
real err
Total error on linearization.
Definition: regression.h:304
std::string to_string() const
Convert the vector to string representation.
Definition: regression.h:526
void fit(const Dataset1 &X, const Dataset2 &Y)
Compute the linear regression of two sets of data of the same size using ordinary least squares linea...
Definition: regression.h:364
real A
Intercept.
Definition: regression.h:289