NAG FL Interface
e02bef (dim1_​spline_​auto)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

e02bef 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 trade-off between closeness of fit and smoothness of fit.

2 Specification

Fortran Interface
Subroutine e02bef ( start, m, x, y, w, s, nest, n, lamda, c, fp, wrk, lwrk, iwrk, ifail)
Integer, Intent (In) :: m, nest, lwrk
Integer, Intent (Inout) :: n, iwrk(nest), ifail
Real (Kind=nag_wp), Intent (In) :: x(m), y(m), w(m), s
Real (Kind=nag_wp), Intent (Inout) :: lamda(nest), wrk(lwrk)
Real (Kind=nag_wp), Intent (Out) :: c(nest), fp
Character (1), Intent (In) :: start
C Header Interface
#include <nag.h>
void  e02bef_ (const char *start, const Integer *m, const double x[], const double y[], const double w[], const double *s, const Integer *nest, Integer *n, double lamda[], double c[], double *fp, double wrk[], const Integer *lwrk, Integer iwrk[], Integer *ifail, const Charlen length_start)
The routine may be called by the names e02bef or nagf_fit_dim1_spline_auto.

3 Description

e02bef determines a smooth cubic spline approximation s(x) to the set of data points (xr,yr), with weights wr, for r=1,2,,m.
The spline is given in the B-spline representation
s(x)=i=1 n-4ciNi(x), (1)
where Ni(x) denotes the normalized cubic B-spline defined upon the knots λi,λi+1,,λi+4.
The total number n of these knots and their values λ1,,λn are chosen automatically by the routine. The knots λ5,,λn-4 are the interior knots; they divide the approximation interval [x1,xm] into n-7 sub-intervals. The coefficients c1,c2,,cn-4 are then determined as the solution of the following constrained minimization problem:
minimize
η=i=5 n-4δi2 (2)
subject to the constraint
θ=r=1mεr2S, (3)
where δi stands for the discontinuity jump in the third order derivative of s(x) at the interior knot λi,
εr denotes the weighted residual wr(yr-s(xr)),
and S is a non-negative number to be specified by you.
The quantity η can be seen as a measure of the (lack of) smoothness of s(x), while closeness of fit is measured through θ. 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 routine will return an interpolating spline (θ=0) if S is set to zero, and the weighted least squares cubic polynomial (η=0) 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 e02bbf, e02bcf or e02bdf, 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: start Character(1) Input
On entry: must be set to 'C' or 'W'.
start='C'
The routine will build up the knot set starting with no interior knots. No values need be assigned to the arguments n, lamda, wrk or iwrk.
start='W'
The routine will restart the knot-placing strategy using the knots found in a previous call of the routine. In this case, the arguments n, lamda, wrk, and iwrk must be unchanged from that previous call. This warm start can save much time in searching for a satisfactory value of s .
Constraint: start='C' or 'W'.
2: m Integer Input
On entry: m , the number of data points.
Constraint: m4.
3: x(m) Real (Kind=nag_wp) array Input
On entry: the values xr of the independent variable (abscissa) x , for r=1,2,,m.
Constraint: x1<x2<<xm.
4: y(m) Real (Kind=nag_wp) array Input
On entry: the values yr of the dependent variable (ordinate) y , for r=1,2,,m.
5: w(m) Real (Kind=nag_wp) array Input
On entry: the values wr of the weights, for r=1,2,,m. For advice on the choice of weights, see Section 2.1.2 in the E02 Chapter Introduction.
Constraint: w(r)>0.0, for r=1,2,,m.
6: s Real (Kind=nag_wp) Input
On entry: the smoothing factor, S .
If S=0.0, the routine 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: s0.0.
7: nest Integer Input
On entry: an overestimate for the number, n, of knots required.
Constraint: nest8. In most practical situations, nest=m/2 is sufficient. nest never needs to be larger than m+4, the number of knots needed for interpolation (s=0.0).
8: n Integer Input/Output
On entry: if the warm start option is used, the value of n must be left unchanged from the previous call.
On exit: the total number, n , of knots of the computed spline.
9: lamda(nest) Real (Kind=nag_wp) array Input/Output
On entry: if the warm start option is used, the values lamda(1),lamda(2),,lamda(n) must be left unchanged from the previous call.
On exit: the knots of the spline, i.e., the positions of the interior knots lamda(5), lamda(6),,lamda( n-4) as well as the positions of the additional knots
lamda(1)=lamda(2)=lamda(3)=lamda(4)=x1  
and
lamda( n-3)=lamda( n-2)=lamda( n-1)=lamda(n)=xm  
needed for the B-spline representation.
10: c(nest) Real (Kind=nag_wp) array Output
On exit: the coefficient ci of the B-spline Ni(x) in the spline approximation s(x), for i=1,2,,n-4.
11: fp Real (Kind=nag_wp) Output
On exit: the sum of the squared weighted residuals, θ, of the computed spline approximation. If fp=0.0, this is an interpolating spline. fp should equal 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, s must be set to a value below the value of fp produced in this case.
12: wrk(lwrk) Real (Kind=nag_wp) array Communication Array
If the warm start option is used on entry, the values wrk(1),,wrk(n) must be left unchanged from the previous call.
13: lwrk Integer Input
On entry: the dimension of the array wrk as declared in the (sub)program from which e02bef is called.
Constraint: lwrk4×m+16×nest+41.
14: iwrk(nest) Integer array Communication Array
If the warm start option is used, on entry, the values iwrk(1),,iwrk(n) must be left unchanged from the previous call.
This array is used as workspace.
15: ifail Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. When the value -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6 Error Indicators and Warnings

