# source:trunk/src/RegressionLinear.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: 2.3 KB
Line
1// $Id: RegressionLinear.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 "Regression.h"
9#include "vector.h"
10
11// Standard C++ includes
12////////////////////////
13#include <gsl/gsl_fit.h>
14
15namespace theplu {
16namespace statistics {
17
18  ///
19  /// Class for Regression.
20  ///
21
22
23  class RegressionLinear : public Regression
24  {
25
26  public:
27    ///
28    /// Default Constructor.
29    ///
30    RegressionLinear(void);
31
32    ///
33    /// Copy Constructor. (not implemented)
34    ///
35    RegressionLinear(const RegressionLinear&);
36
37    ///
38    /// Destructor
39    ///
40    virtual ~RegressionLinear(void) {};
41
42    ///
43    /// This function computes the best-fit linear regression
44    /// coefficients (k,m) of the model  \f$y = kx + m \f$ from
45    /// vectors \a x and \a y, by minimizing \f$\sum{(kx_i+m-y_i)^2} 46 /// \f$.
47    ///
48    inline int fit(const gslapi::vector& x, const gslapi::vector& y)
49    { return gsl_fit_linear(x.gsl_vector_pointer()->data,
50                            x.gsl_vector_pointer()->stride,
51                            y.gsl_vector_pointer()->data,
52                            y.gsl_vector_pointer()->stride,
53                            x.gsl_vector_pointer()->size,
54                            &m_, &k_, &cov00_, &cov01_, &cov11_, &sumsq_); }
55
56    inline double k(void) const { return k_; }
57    inline double m(void) const { return m_; }
58
59
60    ///
61    /// This function computes the best-fit linear regression
62    /// coefficients (k,m) of the model \f$y = kx + m \f$ from
63    /// vectors \a x and \a y, by minimizing \f$\sum w_i(kx_i+m-y_i)^2 64 /// \f$. The weight \f$w_i \f$ is the inverse of the variance for
65    /// \f$y_i \f$
66    ///
67    inline int fit_weighted(const gslapi::vector& x, const gslapi::vector& y,
68                            const gslapi::vector& w)
69    { return gsl_fit_wlinear(x.gsl_vector_pointer()->data,
70                             x.gsl_vector_pointer()->stride,
71                             w.gsl_vector_pointer()->data,
72                             w.gsl_vector_pointer()->stride,
73                             y.gsl_vector_pointer()->data,
74                             y.gsl_vector_pointer()->stride,
75                             x.gsl_vector_pointer()->size,
76                             &m_, &k_,  &cov00_, &cov01_, &cov11_, &sumsq_); }
77
78
79  private:
80    double cov00_;
81    double cov01_;
82    double cov11_;
83    double k_;
84    double m_;
85
86  };
87
88}} // of namespace statistics and namespace theplu
89
90#endif
91
Note: See TracBrowser for help on using the repository browser.