1 | #ifndef _theplu_yat_regression_linearweighted_ |
---|
2 | #define _theplu_yat_regression_linearweighted_ |
---|
3 | |
---|
4 | // $Id: LinearWeighted.h 3076 2013-09-05 08:01:19Z 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, 2008 Jari Häkkinen, Peter Johansson |
---|
10 | Copyright (C) 2012 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>. |
---|
26 | */ |
---|
27 | |
---|
28 | #include "OneDimensionalWeighted.h" |
---|
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 | class LinearWeighted : public OneDimensionalWeighted |
---|
41 | { |
---|
42 | |
---|
43 | public: |
---|
44 | /// |
---|
45 | /// @brief The default constructor. |
---|
46 | /// |
---|
47 | LinearWeighted(void); |
---|
48 | |
---|
49 | /// |
---|
50 | /// @brief The destructor |
---|
51 | /// |
---|
52 | virtual ~LinearWeighted(void); |
---|
53 | |
---|
54 | /** |
---|
55 | \f$ alpha \f$ is estimated as \f$ \frac{\sum w_iy_i}{\sum w_i} \f$ |
---|
56 | |
---|
57 | @return the parameter \f$ \alpha \f$ |
---|
58 | */ |
---|
59 | double alpha(void) const; |
---|
60 | |
---|
61 | /** |
---|
62 | Variance is estimated as \f$ \frac{s^2}{\sum w_i} \f$ |
---|
63 | |
---|
64 | @see s2() |
---|
65 | |
---|
66 | @return variance of parameter \f$ \alpha \f$ |
---|
67 | */ |
---|
68 | double alpha_var(void) const; |
---|
69 | |
---|
70 | /** |
---|
71 | \f$ beta \f$ is estimated as \f$ \frac{\sum |
---|
72 | w_i(y_i-m_y)(x_i-m_x)}{\sum w_i(x_i-m_x)^2} \f$ |
---|
73 | |
---|
74 | @return the parameter \f$ \beta \f$ |
---|
75 | */ |
---|
76 | double beta(void) const; |
---|
77 | |
---|
78 | /** |
---|
79 | Variance is estimated as \f$ \frac{s^2}{\sum w_i(x_i-m_x)^2} \f$ |
---|
80 | |
---|
81 | @see s2() |
---|
82 | |
---|
83 | @return variance of parameter \f$ \beta \f$ |
---|
84 | */ |
---|
85 | double beta_var(void) const; |
---|
86 | |
---|
87 | /** |
---|
88 | This function computes the best-fit linear regression |
---|
89 | coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y = |
---|
90 | \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by |
---|
91 | minimizing \f$ \sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$, |
---|
92 | where \f$ m_x \f$ is the weighted average. By construction \f$ |
---|
93 | \alpha \f$ and \f$ \beta \f$ are independent. |
---|
94 | **/ |
---|
95 | void fit(const utility::VectorBase& x, const utility::VectorBase& y, |
---|
96 | const utility::VectorBase& w); |
---|
97 | |
---|
98 | /// |
---|
99 | /// Function predicting value using the linear model: |
---|
100 | /// \f$ y =\alpha + \beta (x - m) \f$ |
---|
101 | /// |
---|
102 | double predict(const double x) const; |
---|
103 | |
---|
104 | /** |
---|
105 | Noise level for points with weight @a w. |
---|
106 | */ |
---|
107 | double s2(double w=1) const; |
---|
108 | |
---|
109 | /** |
---|
110 | estimated error \f$ y_{err} = \sqrt{ Var(\alpha) + |
---|
111 | Var(\beta)*(x-m)} \f$. |
---|
112 | */ |
---|
113 | double standard_error2(const double x) const; |
---|
114 | |
---|
115 | private: |
---|
116 | /// |
---|
117 | /// Copy Constructor. (not implemented) |
---|
118 | /// |
---|
119 | LinearWeighted(const LinearWeighted&); |
---|
120 | |
---|
121 | double m_x(void) const; |
---|
122 | double m_y(void) const; |
---|
123 | double sxx(void) const; |
---|
124 | double syy(void) const; |
---|
125 | double sxy(void) const; |
---|
126 | |
---|
127 | double alpha_; |
---|
128 | double alpha_var_; |
---|
129 | double beta_; |
---|
130 | double beta_var_; |
---|
131 | }; |
---|
132 | |
---|
133 | }}} // of namespaces regression, yat, and theplu |
---|
134 | |
---|
135 | #endif |
---|