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