1 | // $Id: Linear.h 430 2005-12-08 22:53:08Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_statistics_regression_linear_ |
---|
4 | #define _theplu_statistics_regression_linear_ |
---|
5 | |
---|
6 | #include <c++_tools/statistics/OneDimensional.h> |
---|
7 | #include <c++_tools/gslapi/vector.h> |
---|
8 | |
---|
9 | #include <cmath> |
---|
10 | |
---|
11 | namespace theplu { |
---|
12 | namespace statistics { |
---|
13 | namespace regression { |
---|
14 | |
---|
15 | /// |
---|
16 | /// @brief linear regression. |
---|
17 | /// |
---|
18 | /// @todo document |
---|
19 | /// |
---|
20 | class Linear : public OneDimensional |
---|
21 | { |
---|
22 | |
---|
23 | public: |
---|
24 | /// |
---|
25 | /// Default Constructor. |
---|
26 | /// |
---|
27 | inline Linear(void) |
---|
28 | : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0), |
---|
29 | m_x_(0), s2_(0) {} |
---|
30 | |
---|
31 | /// |
---|
32 | /// Destructor |
---|
33 | /// |
---|
34 | inline virtual ~Linear(void) {}; |
---|
35 | |
---|
36 | /// |
---|
37 | /// @return the parameter \f$ \alpha \f$ |
---|
38 | /// |
---|
39 | inline double alpha(void) const { return alpha_; } |
---|
40 | |
---|
41 | /// |
---|
42 | /// @return standard deviation of parameter \f$ \alpha \f$ |
---|
43 | /// |
---|
44 | inline double alpha_err(void) const { return sqrt(alpha_var_); } |
---|
45 | |
---|
46 | /// |
---|
47 | /// @return the parameter \f$ \beta \f$ |
---|
48 | /// |
---|
49 | inline double beta(void) const { return beta_; } |
---|
50 | |
---|
51 | /// |
---|
52 | /// @return standard deviation of parameter \f$ \beta \f$ |
---|
53 | /// |
---|
54 | inline double beta_err(void) const { return sqrt(beta_var_); } |
---|
55 | |
---|
56 | /// |
---|
57 | /// This function computes the best-fit linear regression |
---|
58 | /// coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y = |
---|
59 | /// \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by |
---|
60 | /// minimizing \f$ \sum{(y_i - \alpha - \beta (x-m_x))^2} \f$. By |
---|
61 | /// construction \f$ \alpha \f$ and \f$ \beta \f$ are independent. |
---|
62 | /// |
---|
63 | void fit(const gslapi::vector& x, const gslapi::vector& y) ; |
---|
64 | |
---|
65 | /// |
---|
66 | /// Function predicting value using the linear model. \a y_err is |
---|
67 | /// the expected deviation from the line for a new data point. |
---|
68 | /// |
---|
69 | void predict(const double x, double& y, double& y_err) const; |
---|
70 | |
---|
71 | /// |
---|
72 | /// @return prediction value and parameters |
---|
73 | /// |
---|
74 | std::ostream& print(std::ostream&) const; |
---|
75 | |
---|
76 | /// |
---|
77 | /// @return header for print() |
---|
78 | /// |
---|
79 | std::ostream& print_header(std::ostream&) const; |
---|
80 | |
---|
81 | /// |
---|
82 | /// Function returning the coefficient of determination, |
---|
83 | /// i.e. fraction of variance explained by the linear model. |
---|
84 | /// @todo implement r2's calculation in fit function |
---|
85 | /// |
---|
86 | inline double r2(void) const { return r2_; } |
---|
87 | |
---|
88 | private: |
---|
89 | /// |
---|
90 | /// Copy Constructor. (not implemented) |
---|
91 | /// |
---|
92 | Linear(const Linear&); |
---|
93 | |
---|
94 | double alpha_; |
---|
95 | double alpha_var_; |
---|
96 | double beta_; |
---|
97 | double beta_var_; |
---|
98 | double m_x_; // average of x values |
---|
99 | double s2_; // var(y|x) |
---|
100 | double r2_; // coefficient of determination |
---|
101 | }; |
---|
102 | |
---|
103 | }}} // of namespaces regression, statisitcs and thep |
---|
104 | |
---|
105 | #endif |
---|