STK++ 1.0

STK_IRegressMultivariate.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:  partial specialization for regression methods.
00029  * Author:   iovleff, serge.iovleff@stkpp.org
00030  **/
00031 
00036 #ifndef STK_IREGRESSMULTIVARIATE_H
00037 #define STK_IREGRESSMULTIVARIATE_H
00038 
00039 #include "STK_IRegress.h"
00040 
00041 #include "../../Algebra/include/STK_Matrix.h"
00042 
00043 namespace STK
00044 {
00045 
00051 template <class YContainer>
00052 class IRegress<YContainer, Matrix>
00053 {
00054   public:
00055     typedef IContainerBase<YContainer> YContainer;
00056 
00057   protected:
00059     YContainer const* y_;
00061     Matrix const* x_;
00063     Vector const* p_weights_;
00065     YContainer* p_predicted_;
00067     YContainer* p_residuals_;
00068 
00069   public:
00074     IRegress( YContainer const* y =0, Matrix const* x=0)
00075             : y_(y)
00076             , x_(x)
00077             , p_weights_(0)
00078             , p_predicted_(0)
00079             , p_residuals_(0)
00080             , decomposition(*x)
00081     { ;}
00082 
00084     virtual ~IRegress()
00085     { clean();}
00086 
00088     void run()
00089     {
00090       // remove any existing storage
00091       clean();
00092       // compute regression
00093       regression();
00094       // predictions
00095       prediction();
00096       // compute residuals
00097       computeResiduals();
00098     }
00099 
00103     void run( Vector const* p_weights)
00104     {
00105       // set weights
00106       p_weights_ = p_weights;
00107       // remove any existing storage
00108       clean();
00109       // compute weighted regression
00110       wregression();
00111       // create container of the predicted value and compute predictions
00112       prediction();
00113       // create container of the residuals and compute them
00114       computeResiduals();
00115     }
00116 
00118     void deleteCol( Integer const& pos);
00120     void insertCol( Integer const& pos, Vector const& v);
00121 
00126     inline YContainer* p_predicted() const { return p_predicted_;}
00131     inline YContainer* p_residuals() const { return p_residuals_;}
00132 
00137     void setData( YContainer const* y, Matrix const* x)
00138     { y_ = y; x_ = x; decomposition.run(*x);}
00139 
00141     void clean()
00142     {
00143       if (p_predicted_) delete p_predicted_;
00144       if (p_residuals_) delete p_residuals_;
00145     }
00146 
00147   protected:
00153     inline void computeResiduals()
00154     {
00155       if (p_residuals_) delete p_residuals_;
00156        p_residuals_ = y_->clone();
00157       *p_residuals_ = y_->asLeaf() - *p_predicted_;
00158     }
00159 
00160   private:
00162     virtual void regression() =0;
00164     virtual void wregression() =0;
00166     virtual void prediction() =0;
00168     Qr decomposition;
00169 };
00170 
00171 }
00172 
00173 #endif /* STK_IREGRESS_H */