|
STK++ 1.0
|
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: 31 juil. 2010 00028 * Purpose: implementation of the BSplineRegression class. 00029 * Author: iovleff, serge.iovleff@stkpp.org 00030 **/ 00031 00037 #include "../../Algebra/include/STK_LinAlgebra2D.h" 00038 #include "../../Algebra/include/STK_LinAlgebra3D.h" 00039 #include "../../Algebra/include/STK_GinvSymmetric.h" 00040 00041 #include "../../Algebra/include/STK_TExpAlgebra.h" 00042 00043 #include "../include/STK_BSplineRegression.h" 00044 00045 namespace STK 00046 { 00047 00048 BSplineRegression::BSplineRegression( Matrix const* p_y 00049 , Vector const* p_x 00050 , Integer const& nbControlPoints 00051 , Integer const& degree 00052 , const _Kposition& position 00053 ) 00054 : IRegression<Matrix, Vector, Vector>(p_y, p_x) 00055 , nbControlPoints_(nbControlPoints) 00056 , degree_(degree) 00057 , position_(position) 00058 , coefs_(p_x, nbControlPoints_, degree_, position_) 00059 , controlPoints_() 00060 { } 00061 00062 BSplineRegression::~BSplineRegression() 00063 {} 00064 00065 /* compute the regression function. */ 00066 void BSplineRegression::regression() 00067 { 00068 // compute X'X 00069 MatrixSquare prod; 00070 multLeftTranspose(coefs_.coefficients(), prod); 00071 00072 // compute (X'X)^{-1} 00073 GinvSymmetric inv; 00074 inv(prod); 00075 00076 // compute X'Y 00077 Matrix temp; 00078 multLeftTranspose(coefs_.coefficients(), p_y_->asLeaf(), temp); 00079 00080 // compute (X'X)^{-1}X'Y 00081 mult(prod, temp, controlPoints_); 00082 } 00083 00084 /* compute the regression function. */ 00085 void BSplineRegression::regression(Vector const& weights) 00086 { 00087 // compute X'X 00088 MatrixSquare* prod = weightedMultLeftTranspose(coefs_.coefficients(), weights); 00089 00090 // compute (X'X)^{-1} 00091 GinvSymmetric inv; 00092 inv(prod); 00093 00094 // compute X'Y 00095 Matrix* temp = weightedMultLeftTranspose(coefs_.coefficients(), p_y_->asLeaf(), weights); 00096 00097 // compute (X'X)^{-1}X'Y 00098 mult(*prod, *temp, controlPoints_); 00099 // remove temporary storages 00100 delete temp; 00101 delete prod; 00102 } 00103 00104 /* Compute the predicted outputs by the regression function. */ 00105 void BSplineRegression::prediction() 00106 { 00107 // create predictions if it does not exist any 00108 if (!p_predicted_) p_predicted_ = new Matrix; 00109 // compute predictions 00110 mult(coefs_.coefficients(), controlPoints_, *p_predicted_); 00111 } 00112 00113 }