If on entry ifail=0 or −1, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
ifail=1
On entry, lwrk=value.
Constraint: lwrkvalue.
On entry, m=value.
Constraint: m4.
On entry, nest=value.
Constraint: nest8.
On entry, nest=value.
Constraint: nestvalue when s=0.0.
On entry, r=value and w(r)=value.
Constraint: w(r)>0.0 for all r.
On entry, s=value.
Constraint: s0.0.
On entry, start'W' or 'C': start=value.
ifail=2
On entry, r=value and w(r)=value.
Constraint: w(r)>0.0 for all r.
ifail=3
On entry, r=value, x(r-1)=value and x(r)=value.
Constraint: x(r-1)<x(r) for all r.
ifail=4
The number of knots needed is greater than nest: nest=value. Possibly s is too small: s=value.
ifail=5
The iterative process has failed to converge. Possibly s is too small: s=value.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

On successful exit, the approximation returned is such that its weighted sum of squared residuals θ (as in (3)) is equal to the smoothing factor S , up to a specified relative tolerance of 0.001 – except that if n=8, θ 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

Background information to multithreading can be found in the Multithreading documentation.
e02bef makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9 Further Comments

9.1 Timing

The time taken for a call of e02bef depends on the complexity of the shape of the data, the value of the smoothing factor S , and the number of data points. If e02bef is to be called for different values of S , much time can be saved by setting start='W' 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 wryr would be the same for all r , equal to σ, say. In this case, choosing the smoothing factor S in the range σ2(m±2m), 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 θ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=θ0/10, S=θ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 e02bef is called with start='W', 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 start='W', a fit can finally be accepted as satisfactory, it may be worthwhile to call e02bef once more with the selected value for S but now using start='C'. Often, e02bef 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 λi=xi-2, for i=5,6,,n-4. The corresponding least squares spline (see e02baf) 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 e02baf) and θ, the weighted sum of squares of residuals, is computed. If θ>S , new knots are added to the knot set to reduce θ 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 θ. Sooner or later, we find that θS and at that point the knot set is accepted. The routine 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 θ=S . The routine computes the spline by an iterative scheme which is ended when θ=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 e02baf.
An exception occurs when the routine finds at the start that, even with no interior knots (n=8), the least squares spline already has its weighted sum of squares of residuals S . In this case, since this spline (which is simply a cubic polynomial) also has an optimal value for the smoothness measure η, 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 real variable s by the call:
Call e02bbf(n,lamda,c,x,s,ifail)
where n, lamda and c are the output arguments of e02bef.
The values of the spline and its first three derivatives at a given value x may be obtained in the real array s of dimension at least 4 by the call:
Call e02bcf(n,lamda,c,x,left,s,ifail)
where if left=1, left-hand derivatives are computed and if left1, right-hand derivatives are calculated. The value of left is only relevant if x is an interior knot (see e02bcf).
The value of the definite integral of the spline over the interval x(1) to x(m) can be obtained in the real variable dint by the call:
Call e02bdf(n,lamda,c,dint,ifail)
(see e02bdf).

10 Example

This example reads in a set of data values, followed by a set of values of s . For each value of s it calls e02bef to compute a spline approximation, and prints the values of the knots and the B-spline coefficients ci.
The program includes code to evaluate the computed splines, by calls to e02bbf, at the points xr and at points mid-way 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 ×) and the positions of the knots (indicated by vertical lines): the effect of decreasing s can be clearly seen.

10.1 Program Text

Program Text (e02befe.f90)

10.2 Program Data

Program Data (e02befe.d)

10.3 Program Results

Program Results (e02befe.r)
GnuplotProduced by GNUPLOT 4.6 patchlevel 3 −2 0 2 4 6 8 0 1 2 3 4 5 6 7 8 B-spline x Example Program Calculation and Evaluation of B-splines Representation Smoothing Factor S=1.0 gnuplot_plot_1 B-spline gnuplot_plot_2 gnuplot_plot_3 data points
GnuplotProduced by GNUPLOT 4.6 patchlevel 3 −2 0 2 4 6 8 0 1 2 3 4 5 6 7 8 B-spline x Smoothing Factor S=0.5 gnuplot_plot_1 B-spline gnuplot_plot_2 gnuplot_plot_3 data points
GnuplotProduced by GNUPLOT 4.6 patchlevel 3 −2 0 2 4 6 8 0 1 2 3 4 5 6 7 8 B-spline x Smoothing Factor S=0.1 gnuplot_plot_1 B-spline gnuplot_plot_2 gnuplot_plot_3 data points