|
STK++ 1.0
|
00001 /*--------------------------------------------------------------------*/ 00002 /* Copyright (C) 2004-2007 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 * Project: stkpp::AAModels 00026 * Purpose: implementation class for AA linear models. 00027 * Author: Serge Iovleff, serge.iovleff@stkpp.org 00028 * 00029 **/ 00030 00031 #include "../../Arrays/include/STK_Matrix.h" 00032 #include "../../Arrays/include/STK_Display2D.h" 00033 00034 #include "../../Algebra/include/STK_LinAlgebra2D.h" 00035 #include "../../Algebra/include/STK_LinAlgebra3D.h" 00036 #include "../../Algebra/include/STK_GramSchmidt.h" 00037 #include "../../Algebra/include/STK_TExpAlgebra.h" 00038 00039 #include "../../STatistiK/include/STK_Law_ITUnivariate.h" 00040 #include "../../STatistiK/include/STK_Law_Normal.h" 00041 00042 #include "../../Regress/include/STK_MultidimRegression.h" 00043 00044 #include "../include/STK_LinearAAModel.h" 00045 00046 #include <cmath> // sqrt() 00047 00048 00049 namespace STK 00050 { 00051 00052 /* Constructors. */ 00053 /* Default constructor : compute the Linear AA models of the matrix X 00054 * using the local variance as criteria. 00055 **/ 00056 LinearAAModel::LinearAAModel( Matrix const& data) 00057 : Runner(data) 00058 , GaussianAAModel(workData_) 00059 , workData_(data) 00060 { 00061 p_regressor_ = new MultidimRegression(); 00062 setWorkData(workData_); 00063 } 00064 00065 /* Virtual destructor */ 00066 LinearAAModel::~LinearAAModel() 00067 { 00068 delete p_regressor_; 00069 } 00070 00071 /* run the probabilistic AAModel */ 00072 bool LinearAAModel::run( Integer const& dim) 00073 { 00074 setDimension(dim); 00075 return run(); 00076 } 00077 00078 00079 /* run the probabilistic AAModel */ 00080 bool LinearAAModel::run() 00081 { 00082 // compute AAM 00083 try 00084 { 00085 if (!p_reductor_) 00086 throw runtime_error(_T("reductor have not be set.")); 00087 if (!p_regressor_) 00088 throw runtime_error(_T("regressor have not be set.")); 00089 // set p_workData to the reductor 00090 p_reductor_->setY(*p_workData_); 00091 // compute the projected data set 00092 reduction(); 00093 // compute the projected covariance 00094 computeProjectedCovariance(); 00095 #ifdef STK_VERBOSE 00096 stk_cout << _T("In LinearAAModel::run(), reduction done.\n"); 00097 #endif 00098 // set data 00099 p_regressor_->setY(p_workData_); 00100 p_regressor_->setX(p_reduced_); 00101 // compute the regression function 00102 regression(); 00103 #ifdef STK_VERBOSE 00104 stk_cout << _T("In LinearAAModel::run(), regression done.\n"); 00105 #endif 00106 computeLogLikelihood(); 00107 // check if data have been standardized or centered 00108 if (isStandardized()) { destandardizeResults();} 00109 else if (isCentered()){ decenterResults();} 00110 } 00111 catch (Exception error) 00112 { 00113 msg_error_ = _T("Error in LinearAAModel::run():\nWhat: "); 00114 msg_error_ += error.error(); 00115 return false; 00116 } 00117 return true; 00118 } 00119 00120 /* run the probabilistic AAModel */ 00121 bool LinearAAModel::run( Vector const& weights, Integer const& dim) 00122 { 00123 setDimension(dim); 00124 return run(weights); 00125 } 00126 bool LinearAAModel::run( Vector const& weights) 00127 { 00128 try 00129 { 00130 if (!p_reductor_) 00131 throw runtime_error(_T("reductor have not be set.")); 00132 if (!p_regressor_) 00133 throw runtime_error(_T("regressor have not be set.")); 00134 // set p_workData to the reductor 00135 p_reductor_->setY(*p_workData_); 00136 // compute the weighted reduced data set 00137 reduction(weights); 00138 // compute the projected covariance 00139 computeProjectedCovariance(); 00140 #ifdef STK_VERBOSE 00141 stk_cout << _T("In LinearAAModel::run(weights), reduction done.\n"); 00142 #endif 00143 // set data 00144 p_regressor_->setY(p_workData_); 00145 p_regressor_->setX(p_reduced_); 00146 // compute the weighted regression vectors 00147 regression(weights); 00148 #ifdef STK_VERBOSE 00149 stk_cout << _T("In LinearAAModel::run(weights), regression done.\n"); 00150 #endif 00151 computeLogLikelihood(); 00152 // check if data have been standardized or centered 00153 if (isStandardized()) { destandardizeResults();} 00154 else if (isCentered()){ decenterResults();} 00155 } 00156 catch (Exception error) 00157 { 00158 msg_error_ = _T("Error in LinearAAModel::run(weights): "); 00159 msg_error_ += error.error(); 00160 return false; 00161 } 00162 return true; 00163 } 00164 00165 /* Simulate a centered auto-associative linear model of the form 00166 * \f[ 00167 * X = X.P.P' + \epsilon 00168 * \f] 00169 * with \f$ P'P = I_d \f$ and d < p. 00170 * @param law, the law to use in order to simulate the data. 00171 * @param std the standard deviation of the gaussian noise 00172 * @param data the data to simulate. The dimension of the container 00173 * give the number of the samples and variables. 00174 * @param proj the simulated projection matrix. The dimension of the 00175 * container give the dimension of the AA model. 00176 **/ 00177 void LinearAAModel::simul( const Law::ITUnivariate<Real>& law 00178 , Vector const& mu 00179 , Real const& std 00180 , Matrix& proj 00181 , Matrix& data 00182 ) 00183 { 00184 // simul AA model 00185 Matrix* sim = data.clone(); 00186 law.rand2D(*sim); 00187 law.rand2D(proj); 00188 // orthonormalize proj 00189 gramSchmidt(proj); 00190 MatrixSquare prod; 00191 multRightTranspose(proj, prod); 00192 // compute data 00193 mult(data, *sim, prod); 00194 // release memory 00195 delete sim; 00196 00197 // get dimensions 00198 const Integer firstCol = data.firstCol(), lastCol = data.lastCol(); 00199 const Integer firstRow = data.firstRow(), lastRow = data.lastRow(); 00200 // add noise to the model 00201 for (Integer j= firstCol; j<= lastCol; j++) 00202 { 00203 Law::Normal noise(mu[j], std); 00204 for (Integer i= firstRow; i<= lastRow; i++) 00205 data(i, j) += noise.rand(); 00206 } 00207 } 00208 00209 } // Namespace STK