1 | #ifndef _theplu_yat_regression_linear_ |
---|
2 | #define _theplu_yat_regression_linear_ |
---|
3 | |
---|
4 | // $Id: Linear.h 703 2006-12-18 00:47:44Z 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 "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 | /// @todo document |
---|
42 | /// |
---|
43 | class Linear : public OneDimensional |
---|
44 | { |
---|
45 | |
---|
46 | public: |
---|
47 | /// |
---|
48 | /// @brief The default constructor |
---|
49 | /// |
---|
50 | Linear(void); |
---|
51 | |
---|
52 | /// |
---|
53 | /// @brief The destructor |
---|
54 | /// |
---|
55 | virtual ~Linear(void); |
---|
56 | |
---|
57 | /// |
---|
58 | /// @return the parameter \f$ \alpha \f$ |
---|
59 | /// |
---|
60 | inline double alpha(void) const { return alpha_; } |
---|
61 | |
---|
62 | /// |
---|
63 | /// @return standard deviation of parameter \f$ \alpha \f$ |
---|
64 | /// |
---|
65 | inline double alpha_err(void) const { return sqrt(alpha_var_); } |
---|
66 | |
---|
67 | /// |
---|
68 | /// @return the parameter \f$ \beta \f$ |
---|
69 | /// |
---|
70 | inline double beta(void) const { return beta_; } |
---|
71 | |
---|
72 | /// |
---|
73 | /// @return standard deviation of parameter \f$ \beta \f$ |
---|
74 | /// |
---|
75 | inline double beta_err(void) const { return sqrt(beta_var_); } |
---|
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{(y_i - \alpha - \beta (x-m_x))^2} \f$. By |
---|
82 | /// construction \f$ \alpha \f$ and \f$ \beta \f$ are independent. |
---|
83 | /// |
---|
84 | void fit(const utility::vector& x, const utility::vector& y) ; |
---|
85 | |
---|
86 | /// |
---|
87 | /// @brief Mean Squared Error |
---|
88 | /// |
---|
89 | inline double mse(void) const { return mse_; } |
---|
90 | |
---|
91 | /// |
---|
92 | /// @return value in @a x of model |
---|
93 | /// |
---|
94 | double predict(const double x) const; |
---|
95 | |
---|
96 | /// |
---|
97 | /// @return error of model value in @a x |
---|
98 | /// |
---|
99 | double standard_error(const double x) const; |
---|
100 | |
---|
101 | /// |
---|
102 | /// Function returning the coefficient of determination, |
---|
103 | /// i.e. fraction of variance explained by the linear model. |
---|
104 | /// |
---|
105 | inline double r2(void) const { return r2_; } |
---|
106 | |
---|
107 | private: |
---|
108 | /// |
---|
109 | /// Copy Constructor. (not implemented) |
---|
110 | /// |
---|
111 | Linear(const Linear&); |
---|
112 | |
---|
113 | double alpha_; |
---|
114 | double alpha_var_; |
---|
115 | double beta_; |
---|
116 | double beta_var_; |
---|
117 | double mse_; |
---|
118 | double m_x_; // average of x values |
---|
119 | double r2_; // coefficient of determination |
---|
120 | }; |
---|
121 | |
---|
122 | }}} // of namespaces regression, yat, and theplu |
---|
123 | |
---|
124 | #endif |
---|