1 | // $Id: RegressionLinear.h 221 2004-12-30 22:36:25Z 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 <cmath> |
---|
14 | |
---|
15 | namespace theplu { |
---|
16 | namespace statistics { |
---|
17 | |
---|
18 | /// |
---|
19 | /// Class for Regression. |
---|
20 | /// |
---|
21 | |
---|
22 | class RegressionLinear : public Regression |
---|
23 | { |
---|
24 | |
---|
25 | public: |
---|
26 | /// |
---|
27 | /// Default Constructor. |
---|
28 | /// |
---|
29 | RegressionLinear(void); |
---|
30 | |
---|
31 | /// |
---|
32 | /// Copy Constructor. (not implemented) |
---|
33 | /// |
---|
34 | RegressionLinear(const RegressionLinear&); |
---|
35 | |
---|
36 | /// |
---|
37 | /// Destructor |
---|
38 | /// |
---|
39 | virtual ~RegressionLinear(void) {}; |
---|
40 | |
---|
41 | /// |
---|
42 | /// @return the parameter \f$ \alpha \f$ |
---|
43 | /// |
---|
44 | inline double alpha(void) const { return alpha_; } |
---|
45 | |
---|
46 | /// |
---|
47 | /// @return standard deviation of parameter \f$ \alpha \f$ |
---|
48 | /// |
---|
49 | inline double alpha_var(void) const { return sqrt(alpha_var_); } |
---|
50 | |
---|
51 | /// |
---|
52 | /// @return the parameter \f$ \beta |
---|
53 | /// |
---|
54 | inline double beta(void) const { return beta_; } |
---|
55 | |
---|
56 | /// |
---|
57 | /// @return standard devaition of parameter \f$ \beta \f$ |
---|
58 | /// |
---|
59 | inline double beta_var(void) const { return sqrt(beta_var_); } |
---|
60 | |
---|
61 | /// |
---|
62 | /// This function computes the best-fit linear regression |
---|
63 | /// coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y = |
---|
64 | /// \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by |
---|
65 | /// minimizing \f$ \sum{(y_i - \alpha - \beta (x-m_x))^2} \f$. |
---|
66 | /// |
---|
67 | void fit(const gslapi::vector& x, const gslapi::vector& y) ; |
---|
68 | |
---|
69 | /// |
---|
70 | /// This function computes the best-fit linear regression |
---|
71 | /// coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y = |
---|
72 | /// \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by |
---|
73 | /// minimizing \f$ \sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$. |
---|
74 | /// |
---|
75 | void fit(const gslapi::vector& x, const gslapi::vector& y, |
---|
76 | const gslapi::vector& w); |
---|
77 | |
---|
78 | /// |
---|
79 | /// Function predicting value using the linear model. \a y_err is |
---|
80 | /// the expected deviation from the line for a new data point. If |
---|
81 | /// weights are used a weight can be specified for the new point. |
---|
82 | /// |
---|
83 | inline void predict(const double x, double& y, double& y_err, |
---|
84 | const double w=1.0) const |
---|
85 | { |
---|
86 | y = alpha_ + beta_ * (x-m_x_); |
---|
87 | y_err = sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)+s2_/w ); |
---|
88 | } |
---|
89 | |
---|
90 | /// |
---|
91 | /// Function returning the coefficient of determination, |
---|
92 | /// i.e. share of variance explained by the linear model. |
---|
93 | /// |
---|
94 | inline double r2(void) const { return r2_; } |
---|
95 | |
---|
96 | private: |
---|
97 | double alpha_; |
---|
98 | double alpha_var_; |
---|
99 | double beta_; |
---|
100 | double beta_var_; |
---|
101 | double m_x_; // average of x values |
---|
102 | double s2_; // var(y|x) |
---|
103 | double r2_; // coefficient of determination |
---|
104 | }; |
---|
105 | |
---|
106 | }} // of namespace statistics and namespace theplu |
---|
107 | |
---|
108 | #endif |
---|
109 | |
---|