1 | // $Id: RegressionNaive.h 216 2004-12-29 09:29:54Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_statistics_regression_linear_ |
---|
4 | #define _theplu_statistics_regression_linear_ |
---|
5 | |
---|
6 | // C++ tools include |
---|
7 | ///////////////////// |
---|
8 | #include "Averager.h" |
---|
9 | #include "Regression.h" |
---|
10 | #include "vector.h" |
---|
11 | #include "WeightedAverager.h" |
---|
12 | // Standard C++ includes |
---|
13 | //////////////////////// |
---|
14 | #include <gsl/gsl_fit.h> |
---|
15 | #include <utility> |
---|
16 | |
---|
17 | namespace theplu { |
---|
18 | namespace statistics { |
---|
19 | |
---|
20 | /// |
---|
21 | /// Class for Regression. |
---|
22 | /// |
---|
23 | |
---|
24 | |
---|
25 | class RegressionNaive : public Regression |
---|
26 | { |
---|
27 | |
---|
28 | public: |
---|
29 | /// |
---|
30 | /// Default Constructor. |
---|
31 | /// |
---|
32 | RegressionNaive(void); |
---|
33 | |
---|
34 | /// |
---|
35 | /// Copy Constructor. (not implemented) |
---|
36 | /// |
---|
37 | RegressionNaive(const RegressionNaive&); |
---|
38 | |
---|
39 | /// |
---|
40 | /// Destructor |
---|
41 | /// |
---|
42 | virtual ~RegressionNaive(void) {}; |
---|
43 | |
---|
44 | inline void estimate(const double x, double& y, double& y_err) |
---|
45 | { y=m_; y_err=sqrt(var_); } |
---|
46 | |
---|
47 | /// |
---|
48 | /// This function computes the best-fit for the naive model \f$ |
---|
49 | /// y = m \f$ from vectors \a x and \a y, by minimizing \f$ |
---|
50 | /// \sum{(y_i-m)^2} \f$. |
---|
51 | /// |
---|
52 | inline int fit(const gslapi::vector& x, const gslapi::vector& y) |
---|
53 | { |
---|
54 | Averager a; |
---|
55 | for (size_t i=0; i<y.size(); i++) |
---|
56 | a.add(y(i)); |
---|
57 | m_=a.mean(); |
---|
58 | var_=a.standard_error()*a.standard_error(); |
---|
59 | return 0; |
---|
60 | } |
---|
61 | |
---|
62 | /// |
---|
63 | /// This function computes the best-fit for the naive model \f$ y |
---|
64 | /// = m \f$ from vectors \a x and \a y, by minimizing \f$ \sum |
---|
65 | /// w_i(y_i-m)^2 \f$. The weight \f$ w_i \f$ is the inverse of the |
---|
66 | /// variance for \f$ y_i \f$ |
---|
67 | /// |
---|
68 | inline int fit_weighted(const gslapi::vector& x, |
---|
69 | const gslapi::vector& y, |
---|
70 | const gslapi::vector& w) |
---|
71 | { |
---|
72 | WeightedAverager a; |
---|
73 | for (size_t i=0; i<y.size(); i++) |
---|
74 | a.add(y(i), w(i)); |
---|
75 | m_=a.mean(); |
---|
76 | var_=a.standard_error()*a.standard_error(); |
---|
77 | return 0; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | private: |
---|
82 | double var_; |
---|
83 | double m_; |
---|
84 | |
---|
85 | }; |
---|
86 | |
---|
87 | }} // of namespace statistics and namespace theplu |
---|
88 | |
---|
89 | #endif |
---|
90 | |
---|