STK++ 1.0
STK_MultidimRegression.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::
00027  * created on: 27 oct. 2010
00028  * Purpose: Definition of the class MultidimRegression .
00029  * Author:   iovleff, serge.iovleff@stkpp.org
00030  **/
00031 
00036 #include "../../Algebra/include/STK_LinAlgebra2D.h"
00037 #include "../../Algebra/include/STK_LinAlgebra3D.h"
00038 #include "../../Algebra/include/STK_GinvSymmetric.h"
00039 
00040 #include "../../Algebra/include/STK_TExpAlgebra.h"
00041 
00042 #include "../include/STK_MultidimRegression.h"
00043 
00044 
00045 namespace STK
00046 {
00047 
00048 MultidimRegression::MultidimRegression( Matrix const* y, Matrix const* x)
00049                                       : IRegression<Matrix, Matrix, Vector>(y, x)
00050                                       , coefs_()
00051 { }
00052 
00053 MultidimRegression::~MultidimRegression()
00054 { }
00055 
00056 /* compute the regression function. */
00057 void MultidimRegression::regression()
00058 {
00059   // compute X'X
00060   MatrixSquare prod;
00061   multLeftTranspose(p_x_->asLeaf(), prod);
00062 
00063   // compute (X'X)^{-1}
00064   GinvSymmetric inv;
00065   inv(prod);
00066 
00067   // compute X'Y
00068   Matrix temp;
00069   multLeftTranspose(p_x_->asLeaf(), p_y_->asLeaf(), temp);
00070 
00071   // compute (X'X)^{-1}X'Y
00072   mult(prod, temp, coefs_);
00073 }
00074 
00075 /* compute the regression function. */
00076 void MultidimRegression::regression(Vector const& weights)
00077 {
00078   // compute X'WX
00079   MatrixSquare* prod = weightedMultLeftTranspose(p_x_->asLeaf(), weights);
00080 
00081   // compute (X'WX)^{-1}
00082   GinvSymmetric inv;
00083   inv(prod);
00084 
00085   // compute X'WY
00086   Matrix* temp = weightedMultLeftTranspose(p_x_->asLeaf(), p_y_->asLeaf(), weights);
00087 
00088   // compute (X'WX)^{-1}X'WY
00089   mult(*prod, *temp, coefs_);
00090   // remove temporary storages
00091   delete temp;
00092   delete prod;
00093 }
00094 
00095 /* Compute the predicted outputs by the regression function. */
00096 void MultidimRegression::prediction()
00097 {
00098   // remove existing predictions if any (should not be the case)
00099   if (!p_predicted_) p_predicted_ = new Matrix;
00100   // compute predictions
00101   mult(p_x_->asLeaf(), coefs_, *p_predicted_);
00102 }
00103 
00104 
00105 }