STK++ 0.9.13
STK_AdditiveBSplineRegression.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 AdditiveBSplineRegression class.
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 **/
31
36#ifndef STK_ADDITIVEBSPLINEREGRESSION_H
37#define STK_ADDITIVEBSPLINEREGRESSION_H
38
40#include "STK_IRegression.h"
41
42#ifdef STKUSELAPACK
44#else
46#endif
47
48namespace STK
49{
50
55template <class YArray, class XArray, class Weights = VectorX>
56class AdditiveBSplineRegression: public IRegression<YArray, XArray, Weights>
57{
58 public:
61 using Base::p_x_;
62 using Base::p_y_;
63 using Base::predicted_;
64 using Base::residuals_;
73 , XArray const* p_x
74 , int nbControlPoints
75 , int degree = 3
77 );
86 , XArray const& x
87 , int nbControlPoints
88 , int degree = 3
90 );
94 inline int degree() const { return degree_;}
96 inline int nbControlPoints() const { return nbControlPoints_;}
98 inline YArray const& controlPoints() const { return controlPoints_; }
102 inline ArrayXX const& coefficients() const { return coefs_.coefficients();}
109 virtual YArray extrapolate( XArray const& x) const;
110
111 protected:
125 virtual bool initializeStep();
127 virtual bool regressionStep();
131 virtual bool regressionStep(Weights const& weights);
133 virtual bool predictionStep();
135 inline virtual int computeNbFreeParameter() const
136 { return controlPoints_.sizeCols() * controlPoints_.sizeRows(); }
137};
138
139/* Constructor.
140 * @param p_y p-dimensional array of output to fit
141 * @param p_x d-dimensional array of predictor
142 * @param nbControlPoints number of control points of the spline
143 * @param degree degree of the BSpline basis
144 * @param position position of the knots to used
145 **/
146template <class YArray, class XArray, class Weights>
148 YArray const* p_y
149 , XArray const* p_x
150 , int nbControlPoints
151 , int degree
152 , KnotsPosition const& position
153 )
154 : Base(p_y, p_x)
155 , nbControlPoints_(nbControlPoints)
156 , degree_(degree)
157 , position_(position)
158 , coefs_(p_x, nbControlPoints_, degree_, position_)
159 , controlPoints_()
160{}
161/* Constructor.
162 * @param y p-dimensional array of output to fit
163 * @param x d-dimensional array of predictor
164 * @param nbControlPoints number of control points of the spline
165 * @param degree degree of the BSpline basis
166 * @param position position of the knots to used
167 **/
168template <class YArray, class XArray, class Weights>
170 , XArray const& x
171 , int nbControlPoints
172 , int degree
173 , KnotsPosition const& position
174 )
175 : Base(&y, &x)
176 , nbControlPoints_(nbControlPoints)
177 , degree_(degree)
178 , position_(position)
179 , coefs_(&x, nbControlPoints_, degree_, position_)
180 , controlPoints_()
181{}
182
183/* compute the coefficients of the BSpline basis. This method will be
184 * called in the base class @c IRegression::run()
185 **/
186template <class YArray, class XArray, class Weights>
188{
189 coefs_.setData(p_x_, nbControlPoints_, degree_, position_);
190 if (!coefs_.run())
191 {
192 this->msg_error_ = coefs_.error();
193 return false;
194 }
195 return true;
196}
197
198/* compute the regression function. */
199template <class YArray, class XArray, class Weights>
201{
202 // coefs_.coefficients() is Array2D
203#ifdef STKUSELAPACK
204 lapack::MultiLeastSquare<YArray, ArrayXX> reg(*p_y_, coefs_.coefficients(), false, false);
205#else
206 MultiLeastSquare<YArray, ArrayXX> reg(*p_y_, coefs_.coefficients(), false, false);
207#endif
208 if (!reg.run())
209 {
210 this->msg_error_ = reg.error();
211 return false;
212 }
213 controlPoints_.move(reg.x());
214 return true;
215}
216
217/* compute the regression function. */
218template <class YArray, class XArray, class Weights>
220{
221#ifdef STKUSELAPACK
222 lapack::MultiLeastSquare<YArray, ArrayXX> reg(*p_y_, coefs_.coefficients(), false, false);
223#else
224 MultiLeastSquare<YArray, ArrayXX> reg(*p_y_, coefs_.coefficients(), false, false);
225#endif
226 if (!reg.run(weights))
227 {
228 this->msg_error_ = reg.error();
229 return false;
230 }
231 controlPoints_ = reg.x();
232 return true;
233}
234
235/* Compute the predicted outputs by the regression function. */
236template <class YArray, class XArray, class Weights>
238{
239 predicted_ = coefs_.coefficients() * controlPoints_;
240 return true;
241}
242
243
244/* @brief Extrapolate the values @c y from the value @c x.
245 * Given the data set @c x will compute the values \f$ y = \psi(x) \hat{\beta} \f$
246 * where \f$ \psi \f$ represents the B-spline basis functions and \f$ \hat{beta} \f$
247 * the estimated coefficients.
248 */
249template <class YArray, class XArray, class Weights>
251{
252 YArray res = coefs_.extrapolate(x) * controlPoints_;
253 return res;
254}
255
256} // namespace STK
257
258#endif /* STK_ADDITIVEBSPLINEREGRESSION_H */
In this file we define the AdditiveBSplineCoefficients class.
In this file we define the Interface base class IRegression.
In this file we define and implement the class MultiLeastSQquare.
In this file we define the class MultiLeastSQquare using lapack.
ArrayXX const & coefficients() const
give the computed coefficients of the B-spline curves.
Compute an additive BSpline, multivalued, regression function using BSpline basis.
int nbControlPoints_
number of control points of the B-spline curve.
AdditiveBSplineCoefficients< XArray > coefs_
Coefficients of the regression matrix.
virtual bool initializeStep()
compute the coefficients of the BSpline basis.
virtual bool predictionStep()
Compute the predicted outputs.
AdditiveBSplineRegression(YArray const *p_y, XArray const *p_x, int nbControlPoints, int degree=3, KnotsPosition const &position=Regress::uniformKnotsPositions_)
Constructor.
KnotsPosition position_
method of position of the knots of the B-spline curve
YArray controlPoints_
Estimated control points of the B-spline curve.
int degree_
degree of the B_Spline curve
virtual YArray extrapolate(XArray const &x) const
IRegression< YArray, XArray, Weights > Base
ArrayXX const & coefficients() const
This is a matrix of size (p_x_->range(), 0:lastControlPoints).
virtual bool regressionStep()
Compute the regression function.
virtual ~AdditiveBSplineRegression()
virtual destructor.
Interface base class for Regression methods.
virtual bool run()
run the computations.
YArray residuals_
Container of the residuals.
YArray predicted_
Container of the predicted output.
String msg_error_
String with the last error message.
Definition STK_IRunner.h:96
String const & error() const
get the last error message.
Definition STK_IRunner.h:82
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...
The class MultiLeastSQquare solve the least square problem when the response b is multidimensional.
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.