1 | // $Id: RegressionLinear.h 216 2004-12-29 09:29:54Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_statistics_regression_linear_ |
---|
4 | #define _theplu_statistics_regression_linear_ |
---|
5 | |
---|
6 | // C++ tools include |
---|
7 | ///////////////////// |
---|
8 | #include "Regression.h" |
---|
9 | #include "vector.h" |
---|
10 | |
---|
11 | // Standard C++ includes |
---|
12 | //////////////////////// |
---|
13 | #include <gsl/gsl_fit.h> |
---|
14 | |
---|
15 | namespace theplu { |
---|
16 | namespace statistics { |
---|
17 | |
---|
18 | /// |
---|
19 | /// Class for Regression. |
---|
20 | /// |
---|
21 | |
---|
22 | |
---|
23 | class RegressionLinear : public Regression |
---|
24 | { |
---|
25 | |
---|
26 | public: |
---|
27 | /// |
---|
28 | /// Default Constructor. |
---|
29 | /// |
---|
30 | RegressionLinear(void); |
---|
31 | |
---|
32 | /// |
---|
33 | /// Copy Constructor. (not implemented) |
---|
34 | /// |
---|
35 | RegressionLinear(const RegressionLinear&); |
---|
36 | |
---|
37 | /// |
---|
38 | /// Destructor |
---|
39 | /// |
---|
40 | virtual ~RegressionLinear(void) {}; |
---|
41 | |
---|
42 | inline void estimate(const double x, double& y, double& y_err) const |
---|
43 | { |
---|
44 | gsl_fit_linear_est(x, m_, k_, cov00_, cov01_, cov11_, &y, &y_err); |
---|
45 | } |
---|
46 | |
---|
47 | /// |
---|
48 | /// This function computes the best-fit linear regression |
---|
49 | /// coefficients (k,m) of the model \f$ y = kx + m \f$ from |
---|
50 | /// vectors \a x and \a y, by minimizing \f$ \sum{(kx_i+m-y_i)^2} |
---|
51 | /// \f$. |
---|
52 | /// |
---|
53 | inline int fit(const gslapi::vector& x, const gslapi::vector& y) |
---|
54 | { |
---|
55 | return gsl_fit_linear(x.gsl_vector_pointer()->data, |
---|
56 | x.gsl_vector_pointer()->stride, |
---|
57 | y.gsl_vector_pointer()->data, |
---|
58 | y.gsl_vector_pointer()->stride, |
---|
59 | x.gsl_vector_pointer()->size, |
---|
60 | &m_, &k_, &cov00_, &cov01_, &cov11_, &sumsq_); } |
---|
61 | |
---|
62 | inline double k(void) const { return k_; } |
---|
63 | inline double m(void) const { return m_; } |
---|
64 | |
---|
65 | |
---|
66 | /// |
---|
67 | /// This function computes the best-fit linear regression |
---|
68 | /// coefficients (k,m) of the model \f$ y = kx + m \f$ from |
---|
69 | /// vectors \a x and \a y, by minimizing \f$ \sum w_i(kx_i+m-y_i)^2 |
---|
70 | /// \f$. The weight \f$ w_i \f$ is the inverse of the variance for |
---|
71 | /// \f$ y_i \f$ |
---|
72 | /// |
---|
73 | inline int fit_weighted(const gslapi::vector& x, const gslapi::vector& y, |
---|
74 | const gslapi::vector& w) |
---|
75 | { |
---|
76 | return gsl_fit_wlinear(x.gsl_vector_pointer()->data, |
---|
77 | x.gsl_vector_pointer()->stride, |
---|
78 | w.gsl_vector_pointer()->data, |
---|
79 | w.gsl_vector_pointer()->stride, |
---|
80 | y.gsl_vector_pointer()->data, |
---|
81 | y.gsl_vector_pointer()->stride, |
---|
82 | x.gsl_vector_pointer()->size, |
---|
83 | &m_, &k_, &cov00_, &cov01_, &cov11_, &sumsq_); } |
---|
84 | |
---|
85 | |
---|
86 | private: |
---|
87 | double cov00_; |
---|
88 | double cov01_; |
---|
89 | double cov11_; |
---|
90 | double k_; |
---|
91 | double m_; |
---|
92 | |
---|
93 | }; |
---|
94 | |
---|
95 | }} // of namespace statistics and namespace theplu |
---|
96 | |
---|
97 | #endif |
---|
98 | |
---|