1 | #ifndef _theplu_yat_regression_linear_ |
---|
2 | #define _theplu_yat_regression_linear_ |
---|
3 | |
---|
4 | // $Id: Linear.h 1392 2008-07-28 19:35:30Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2004, 2005, 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2008 Peter Johansson |
---|
9 | |
---|
10 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
11 | |
---|
12 | The yat library is free software; you can redistribute it and/or |
---|
13 | modify it under the terms of the GNU General Public License as |
---|
14 | published by the Free Software Foundation; either version 2 of the |
---|
15 | License, or (at your option) any later version. |
---|
16 | |
---|
17 | The yat library is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
20 | General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License |
---|
23 | along with this program; if not, write to the Free Software |
---|
24 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
25 | 02111-1307, USA. |
---|
26 | */ |
---|
27 | |
---|
28 | #include "OneDimensional.h" |
---|
29 | |
---|
30 | #include <cmath> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace utility { |
---|
35 | class VectorBase; |
---|
36 | } |
---|
37 | namespace regression { |
---|
38 | |
---|
39 | /** |
---|
40 | @brief linear regression. |
---|
41 | |
---|
42 | Data are modeled as \f$ y_i = \alpha + \beta (x_i-m_x) + |
---|
43 | \epsilon_i \f$. |
---|
44 | */ |
---|
45 | class Linear : public OneDimensional |
---|
46 | { |
---|
47 | |
---|
48 | public: |
---|
49 | /// |
---|
50 | /// @brief The default constructor |
---|
51 | /// |
---|
52 | Linear(void); |
---|
53 | |
---|
54 | /// |
---|
55 | /// @brief The destructor |
---|
56 | /// |
---|
57 | virtual ~Linear(void); |
---|
58 | |
---|
59 | /** |
---|
60 | The parameter \f$ \alpha \f$ is estimated as \f$ |
---|
61 | \frac{1}{n}\sum y_i \f$ |
---|
62 | |
---|
63 | @return the parameter \f$ \alpha \f$ |
---|
64 | */ |
---|
65 | double alpha(void) const; |
---|
66 | |
---|
67 | /** |
---|
68 | The standard deviation is estimated as \f$ \sqrt{\frac{s^2}{n}} |
---|
69 | \f$ where \f$ s^2 = \frac{\sum \epsilon^2}{n-2} \f$ |
---|
70 | |
---|
71 | @return standard deviation of parameter \f$ \alpha \f$ |
---|
72 | */ |
---|
73 | double alpha_var(void) const; |
---|
74 | |
---|
75 | /** |
---|
76 | The parameter \f$ \beta \f$ is estimated as \f$ |
---|
77 | \frac{\textrm{Cov}(x,y)}{\textrm{Var}(x)} \f$ |
---|
78 | |
---|
79 | @return the parameter \f$ \beta \f$ |
---|
80 | */ |
---|
81 | double beta(void) const; |
---|
82 | |
---|
83 | /** |
---|
84 | The standard deviation is estimated as \f$ \frac{s^2}{\sum |
---|
85 | (x-m_x)^2} \f$ where \f$ s^2 = \frac{\sum \epsilon^2}{n-2} \f$ |
---|
86 | |
---|
87 | @return standard deviation of parameter \f$ \beta \f$ |
---|
88 | */ |
---|
89 | double beta_var(void) const; |
---|
90 | |
---|
91 | /** |
---|
92 | Model is fitted by minimizing \f$ \sum{(y_i - \alpha - \beta |
---|
93 | (x-m_x))^2} \f$. By construction \f$ \alpha \f$ and \f$ \beta \f$ |
---|
94 | are independent. |
---|
95 | */ |
---|
96 | void fit(const utility::VectorBase& x, const utility::VectorBase& y) ; |
---|
97 | |
---|
98 | /// |
---|
99 | /// @return \f$ \alpha + \beta x \f$ |
---|
100 | /// |
---|
101 | double predict(const double x) const; |
---|
102 | |
---|
103 | /// |
---|
104 | /// Function returning the coefficient of determination, |
---|
105 | /// i.e. fraction of variance explained by the linear model. |
---|
106 | /// |
---|
107 | double r2(void) const; |
---|
108 | |
---|
109 | /** |
---|
110 | \f$ \frac{\sum \epsilon_i^2}{N-2} \f$ |
---|
111 | |
---|
112 | @return variance of residuals |
---|
113 | */ |
---|
114 | double s2(void) const; |
---|
115 | |
---|
116 | /** |
---|
117 | The error of the model is estimated as \f$ |
---|
118 | \textrm{alpha\_err}^2+\textrm{beta\_err}^2*(x-m_x)*(x-m_x)\f$ |
---|
119 | |
---|
120 | @return estimated error of model in @a x |
---|
121 | */ |
---|
122 | double standard_error2(const double x) const; |
---|
123 | |
---|
124 | |
---|
125 | private: |
---|
126 | /// |
---|
127 | /// Copy Constructor. (not implemented) |
---|
128 | /// |
---|
129 | Linear(const Linear&); |
---|
130 | |
---|
131 | double alpha_; |
---|
132 | double alpha_var_; |
---|
133 | double beta_; |
---|
134 | double beta_var_; |
---|
135 | }; |
---|
136 | |
---|
137 | }}} // of namespaces regression, yat, and theplu |
---|
138 | |
---|
139 | #endif |
---|