1 | #ifndef _theplu_yat_regression_linearweighted_ |
---|
2 | #define _theplu_yat_regression_linearweighted_ |
---|
3 | |
---|
4 | // $Id: LinearWeighted.h 1437 2008-08-25 17:55:00Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2005 Peter Johansson |
---|
8 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
9 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
10 | Copyright (C) 2008 Peter Johansson |
---|
11 | |
---|
12 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
13 | |
---|
14 | The yat library is free software; you can redistribute it and/or |
---|
15 | modify it under the terms of the GNU General Public License as |
---|
16 | published by the Free Software Foundation; either version 2 of the |
---|
17 | License, or (at your option) any later version. |
---|
18 | |
---|
19 | The yat library is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU General Public License |
---|
25 | along with this program; if not, write to the Free Software |
---|
26 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
27 | 02111-1307, USA. |
---|
28 | */ |
---|
29 | |
---|
30 | #include "OneDimensionalWeighted.h" |
---|
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 | /// @todo document |
---|
43 | /// |
---|
44 | class LinearWeighted : public OneDimensionalWeighted |
---|
45 | { |
---|
46 | |
---|
47 | public: |
---|
48 | /// |
---|
49 | /// @brief The default constructor. |
---|
50 | /// |
---|
51 | LinearWeighted(void); |
---|
52 | |
---|
53 | /// |
---|
54 | /// @brief The destructor |
---|
55 | /// |
---|
56 | virtual ~LinearWeighted(void); |
---|
57 | |
---|
58 | /** |
---|
59 | \f$ alpha \f$ is estimated as \f$ \frac{\sum w_iy_i}{\sum w_i} \f$ |
---|
60 | |
---|
61 | @return the parameter \f$ \alpha \f$ |
---|
62 | */ |
---|
63 | double alpha(void) const; |
---|
64 | |
---|
65 | /** |
---|
66 | Variance is estimated as \f$ \frac{s^2}{\sum w_i} \f$ |
---|
67 | |
---|
68 | @see s2() |
---|
69 | |
---|
70 | @return variance of parameter \f$ \alpha \f$ |
---|
71 | */ |
---|
72 | double alpha_var(void) const; |
---|
73 | |
---|
74 | /** |
---|
75 | \f$ beta \f$ is estimated as \f$ \frac{\sum |
---|
76 | w_i(y_i-m_y)(x_i-m_x)}{\sum w_i(x_i-m_x)^2} \f$ |
---|
77 | |
---|
78 | @return the parameter \f$ \beta \f$ |
---|
79 | */ |
---|
80 | double beta(void) const; |
---|
81 | |
---|
82 | /** |
---|
83 | Variance is estimated as \f$ \frac{s^2}{\sum w_i(x_i-m_x)^2} \f$ |
---|
84 | |
---|
85 | @see s2() |
---|
86 | |
---|
87 | @return variance of parameter \f$ \beta \f$ |
---|
88 | */ |
---|
89 | double beta_var(void) const; |
---|
90 | |
---|
91 | /** |
---|
92 | This function computes the best-fit linear regression |
---|
93 | coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y = |
---|
94 | \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by |
---|
95 | minimizing \f$ \sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$, |
---|
96 | where \f$ m_x \f$ is the weighted average. By construction \f$ |
---|
97 | \alpha \f$ and \f$ \beta \f$ are independent. |
---|
98 | **/ |
---|
99 | /// @todo calculate mse |
---|
100 | void fit(const utility::VectorBase& x, const utility::VectorBase& y, |
---|
101 | const utility::VectorBase& w); |
---|
102 | |
---|
103 | /// |
---|
104 | /// Function predicting value using the linear model: |
---|
105 | /// \f$ y =\alpha + \beta (x - m) \f$ |
---|
106 | /// |
---|
107 | double predict(const double x) const; |
---|
108 | |
---|
109 | /** |
---|
110 | Noise level for points with weight @a w. |
---|
111 | */ |
---|
112 | double s2(double w=1) const; |
---|
113 | |
---|
114 | /** |
---|
115 | estimated error \f$ y_{err} = \sqrt{ Var(\alpha) + |
---|
116 | Var(\beta)*(x-m)} \f$. |
---|
117 | */ |
---|
118 | double standard_error2(const double x) const; |
---|
119 | |
---|
120 | private: |
---|
121 | /// |
---|
122 | /// Copy Constructor. (not implemented) |
---|
123 | /// |
---|
124 | LinearWeighted(const LinearWeighted&); |
---|
125 | |
---|
126 | double m_x(void) const; |
---|
127 | double m_y(void) const; |
---|
128 | double sxx(void) const; |
---|
129 | double syy(void) const; |
---|
130 | double sxy(void) const; |
---|
131 | |
---|
132 | double alpha_; |
---|
133 | double alpha_var_; |
---|
134 | double beta_; |
---|
135 | double beta_var_; |
---|
136 | }; |
---|
137 | |
---|
138 | }}} // of namespaces regression, yat, and theplu |
---|
139 | |
---|
140 | #endif |
---|