STK++ 1.0
STK_IAAModel.cpp
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::AAModels
00027  * created on: 23 déc. 2010
00028  * Purpose:  implement the Interface base class .
00029  * Author:   iovleff, serge.iovleff@stkpp.org
00030  *
00031  **/
00032 
00037 #include "../include/STK_IAAModel.h"
00038 
00039 #include "../../Arrays/include/STK_MatrixSquare.h"
00040 
00041 #include "../../Algebra/include/STK_LinAlgebra2D.h"
00042 #include "../../Algebra/include/STK_TExpAlgebra.h"
00043 
00044 #include "../../STatistiK/include/STK_Stat_Transform.h"
00045 #include "../../STatistiK/include/STK_Stat_MultivariateReal.h"
00046 
00047 #include "../../Reduct/include/STK_IReduct.h"
00048 #include "../../Regress/include/STK_IRegression.h"
00049 
00050 #ifdef STK_VERBOSE
00051 #include "../../Arrays/include/STK_Display2D.h"
00052 #endif
00053 
00054 namespace STK
00055 {
00056 
00057 IAAModel::IAAModel( Matrix& workData)
00058                   :  p_regressor_(0)
00059                   , p_reductor_(0)
00060                   , p_workData_(&workData)
00061                   , p_reduced_(0)
00062                   , p_predicted_(0)
00063                   , p_residuals_(0)
00064                   , dim_(0)
00065                   , mean_()
00066                   , std_()
00067                   , isCentered_(false)
00068                   , isStandardized_(false)
00069                   , covProjected_()
00070                   , covResiduals_()
00071                   , varResiduals_(0.)
00072 { }
00073 
00074 /* destructor */
00075 IAAModel::~IAAModel()
00076 { }
00077 
00078 /* set the dimension of the model
00079  */
00080 void IAAModel::setDimension( Integer const& dim) { dim_ = dim;}
00081 
00082 /* set the working set with the Data to treat.
00083  * @param p_reductor a pointer on the reduction dimension method to use
00084  */
00085 void IAAModel::setWorkData( Matrix& workData)
00086 {
00087   p_workData_ = &workData;
00088   isCentered_     = false;
00089   isStandardized_ = false;
00090 }
00091 /* set the reduction dimension method.
00092  * @param p_reductor a pointer on the reduction dimension method to use
00093  */
00094 void IAAModel::setReductor( IReduct* p_reductor)
00095 { p_reductor_ = p_reductor;}
00096 /* set the regression method.
00097  * @param p_regressor a pointer on the regresssion method to use
00098  */
00099 void IAAModel::setRegressor( IRegression<Matrix, Matrix, Vector>* p_regressor)
00100 { p_regressor_ = p_regressor;}
00101 
00103 void IAAModel::freeReductor()
00104 {
00105   if (p_reductor_) delete p_reductor_;
00106   p_reductor_ = 0;
00107 }
00109 void IAAModel::freeRegressor()
00110 {
00111   if (p_regressor_) delete p_regressor_;
00112   p_regressor_ = 0;
00113 }
00114 
00115 /* standardize the local data set */
00116 void IAAModel::center()
00117 {
00118 #ifdef STK_DEBUG
00119   if (!p_workData_)
00120     throw runtime_error(_T("Error in IAAModel::center(): "
00121                            "work data is not initialized."));
00122 #endif
00123   // we have to decenter in case they have been centered with weights
00124   if (isCentered_)
00125   {
00126     Stat::decenter(*p_workData_, mean_);
00127     isCentered_ = false;
00128   }
00129   Stat::center(*p_workData_, mean_);
00130   isCentered_ = true;
00131 }
00132 
00133 /* center the local data set */
00134 void IAAModel::center(Vector const& weights)
00135 {
00136 #ifdef STK_DEBUG
00137   if (!p_workData_)
00138     throw runtime_error(_T("Error in IAAModel::center(weights): "
00139                            "work data is not initialized."));
00140 #endif
00141   // we have to decenter in case of this is not the same weights
00142   if (isCentered_)
00143   {
00144     Stat::decenter(*p_workData_, mean_);
00145     isCentered_ = false;
00146   }
00147   // center
00148   Stat::center(*p_workData_, weights, mean_);
00149   isCentered_ = true;
00150 }
00151 
00152 /* standardize the local data set */
00153 void IAAModel::standardize()
00154 {
00155 #ifdef STK_DEBUG
00156   if (!p_workData_)
00157     throw runtime_error(_T("Error in IAAModel::standardize(): "
00158                            "work data is not initialized."));
00159 #endif
00160 
00161   // we have to destandardize in case they have been standardized with weights
00162   if (isStandardized_)
00163   {
00164     Stat::destandardize(*p_workData_, mean_, std_);
00165     isStandardized_ = false;
00166   }
00167   Stat::standardize(*p_workData_, mean_, std_);
00168   isStandardized_ = true;
00169 }
00170 
00171 /* standardize the local data set */
00172 void IAAModel::standardize(Vector const& weights)
00173 {
00174 #ifdef STK_DEBUG
00175   if (!p_workData_)
00176     throw runtime_error(_T("Error in IAAModel::standardize(weights): "
00177                             "work data is not initialized."));
00178 #endif
00179 
00180   // we have to destandardize in case of this is not the same weights
00181   if (isStandardized_)
00182   {
00183     Stat::destandardize(*p_workData_, mean_, std_);
00184     isStandardized_ = false;
00185   }
00186   // standardize
00187   Stat::standardize(*p_workData_, weights, mean_, std_);
00188   isStandardized_ = true;
00189 }
00190 
00191 
00192 /* compute the dimension reduction **/
00193 void IAAModel::reduction()
00194 {
00195   if (!p_reductor_)
00196     throw runtime_error(_T("Error in IAAModel::reduction(): "
00197                            "reductor have not be set."));
00198   // compute axis
00199   p_reductor_->setDimension(dim_);
00200   p_reductor_->run();
00201   // compute matrix multiplication
00202   p_reduced_= p_reductor_->p_reduced();
00203 }
00204 
00205 /* compute the weighted dimension reduction **/
00206 void IAAModel::reduction( Vector const& weights)
00207 {
00208   if (!p_reductor_)
00209     throw runtime_error(_T("Error in IAAModel::reduction(weigths): "
00210                            "reductor have not be set."));
00211   // compute axis
00212   p_reductor_->setDimension(dim_);
00213   p_reductor_->run(weights);
00214   // get the reduced data set
00215   p_reduced_= p_reductor_->p_reduced();
00216 }
00217 
00218 /* compute the regression **/
00219 void IAAModel::regression()
00220 {
00221   if (!p_regressor_)
00222     throw runtime_error(_T("Error in IAAModel::regression(): "
00223                            "regressor have not be set."));
00224   // compute regression
00225   p_regressor_->run();
00226   // get results
00227   p_predicted_ = p_regressor_->p_predicted();
00228   p_residuals_ = p_regressor_->p_residuals();
00229 }
00230 /* compute the weighted regression **/
00231 void IAAModel::regression( Vector const& weights)
00232 {
00233   if (!p_regressor_)
00234     throw runtime_error(_T("Error in IAAModel::regression(weights): "
00235                            "regressor have not be set."));
00236   p_regressor_->run(weights);
00237   // get results
00238   p_predicted_ = p_regressor_->p_predicted();
00239   p_residuals_ = p_regressor_->p_residuals();
00240 }
00241 
00242 /* destandardize the predicted result and residuals */
00243 void IAAModel::decenterResults()
00244 {
00245   if (!p_predicted_)
00246     throw runtime_error(_T("Error in IAAModel::decenterResults(): "
00247                            "predictions have not been computed."));
00248   Stat::decenter(*p_predicted_, mean_);
00249 }
00250 /* destandardize the predicted result and residuals */
00251 void IAAModel::destandardizeResults()
00252 {
00253   if (!p_predicted_)
00254     throw runtime_error(_T("Error in IAAModel::destandardizeResults(): "
00255                            "predictions have not been computed."));
00256   if (!p_residuals_)
00257     throw runtime_error(_T("Error in IAAModel::destandardizeResults(): "
00258                            "residuals have not been computed."));
00259   Stat::destandardize(*p_predicted_, mean_, std_);
00260   Stat::destandardize(*p_residuals_, std_);
00261 }
00262 
00263 /* compute the covariance matrix of the projected data set. */
00264 void IAAModel::computeProjectedCovariance()
00265 {
00266   if (!p_reduced_)
00267     throw runtime_error(_T("Error in IAAModel::computeProjectedCovariance(): "
00268                            "projected data have not been computed."));
00269 #ifdef STK_VERBOSE
00270   stk_cout << "in IAAModel::computeProjectedCovariance().\n";
00271 #endif
00272   Stat::covariance(*p_reduced_, covProjected_);
00273 #ifdef STK_VERBOSE
00274   stk_cout << "in IAAModel::computeProjectedCovariance() done.\n";
00275 #endif
00276 }
00277 /* compute the covariance matrix of the residuals. */
00278 void IAAModel::computeResidualsCovariance()
00279 {
00280   if (!p_residuals_)
00281     throw runtime_error(_T("Error in IAAModel::computeResidualsCovariance(): "
00282                            "residuals have not been computed."));
00283   Stat::covariance(*p_residuals_, covResiduals_);
00284   varResiduals_ = trace(covResiduals_);
00285 }
00286 
00287 }