NAG CL Interface
e02bec (dim1_spline_auto)
1
Purpose
e02bec computes a cubic spline approximation to an arbitrary set of data points. The knots of the spline are located automatically, but a single argument must be specified to control the tradeoff between closeness of fit and smoothness of fit.
2
Specification
void 
e02bec (Nag_Start start,
Integer m,
const double x[],
const double y[],
const double weights[],
double s,
Integer nest,
double *fp,
Nag_Comm *warmstartinf,
Nag_Spline *spline,
NagError *fail) 

The function may be called by the names: e02bec, nag_fit_dim1_spline_auto or nag_1d_spline_fit.
3
Description
e02bec determines a smooth cubic spline approximation $s\left(x\right)$ to the set of data points $\left({x}_{\mathit{r}},{y}_{\mathit{r}}\right)$, with weights ${w}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,m$.
The spline is given in the Bspline representation
where
${N}_{i}\left(x\right)$ denotes the normalized cubic Bspline defined upon the knots
${\lambda}_{i},{\lambda}_{i+1},\dots ,{\lambda}_{i+4}$.
The total number $n$ of these knots and their values ${\lambda}_{1},\dots ,{\lambda}_{n}$ are chosen automatically by the function. The knots ${\lambda}_{5},\dots ,{\lambda}_{n4}$ are the interior knots; they divide the approximation interval $\left[{x}_{1},{x}_{m}\right]$ into $n7$ subintervals. The coefficients ${c}_{1},{c}_{2},\dots ,{c}_{n4}$ are then determined as the solution of the following constrained minimization problem:
minimize
subject to the constraint
where 
${\delta}_{i}$ 
stands for the discontinuity jump in the third order derivative of $s\left(x\right)$ at the interior knot ${\lambda}_{i}$, 

${\epsilon}_{r}$ 
denotes the weighted residual ${w}_{r}\left({y}_{r}s\left({x}_{r}\right)\right)$, 
and 
$S$ 
is a nonnegative number to be specified by you. 
The quantity
$\eta $ can be seen as a measure of the (lack of) smoothness of
$s\left(x\right)$, while closeness of fit is measured through
$\theta $. By means of the argument
$S$, ‘the smoothing factor’, you can then control the balance between these two (usually conflicting) properties. If
$S$ is too large, the spline will be too smooth and signal will be lost (underfit); if
$S$ is too small, the spline will pick up too much noise (overfit). In the extreme cases the function will return an interpolating spline
$\left(\theta =0\right)$ if
$S$ is set to zero, and the weighted least squares cubic polynomial
$\left(\eta =0\right)$ if
$S$ is set very large. Experimenting with
$S$ values between these two extremes should result in a good compromise. (See
Section 9.2 for advice on choice of
$S$.)
The method employed is outlined in
Section 9.3 and fully described in
Dierckx (1975),
Dierckx (1981) and
Dierckx (1982). It involves an adaptive strategy for locating the knots of the cubic spline (depending on the function underlying the data and on the value of
$S$), and an iterative method for solving the constrained minimization problem once the knots have been determined.
Values of the computed spline, or of its derivatives or definite integral, can subsequently be computed by calling
e02bbc,
e02bcc or
e02bdc, as described in
Section 9.4.
4
References
Dierckx P (1975) An algorithm for smoothing, differentiating and integration of experimental data using spline functions J. Comput. Appl. Math. 1 165–184
Dierckx P (1981) An improved algorithm for curve fitting with spline functions Report TW54 Department of Computer Science, Katholieke Univerciteit Leuven
Dierckx P (1982) A fast algorithm for smoothing data on a rectangular grid while using spline functions SIAM J. Numer. Anal. 19 1286–1304
Reinsch C H (1967) Smoothing by spline functions Numer. Math. 10 177–183
5
Arguments

1:
$\mathbf{start}$ – Nag_Start
Input

