1 | #ifndef _theplu_statistics_regression_linear_ |
---|
2 | #define _theplu_statistics_regression_linear_ |
---|
3 | |
---|
4 | // $Id: Linear.h 675 2006-10-10 12:08:45Z 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 "yat/statistics/OneDimensional.h" |
---|
28 | |
---|
29 | #include <cmath> |
---|
30 | |
---|
31 | namespace theplu { |
---|
32 | namespace utility { |
---|
33 | class vector; |
---|
34 | } |
---|
35 | namespace statistics { |
---|
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 | /// Default Constructor. |
---|
49 | /// |
---|
50 | inline Linear(void) |
---|
51 | : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0), |
---|
52 | m_x_(0){} |
---|
53 | |
---|
54 | /// |
---|
55 | /// Destructor |
---|
56 | /// |
---|
57 | inline virtual ~Linear(void) {}; |
---|
58 | |
---|
59 | /// |
---|
60 | /// @return the parameter \f$ \alpha \f$ |
---|
61 | /// |
---|
62 | inline double alpha(void) const { return alpha_; } |
---|
63 | |
---|
64 | /// |
---|
65 | /// @return standard deviation of parameter \f$ \alpha \f$ |
---|
66 | /// |
---|
67 | inline double alpha_err(void) const { return sqrt(alpha_var_); } |
---|
68 | |
---|
69 | /// |
---|
70 | /// @return the parameter \f$ \beta \f$ |
---|
71 | /// |
---|
72 | inline double beta(void) const { return beta_; } |
---|
73 | |
---|
74 | /// |
---|
75 | /// @return standard deviation of parameter \f$ \beta \f$ |
---|
76 | /// |
---|
77 | inline double beta_err(void) const { return sqrt(beta_var_); } |
---|
78 | |
---|
79 | /// |
---|
80 | /// This function computes the best-fit linear regression |
---|
81 | /// coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y = |
---|
82 | /// \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by |
---|
83 | /// minimizing \f$ \sum{(y_i - \alpha - \beta (x-m_x))^2} \f$. By |
---|
84 | /// construction \f$ \alpha \f$ and \f$ \beta \f$ are independent. |
---|
85 | /// |
---|
86 | void fit(const utility::vector& x, const utility::vector& y) ; |
---|
87 | |
---|
88 | /// |
---|
89 | /// @return value in @a x of model |
---|
90 | /// |
---|
91 | double predict(const double x) const; |
---|
92 | |
---|
93 | /// |
---|
94 | /// @return expected prediction error for a new data point in @a x |
---|
95 | /// |
---|
96 | double prediction_error(const double x) const; |
---|
97 | |
---|
98 | /// |
---|
99 | /// @return error of model value in @a x |
---|
100 | /// |
---|
101 | double standard_error(const double x) const; |
---|
102 | |
---|
103 | /// |
---|
104 | /// Function returning the coefficient of determination, |
---|
105 | /// i.e. fraction of variance explained by the linear model. |
---|
106 | /// |
---|
107 | inline double r2(void) const { return r2_; } |
---|
108 | |
---|
109 | private: |
---|
110 | /// |
---|
111 | /// Copy Constructor. (not implemented) |
---|
112 | /// |
---|
113 | Linear(const Linear&); |
---|
114 | |
---|
115 | double alpha_; |
---|
116 | double alpha_var_; |
---|
117 | double beta_; |
---|
118 | double beta_var_; |
---|
119 | double m_x_; // average of x values |
---|
120 | double r2_; // coefficient of determination |
---|
121 | }; |
---|
122 | |
---|
123 | }}} // of namespaces regression, statisitcs and thep |
---|
124 | |
---|
125 | #endif |
---|