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