source: trunk/lib/statistics/LinearWeighted.h @ 430

Last change on this file since 430 was 430, checked in by Peter, 18 years ago

changed interface of Regression::Local

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: LinearWeighted.h 430 2005-12-08 22:53:08Z 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), s2_(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. \a y_err is
70    /// the expected deviation from the line for a new data point. If
71    /// weights are used a weight can be specified for the new point.
72    ///
73    inline void predict(const double x, double& y, double& y_err, 
74                        const double w=1.0) 
75    { 
76      y = alpha_ + beta_ * (x-m_x_); 
77      y_err_ = y_err = sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)+s2_/w ); 
78      x_=x;
79      y_=y;
80    }
81
82    ///
83    /// @return prediction value and parameters
84    ///
85    std::ostream& print(std::ostream&) const;
86             
87    ///
88    /// @return header for print()
89    ///
90    std::ostream& print_header(std::ostream&) const;
91
92    ///
93    /// Function returning the coefficient of determination,
94    /// i.e. fraction of variance explained by the linear model.
95    ///
96    inline double r2(void) const { return r2_; }
97
98  private:
99    ///
100    /// Copy Constructor. (not implemented)
101    ///
102    LinearWeighted(const LinearWeighted&);
103
104    double alpha_;
105    double alpha_var_;
106    double beta_;
107    double beta_var_;
108    double m_x_; // average of x values
109    double s2_; // var(y|x)
110    double r2_; // coefficient of determination
111  };
112
113}}} // of namespaces regression, statisitcs and thep
114
115#endif
Note: See TracBrowser for help on using the repository browser.