681 |
11 Oct 06 |
jari |
1 |
#ifndef _theplu_yat_regression_linearweighted_ |
681 |
11 Oct 06 |
jari |
2 |
#define _theplu_yat_regression_linearweighted_ |
429 |
08 Dec 05 |
peter |
3 |
|
616 |
31 Aug 06 |
jari |
// $Id$ |
616 |
31 Aug 06 |
jari |
5 |
|
675 |
10 Oct 06 |
jari |
6 |
/* |
831 |
27 Mar 07 |
peter |
Copyright (C) 2005 Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
429 |
08 Dec 05 |
peter |
11 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
675 |
10 Oct 06 |
jari |
13 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
675 |
10 Oct 06 |
jari |
18 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
675 |
10 Oct 06 |
jari |
General Public License for more details. |
675 |
10 Oct 06 |
jari |
23 |
|
675 |
10 Oct 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
675 |
10 Oct 06 |
jari |
26 |
*/ |
675 |
10 Oct 06 |
jari |
27 |
|
680 |
11 Oct 06 |
jari |
28 |
#include "OneDimensionalWeighted.h" |
675 |
10 Oct 06 |
jari |
29 |
|
429 |
08 Dec 05 |
peter |
30 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
31 |
namespace yat { |
616 |
31 Aug 06 |
jari |
32 |
namespace utility { |
1020 |
01 Feb 08 |
peter |
33 |
class VectorBase; |
616 |
31 Aug 06 |
jari |
34 |
} |
429 |
08 Dec 05 |
peter |
35 |
namespace regression { |
429 |
08 Dec 05 |
peter |
36 |
|
429 |
08 Dec 05 |
peter |
37 |
/// |
2842 |
17 Sep 12 |
peter |
/// @brief linear regression. |
429 |
08 Dec 05 |
peter |
39 |
/// |
2842 |
17 Sep 12 |
peter |
40 |
class LinearWeighted : public OneDimensionalWeighted |
429 |
08 Dec 05 |
peter |
41 |
{ |
2842 |
17 Sep 12 |
peter |
42 |
|
429 |
08 Dec 05 |
peter |
43 |
public: |
429 |
08 Dec 05 |
peter |
44 |
/// |
703 |
18 Dec 06 |
jari |
/// @brief The default constructor. |
429 |
08 Dec 05 |
peter |
46 |
/// |
703 |
18 Dec 06 |
jari |
47 |
LinearWeighted(void); |
429 |
08 Dec 05 |
peter |
48 |
|
429 |
08 Dec 05 |
peter |
49 |
/// |
703 |
18 Dec 06 |
jari |
/// @brief The destructor |
429 |
08 Dec 05 |
peter |
51 |
/// |
703 |
18 Dec 06 |
jari |
52 |
virtual ~LinearWeighted(void); |
2842 |
17 Sep 12 |
peter |
53 |
|
730 |
06 Jan 07 |
peter |
54 |
/** |
730 |
06 Jan 07 |
peter |
\f$ alpha \f$ is estimated as \f$ \frac{\sum w_iy_i}{\sum w_i} \f$ |
2842 |
17 Sep 12 |
peter |
56 |
|
730 |
06 Jan 07 |
peter |
@return the parameter \f$ \alpha \f$ |
730 |
06 Jan 07 |
peter |
58 |
*/ |
718 |
26 Dec 06 |
jari |
59 |
double alpha(void) const; |
429 |
08 Dec 05 |
peter |
60 |
|
730 |
06 Jan 07 |
peter |
61 |
/** |
730 |
06 Jan 07 |
peter |
Variance is estimated as \f$ \frac{s^2}{\sum w_i} \f$ |
730 |
06 Jan 07 |
peter |
63 |
|
730 |
06 Jan 07 |
peter |
@see s2() |
730 |
06 Jan 07 |
peter |
65 |
|
730 |
06 Jan 07 |
peter |
@return variance of parameter \f$ \alpha \f$ |
730 |
06 Jan 07 |
peter |
67 |
*/ |
729 |
05 Jan 07 |
peter |
68 |
double alpha_var(void) const; |
429 |
08 Dec 05 |
peter |
69 |
|
730 |
06 Jan 07 |
peter |
70 |
/** |
730 |
06 Jan 07 |
peter |
\f$ beta \f$ is estimated as \f$ \frac{\sum |
730 |
06 Jan 07 |
peter |
w_i(y_i-m_y)(x_i-m_x)}{\sum w_i(x_i-m_x)^2} \f$ |
2842 |
17 Sep 12 |
peter |
73 |
|
730 |
06 Jan 07 |
peter |
@return the parameter \f$ \beta \f$ |
730 |
06 Jan 07 |
peter |
75 |
*/ |
718 |
26 Dec 06 |
jari |
76 |
double beta(void) const; |
429 |
08 Dec 05 |
peter |
77 |
|
730 |
06 Jan 07 |
peter |
78 |
/** |
730 |
06 Jan 07 |
peter |
Variance is estimated as \f$ \frac{s^2}{\sum w_i(x_i-m_x)^2} \f$ |
730 |
06 Jan 07 |
peter |
80 |
|
730 |
06 Jan 07 |
peter |
@see s2() |
730 |
06 Jan 07 |
peter |
82 |
|
730 |
06 Jan 07 |
peter |
@return variance of parameter \f$ \beta \f$ |
730 |
06 Jan 07 |
peter |
84 |
*/ |
729 |
05 Jan 07 |
peter |
85 |
double beta_var(void) const; |
2842 |
17 Sep 12 |
peter |
86 |
|
639 |
06 Sep 06 |
markus |
87 |
/** |
639 |
06 Sep 06 |
markus |
This function computes the best-fit linear regression |
639 |
06 Sep 06 |
markus |
coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y = |
639 |
06 Sep 06 |
markus |
\alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by |
639 |
06 Sep 06 |
markus |
minimizing \f$ \sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$, |
639 |
06 Sep 06 |
markus |
where \f$ m_x \f$ is the weighted average. By construction \f$ |
639 |
06 Sep 06 |
markus |
\alpha \f$ and \f$ \beta \f$ are independent. |
639 |
06 Sep 06 |
markus |
94 |
**/ |
1020 |
01 Feb 08 |
peter |
95 |
void fit(const utility::VectorBase& x, const utility::VectorBase& y, |
1020 |
01 Feb 08 |
peter |
96 |
const utility::VectorBase& w); |
2842 |
17 Sep 12 |
peter |
97 |
|
429 |
08 Dec 05 |
peter |
98 |
/// |
2842 |
17 Sep 12 |
peter |
/// Function predicting value using the linear model: |
639 |
06 Sep 06 |
markus |
/// \f$ y =\alpha + \beta (x - m) \f$ |
639 |
06 Sep 06 |
markus |
101 |
/// |
730 |
06 Jan 07 |
peter |
102 |
double predict(const double x) const; |
429 |
08 Dec 05 |
peter |
103 |
|
639 |
06 Sep 06 |
markus |
104 |
/** |
718 |
26 Dec 06 |
jari |
Noise level for points with weight @a w. |
702 |
26 Oct 06 |
peter |
106 |
*/ |
718 |
26 Dec 06 |
jari |
107 |
double s2(double w=1) const; |
429 |
08 Dec 05 |
peter |
108 |
|
702 |
26 Oct 06 |
peter |
109 |
/** |
718 |
26 Dec 06 |
jari |
estimated error \f$ y_{err} = \sqrt{ Var(\alpha) + |
718 |
26 Dec 06 |
jari |
Var(\beta)*(x-m)} \f$. |
702 |
26 Oct 06 |
peter |
112 |
*/ |
729 |
05 Jan 07 |
peter |
113 |
double standard_error2(const double x) const; |
429 |
08 Dec 05 |
peter |
114 |
|
429 |
08 Dec 05 |
peter |
115 |
private: |
429 |
08 Dec 05 |
peter |
116 |
/// |
429 |
08 Dec 05 |
peter |
/// Copy Constructor. (not implemented) |
429 |
08 Dec 05 |
peter |
118 |
/// |
429 |
08 Dec 05 |
peter |
119 |
LinearWeighted(const LinearWeighted&); |
429 |
08 Dec 05 |
peter |
120 |
|
718 |
26 Dec 06 |
jari |
121 |
double m_x(void) const; |
718 |
26 Dec 06 |
jari |
122 |
double m_y(void) const; |
718 |
26 Dec 06 |
jari |
123 |
double sxx(void) const; |
718 |
26 Dec 06 |
jari |
124 |
double syy(void) const; |
718 |
26 Dec 06 |
jari |
125 |
double sxy(void) const; |
2842 |
17 Sep 12 |
peter |
126 |
|
429 |
08 Dec 05 |
peter |
127 |
double alpha_; |
429 |
08 Dec 05 |
peter |
128 |
double alpha_var_; |
429 |
08 Dec 05 |
peter |
129 |
double beta_; |
429 |
08 Dec 05 |
peter |
130 |
double beta_var_; |
429 |
08 Dec 05 |
peter |
131 |
}; |
429 |
08 Dec 05 |
peter |
132 |
|
681 |
11 Oct 06 |
jari |
133 |
}}} // of namespaces regression, yat, and theplu |
429 |
08 Dec 05 |
peter |
134 |
|
429 |
08 Dec 05 |
peter |
135 |
#endif |