E02BEF computes a cubic spline approximation to an arbitrary set of data points. The knots of the spline are located automatically, but a single parameter must be specified to control the trade-off between closeness of fit and smoothness of fit.
E02BEF determines a smooth cubic spline approximation to the set of data points , with weights , for .
The spline is given in the B-spline representation
where denotes the normalized cubic B-spline defined upon the knots .
The total number of these knots and their values are chosen automatically by the routine. The knots are the interior knots; they divide the approximation interval into sub-intervals. The coefficients are then determined as the solution of the following constrained minimization problem:
subject to the constraint
stands for the discontinuity jump in the third order derivative of at the interior knot ,
denotes the weighted residual ,
is a non-negative number to be specified by you.
The quantity can be seen as a measure of the (lack of) smoothness of , while closeness of fit is measured through . By means of the parameter , ‘the smoothing factor’, you can then control the balance between these two (usually conflicting) properties. If is too large, the spline will be too smooth and signal will be lost (underfit); if is too small, the spline will pick up too much noise (overfit). In the extreme cases the routine will return an interpolating spline if is set to zero, and the weighted least squares cubic polynomial if is set very large. Experimenting with values between these two extremes should result in a good compromise. (See Section 8.2 for advice on choice of .)
The method employed is outlined in Section 8.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 ), 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 8.4.
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
1: START – CHARACTER(1)Input
On entry: must be set to 'C' or 'W'.
The routine will build up the knot set starting with no interior knots. No values need be assigned to the parameters N, LAMDA, WRK or IWRK.
The routine will restart the knot-placing strategy using the knots found in a previous call of the routine. In this case, the parameters 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 .
On exit: the coefficient
of the B-spline in the spline approximation , for .
11: FP – REAL (KIND=nag_wp)Output
On exit: the sum of the squared weighted residuals, , of the computed spline approximation. If , this is an interpolating spline. FP should equal within a relative tolerance of unless when the spline has no interior knots and so is simply a cubic polynomial. For knots to be inserted, must be set to a value below the value of FP produced in this case.
12: WRK(LWRK) – REAL (KIND=nag_wp) arrayCommunication Array
If the warm start option is used on entry, the values must be left unchanged from the previous call.
13: LWRK – INTEGERInput
On entry: the dimension of the array WRK as declared in the (sub)program from which E02BEF is called.
If the warm start option is used, on entry, the values must be left unchanged from the previous call.
This array is used as workspace.
15: IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to , . If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value is recommended. If the output of error messages is undesirable, then the value is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is . When the value is used it is essential to test the value of IFAIL on exit.
On exit: unless the routine detects an error or a warning has been flagged (see Section 6).
6 Error Indicators and Warnings
If on entry or , explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
The weights are not all strictly positive.
The values of , for , are not in strictly increasing order.
The number of knots required is greater than NEST. Try increasing NEST and, if necessary, supplying larger arrays for the parameters LAMDA, C, WRK and IWRK. However, if NEST is already large, say , then this error exit may indicate that S is too small.
The iterative process used to compute the coefficients of the approximating spline has failed to converge. This error exit may occur if S has been set very small. If the error persists with increased S, contact NAG.
If or , a spline approximation is returned, but it fails to satisfy the fitting criterion (see (2) and (3)) – perhaps by only a small amount, however.
On successful exit, the approximation returned is such that its weighted sum of squared residuals (as in (3)) is equal to the smoothing factor , up to a specified relative tolerance of – except that if , may be significantly less than : in this case the computed spline is simply a weighted least squares polynomial approximation of degree , i.e., a spline with no interior knots.
8 Further Comments
The time taken for a call of E02BEF depends on the complexity of the shape of the data, the value of the smoothing factor , and the number of data points. If E02BEF is to be called for different values of , much time can be saved by setting after the first call.
8.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 would be the same for all , equal to , say. In this case, choosing the smoothing factor in the range , 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 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 and so determine the least squares cubic polynomial; the value returned in FP, call it , gives an upper bound for . Then progressively decrease the value of to obtain closer fits – say by a factor of in the beginning, i.e., , , 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 and on the behaviour of the function underlying the data. However, if E02BEF is called with , 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 and , a fit can finally be accepted as satisfactory, it may be worthwhile to call E02BEF once more with the selected value for but now using . 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.
8.3 Outline of Method Used
If , the requisite number of knots is known in advance, i.e., ; the interior knots are located immediately as , for . The corresponding least squares spline (see E02BAF) is then an interpolating spline and therefore a solution of the problem.
If , 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 , 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 and on the progress made so far in reducing . Sooner or later, we find that 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 . The routine computes the spline by an iterative scheme which is ended when within a relative tolerance of . 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 , the least squares spline already has its weighted sum of squares of residuals . 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 has been chosen too large.
This example reads in a set of data values, followed by a set of values of . For each value of it calls E02BEF to compute a spline approximation, and prints the values of the knots and the B-spline coefficients .
The program includes code to evaluate the computed splines, by calls to E02BBF, at the points 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 can be clearly seen.