#ifndef _theplu_yat_regression_linearweighted_ #define _theplu_yat_regression_linearweighted_ // $Id: LinearWeighted.h 1437 2008-08-25 17:55:00Z peter$ /* Copyright (C) 2005 Peter Johansson Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér Copyright (C) 2007 Jari Häkkinen, Peter Johansson Copyright (C) 2008 Peter Johansson This file is part of the yat library, http://dev.thep.lu.se/yat The yat library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The yat library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "OneDimensionalWeighted.h" namespace theplu { namespace yat { namespace utility { class VectorBase; } namespace regression { /// /// @brief linear regression. /// /// @todo document /// class LinearWeighted : public OneDimensionalWeighted { public: /// /// @brief The default constructor. /// LinearWeighted(void); /// /// @brief The destructor /// virtual ~LinearWeighted(void); /** \f$alpha \f$ is estimated as \f$\frac{\sum w_iy_i}{\sum w_i} \f$ @return the parameter \f$\alpha \f$ */ double alpha(void) const; /** Variance is estimated as \f$\frac{s^2}{\sum w_i} \f$ @see s2() @return variance of parameter \f$\alpha \f$ */ double alpha_var(void) const; /** \f$beta \f$ is estimated as \f$\frac{\sum w_i(y_i-m_y)(x_i-m_x)}{\sum w_i(x_i-m_x)^2} \f$ @return the parameter \f$\beta \f$ */ double beta(void) const; /** Variance is estimated as \f$\frac{s^2}{\sum w_i(x_i-m_x)^2} \f$ @see s2() @return variance of parameter \f$\beta \f$ */ double beta_var(void) const; /** This function computes the best-fit linear regression coefficients \f$(\alpha, \beta)\f$ of the model \f$y = \alpha + \beta (x-m_x) \f$ from vectors \a x and \a y, by minimizing \f$\sum{w_i(y_i - \alpha - \beta (x-m_x))^2} \f$, where \f$m_x \f$ is the weighted average. By construction \f$\alpha \f$ and \f$\beta \f$ are independent. **/ /// @todo calculate mse void fit(const utility::VectorBase& x, const utility::VectorBase& y, const utility::VectorBase& w); /// /// Function predicting value using the linear model: /// \f$y =\alpha + \beta (x - m) \f$ /// double predict(const double x) const; /** Noise level for points with weight @a w. */ double s2(double w=1) const; /** estimated error \f$y_{err} = \sqrt{ Var(\alpha) + Var(\beta)*(x-m)} \f$. */ double standard_error2(const double x) const; private: /// /// Copy Constructor. (not implemented) /// LinearWeighted(const LinearWeighted&); double m_x(void) const; double m_y(void) const; double sxx(void) const; double syy(void) const; double sxy(void) const; double alpha_; double alpha_var_; double beta_; double beta_var_; }; }}} // of namespaces regression, yat, and theplu #endif