On entry: must be set to
$\mathrm{Nag\_Cold}$ or
$\mathrm{Nag\_Warm}$.
 ${\mathbf{start}}=\mathrm{Nag\_Cold}$
 The function will build up the knot set starting with no interior knots. No values need be assigned to the argument $\mathbf{spline}\mathbf{\to}\mathbf{n}$, and memory will be allocated internally to $\mathbf{spline}\mathbf{\to}\mathbf{lamda}$, $\mathbf{spline}\mathbf{\to}\mathbf{c}$, $\mathbf{warmstartinf}\mathbf{\to}\mathbf{nag\_w}$ and $\mathbf{warmstartinf}\mathbf{\to}\mathbf{nag\_iw}$.
 ${\mathbf{start}}=\mathrm{Nag\_Warm}$
 The function will restart the knotplacing strategy using the knots found in a previous call of the function. In this case, all arguments except s must be unchanged from that previous call. This warm start can save much time in searching for a satisfactory value of the smoothing factor $S$.
Constraint:
${\mathbf{start}}=\mathrm{Nag\_Cold}$ or $\mathrm{Nag\_Warm}$.

2:
$\mathbf{m}$ – Integer
Input

On entry:
$m$, the number of data points.
Constraint:
${\mathbf{m}}\ge 4$.

3:
$\mathbf{x}\left[{\mathbf{m}}\right]$ – const double
Input

On entry:
${\mathbf{x}}\left[r1\right]$ holds the value ${x}_{r}$ of the independent variable (abscissa) $x$, for $\mathit{r}=1,2,\dots ,m$.
Constraint:
${x}_{1}<{x}_{2}<\cdots <{x}_{m}$.

4:
$\mathbf{y}\left[{\mathbf{m}}\right]$ – const double
Input

On entry:
${\mathbf{y}}\left[r1\right]$ holds the value ${y}_{r}$ of the dependent variable (ordinate) $y$, for $\mathit{r}=1,2,\dots ,m$.

5:
$\mathbf{weights}\left[{\mathbf{m}}\right]$ – const double
Input

On entry: the values
${w}_{\mathit{r}}$ of the weights, for
$\mathit{r}=1,2,\dots ,m$. For advice on the choice of weights, see
Section 2.1.2 in the
E02 Chapter Introduction.
Constraint:
${\mathbf{weights}}\left[\mathit{r}1\right]>0.0$, for $\mathit{r}=1,2,\dots ,m$.

6:
$\mathbf{s}$ – double
Input

On entry: the smoothing factor,
$S$.
If $S=0.0$, the function returns an interpolating spline.
If $S$ is smaller than machine precision, it is assumed equal to zero.
For advice on the choice of
$S$, see
Sections 3 and
9.2.
Constraint:
${\mathbf{s}}\ge 0.0$.

7:
$\mathbf{nest}$ – Integer
Input

On entry:
an overestimate for the number, $n$, of knots required.
Constraint:
${\mathbf{nest}}\ge 8$. In most practical situations,
${\mathbf{nest}}={\mathbf{m}}/2$ is sufficient.
nest never needs to be larger than
${\mathbf{m}}+4$, the number of knots needed for interpolation
$\left({\mathbf{s}}=0.0\right)$.

8:
$\mathbf{fp}$ – double *
Output

On exit: the sum of the squared weighted residuals,
$\theta $, of the computed spline approximation. If
${\mathbf{fp}}=0.0$, this is an interpolating spline.
fp should equal
${\mathbf{s}}$ within a relative tolerance of
$0.001$ unless
$n=8$ when the spline has no interior knots and so is simply a cubic polynomial. For knots to be inserted,
${\mathbf{s}}$ must be set to a value below the value of
fp produced in this case.

9:
$\mathbf{warmstartinf}$ – Nag_Comm *

Pointer to structure of type Nag_Comm with the following members:
 nag_w – double *Input

On entry: if the warm start option is used, the values $\mathbf{nag\_w}\left[0\right],\dots ,\mathbf{nag\_w}\left[\mathbf{spline}\mathbf{\to}\mathbf{n}1\right]$ must be left unchanged from the previous call.
 nag_iw – Integer *Input

