1 | #ifndef _theplu_yat_regression_linear_ |
---|
2 | #define _theplu_yat_regression_linear_ |
---|
3 | |
---|
4 | // $Id: Linear.h 4078 2021-08-26 06:52:34Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2004, 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2010 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "OneDimensional.h" |
---|
27 | |
---|
28 | #include <cmath> |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace utility { |
---|
33 | class VectorBase; |
---|
34 | } |
---|
35 | namespace regression { |
---|
36 | |
---|
37 | /** |
---|
38 | @brief linear regression. |
---|
39 | |
---|
40 | Data are modeled as \f$ y_i = \alpha + \beta (x_i-m_x) + |
---|
41 | \epsilon_i \f$. |
---|
42 | */ |
---|
43 | class Linear : public OneDimensional |
---|
44 | { |
---|
45 | |
---|
46 | public: |
---|
47 | /// |
---|
48 | /// @brief The default constructor |
---|
49 | /// |
---|
50 | Linear(void); |
---|
51 | |
---|
52 | /// |
---|
53 | /// @brief The destructor |
---|
54 | /// |
---|
55 | virtual ~Linear(void); |
---|
56 | |
---|
57 | /** |
---|
58 | The parameter \f$ \alpha \f$ is estimated as \f$ |
---|
59 | \frac{1}{n}\sum y_i \f$ |
---|
60 | |
---|
61 | @return the parameter \f$ \alpha \f$ |
---|
62 | */ |
---|
63 | double alpha(void) const; |
---|
64 | |
---|
65 | /** |
---|
66 | The variance is estimated as \f$ \frac{s^2}{n} |
---|
67 | \f$ where \f$ s^2 = \frac{\sum \epsilon^2}{n-2} \f$ |
---|
68 | |
---|
69 | @return variance of parameter \f$ \alpha \f$ |
---|
70 | */ |
---|
71 | double alpha_var(void) const; |
---|
72 | |
---|
73 | /** |
---|
74 | The parameter \f$ \beta \f$ is estimated as \f$ |
---|
75 | \frac{\textrm{Cov}(x,y)}{\textrm{Var}(x)} \f$ |
---|
76 | |
---|
77 | @return the parameter \f$ \beta \f$ |
---|
78 | */ |
---|
79 | double beta(void) const; |
---|
80 | |
---|
81 | /** |
---|
82 | The variance is estimated as \f$ \frac{s^2}{\sum |
---|
83 | (x-m_x)^2} \f$ where \f$ s^2 = \frac{\sum \epsilon^2}{n-2} \f$ |
---|
84 | |
---|
85 | @return variance of parameter \f$ \beta \f$ |
---|
86 | */ |
---|
87 | double beta_var(void) const; |
---|
88 | |
---|
89 | /** |
---|
90 | Model is fitted by minimizing \f$ \sum{(y_i - \alpha - \beta |
---|
91 | (x-m_x))^2} \f$. By construction \f$ \alpha \f$ and \f$ \beta \f$ |
---|
92 | are independent. |
---|
93 | */ |
---|
94 | void fit(const utility::VectorBase& x, const utility::VectorBase& y) ; |
---|
95 | |
---|
96 | /// |
---|
97 | /// @return \f$ \alpha + \beta x \f$ |
---|
98 | /// |
---|
99 | double predict(const double x) const; |
---|
100 | |
---|
101 | /** |
---|
102 | \f$ \frac{\sum \epsilon_i^2}{N-2} \f$ |
---|
103 | |
---|
104 | @return variance of residuals |
---|
105 | */ |
---|
106 | double s2(void) const; |
---|
107 | |
---|
108 | /** |
---|
109 | The error of the model is estimated as \f$ |
---|
110 | \textrm{alpha\_err}^2+\textrm{beta\_err}^2*(x-m_x)*(x-m_x)\f$ |
---|
111 | |
---|
112 | @return estimated error of model in @a x |
---|
113 | */ |
---|
114 | double standard_error2(const double x) const; |
---|
115 | |
---|
116 | |
---|
117 | private: |
---|
118 | /// |
---|
119 | /// Copy Constructor. (not implemented) |
---|
120 | /// |
---|
121 | Linear(const Linear&); |
---|
122 | |
---|
123 | double alpha_; |
---|
124 | double alpha_var_; |
---|
125 | double beta_; |
---|
126 | double beta_var_; |
---|
127 | }; |
---|
128 | |
---|
129 | }}} // of namespaces regression, yat, and theplu |
---|
130 | |
---|
131 | #endif |
---|