source: trunk/c++_tools/statistics/LinearWeighted.h @ 616

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

Removed gslapi namespace and put the code into utility namespace.
Move #ifndef _header_ idiom to top of touched header files.
Removed unneccesary #includes, and added needed #includes.

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