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

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

Continued fixing of regression stuff.

  • 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 385 2005-08-13 09:24:18Z 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    /// Destructor
32    ///
33    virtual ~Linear(void) {};
34         
35    ///
36    /// @return the parameter \f$ \alpha \f$
37    ///
38    inline double alpha(void) const { return alpha_; }
39
40    ///
41    /// @return standard deviation of parameter \f$ \alpha \f$
42    ///
43    inline double alpha_err(void) const { return sqrt(alpha_var_); }
44
45    ///
46    /// @return the parameter \f$ \beta \f$
47    ///
48    inline double beta(void) const { return beta_; }
49
50    ///
51    /// @return standard deviation of parameter \f$ \beta \f$
52    ///
53    inline double beta_err(void) const { return sqrt(beta_var_); }
54   
55    ///
56    /// This function computes the best-fit linear regression
57    /// coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y =
58    /// \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by
59    /// minimizing \f$ \sum{(y_i - \alpha - \beta (x-m_x))^2} \f$. By
60    /// construction \f$ \alpha \f$ and \f$ \beta \f$ are independent.
61    ///
62    void fit(const gslapi::vector& x, const gslapi::vector& y) ;
63   
64    ///
65    /// This function computes the best-fit linear regression
66    /// coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y =
67    /// \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by
68    /// minimizing \f$ \sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$,
69    /// where \f$ m_x \f$ is the weighted average. By construction \f$
70    /// \alpha \f$ and \f$ \beta \f$ are independent.
71    ///
72    void fit(const gslapi::vector& x, const gslapi::vector& y,
73             const gslapi::vector& w);
74   
75    ///
76    /// Function predicting value using the linear model. \a y_err is
77    /// the expected deviation from the line for a new data point. If
78    /// weights are used a weight can be specified for the new point.
79    ///
80    inline void predict(const double x, double& y, double& y_err, 
81                        const double w=1.0) 
82    { 
83      y = alpha_ + beta_ * (x-m_x_); 
84      y_err = sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)+s2_/w ); 
85      x_=x;
86      y_=y;
87      y_err_=y_err;
88    }
89
90    ///
91    /// @return prediction value and parameters
92    ///
93    std::ostream& print(std::ostream&) const;
94             
95    ///
96    /// @return header for print()
97    ///
98    std::ostream& print_header(std::ostream&) const;
99
100    ///
101    /// Function returning the coefficient of determination,
102    /// i.e. share of variance explained by the linear model.
103    ///
104    inline double r2(void) const { return r2_; }
105
106  private:
107    ///
108    /// Copy Constructor. (not implemented)
109    ///
110    Linear(const Linear&);
111
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.