Bayeux  3.4.1
Core Foundation library for SuperNEMO
linear_regression.h
Go to the documentation of this file.
1 // \file mygsl/linear_regression.h
2 
3 #ifndef MYGSL_LINEAR_REGRESSION_H
4 #define MYGSL_LINEAR_REGRESSION_H 1
5 
6 // Standard library:
7 #include <vector>
8 
9 // Third party:
10 // - Boost:
11 #include <boost/serialization/base_object.hpp>
12 #include <boost/serialization/nvp.hpp>
13 // - Bayeux/datatools:
14 #include <datatools/i_serializable.ipp>
15 
16 // This project:
17 #include <mygsl/i_unary_function.h>
18 #include <mygsl/datapoint.h>
19 
20 namespace mygsl {
21 
24  {
25  public:
26 
29  {
30 
31  public:
32 
34  fit_data();
35 
37  virtual ~fit_data();
38 
40  bool is_valid() const;
41 
43  bool is_weighted() const;
44 
46  bool has_constant() const;
47 
49  void reset();
50 
52  double get_slope() const;
53 
55  double get_constant() const;
56 
58  size_t get_ndof() const;
59 
61  double get_chisq() const;
62 
64  double get_sumsq() const;
65 
67  double get_slope_err() const;
68 
70  double get_constant_err() const;
71 
72  public:
73 
74  uint32_t n;
75  int32_t status;
76  bool weighted;
78  double c1;
79  double c0;
80  double cov00;
81  double cov01;
82  double cov11;
83  double sumsq;
84  double chisq;
85 
87 
88  };
89 
91  class function : public i_unary_function {
92 
93  public:
94 
96  function(const fit_data &);
97 
99  void set_fit_data(const fit_data &);
100 
102  const fit_data & get_fit_data() const;
103 
105  void eval_err(double x_, double & y_, double & yerr_) const;
106 
107  protected:
108 
110  virtual double _eval(double x_) const;
111 
112  private:
113 
114  fit_data _fit_data_;
115 
116  };
117 
118  public:
119 
121  static const unsigned int MINIMUM_NUMBER_OF_DATA_POINTS = 2;
122 
124  bool is_initialized() const;
125 
127  bool can_weighted() const;
128 
130  const fit_data & get_fit_data() const;
131 
134 
136  linear_regression(const std::vector<datapoint> & p_);
137 
139  linear_regression(size_t npoints_,
140  const double * x_,
141  const double * y_,
142  const double * w_ = 0);
143 
145  linear_regression(const std::vector<double> & x_,
146  const std::vector<double> & y_,
147  const std::vector<double> & w_);
148 
150  linear_regression(const std::vector<double> & x_,
151  const std::vector<double> & y_);
152 
154  virtual ~linear_regression();
155 
157  void init(const std::vector<datapoint> & p_);
158 
160  void init(size_t npoints_,
161  const double * x_,
162  const double * y_,
163  const double * w_ = 0);
164 
166  void init(const std::vector<double> & x_,
167  const std::vector<double> & y_,
168  const std::vector<double> & w_);
169 
171  void init(const std::vector<double> & x_,
172  const std::vector<double> & y_);
173 
175  void reset();
176 
178  bool fit_linear();
179 
181  bool fit_weighted_linear();
182 
184  bool fit_linear_no_constant();
185 
188 
189  private:
190 
191  bool _initialized_;
192  fit_data _fit_data_;
193  bool _delete_;
194  double * _x_;
195  double * _y_;
196  double * _w_;
197  bool _can_weighted_;
198 
199  };
200 
201 }
202 
203 #endif // MYGSL_LINEAR_REGRESSION_H
204 
205 /* Local Variables: */
206 /* mode: c++ */
207 /* coding: utf-8 */
208 /* End: */
int32_t status
Number of fitted points.
Definition: linear_regression.h:75
bool is_valid() const
Check if the fit data are valid.
bool is_weighted() const
Check if the fit was weighted.
linear_regression()
Default constructor.
double get_constant() const
Return the constant parameter.
double get_constant_err() const
Return the error on the constant.
Base abstract class of all serializable (and possibly introspectable) classes.
Definition: i_serializable.h:51
const fit_data & get_fit_data() const
Return the fit data.
void init(const std::vector< datapoint > &p_)
Initialize from an array of datapoints (x,y,w) triplets.
double cov00
Constant parameter.
Definition: linear_regression.h:80
void eval_err(double x_, double &y_, double &yerr_) const
Evaluate the Y value and associated error for a given X from the result of the fit.
The serializable result data of the linear regression algorithm.
Definition: linear_regression.h:28
virtual double _eval(double x_) const
Evaluate the Y value for a given X.
bool fit_weighted_linear()
Perform the linear weighted fit.
double get_chisq() const
Return the chi squared.
double get_slope() const
Return the slope parameter.
void set_fit_data(const fit_data &)
Set the fit fata.
virtual ~fit_data()
Destructor.
#define DATATOOLS_SERIALIZATION_DECLARATION()
Definition: i_serializable.h:266
double cov11
Covariance coefficient.
Definition: linear_regression.h:82
double cov01
Covariance coefficient.
Definition: linear_regression.h:81
double c1
Flag for a linear fit with a constant.
Definition: linear_regression.h:78
bool weighted
Status of the fit 0 == fit results; no fit otherwise.
Definition: linear_regression.h:76
static const unsigned int MINIMUM_NUMBER_OF_DATA_POINTS
Minimum number of data points to perform the linear regression fit.
Definition: linear_regression.h:121
bool has_constant() const
Check if the linear fit uses a constant parameter.
bool fit_linear()
Perform the linear unweighted fit.
Abstract interface for unary functions : R -> R.
Definition: i_unary_function.h:44
double sumsq
Covariance coefficient.
Definition: linear_regression.h:83
const fit_data & get_fit_data() const
Return the result of the fit.
bool fit_linear_no_constant()
Perform the linear unweighted fit without constant parameter.
bool is_initialized() const
Check initialization flag.
size_t get_ndof() const
Return the number of degrees of freedom.
The linear regression algorithm.
Definition: linear_regression.h:23
void reset()
Reset the fit data.
Top-level namespace of the Bayeux/mygsl module library.
Definition: base_decay_driver.h:47
double get_sumsq() const
Return the sum of squared residuals.
bool can_weighted() const
Check if a weighted fit can pe performed.
bool fit_weighted_linear_no_constant()
Perform the linear weighted fit without constant parameter.
double c0
Slope parameter.
Definition: linear_regression.h:79
double chisq
Sum of squared residuals.
Definition: linear_regression.h:84
uint32_t n
Definition: linear_regression.h:74
A linear regression fit function.
Definition: linear_regression.h:91
virtual ~linear_regression()
Destructor.
double get_slope_err() const
Return the error on the slope.
bool with_constant
Flag for a weighted fut.
Definition: linear_regression.h:77