source: trunk/src/RegressionNaive.h @ 221

Last change on this file since 221 was 221, checked in by Peter, 17 years ago

interface to regression modified

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.4 KB
Line 
1// $Id: RegressionNaive.h 221 2004-12-30 22:36:25Z peter $
2
3#ifndef _theplu_statistics_regression_naive_
4#define _theplu_statistics_regression_naive_
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
17namespace theplu {
18namespace statistics { 
19 
20  ///
21  /// Class for Regression.   
22  ///
23 
24  class RegressionNaive : public Regression
25  {
26 
27  public:
28    ///
29    /// Default Constructor.
30    ///
31    RegressionNaive(void);
32
33    ///
34    /// Copy Constructor. (not implemented)
35    ///
36    RegressionNaive(const RegressionNaive&);
37
38    ///
39    /// Destructor
40    ///
41    virtual ~RegressionNaive(void) {};
42         
43    ///
44    /// This function computes the best-fit for the naive model \f$
45    /// y = m \f$ from vectors \a x and \a y, by minimizing \f$
46    /// \sum{(y_i-m)^2} \f$.
47    ///
48    inline void fit(const gslapi::vector& x, const gslapi::vector& y)
49    {
50      Averager a;
51      for (size_t i=0; i<y.size(); i++)
52        a.add(y(i));
53      m_=a.mean();
54      s2_=a.variance();
55      m_err_=a.standard_error();
56    }
57
58    ///
59    /// This function computes the best-fit for the naive model \f$ y
60    /// = m \f$ from vectors \a x and \a y, by minimizing \f$ \sum
61    /// w_i(y_i-m)^2 \f$. The weight \f$ w_i \f$ is proportional to
62    /// the inverse of the variance for \f$ y_i \f$
63    ///
64    inline void fit(const gslapi::vector& x,
65                    const gslapi::vector& y,
66                    const gslapi::vector& w)
67    {
68      WeightedAverager a;
69      for (size_t i=0; i<y.size(); i++)
70        a.add(y(i), w(i));
71      m_=a.mean();
72      m_err_=a.standard_error();
73      s2_=m_err_*m_err_*w.sum(); 
74    }
75
76    ///
77    /// @return the parameter m
78    ///
79    inline double m(void) const { return m_; }
80
81    ///
82    /// @return standard deviation of parameter m
83    ///
84    inline double alpha_var(void) const { return m_err_; }
85
86    ///
87    /// Function predicting value using the naive model. \a y_err is
88    /// the expected deviation from the line for a new data point. If
89    /// weights are used a weight can be specified for the new point.
90    ///
91    inline void predict(const double x, double& y, double& y_err, 
92                        const double w=1.0) const 
93    { 
94      y = m_; 
95      y_err = sqrt(m_err_*m_err_ + s2_/w);
96    }
97
98         
99  private:
100    double s2_; // noise level ( var = s2/w in weighted case)
101    double m_;
102    double m_err_;
103  };
104
105}} // of namespace statistics and namespace theplu
106
107#endif
108
Note: See TracBrowser for help on using the repository browser.