source:trunk/src/RegressionLinear.h@235

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

Major modifications

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