On entry: if the warm start option is used, the values $\mathbf{nag\_iw}\left[0\right],\dots ,\mathbf{nag\_iw}\left[\mathbf{spline}\mathbf{\to}\mathbf{n}1\right]$ must be left unchanged from the previous call.
Note that when the information contained in the pointers
$\mathbf{nag\_w}$ and
$\mathbf{nag\_iw}$ is no longer of use, or before a new call to
e02bec with the same
warmstartinf, you should free this storage using the NAG macro
NAG_FREE. This storage will have been allocated only if this function returns with
${\mathbf{fail}}\mathbf{.}\mathbf{code}=\mathrm{NE\_NOERROR}$,
NE_SPLINE_COEFF_CONV or
NE_NUM_KNOTS_1D_GT.

10:
$\mathbf{spline}$ – Nag_Spline *

Pointer to structure of type Nag_Spline with the following members:
 n – IntegerInput/Output

On entry: if the warm start option is used, the value of $\mathbf{n}$ must be left unchanged from the previous call.
On exit: the total number, $n$, of knots of the computed spline.
 lamda – double *Input/Output

On entry: a pointer to which, if
${\mathbf{start}}=\mathrm{Nag\_Cold}$, memory of size
nest is internally allocated. If the warm start option is used, the values
$\mathbf{lamda}\left[0\right]$,
$\mathbf{lamda}\left[1\right]$,
$\dots $,
$\mathbf{lamda}\left[\mathbf{n}1\right]$ must be left unchanged from the previous call.
On exit: the knots of the spline, i.e., the positions of the interior knots $\mathbf{lamda}\left[4\right]$, $\mathbf{lamda}\left[5\right]$, $\dots $, $\mathbf{lamda}\left[\mathbf{n}5\right]$ as well as the positions of the additional knots $\mathbf{lamda}\left[0\right]=\mathbf{lamda}\left[1\right]=\mathbf{lamda}\left[2\right]=\mathbf{lamda}\left[3\right]={\mathbf{x}}\left[0\right]$ and $\mathbf{lamda}\left[\mathbf{n}4\right]=\mathbf{lamda}\left[\mathbf{n}3\right]=\mathbf{lamda}\left[\mathbf{n}2\right]=\mathbf{lamda}\left[\mathbf{n}1\right]={\mathbf{x}}\left[{\mathbf{m}}1\right]$ needed for the Bspline representation.
 c – double *Output

On exit: a pointer to which, if ${\mathbf{start}}=\mathrm{Nag\_Cold}$, memory of size ${\mathbf{nest}}4$ is internally allocated. $\mathbf{c}\left[i1\right]$ holds the coefficient ${c}_{i}$ of the Bspline ${N}_{i}\left(x\right)$ in the spline approximation $s\left(x\right)$, for $i=1,2,\dots ,n4$.
Note that when the information contained in the pointers
$\mathbf{lamda}$ and
$\mathbf{c}$ is no longer of use, or before a new call to
e02bec with the same
spline, you should free this storage using the NAG macro
NAG_FREE. This storage will have been allocated only if this function returns with
${\mathbf{fail}}\mathbf{.}\mathbf{code}=\mathrm{NE\_NOERROR}$,
NE_SPLINE_COEFF_CONV, or
NE_NUM_KNOTS_1D_GT.

11:
$\mathbf{fail}$ – NagError *
Input/Output

The NAG error argument (see
Section 7 in the Introduction to the NAG Library CL Interface).
6
Error Indicators and Warnings
 NE_ALLOC_FAIL

Dynamic memory allocation failed.
 NE_BAD_PARAM

On entry, argument
start had an illegal value.
 NE_ENUMTYPE_WARM

${\mathbf{start}}=\mathrm{Nag\_Warm}$ at the first call of this function.
start must be set to
${\mathbf{start}}=\mathrm{Nag\_Cold}$ at the first call.
 NE_INT_ARG_LT

On entry, ${\mathbf{m}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{m}}\ge 4$.
On entry, ${\mathbf{nest}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{nest}}\ge 8$.
 NE_NOT_STRICTLY_INCREASING

