681 |
11 Oct 06 |
jari |
1 |
#ifndef _theplu_yat_regression_onedimensioanlweighted_ |
681 |
11 Oct 06 |
jari |
2 |
#define _theplu_yat_regression_onedimensioanlweighted_ |
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 |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
675 |
10 Oct 06 |
jari |
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 |
|
702 |
26 Oct 06 |
peter |
28 |
#include "yat/statistics/AveragerPairWeighted.h" |
702 |
26 Oct 06 |
peter |
29 |
|
429 |
08 Dec 05 |
peter |
30 |
#include <ostream> |
429 |
08 Dec 05 |
peter |
31 |
|
429 |
08 Dec 05 |
peter |
32 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
33 |
namespace yat { |
616 |
31 Aug 06 |
jari |
34 |
namespace utility { |
1020 |
01 Feb 08 |
peter |
35 |
class VectorBase; |
429 |
08 Dec 05 |
peter |
36 |
} |
429 |
08 Dec 05 |
peter |
37 |
namespace regression { |
4200 |
19 Aug 22 |
peter |
38 |
|
429 |
08 Dec 05 |
peter |
39 |
/// |
767 |
22 Feb 07 |
peter |
/// @brief Interface Class for One Dimensional fitting in a weighted |
702 |
26 Oct 06 |
peter |
/// fashion. |
429 |
08 Dec 05 |
peter |
42 |
/// |
4200 |
19 Aug 22 |
peter |
43 |
class OneDimensionalWeighted |
429 |
08 Dec 05 |
peter |
44 |
{ |
4200 |
19 Aug 22 |
peter |
45 |
|
429 |
08 Dec 05 |
peter |
46 |
public: |
702 |
26 Oct 06 |
peter |
47 |
/// |
702 |
26 Oct 06 |
peter |
/// Default Constructor. |
702 |
26 Oct 06 |
peter |
49 |
/// |
729 |
05 Jan 07 |
peter |
50 |
OneDimensionalWeighted(void); |
429 |
08 Dec 05 |
peter |
51 |
|
429 |
08 Dec 05 |
peter |
52 |
/// |
429 |
08 Dec 05 |
peter |
/// Destructor |
429 |
08 Dec 05 |
peter |
54 |
/// |
729 |
05 Jan 07 |
peter |
55 |
virtual ~OneDimensionalWeighted(void); |
4200 |
19 Aug 22 |
peter |
56 |
|
696 |
25 Oct 06 |
peter |
57 |
/** |
696 |
25 Oct 06 |
peter |
This function computes the best-fit given a model (see |
696 |
25 Oct 06 |
peter |
specific class for details) by minimizing \f$ |
696 |
25 Oct 06 |
peter |
\sum{w_i(\hat{y_i}-y_i)^2} \f$, where \f$ \hat{y} \f$ is the |
696 |
25 Oct 06 |
peter |
fitted value. The weight \f$ w_i \f$ should be proportional |
696 |
25 Oct 06 |
peter |
to the inverse of the variance for \f$ y_i \f$ |
696 |
25 Oct 06 |
peter |
63 |
*/ |
4200 |
19 Aug 22 |
peter |
64 |
virtual void fit(const utility::VectorBase& x, const utility::VectorBase& y, |
1020 |
01 Feb 08 |
peter |
65 |
const utility::VectorBase& w)=0; |
586 |
19 Jun 06 |
peter |
66 |
|
429 |
08 Dec 05 |
peter |
67 |
/// |
702 |
26 Oct 06 |
peter |
/// @return expected value in @a x according to the fitted model |
429 |
08 Dec 05 |
peter |
69 |
/// |
586 |
19 Jun 06 |
peter |
70 |
virtual double predict(const double x) const=0; |
586 |
19 Jun 06 |
peter |
71 |
|
702 |
26 Oct 06 |
peter |
72 |
/** |
729 |
05 Jan 07 |
peter |
The prediction error is defined as expected squared deviation a |
729 |
05 Jan 07 |
peter |
new data point (with weight @a w) will be from the model |
729 |
05 Jan 07 |
peter |
value \f$ E((Y|x - \hat{y}(x))^2|w) \f$ and is typically |
729 |
05 Jan 07 |
peter |
divided into the conditional variance ( see s2() ) |
729 |
05 Jan 07 |
peter |
given \f$ x \f$ and the squared standard error ( see |
729 |
05 Jan 07 |
peter |
standard_error2() ) of the model estimation in \f$ x \f$. |
586 |
19 Jun 06 |
peter |
79 |
|
729 |
05 Jan 07 |
peter |
\f$ E((Y|x - E(Y|x))^2|w) + E((E(Y|x) - \hat{y}(x))^2) \f$ |
729 |
05 Jan 07 |
peter |
81 |
|
702 |
26 Oct 06 |
peter |
@return expected prediction error for a new data point in @a x |
729 |
05 Jan 07 |
peter |
with weight @a w. |
702 |
26 Oct 06 |
peter |
84 |
*/ |
4200 |
19 Aug 22 |
peter |
85 |
double prediction_error2(const double x, const double w=1.0) const; |
702 |
26 Oct 06 |
peter |
86 |
|
696 |
25 Oct 06 |
peter |
87 |
/** |
729 |
05 Jan 07 |
peter |
r2 is defined as \f$ \frac{\sum |
702 |
26 Oct 06 |
peter |
w_i(y_i-\hat{y}_i)^2}{\sum w_i(y_i-m_y)^2} \f$ or the fraction |
702 |
26 Oct 06 |
peter |
of the variance explained by the regression model. |
702 |
26 Oct 06 |
peter |
91 |
*/ |
4200 |
19 Aug 22 |
peter |
92 |
double r2(void) const; |
696 |
25 Oct 06 |
peter |
93 |
|
702 |
26 Oct 06 |
peter |
94 |
/** |
729 |
05 Jan 07 |
peter |
\f$ s^2 \f$ is the estimation of variance of residuals or |
729 |
05 Jan 07 |
peter |
equivalently the conditional variance of Y. |
702 |
26 Oct 06 |
peter |
97 |
|
729 |
05 Jan 07 |
peter |
@return Conditional variance of Y |
729 |
05 Jan 07 |
peter |
99 |
*/ |
729 |
05 Jan 07 |
peter |
100 |
virtual double s2(double w=1) const=0; |
729 |
05 Jan 07 |
peter |
101 |
|
729 |
05 Jan 07 |
peter |
102 |
/** |
729 |
05 Jan 07 |
peter |
The standard error is defined as \f$ E((Y|x,w - |
729 |
05 Jan 07 |
peter |
\hat{y}(x))^2) \f$ |
729 |
05 Jan 07 |
peter |
105 |
|
696 |
25 Oct 06 |
peter |
@return error of model value in @a x |
696 |
25 Oct 06 |
peter |
107 |
*/ |
729 |
05 Jan 07 |
peter |
108 |
virtual double standard_error2(const double x) const=0; |
586 |
19 Jun 06 |
peter |
109 |
|
702 |
26 Oct 06 |
peter |
110 |
protected: |
648 |
14 Sep 06 |
peter |
111 |
/// |
702 |
26 Oct 06 |
peter |
/// Averager for pair of x and y |
648 |
14 Sep 06 |
peter |
113 |
/// |
702 |
26 Oct 06 |
peter |
114 |
statistics::AveragerPairWeighted ap_; |
702 |
26 Oct 06 |
peter |
115 |
|
729 |
05 Jan 07 |
peter |
116 |
/** |
729 |
05 Jan 07 |
peter |
@brief Chi-squared |
4200 |
19 Aug 22 |
peter |
118 |
|
4200 |
19 Aug 22 |
peter |
Chi-squared is defined as the \f$ |
729 |
05 Jan 07 |
peter |
\sum{w_i(\hat{y_i}-y_i)^2} \f$ |
729 |
05 Jan 07 |
peter |
121 |
*/ |
729 |
05 Jan 07 |
peter |
122 |
double chisq_; |
729 |
05 Jan 07 |
peter |
123 |
|
702 |
26 Oct 06 |
peter |
124 |
private: |
429 |
08 Dec 05 |
peter |
125 |
}; |
429 |
08 Dec 05 |
peter |
126 |
|
681 |
11 Oct 06 |
jari |
127 |
}}} // of namespaces regression, yat, and theplu |
429 |
08 Dec 05 |
peter |
128 |
|
429 |
08 Dec 05 |
peter |
129 |
#endif |