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

Last change on this file since 730 was 730, checked in by Peter, 16 years ago

fixes #167 and #160

  • 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 730 2007-01-06 11:02:21Z peter $
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 "OneDimensionalWeighted.h"
28
29namespace theplu {
30namespace yat {
31  namespace utility {
32    class vector;
33  }
34namespace regression {
35
36  ///
37  /// @brief linear regression.   
38  ///
39  /// @todo document
40  ///
41  class LinearWeighted : public OneDimensionalWeighted
42  {
43 
44  public:
45    ///
46    /// @brief The default constructor.
47    ///
48    LinearWeighted(void);
49
50    ///
51    /// @brief The destructor
52    ///
53    virtual ~LinearWeighted(void);
54         
55    /**
56       \f$ alpha \f$ is estimated as \f$ \frac{\sum w_iy_i}{\sum w_i} \f$
57   
58       @return the parameter \f$ \alpha \f$
59    */
60    double alpha(void) const;
61
62    /**
63       Variance is estimated as \f$ \frac{s^2}{\sum w_i} \f$
64
65       @see s2()
66
67       @return variance of parameter \f$ \alpha \f$
68    */
69    double alpha_var(void) const;
70
71    /**
72       \f$ beta \f$ is estimated as \f$ \frac{\sum
73       w_i(y_i-m_y)(x_i-m_x)}{\sum w_i(x_i-m_x)^2} \f$
74   
75       @return the parameter \f$ \beta \f$
76    */
77    double beta(void) const;
78
79    /**
80       Variance is estimated as \f$ \frac{s^2}{\sum w_i(x_i-m_x)^2} \f$
81
82       @see s2()
83
84       @return variance of parameter \f$ \beta \f$
85    */
86    double beta_var(void) const;
87   
88    /**
89       This function computes the best-fit linear regression
90       coefficients \f$ (\alpha, \beta)\f$ of the model \f$ y =
91       \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by
92       minimizing \f$ \sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$,
93       where \f$ m_x \f$ is the weighted average. By construction \f$
94       \alpha \f$ and \f$ \beta \f$ are independent.
95    **/
96    /// @todo calculate mse
97    void fit(const utility::vector& x, const utility::vector& y,
98             const utility::vector& w);
99   
100    ///
101    ///  Function predicting value using the linear model:
102    /// \f$ y =\alpha + \beta (x - m) \f$
103    ///
104    double predict(const double x) const;
105
106    /**
107       Noise level for points with weight @a w.
108    */
109    double s2(double w=1) const;
110
111    /**
112       estimated error \f$ y_{err} = \sqrt{ Var(\alpha) +
113       Var(\beta)*(x-m)} \f$.
114    */
115    double standard_error2(const double x) const;
116
117  private:
118    ///
119    /// Copy Constructor. (not implemented)
120    ///
121    LinearWeighted(const LinearWeighted&);
122
123    double m_x(void) const;
124    double m_y(void) const;
125    double sxx(void) const;
126    double syy(void) const;
127    double sxy(void) const;
128   
129    double alpha_;
130    double alpha_var_;
131    double beta_;
132    double beta_var_;
133  };
134
135}}} // of namespaces regression, yat, and theplu
136
137#endif
Note: See TracBrowser for help on using the repository browser.