The sequence
x is not strictly increasing:
${\mathbf{x}}\left[\u2329\mathit{\text{value}}\u232a\right]=\u2329\mathit{\text{value}}\u232a$,
${\mathbf{x}}\left[\u2329\mathit{\text{value}}\u232a\right]=\u2329\mathit{\text{value}}\u232a$.
 NE_NUM_KNOTS_1D_GT

The number of knots needed is greater than
nest,
${\mathbf{nest}}=\u2329\mathit{\text{value}}\u232a$. If
nest is already large, say
${\mathbf{nest}}>{\mathbf{m}}/2$, this may indicate that possibly
s is too small:
${\mathbf{s}}=\u2329\mathit{\text{value}}\u232a$.
 NE_REAL_ARG_LT

On entry, ${\mathbf{s}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{s}}\ge 0.0$.
 NE_SF_D_K_CONS

On entry, ${\mathbf{nest}}=\u2329\mathit{\text{value}}\u232a$, ${\mathbf{s}}=\u2329\mathit{\text{value}}\u232a$, ${\mathbf{m}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{nest}}\ge {\mathbf{m}}+4$ when ${\mathbf{s}}=0.0$.
 NE_SPLINE_COEFF_CONV

The iterative process has failed to converge. Possibly
s is too small:
${\mathbf{s}}=\u2329\mathit{\text{value}}\u232a$.
 NE_WEIGHTS_NOT_POSITIVE

