STK++ 1.0

STK_IRegress.h

Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2010  Serge Iovleff
00003 
00004  This program is free software; you can redistribute it and/or modify
00005  it under the terms of the GNU Lesser General Public License as
00006  published by the Free Software Foundation; either version 2 of the
00007  License, or (at your option) any later version.
00008 
00009  This program is distributed in the hope that it will be useful,
00010  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  GNU Lesser General Public License for more details.
00013 
00014  You should have received a copy of the GNU Lesser General Public
00015  License along with this program; if not, write to the
00016  Free Software Foundation, Inc.,
00017  59 Temple Place,
00018  Suite 330,
00019  Boston, MA 02111-1307
00020  USA
00021 
00022  Contact : Serge.Iovleff@stkpp.org
00023  */
00024 
00025 /*
00026  * Project:  stkpp::Regress
00027  * created on: 23 juin 2010
00028  * Purpose:  Interface base class for regression methods.
00029  * Author:   iovleff, serge.iovleff@stkpp.org
00030  **/
00031 
00036 #ifndef STK_IREGRESS_H
00037 #define STK_IREGRESS_H
00038 
00039 #include "../../Algebra/include/STK_Vector.h"
00040 #include "../../Algebra/include/STK_TExpAlgebra.h"
00041 
00042 namespace STK
00043 {
00044 
00049 template <class YContainer, class XContainer>
00050 class IRegress
00051 {
00052   protected:
00054     YContainer const* p_y_;
00056     XContainer const* p_x_;
00058     Vector const* p_weights_;
00060     YContainer* p_predicted_;
00062     YContainer* p_residuals_;
00063 
00064   public:
00069     IRegress( YContainer const* p_y =0, XContainer const* p_x =0)
00070             : p_y_(p_y)
00071             , p_x_(p_x)
00072             , p_weights_(0)
00073             , p_predicted_(0)
00074             , p_residuals_(0)
00075     { ;}
00076 
00078     virtual ~IRegress()
00079     { clear();}
00080 
00082     void run()
00083     {
00084       // remove any existing storage
00085       clear();
00086       // compute regression
00087       regression();
00088       // predictions
00089       prediction();
00090       // compute residuals
00091       residuals();
00092     }
00093 
00097     void run( Vector const* p_weights)
00098     {
00099       // set weights
00100       p_weights_ = p_weights;
00101       // remove any existing storage
00102       clear();
00103       // compute weighted regression
00104       wregression();
00105       // create container of the predicted value and compute prediction
00106       prediction();
00107       // create container of the residuals and compute them
00108       residuals();
00109     }
00110 
00115     inline YContainer* p_predicted() const
00116     { return p_predicted_;}
00121     inline YContainer* p_residuals() const
00122     {  return p_residuals_;}
00123 
00128     void setData( YContainer const* p_y, XContainer const* p_x)
00129     { p_y_ = p_y; p_x_ = p_x;}
00130 
00134     void setY( YContainer const* p_y)
00135     { p_y_ = p_y;}
00136 
00140     void setX( XContainer const* p_x)
00141     { p_x_ = p_x;}
00142 
00143   protected:
00148     virtual void preWork()
00149     { }
00150 
00156     inline void residuals()
00157     {
00158        p_residuals_ =  p_y_->clone();
00159       *p_residuals_ = *p_y_ - *p_predicted_;
00160     }
00161 
00162   private:
00164     virtual void regression() =0;
00166     virtual void wregression() =0;
00169     virtual void prediction() =0;
00171     void clear()
00172     {
00173       if (p_predicted_) delete p_predicted_;
00174       if (p_residuals_) delete p_residuals_;
00175       p_predicted_ = 0;
00176       p_residuals_ = 0;
00177     }
00178 
00179 };
00180 
00181 }
00182 
00183 #endif /* STK_IREGRESS_H */