source: trunk/yat/regression/LinearWeighted.h @ 1650

Last change on this file since 1650 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1#ifndef _theplu_yat_regression_linearweighted_
2#define _theplu_yat_regression_linearweighted_
3
4// $Id: LinearWeighted.h 1487 2008-09-10 08:41:36Z jari $
5
6/*
7  Copyright (C) 2005 Peter Johansson
8  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
9  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
10  Copyright (C) 2008 Peter Johansson
11
12  This file is part of the yat library, http://dev.thep.lu.se/yat
13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 3 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with yat. If not, see <http://www.gnu.org/licenses/>.
26*/
27
28#include "OneDimensionalWeighted.h"
29
30namespace theplu {
31namespace yat {
32  namespace utility {
33    class VectorBase;
34  }
35namespace regression {
36
37  ///
38  /// @brief linear regression.   
39  ///
40  /// @todo document
41  ///
42  class LinearWeighted : public OneDimensionalWeighted
43  {
44 
45  public:
46    ///
47    /// @brief The default constructor.
48    ///
49    LinearWeighted(void);
50
51    ///
52    /// @brief The destructor
53    ///
54    virtual ~LinearWeighted(void);
55         
56    /**
57       \f$ alpha \f$ is estimated as \f$ \frac{\sum w_iy_i}{\sum w_i} \f$
58   
59       @return the parameter \f$ \alpha \f$
60    */
61    double alpha(void) const;
62
63    /**
64       Variance is estimated as \f$ \frac{s^2}{\sum w_i} \f$
65
66       @see s2()
67
68       @return variance of parameter \f$ \alpha \f$
69    */
70    double alpha_var(void) const;
71
72    /**
73       \f$ beta \f$ is estimated as \f$ \frac{\sum
74       w_i(y_i-m_y)(x_i-m_x)}{\sum w_i(x_i-m_x)^2} \f$
75   
76       @return the parameter \f$ \beta \f$
77    */
78    double beta(void) const;
79
80    /**
81       Variance is estimated as \f$ \frac{s^2}{\sum w_i(x_i-m_x)^2} \f$
82
83       @see s2()
84
85       @return variance of parameter \f$ \beta \f$
86    */
87    double beta_var(void) const;
88   
89    /**
90       This function computes the best-fit linear regression
91       coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y =
92       \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by
93       minimizing \f$ \sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$,
94       where \f$ m_x \f$ is the weighted average. By construction \f$
95       \alpha \f$ and \f$ \beta \f$ are independent.
96    **/
97    /// @todo calculate mse
98    void fit(const utility::VectorBase& x, const utility::VectorBase& y,
99             const utility::VectorBase& w);
100   
101    ///
102    ///  Function predicting value using the linear model:
103    /// \f$ y =\alpha + \beta (x - m) \f$
104    ///
105    double predict(const double x) const;
106
107    /**
108       Noise level for points with weight @a w.
109    */
110    double s2(double w=1) const;
111
112    /**
113       estimated error \f$ y_{err} = \sqrt{ Var(\alpha) +
114       Var(\beta)*(x-m)} \f$.
115    */
116    double standard_error2(const double x) const;
117
118  private:
119    ///
120    /// Copy Constructor. (not implemented)
121    ///
122    LinearWeighted(const LinearWeighted&);
123
124    double m_x(void) const;
125    double m_y(void) const;
126    double sxx(void) const;
127    double syy(void) const;
128    double sxy(void) const;
129   
130    double alpha_;
131    double alpha_var_;
132    double beta_;
133    double beta_var_;
134  };
135
136}}} // of namespaces regression, yat, and theplu
137
138#endif
Note: See TracBrowser for help on using the repository browser.