source: trunk/c++_tools/statistics/Linear.h @ 586

Last change on this file since 586 was 586, checked in by Peter, 15 years ago

closes #23 redesign of regression classes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.3 KB
Line 
1// $Id: Linear.h 586 2006-06-19 09:56:04Z peter $
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  /// @brief linear regression.   
17  ///
18  /// @todo document
19  ///
20  class Linear : public OneDimensional
21  {
22 
23  public:
24    ///
25    /// Default Constructor.
26    ///
27    inline Linear(void)
28      : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0),
29        m_x_(0){}
30
31    ///
32    /// Destructor
33    ///
34    inline virtual ~Linear(void) {};
35         
36    ///
37    /// @return the parameter \f$ \alpha \f$
38    ///
39    inline double alpha(void) const { return alpha_; }
40
41    ///
42    /// @return standard deviation of parameter \f$ \alpha \f$
43    ///
44    inline double alpha_err(void) const { return sqrt(alpha_var_); }
45
46    ///
47    /// @return the parameter \f$ \beta \f$
48    ///
49    inline double beta(void) const { return beta_; }
50
51    ///
52    /// @return standard deviation of parameter \f$ \beta \f$
53    ///
54    inline double beta_err(void) const { return sqrt(beta_var_); }
55   
56    ///
57    /// This function computes the best-fit linear regression
58    /// coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y =
59    /// \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by
60    /// minimizing \f$ \sum{(y_i - \alpha - \beta (x-m_x))^2} \f$. By
61    /// construction \f$ \alpha \f$ and \f$ \beta \f$ are independent.
62    ///
63    void fit(const gslapi::vector& x, const gslapi::vector& y) ;
64   
65    ///
66    /// @return value in @a x of model
67    ///
68    double predict(const double x) const;
69
70    ///
71    /// @return expected prediction error for a new data point in @a x
72    ///
73    double prediction_error(const double x) const;
74
75    ///
76    /// @return error of model value in @a x
77    ///
78    double standard_error(const double x) const;
79
80    ///
81    /// Function returning the coefficient of determination,
82    /// i.e. fraction of variance explained by the linear model.
83    ///
84    inline double r2(void) const { return r2_; }
85
86  private:
87    ///
88    /// Copy Constructor. (not implemented)
89    ///
90    Linear(const Linear&);
91
92    double alpha_;
93    double alpha_var_;
94    double beta_;
95    double beta_var_;
96    double m_x_; // average of x values
97    double r2_; // coefficient of determination
98  };
99
100}}} // of namespaces regression, statisitcs and thep
101
102#endif
Note: See TracBrowser for help on using the repository browser.