1 | #ifndef _theplu_yat_regression_linearweighted_ |
---|
2 | #define _theplu_yat_regression_linearweighted_ |
---|
3 | |
---|
4 | // $Id: LinearWeighted.h 718 2006-12-26 09:56:26Z jari $ |
---|
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 "OneDimensionalWeighted.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 | /// @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 | /// @return the parameter \f$ \alpha \f$ |
---|
59 | /// |
---|
60 | double alpha(void) const; |
---|
61 | |
---|
62 | /// |
---|
63 | /// @return standard deviation of parameter \f$ \alpha \f$ |
---|
64 | /// |
---|
65 | double alpha_err(void) const; |
---|
66 | |
---|
67 | /// |
---|
68 | /// @return the parameter \f$ \beta \f$ |
---|
69 | /// |
---|
70 | double beta(void) const; |
---|
71 | |
---|
72 | /// |
---|
73 | /// @return standard deviation of parameter \f$ \beta \f$ |
---|
74 | /// |
---|
75 | double beta_err(void) const; |
---|
76 | |
---|
77 | /** |
---|
78 | This function computes the best-fit linear regression |
---|
79 | coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y = |
---|
80 | \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by |
---|
81 | minimizing \f$ \sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$, |
---|
82 | where \f$ m_x \f$ is the weighted average. By construction \f$ |
---|
83 | \alpha \f$ and \f$ \beta \f$ are independent. |
---|
84 | **/ |
---|
85 | /// @todo calculate mse |
---|
86 | void fit(const utility::vector& x, const utility::vector& y, |
---|
87 | const utility::vector& w); |
---|
88 | |
---|
89 | /// |
---|
90 | /// @brief Mean Squared Error |
---|
91 | /// |
---|
92 | double mse(void) const; |
---|
93 | |
---|
94 | /// |
---|
95 | /// Function predicting value using the linear model: |
---|
96 | /// \f$ y =\alpha + \beta (x - m) \f$ |
---|
97 | /// |
---|
98 | double predict(const double x) const { return alpha_ + beta_ * (x-m_x_); } |
---|
99 | |
---|
100 | /// |
---|
101 | /// estimated deviation from predicted value for a new data point |
---|
102 | /// in @a x with weight @a w |
---|
103 | /// |
---|
104 | double prediction_error(const double x, const double w=1) const; |
---|
105 | |
---|
106 | /** |
---|
107 | Noise level for points with weight @a w. |
---|
108 | */ |
---|
109 | double s2(double w=1) const; |
---|
110 | |
---|
111 | /** |
---|
112 | estimated error \f$ y_{err} = \sqrt{ Var(\alpha) + |
---|
113 | Var(\beta)*(x-m)} \f$. |
---|
114 | */ |
---|
115 | double standard_error(const double x) const; |
---|
116 | |
---|
117 | private: |
---|
118 | /// |
---|
119 | /// Copy Constructor. (not implemented) |
---|
120 | /// |
---|
121 | LinearWeighted(const LinearWeighted&); |
---|
122 | |
---|
123 | double m_x(void) const; |
---|
124 | double m_y(void) const; |
---|
125 | double sxx(void) const; |
---|
126 | double syy(void) const; |
---|
127 | double sxy(void) const; |
---|
128 | |
---|
129 | double alpha_; |
---|
130 | double alpha_var_; |
---|
131 | double beta_; |
---|
132 | double beta_var_; |
---|
133 | double m_x_; // average of x values |
---|
134 | double r2_; // coefficient of determination |
---|
135 | double s2_; |
---|
136 | double mse_; |
---|
137 | }; |
---|
138 | |
---|
139 | }}} // of namespaces regression, yat, and theplu |
---|
140 | |
---|
141 | #endif |
---|