source: trunk/src/RegressionNaive.h @ 213

Last change on this file since 213 was 213, checked in by Peter, 18 years ago

moving minor stuff to base class

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.7 KB
Line 
1// $Id: RegressionNaive.h 213 2004-11-04 19:58:08Z 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
16namespace theplu {
17namespace statistics { 
18 
19  ///
20  /// Class for Regression.   
21  ///
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 int 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      var_=a.standard_error()*a.standard_error(); 
55      return 0;
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 the inverse of the
62    /// variance for \f$ y_i \f$
63    ///
64    inline int fit_weighted(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      var_=a.standard_error()*a.standard_error(); 
73      return 0;
74    }
75
76         
77  private:
78    double var_;
79    double m_;
80
81  };
82
83}} // of namespace statistics and namespace theplu
84
85#endif
86
Note: See TracBrowser for help on using the repository browser.