source: trunk/lib/statistics/Linear.h @ 383

Last change on this file since 383 was 383, checked in by Jari Häkkinen, 16 years ago

Moved regression stuff into a namespace ...statistics::regression.
Filenames was changed and a first draft of a Polynomial regression is
implemented (compiles but does not run properly).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1// $Id: Linear.h 383 2005-08-12 15:39:24Z jari $
2
3#ifndef _theplu_statistics_regression_linear_
4#define _theplu_statistics_regression_linear_
5
6#include <c++_tools/statistics/OneDimensional.h>
7#include <c++_tools/gslapi/vector.h>
8
9#include <cmath>
10
11namespace theplu {
12namespace statistics { 
13namespace regression {
14
15  ///
16  /// Class for OneDimensional.   
17  ///
18 
19  class Linear : public OneDimensional
20  {
21 
22  public:
23    ///
24    /// Default Constructor.
25    ///
26    inline Linear(void)
27      : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0),
28        m_x_(0), s2_(0) {}
29
30    ///
31    /// Copy Constructor. (not implemented)
32    ///
33    Linear(const Linear&);
34
35    ///
36    /// Destructor
37    ///
38    virtual ~Linear(void) {};
39         
40    ///
41    /// @return the parameter \f$ \alpha \f$
42    ///
43    inline double alpha(void) const { return alpha_; }
44
45    ///
46    /// @return standard deviation of parameter \f$ \alpha \f$
47    ///
48    inline double alpha_err(void) const { return sqrt(alpha_var_); }
49
50    ///
51    /// @return the parameter \f$ \beta \f$
52    ///
53    inline double beta(void) const { return beta_; }
54
55    ///
56    /// @return standard deviation of parameter \f$ \beta \f$
57    ///
58    inline double beta_err(void) const { return sqrt(beta_var_); }
59   
60    ///
61    /// This function computes the best-fit linear regression
62    /// coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y =
63    /// \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by
64    /// minimizing \f$ \sum{(y_i - \alpha - \beta (x-m_x))^2} \f$. By
65    /// construction \f$ \alpha \f$ and \f$ \beta \f$ are independent.
66    ///
67    void fit(const gslapi::vector& x, const gslapi::vector& y) ;
68   
69    ///
70    /// This function computes the best-fit linear regression
71    /// coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y =
72    /// \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by
73    /// minimizing \f$ \sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$,
74    /// where \f$ m_x \f$ is the weighted average. By construction \f$
75    /// \alpha \f$ and \f$ \beta \f$ are independent.
76    ///
77    void fit(const gslapi::vector& x, const gslapi::vector& y,
78             const gslapi::vector& w);
79   
80    ///
81    /// Function predicting value using the linear model. \a y_err is
82    /// the expected deviation from the line for a new data point. If
83    /// weights are used a weight can be specified for the new point.
84    ///
85    inline void predict(const double x, double& y, double& y_err, 
86                        const double w=1.0) 
87    { 
88      y = alpha_ + beta_ * (x-m_x_); 
89      y_err = sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)+s2_/w ); 
90      x_=x;
91      y_=y;
92      y_err_=y_err;
93    }
94
95    ///
96    /// @return prediction value and parameters
97    ///
98    std::ostream& print(std::ostream&) const;
99             
100    ///
101    /// @return header for print()
102    ///
103    std::ostream& print_header(std::ostream&) const;
104
105    ///
106    /// Function returning the coefficient of determination,
107    /// i.e. share of variance explained by the linear model.
108    ///
109    inline double r2(void) const { return r2_; }
110
111  private:
112    double alpha_;
113    double alpha_var_;
114    double beta_;
115    double beta_var_;
116    double m_x_; // average of x values
117    double s2_; // var(y|x)
118    double r2_; // coefficient of determination
119  };
120
121}}} // of namespaces regression, statisitcs and thep
122
123#endif
Note: See TracBrowser for help on using the repository browser.