source: trunk/yat/regression/OneDimensional.h @ 703

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

Addresses #65 and #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1#ifndef _theplu_yat_regression_onedimensional_
2#define _theplu_yat_regression_onedimensional_
3
4// $Id: OneDimensional.h 703 2006-12-18 00:47:44Z 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/AveragerPair.h"
28
29#include <ostream>
30
31namespace theplu {
32namespace yat {
33namespace utility {
34  class vector;
35}
36namespace regression {
37 
38  ///
39  /// Abstract Base Class for One Dimensional fitting.   
40  ///
41  /// @see OneDimensionalWeighted.
42  ///
43  class OneDimensional
44  {
45 
46  public:
47    ///
48    /// @brief The default constructor
49    ///
50    OneDimensional(void);
51
52    ///
53    /// @brief The destructor
54    ///
55    virtual ~OneDimensional(void);
56         
57    /**
58       This function computes the best-fit given a model (see
59       specific class for details) by minimizing \f$
60       \sum{(\hat{y_i}-y_i)^2} \f$, where \f$ \hat{y} \f$ is the fitted value.
61    */
62    virtual void fit(const utility::vector& x, const utility::vector& y)=0; 
63   
64    /**
65       @brief Mean Squared Error
66
67       Mean Squared Error is defined as the \f$ \frac
68       \sum{(\hat{y_i}-y_i)^2{df} \f$ where \f$ df \f$ number of
69       degree of freedom typically is number data points minus number
70       of paramers in model.
71    */
72    virtual double mse(void) const=0;
73
74    ///
75    /// @return expected value in @a x accrding to the fitted model
76    ///
77    virtual double predict(const double x) const=0;
78
79    /**
80       The prediction error is defined as the square root of the
81       expected squared deviation a new data point will have from
82       value the model provides. The expected squared deviation is
83       defined as \f$ E(Y|x - \hat{y}(x))^2 \f$ and is typically
84       divided into two terms \f$ E(Y|x - E(Y|x))^2 \f$ and \f$
85       E(E(Y|x) - \hat{y}(x))^2 \f$, which is the conditional variance
86       in \f$ x \f$ and the squared standard error (see
87       standard_error()) of the model estimation in \f$ x \f$,
88       respectively.
89   
90       @return expected prediction error for a new data point in @a x
91    */
92    double prediction_error(const double x) const 
93    { return sqrt(mse()+pow(standard_error(x),2)); }
94
95    ///
96    /// @brief print output to ostream @a os
97    ///
98    /// Printing estimated model to @a os in the points defined by @a
99    /// min, @a max, and @a n. The values printed for each point is
100    /// the x-value, the estimated y-value, and the estimated standard
101    /// deviation of a new data poiunt will have from the y-value
102    /// given the x-value (see prediction_error()).
103    ///
104    /// @param os Ostream printout is sent to
105    /// @param n number of points printed
106    /// @param min smallest x-value for which the model is printed
107    /// @param max largest x-value for which the model is printed
108    ///
109    std::ostream& print(std::ostream& os,const double min, 
110                        double max, const u_int n) const;
111
112    /**
113       r-squared is defined as \f$ \frac{Var(Y|x)}{Var(Y)} \f$ or the
114       fraction of the variance explained by the regression model.
115    */
116    inline double r_squared(void) const { return mse()/variance(); }
117
118    /**
119       The standard error is defined as \f$ \sqrt{E(Y|x -
120       \hat{y}(x))^2 }\f$
121
122       @return error of model value in @a x
123    */
124    virtual double standard_error(const double x) const=0;
125
126  protected:
127    ///
128    /// Variance of y
129    ///
130    inline double variance(void) const { return ap_.y_averager().variance(); }
131
132    ///
133    /// Averager for pair of x and y
134    ///
135    statistics::AveragerPair ap_;
136
137  };
138
139}}} // of namespaces regression, yat, and theplu
140
141#endif
Note: See TracBrowser for help on using the repository browser.