STK++ 0.9.13
STK_BSplineRegression.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23 */
24
25/*
26 * Project: stkpp::Regress
27 * created on: 31 juil. 2010
28 * Purpose: definition of the BsplineRegression class.
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 **/
31
36#ifndef STK_BSPLINEREGRESSION_H
37#define STK_BSPLINEREGRESSION_H
38
40#include "STK_IRegression.h"
41
43
44namespace STK
45{
46
50template <class YArray, class XVector, class Weights = VectorX>
51class BSplineRegression: public IRegression<YArray, XVector, Weights>
52{
53 public:
56 using Base::p_x_;
57 using Base::p_y_;
58 using Base::predicted_;
59 using Base::residuals_;
68 , XVector const* p_x
69 , int const& nbControlPoints
70 , int const& degree = 3
72 );
74 inline virtual ~BSplineRegression();
76 inline int degree() const { return degree_;}
78 inline int nbControlPoints() const { return nbControlPoints_;}
80 inline YArray const& controlPoints() const { return controlPoints_; }
82 inline VectorX const& knots() const { return coefs_.knots(); }
84 inline YArray const& coefficients() const { return coefs_.coefficients();}
91 virtual YArray extrapolate( XVector const& x) const;
92
93 protected:
107 virtual bool initializeStep();
111 virtual bool regressionStep();
116 virtual bool regressionStep(Weights const& weights);
120 virtual bool predictionStep();
124 inline virtual int computeNbFreeParameter() const
125 { return controlPoints_.sizeCols() * controlPoints_.sizeRows(); }
126};
127
128template <class YArray, class XVector, class Weights>
130 , XVector const* p_x
131 , int const& nbControlPoints
132 , int const& degree
133 , const KnotsPosition& position
134 )
135 : Base(p_y, p_x)
136 , nbControlPoints_(nbControlPoints)
137 , degree_(degree)
138 , position_(position)
139 , coefs_(*p_x, nbControlPoints_, degree_, position_)
140 , controlPoints_()
141{ }
142
143template <class YArray, class XVector, class Weights>
146
147template <class YArray, class XVector, class Weights>
150/* compute the regression function. */
151template <class YArray, class XVector, class Weights>
153{
154 // compute X'X
155 ArraySquareX prod = coefs_.coefficients().transpose() * coefs_.coefficients();
156 // compute (X'X)^{-1}
157// GInvertSymMatrix<ArraySquareX> inv;
158// inv(prod);
159
160 // compute (X'X)^{-1}X'Y
161 controlPoints_ = invert(prod.symmetrize()) * (coefs_.coefficients().transpose() * p_y_->asDerived());
162 return true;
163}
164
165/* compute the regression function. */
166template <class YArray, class XVector, class Weights>
168{
169 // compute X'X
170 ArraySquareX prod = coefs_.coefficients().transpose() * weights.diagonalize() * coefs_.coefficients();
171 // compute (X'X)^{-1}
172 // GInvertSymMatrix<ArraySquareX> inv;
173 // inv(prod);
174 // compute (X'X)^{-1}X'Y
175 controlPoints_ = invert(prod.symmetrize()) * coefs_.coefficients().transpose() * weights.diagonalize() * p_y_->asDerived();
176 //controlPoints_ = prod * coefs_.coefficients().transpose() * weights.diagonalize() * p_y_->asDerived();
177 return true;
178}
179
180/* Compute the predicted outputs by the regression function. */
181template <class YArray, class XVector, class Weights>
183{
184 predicted_ = coefs_.coefficients() * controlPoints_;
185 return true;
186}
187
188/* @brief Extrapolate the values @c y from the value @c x.
189 * Given the data set @c x will compute the values \f$ y = x.\hat{\beta} \f$.
190 * The coefficients @c coefs_ have to be estimated previously.
191 * @param x the input data set
192 * @param y the output (extrapolated) data set
193 */
194template <class YArray, class XVector, class Weights>
196{
197 YArray res = coefs_.extrapolate(x) * controlPoints_;
198 return res;
199}
200
201} // namespace STK
202
203#endif /* STK_BSPLINEREGRESSION_H */
In this file we define the BSplineCoefficients class.
In this file we define the Interface base class IRegression.
In this file we implement inversion method for general matrix.
TransposeOperator< Derived > const transpose() const
SymmetrizeOperator< Derived > const symmetrize() const
VectorX const & knots() const
Compute a BSpline, multi-valued, regression function using BSpline basis.
int degree_
degree of the B_Spline curve
BSplineCoefficients< XVector > coefs_
Coefficients of the regression matrix.
virtual YArray extrapolate(XVector const &x) const
YArray controlPoints_
Estimated control points of the B-spline curve.
IRegression< YArray, XVector, Weights > Base
BSplineRegression(YArray const *p_y, XVector const *p_x, int const &nbControlPoints, int const &degree=3, KnotsPosition const &position=Regress::uniformKnotsPositions_)
Constructor.
virtual bool regressionStep()
Compute the regression function.
virtual bool initializeStep()
Compute the coefficients of the BSpline basis.
YArray const & controlPoints() const
YArray const & coefficients() const
int nbControlPoints_
number of control points of the B-spline curve.
virtual ~BSplineRegression()
virtual destructor.
VectorX const & knots() const
virtual int computeNbFreeParameter() const
Compute the number of parameter of the regression function.
virtual bool predictionStep()
Compute the predicted outputs by the regression function.
Regress::KnotsPosition KnotsPosition
KnotsPosition position_
method of position of the knots of the B-spline curve
Coefs const & coefficients() const
Definition STK_IBasis.h:79
Interface base class for Regression methods.
YArray residuals_
Container of the residuals.
YArray predicted_
Container of the predicted output.
YArray const * p_y_
A pointer on the y data set.
XArray const * p_x_
A pointer on the x data set.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
hidden::AlgebraTraits< InvertMatrix< Matrix, hidden::Traits< Matrix >::sizeRows_ > >::Result invert(Matrix const &mat)
Utility function allowing to compute the inverse of a matrix.
KnotsPosition
Method to use for positioning the knots for BSpline basis.
@ uniformKnotsPositions_
uniform knots
The namespace STK is the main domain space of the Statistical ToolKit project.