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

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

References #83. Changing project name to yat. Compilation will fail in this revision.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1#ifndef _theplu_statistics_regression_linear_weighted_
2#define _theplu_statistics_regression_linear_weighted_
3
4// $Id: LinearWeighted.h 675 2006-10-10 12:08:45Z jari $
5
6/*
7  Copyright (C) The authors contributing to this file.
8
9  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "yat/statistics/OneDimensionalWeighted.h"
28
29#include <cmath>
30
31namespace theplu {
32  namespace utility {
33    class vector;
34  }
35namespace statistics {
36namespace regression {
37
38  ///
39  /// @brief linear regression.   
40  ///
41  /// @todo document
42  ///
43  class LinearWeighted : public OneDimensionalWeighted
44  {
45 
46  public:
47    ///
48    /// Default Constructor.
49    ///
50    inline LinearWeighted(void)
51      : OneDimensionalWeighted(), alpha_(0), alpha_var_(0), beta_(0), 
52        beta_var_(0),
53        m_x_(0){}
54
55    ///
56    /// Destructor
57    ///
58    inline virtual ~LinearWeighted(void) {};
59         
60    ///
61    /// @return the parameter \f$ \alpha \f$
62    ///
63    inline double alpha(void) const { return alpha_; }
64
65    ///
66    /// @return standard deviation of parameter \f$ \alpha \f$
67    ///
68    inline double alpha_err(void) const { return sqrt(alpha_var_); }
69
70    ///
71    /// @return the parameter \f$ \beta \f$
72    ///
73    inline double beta(void) const { return beta_; }
74
75    ///
76    /// @return standard deviation of parameter \f$ \beta \f$
77    ///
78    inline double beta_err(void) const { return sqrt(beta_var_); }
79   
80    /**
81       This function computes the best-fit linear regression
82       coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y =
83       \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by
84       minimizing \f$ \sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$,
85       where \f$ m_x \f$ is the weighted average. By construction \f$
86       \alpha \f$ and \f$ \beta \f$ are independent.
87    **/
88    void fit(const utility::vector& x, const utility::vector& y,
89             const utility::vector& w);
90   
91    ///
92    ///  Function predicting value using the linear model:
93    /// \f$ y =\alpha + \beta (x - m) \f$
94    ///
95    double predict(const double x) const { return alpha_ + beta_ * (x-m_x_); }
96
97    ///
98    /// estimated deviation from predicted value for a new data point
99    /// in @a x with weight @a w
100    ///
101    inline double prediction_error(const double x, const double w=1) const
102    { return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_)+s2_/w); }
103
104    /**
105       estimated error \f$ y_{err} = \sqrt{ Var(\alpha) +
106       Var(\beta)*(x-m)} \f$.
107    **/
108    inline double standard_error(const double x) const
109    { return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_) ); }
110
111    ///
112    /// Function returning the coefficient of determination,
113    /// i.e. fraction of variance explained by the linear model.
114    ///
115    inline double r2(void) const { return r2_; }
116
117  private:
118    ///
119    /// Copy Constructor. (not implemented)
120    ///
121    LinearWeighted(const LinearWeighted&);
122
123    double alpha_;
124    double alpha_var_;
125    double beta_;
126    double beta_var_;
127    double m_x_; // average of x values
128    double r2_; // coefficient of determination
129  };
130
131}}} // of namespaces regression, statisitcs and thep
132
133#endif
Note: See TracBrowser for help on using the repository browser.