On entry, the weights are not strictly positive: ${\mathbf{weights}}\left[\u2329\mathit{\text{value}}\u232a\right]=\u2329\mathit{\text{value}}\u232a$.
If the function fails with an error exit of
NE_SPLINE_COEFF_CONV or
NE_NUM_KNOTS_1D_GT, a spline approximation is returned, but it fails to satisfy the fitting criterion (see
(2) and
(3)) – perhaps by only a small amount, however.
7
Accuracy
On successful exit, the approximation returned is such that its weighted sum of squared residuals
$\theta $ (as in
(3)) is equal to the smoothing factor
$S$, up to a specified relative tolerance of
$0.001$ – except that if
$n=8$,
$\theta $ may be significantly less than
$S$: in this case the computed spline is simply a weighted least squares polynomial approximation of degree
$3$, i.e., a spline with no interior knots.
8
Parallelism and Performance
e02bec is not threaded in any implementation.
9.1
Timing
The time taken for a call of e02bec depends on the complexity of the shape of the data, the value of the smoothing factor $S$, and the number of data points. If e02bec is to be called for different values of $S$, much time can be saved by setting ${\mathbf{start}}=\mathrm{Nag\_Warm}$ after the first call.
9.2
Choice of S
If the weights have been correctly chosen (see
Section 2.1.2 in the
E02 Chapter Introduction), the standard deviation of
${w}_{r}{y}_{r}$ would be the same for all
$r$, equal to
$\sigma $, say. In this case, choosing the smoothing factor
$S$ in the range
${\sigma}^{2}\left(m\pm \sqrt{2m}\right)$, as suggested by
Reinsch (1967), is likely to give a good start in the search for a satisfactory value. Otherwise, experimenting with different values of
$S$ will be required from the start, taking account of the remarks in
Section 3.
In that case, in view of computation time and memory requirements, it is recommended to start with a very large value for
$S$ and so determine the least squares cubic polynomial; the value returned in
fp, call it
${\theta}_{0}$, gives an upper bound for
$S$. Then progressively decrease the value of
$S$ to obtain closer fits – say by a factor of
$10$ in the beginning, i.e.,
$S={\theta}_{0}/10$,
$S={\theta}_{0}/100$, and so on, and more carefully as the approximation shows more details.
The number of knots of the spline returned, and their location, generally depend on the value of $S$ and on the behaviour of the function underlying the data. However, if e02bec is called with ${\mathbf{start}}=\mathrm{Nag\_Warm}$, the knots returned may also depend on the smoothing factors of the previous calls. Therefore if, after a number of trials with different values of $S$ and ${\mathbf{start}}=\mathrm{Nag\_Warm}$, a fit can finally be accepted as satisfactory, it may be worthwhile to call e02bec once more with the selected value for $S$ but now using ${\mathbf{start}}=\mathrm{Nag\_Cold}$. Often, e02bec then returns an approximation with the same quality of fit but with fewer knots, which is therefore better if data reduction is also important.
9.3
Outline of Method Used
If
$S=0$, the requisite number of knots is known in advance, i.e.,
$n=m+4$; the interior knots are located immediately as
${\lambda}_{\mathit{i}}={x}_{\mathit{i}2}$, for
$\mathit{i}=5,6,\dots ,n4$. The corresponding least squares spline (see
e02bac) is then an interpolating spline and therefore a solution of the problem.
If
$S>0$, a suitable knot set is built up in stages (starting with no interior knots in the case of a cold start but with the knot set found in a previous call if a warm start is chosen). At each stage, a spline is fitted to the data by least squares (see
e02bac) and
$\theta $, the weighted sum of squares of residuals, is computed. If
$\theta >S$, new knots are added to the knot set to reduce
$\theta $ at the next stage. The new knots are located in intervals where the fit is particularly poor, their number depending on the value of
$S$ and on the progress made so far in reducing
$\theta $. Sooner or later, we find that
$\theta \le S$ and at that point the knot set is accepted. The function then goes on to compute the (unique) spline which has this knot set and which satisfies the full fitting criterion specified by
(2) and
(3). The theoretical solution has
$\theta =S$. The function computes the spline by an iterative scheme which is ended when
$\theta =S$ within a relative tolerance of
$0.001$. The main part of each iteration consists of a linear least squares computation of special form, done in a similarly stable and efficient manner as in
e02bac.
An exception occurs when the function finds at the start that, even with no interior knots $\left(n=8\right)$, the least squares spline already has its weighted sum of squares of residuals $\text{}\le S$. In this case, since this spline (which is simply a cubic polynomial) also has an optimal value for the smoothness measure $\eta $, namely zero, it is returned at once as the (trivial) solution. It will usually mean that $S$ has been chosen too large.
For further details of the algorithm and its use, see
Dierckx (1981).
9.4
Evaluation of Computed Spline
The value of the computed spline at a given value
x may be obtained in the double variable
s by the call:
nag_fit_dim1_spline_eval(x, &s, &spline, &fail)
where
spline is a structure of type Nag_Spline which is the output argument of
e02bec.
The values of the spline and its first three derivatives at a given value
x may be obtained in the array
s of dimension at least 4 by the call:
nag_fit_dim1_spline_deriv(derivs, x, s, &spline, &fail)
where, if
${\mathbf{derivs}}=\mathrm{Nag\_LeftDerivs}$, lefthand derivatives are computed and, if
${\mathbf{derivs}}=\mathrm{Nag\_RightDerivs}$, righthand derivatives are calculated. The value of
derivs is only relevant if
x is an interior knot (see
e02bcc).
The value of the definite integral of the spline over the interval
${\mathbf{x}}\left[0\right]$ to
${\mathbf{x}}\left[{\mathbf{m}}1\right]$ can be obtained in the variable
integral by the call:
nag_fit_dim1_spline_integ(&spline, &integral, &fail)
see
e02bdc.
10
Example
This example reads in a set of data values, followed by a set of values of ${\mathbf{s}}$. For each value of ${\mathbf{s}}$ it calls e02bec to compute a spline approximation, and prints the values of the knots and the Bspline coefficients ${c}_{i}$.
The program includes code to evaluate the computed splines, by calls to
e02bbc, at the points
${x}_{r}$ and at points midway between them. These values are not printed out, however; instead the results are illustrated by plots of the computed splines, together with the data points (indicated by
$\times $) and the positions of the knots (indicated by vertical lines): the effect of decreasing
${\mathbf{s}}$ can be clearly seen.
10.1
Program Text
10.2
Program Data
10.3
Program Results