STK++ 1.0
STK_AdditiveBSplineRegression.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::Regress
00027  * created on: 31 juil. 2010
00028  * Purpose: definition of the AdditiveBSplineRegression 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 
00044 #include "../include/STK_AdditiveBSplineRegression.h"
00045 
00046 namespace STK
00047 {
00048 
00049 /* Constructor.
00050  * @param p_y p-dimensional array of output to fit
00051  * @param p_x d-dimensional array of predictor
00052  * @param nbControlPoints number of control points of the spline
00053  * @param degree degree of the BSpline basis
00054  * @param position position of the knots to used
00055  **/
00056 AdditiveBSplineRegression::AdditiveBSplineRegression( Matrix const* p_y
00057                                                     , Matrix const* p_x
00058                                                     , Integer const& nbControlPoints
00059                                                     , Integer const& degree
00060                                                     , _Kposition const& position
00061                                                     )
00062                                                     : IRegression<Matrix, Matrix, Vector>(p_y, p_x)
00063                                                     , nbControlPoints_(nbControlPoints)
00064                                                     , degree_(degree)
00065                                                     , position_(position)
00066                                                     , coefs_(p_x, nbControlPoints_, degree_, position_)
00067                                                     , controlPoints_()
00068 { }
00069 
00070 /* virtual destructor. */
00071 AdditiveBSplineRegression::~AdditiveBSplineRegression()
00072 {}
00073 
00074 
00075 /* compute the coefficients of the BSpline basis. This method willl be
00076  * called in the base class @c IRegression::run()
00077  **/
00078 void AdditiveBSplineRegression::preRun()
00079 {
00080   coefs_.setData(p_x_, nbControlPoints_, degree_, position_);
00081   if (!coefs_.run())
00082   {
00083     throw runtime_error(coefs_.error());
00084   }
00085 }
00086 
00087 /* compute the regression function. */
00088 void AdditiveBSplineRegression::regression()
00089 {
00090   // compute X'X
00091   MatrixSquare prod;
00092   multLeftTranspose(coefs_.coefficients(), prod);
00093 
00094   // compute (X'X)^{-1}
00095   GinvSymmetric inv;
00096   inv(prod);
00097 
00098   // compute X'Y
00099   Matrix temp;
00100   multLeftTranspose(coefs_.coefficients(), p_y_->asLeaf(), temp);
00101 
00102   // compute (X'X)^{-1}X'Y
00103   mult(prod, temp, controlPoints_);
00104 }
00105 
00106 
00107 
00108 /* compute the regression function. */
00109 void AdditiveBSplineRegression::regression(Vector const& weights)
00110 {
00111   // compute X'X
00112   MatrixSquare* prod = weightedMultLeftTranspose(coefs_.coefficients(), weights);
00113 
00114   // compute (X'X)^{-1}
00115   GinvSymmetric inv;
00116   inv(prod);
00117 
00118   // compute X'Y
00119   Matrix* temp = weightedMultLeftTranspose(coefs_.coefficients(), p_y_->asLeaf(), weights);
00120 
00121   // compute (X'X)^{-1}X'Y
00122   mult(*prod, *temp, controlPoints_);
00123   // remove temporary storages
00124   delete temp;
00125   delete prod;
00126 }
00127 
00128 /* Compute the predicted outputs by the regression function. */
00129 void AdditiveBSplineRegression::prediction()
00130 {
00131   // remove existing predictions if any (should not be the case)
00132   if (!p_predicted_) p_predicted_ = new Matrix;
00133   // compute predictions
00134   mult(coefs_.coefficients(), controlPoints_, *p_predicted_);
00135 }
00136 
00137 
00138 } // namespace STK
00139