source: trunk/c++_tools/statistics/LinearWeighted.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 Id
File size: 2.8 KB
Line 
1// $Id: LinearWeighted.h 586 2006-06-19 09:56:04Z peter $
2
3#ifndef _theplu_statistics_regression_linear_weighted_
4#define _theplu_statistics_regression_linear_weighted_
5
6#include <c++_tools/statistics/OneDimensionalWeighted.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 LinearWeighted : public OneDimensionalWeighted
21  {
22 
23  public:
24    ///
25    /// Default Constructor.
26    ///
27    inline LinearWeighted(void)
28      : OneDimensionalWeighted(), alpha_(0), alpha_var_(0), beta_(0), 
29        beta_var_(0),
30        m_x_(0){}
31
32    ///
33    /// Destructor
34    ///
35    inline virtual ~LinearWeighted(void) {};
36         
37    ///
38    /// @return the parameter \f$ \alpha \f$
39    ///
40    inline double alpha(void) const { return alpha_; }
41
42    ///
43    /// @return standard deviation of parameter \f$ \alpha \f$
44    ///
45    inline double alpha_err(void) const { return sqrt(alpha_var_); }
46
47    ///
48    /// @return the parameter \f$ \beta \f$
49    ///
50    inline double beta(void) const { return beta_; }
51
52    ///
53    /// @return standard deviation of parameter \f$ \beta \f$
54    ///
55    inline double beta_err(void) const { return sqrt(beta_var_); }
56   
57    ///
58    /// This function computes the best-fit linear regression
59    /// coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y =
60    /// \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by
61    /// minimizing \f$ \sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$,
62    /// where \f$ m_x \f$ is the weighted average. By construction \f$
63    /// \alpha \f$ and \f$ \beta \f$ are independent.
64    ///
65    void fit(const gslapi::vector& x, const gslapi::vector& y,
66             const gslapi::vector& w);
67   
68    ///
69    /// Function predicting value using the linear model: \f$ y =
70    /// \alpha + \beta (x - m)
71    double predict(const double x) const { return alpha_ + beta_ * (x-m_x_); }
72
73    ///
74    /// estimated deviation from predicted value for a new data point
75    /// in @a x with weight @a w
76    ///
77    inline double prediction_error(const double x, const double w=1) const
78    { return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_)+s2_/w); }
79
80    ///
81    /// estimated error @a y_err \f$ y_err = \sqrt{ Var(\alpha) +
82    /// Var(\beta)*(x-m)^2 }.
83    ///
84    inline double standard_error(const double x) const
85    { return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_) ); }
86
87    ///
88    /// Function returning the coefficient of determination,
89    /// i.e. fraction of variance explained by the linear model.
90    ///
91    inline double r2(void) const { return r2_; }
92
93  private:
94    ///
95    /// Copy Constructor. (not implemented)
96    ///
97    LinearWeighted(const LinearWeighted&);
98
99    double alpha_;
100    double alpha_var_;
101    double beta_;
102    double beta_var_;
103    double m_x_; // average of x values
104    double r2_; // coefficient of determination
105  };
106
107}}} // of namespaces regression, statisitcs and thep
108
109#endif
Note: See TracBrowser for help on using the repository browser.