source: trunk/yat/regression/Linear.h @ 718

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

Addresses #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1#ifndef _theplu_yat_regression_linear_
2#define _theplu_yat_regression_linear_
3
4// $Id: Linear.h 718 2006-12-26 09:56:26Z 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 "OneDimensional.h"
28
29#include <cmath>
30
31namespace theplu {
32namespace yat {
33  namespace utility {
34    class vector;
35  }
36namespace regression {
37
38  /**
39     @brief linear regression.   
40     
41     Data are modeled as \f$ y_i = \alpha + \beta (x_i-m_x) +
42     \epsilon_i \f$.
43  */
44  class Linear : public OneDimensional
45  {
46 
47  public:
48    ///
49    /// @brief The default constructor
50    ///
51    Linear(void);
52
53    ///
54    /// @brief The destructor
55    ///
56    virtual ~Linear(void);
57         
58    /**
59       The parameter \f$ \alpha \f$ is estimated as \f$
60       \frac{1}{n}\sum y_i \f$
61       
62       @return the parameter \f$ \alpha \f$
63    */
64    double alpha(void) const;
65
66    /**
67       The standard deviation is estimated as \f$
68       \sqrt{\frac{\chi^2}{n}} \f$
69       
70       @return standard deviation of parameter \f$ \alpha \f$
71    */
72    double alpha_err(void) const;
73
74    /**
75       The parameter \f$ \beta \f$ is estimated as \f$
76       \frac{\textrm{Cov}(x,y)}{\textrm{Var}(x)} \f$
77       
78       @return the parameter \f$ \beta \f$
79    */
80    double beta(void) const;
81
82    /**
83       The standard deviation is estimated as \f$
84       \sqrt{\frac{\chi^2}{\sum (x_i-m_x)^2}} \f$
85       
86       @return standard deviation of parameter \f$ \beta \f$
87    */
88    double beta_err(void) const;
89
90  /**
91       @brief Mean Squared Error
92   
93       Chisq is calculated as \f$ \frac{\sum
94       (y_i-\alpha-\beta(x_i-m_x))^2}{n-2} \f$
95    */
96    double chisq(void) const;
97
98    /**
99       Model is fitted by minimizing \f$ \sum{(y_i - \alpha - \beta
100       (x-m_x))^2} \f$. By construction \f$ \alpha \f$ and \f$ \beta \f$
101       are independent.
102    */
103    void fit(const utility::vector& x, const utility::vector& y) ;
104   
105    ///
106    /// @return \f$ \alpha + \beta x \f$
107    ///
108    double predict(const double x) const;
109
110    ///
111    /// Function returning the coefficient of determination,
112    /// i.e. fraction of variance explained by the linear model.
113    ///
114    double r2(void) const;
115
116    /**
117       The error of the model is estimated as \f$ \sqrt{
118       \textrm{alpha\_err}^2+\textrm{beta\_err}^2*(x-m_x)*(x-m_x)}\f$
119   
120       @return estimated error of model in @a x
121    */
122    double standard_error(const double x) const;
123
124
125  private:
126    ///
127    /// Copy Constructor. (not implemented)
128    ///
129    Linear(const Linear&);
130
131    double alpha_;
132    double alpha_var_;
133    double beta_;
134    double beta_var_;
135    double chisq_;
136    double m_x_; // average of x values
137    double r2_; // coefficient of determination
138  };
139
140}}} // of namespaces regression, yat, and theplu
141
142#endif
Note: See TracBrowser for help on using the repository browser.