e02baf computes a weighted least squares approximation to an arbitrary set of data points by a cubic spline with knots prescribed by you. Cubic spline interpolation can also be carried out.
The routine may be called by the names e02baf or nagf_fit_dim1_spline_knots.
3Description
e02baf determines a least squares cubic spline approximation to the set of data points with weights , for . The value of , where is the number of intervals of the spline (one greater than the number of interior knots), and the values of the knots , interior to the data interval, are prescribed by you.
has the property that it minimizes , the sum of squares of the weighted residuals , for , where
The routine produces this minimizing value of and the coefficients , where , in the B-spline representation
Here denotes the normalized B-spline of degree defined upon the knots .
In order to define the full set of B-splines required, eight additional knots and are inserted automatically by the routine. The first four of these are set equal to the smallest and the last four to the largest .
The representation of in terms of B-splines is the most compact form possible in that only coefficients, in addition to the knots, fully define .
The method employed involves forming and then computing the least squares solution of a set of linear equations in the coefficients , for . The equations are formed using a recurrence relation for B-splines that is unconditionally stable (see Cox (1972) and de Boor (1972)), even for multiple (coincident) knots. The least squares solution is also obtained in a stable manner by using orthogonal transformations, viz. a variant of Givens rotations (see Gentleman (1974) and Gentleman (1973)). This requires only one equation to be stored at a time. Full advantage is taken of the structure of the equations, there being at most four nonzero values of for any value of and hence at most four coefficients in each equation.
Subsequent evaluation of from its B-spline representation may be carried out using e02bbf. If derivatives of are also required, e02bcf may be used. e02bdf can be used to compute the definite integral of .
4References
Cox M G (1972) The numerical evaluation of B-splines J. Inst. Math. Appl.10 134–149
Cox M G (1974) A data-fitting package for the non-specialist user Software for Numerical Mathematics (ed D J Evans) Academic Press
Cox M G (1975) Numerical methods for the interpolation and approximation of data by spline functions PhD Thesis City University, London
Cox M G and Hayes J G (1973) Curve fitting: a guide and suite of algorithms for the non-specialist user NPL Report NAC26 National Physical Laboratory
de Boor C (1972) On calculating with B-splines J. Approx. Theory6 50–62
Gentleman W M (1973) Least squares computations by Givens transformations without square roots J. Inst. Math. Applic.12 329–336
Gentleman W M (1974) Algorithm AS 75. Basic procedures for large sparse or weighted linear least squares problems Appl. Statist.23 448–454
Schoenberg I J and Whitney A (1953) On Polya frequency functions III Trans. Amer. Math. Soc.74 246–259
5Arguments
1: – IntegerInput
On entry: the number of data points.
Constraint:
, where is the number of distinct values in the data.
2: – IntegerInput
On entry: , where is the number of intervals of the spline (which is one greater than the number of interior knots, i.e., the knots strictly within the range to ) over which the spline is defined.
Constraint:
, where is the number of distinct values in the data.
3: – Real (Kind=nag_wp) arrayInput
On entry: the values
of the independent variable (abscissa), for . The values must satisfy the Schoenberg–Whitney conditions (see Section 9).
Constraint:
.
4: – Real (Kind=nag_wp) arrayInput
On entry: the values
of the dependent variable (ordinate), for .
5: – Real (Kind=nag_wp) arrayInput
On entry: the values
of the weights, for . For advice on the choice of weights, see the E02 Chapter Introduction.
Constraint:
, for .
6: – Real (Kind=nag_wp) arrayInput/Output
On entry: must be set to the th (interior) knot, , for .
Constraint:
.
On exit: the input values are unchanged, and
, for , , , , ncap7 contains the additional (exterior) knots introduced by the routine. For advice on the choice of knots, see Section 3.3 in the E02 Chapter Introduction.
7: – Real (Kind=nag_wp) arrayWorkspace
8: – Real (Kind=nag_wp) arrayWorkspace
9: – Real (Kind=nag_wp) arrayOutput
On exit: the coefficient
of the B-spline , for . The remaining elements of the array are not used.
10: – Real (Kind=nag_wp)Output
On exit: the residual sum of squares, .
11: – IntegerInput/Output
On entry: ifail must be set to , or to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of means that an error message is printed while a value of means that it is not.
If halting is not appropriate, the value or is recommended. If message printing is undesirable, then the value is recommended. Otherwise, the value is recommended. When the value or 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).
6Error 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:
On entry, , and .
Constraint: .
On entry, and .
Constraint: .
On entry, , , and .
Constraint: .
On entry, and .
Constraint: .
On entry, the x values are not in nondecreasing order. , , and .
Constraint: , where xdist is the set of distinct -values.
On entry, .
Constraint: .
On entry, and .
Constraint: .
On entry, and .
Constraint: , where mdist is the number of distinct x-values.
On entry, the Schoenberg–Whitney conditions fail to hold for at least one subset of the distinct data abscissae. , , and .
Constraint: , where xdist is the set of distinct -values.
On entry, the Schoenberg–Whitney conditions fail to hold for at least one subset of the distinct data abscissae. , , and .
Constraint: , where xdist is the set of distinct -values.
On entry, the Schoenberg–Whitney conditions fail to hold for at least one subset of the distinct data abscissae. , , and .
Constraint: , where xdist is the set of distinct -values.
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.
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.
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.
7Accuracy
The rounding errors committed are such that the computed coefficients are exact for a slightly perturbed set of ordinates . The ratio of the root-mean-square value for the to the root-mean-square value of the can be expected to be less than a small multiple of , where is a condition number for the problem. Values of for – practical datasets all proved to lie between and (see Cox (1975)). (Note that for these datasets, replacing the coincident end knots at the end points and used in the routine by various choices of non-coincident exterior knots gave values of between and . Again see Cox (1975) for further details.) In general we would not expect to be large unless the choice of knots results in near-violation of the Schoenberg–Whitney conditions.
A cubic spline which adequately fits the data and is free from spurious oscillations is more likely to be obtained if the knots are chosen to be grouped more closely in regions where the function (underlying the data) or its derivatives change more rapidly than elsewhere.
8Parallelism and Performance
Background information to multithreading can be found in the Multithreading documentation.
e02baf is not threaded in any implementation.
9Further Comments
The time taken is approximately seconds, where is a machine-dependent constant.
Multiple knots are permitted as long as their multiplicity does not exceed , i.e., the complete set of knots must satisfy , for , (see Section 6). At a knot of multiplicity one (the usual case), and its first two derivatives are continuous. At a knot of multiplicity two, and its first derivative are continuous. At a knot of multiplicity three, is continuous, and at a knot of multiplicity four, is generally discontinuous.
The routine can be used efficiently for cubic spline interpolation, i.e., if . The abscissae must then of course satisfy . Recommended values for the knots in this case are , for .
The Schoenberg–Whitney conditions (see Schoenberg and Whitney (1953)) state that there must be a subset of strictly increasing values, , among the abscissae such that
,
,
.
If this condition is not satisfied, then there is no unique solution: there are regions containing too many knots compared with the number of data points.
10Example
Determine a weighted least squares cubic spline approximation with five intervals (four interior knots) to a set of given data points. Tabulate the data and the corresponding values of the approximating spline, together with the residual errors, and also the values of the approximating spline at points half-way between each pair of adjacent data points.
The example program is written in a general form that will enable a cubic spline approximation with intervals ( interior knots) to be obtained to data points, with arbitrary positive weights, and the approximation to be tabulated. Note that e02bbf is used to evaluate the approximating spline. The program is self-starting in that any number of datasets can be supplied.