hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_fit_1dspline_knots (e02ba)

Purpose

nag_fit_1dspline_knots (e02ba) 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.

Syntax

[lamda, c, ss, ifail] = e02ba(x, y, w, lamda, 'm', m, 'ncap7', ncap7)
[lamda, c, ss, ifail] = nag_fit_1dspline_knots(x, y, w, lamda, 'm', m, 'ncap7', ncap7)

Description

nag_fit_1dspline_knots (e02ba) determines a least squares cubic spline approximation s(x)s(x) to the set of data points (xr,yr)(xr,yr) with weights wrwr, for r = 1,2,,mr=1,2,,m. The value of ncap7 = n + 7ncap7=n-+7, where nn- is the number of intervals of the spline (one greater than the number of interior knots), and the values of the knots λ5,λ6,,λn + 3λ5,λ6,,λn-+3, interior to the data interval, are prescribed by you.
s(x)s(x) has the property that it minimizes θθ, the sum of squares of the weighted residuals εrεr, for r = 1,2,,mr=1,2,,m, where
εr = wr(yrs(xr)).
εr=wr(yr-s(xr)).
The function produces this minimizing value of θθ and the coefficients c1,c2,,cqc1,c2,,cq, where q = n + 3q=n-+3, in the B-spline representation
q
s(x) = ciNi(x).
i = 1
s(x)=i=1qciNi(x).
Here Ni(x)Ni(x) denotes the normalized B-spline of degree 33 defined upon the knots λi,λi + 1,,λi + 4λi,λi+1,,λi+4.
In order to define the full set of B-splines required, eight additional knots λ1,λ2,λ3,λ4λ1,λ2,λ3,λ4 and λn + 4,λn + 5,λn + 6,λn + 7λn-+4,λn-+5,λn-+6,λn-+7 are inserted automatically by the function. The first four of these are set equal to the smallest xrxr and the last four to the largest xrxr.
The representation of s(x)s(x) in terms of B-splines is the most compact form possible in that only n + 3n-+3 coefficients, in addition to the n + 7n-+7 knots, fully define s(x)s(x).
The method employed involves forming and then computing the least squares solution of a set of mm linear equations in the coefficients cici, for i = 1,2,,n + 3i=1,2,,n-+3. 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 Ni(x)Ni(x) for any value of xx and hence at most four coefficients in each equation.
For further details of the algorithm and its use see Cox (1974), Cox (1975) and Cox and Hayes (1973).
Subsequent evaluation of s(x)s(x) from its B-spline representation may be carried out using nag_fit_1dspline_eval (e02bb). If derivatives of s(x)s(x) are also required, nag_fit_1dspline_deriv (e02bc) may be used. nag_fit_1dspline_integ (e02bd) can be used to compute the definite integral of s(x)s(x).

References

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. Theory 6 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

Parameters

Compulsory Input Parameters

1:     x(m) – double array
m, the dimension of the array, must satisfy the constraint mmdist4mmdist4, where mdistmdist is the number of distinct xx values in the data.
The values xrxr of the independent variable (abscissa), for r = 1,2,,mr=1,2,,m.
Constraint: x1x2xmx1x2xm.
2:     y(m) – double array
m, the dimension of the array, must satisfy the constraint mmdist4mmdist4, where mdistmdist is the number of distinct xx values in the data.
The values yryr of the dependent variable (ordinate), for r = 1,2,,mr=1,2,,m.
3:     w(m) – double array
m, the dimension of the array, must satisfy the constraint mmdist4mmdist4, where mdistmdist is the number of distinct xx values in the data.
The values wrwr of the weights, for r = 1,2,,mr=1,2,,m. For advice on the choice of weights, see the E02 Chapter Introduction.
Constraint: w(r) > 0.0wr>0.0, for r = 1,2,,mr=1,2,,m.
4:     lamda(ncap7) – double array
ncap7, the dimension of the array, must satisfy the constraint 8ncap7mdist + 48ncap7mdist+4, where mdistmdist is the number of distinct xx values in the data.
lamda(i)lamdai must be set to the (i4)(i-4)th (interior) knot, λiλi, for i = 5,6,,n + 3i=5,6,,n-+3.
Constraint: x(1) < lamda(5)lamda(6)lamda(ncap74) < x(m)x1<lamda5lamda6lamdancap7-4<xm.

Optional Input Parameters

1:     m – int64int32nag_int scalar
Default: The dimension of the arrays x, y, w. (An error is raised if these dimensions are not equal.)
The number mm of data points.
Constraint: mmdist4mmdist4, where mdistmdist is the number of distinct xx values in the data.
2:     ncap7 – int64int32nag_int scalar
Default: The dimension of the array lamda.
n + 7n-+7, where nn- 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 x1x1 to xmxm) over which the spline is defined.
Constraint: 8ncap7mdist + 48ncap7mdist+4, where mdistmdist is the number of distinct xx values in the data.

Input Parameters Omitted from the MATLAB Interface

work1 work2

Output Parameters

1:     lamda(ncap7) – double array
The input values are unchanged, and lamda(i)lamdai, for i = 1,2,3,4i=1,2,3,4, ncap73ncap7-3, ncap72ncap7-2, ncap71ncap7-1, ncap7 contains the additional (exterior) knots introduced by the function. For advice on the choice of knots, see Section [Cubic Spline Curves] in the E02 Chapter Introduction.
2:     c(ncap7) – double array
The coefficient cici of the B-spline Ni(x)Ni(x), for i = 1,2,,n + 3i=1,2,,n-+3. The remaining elements of the array are not used.
3:     ss – double scalar
The residual sum of squares, θθ.
4:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
  ifail = 1ifail=1
The knots fail to satisfy the condition
x(1) < lamda(5)lamda(6)lamda(ncap74) < x(m)x1<lamda5lamda6lamdancap7-4<xm.
Thus the knots are not in correct order or are not interior to the data interval.
  ifail = 2ifail=2
The weights are not all strictly positive.
  ifail = 3ifail=3
The values of x(r)xr, for r = 1,2,,mr=1,2,,m, are not in nondecreasing order.
  ifail = 4ifail=4
ncap7 < 8ncap7<8 (so the number of interior knots is negative) or ncap7 > mdist + 4ncap7>mdist+4, where mdistmdist is the number of distinct xx values in the data (so there cannot be a unique solution).
  ifail = 5ifail=5
The conditions specified by Schoenberg and Whitney (1953) fail to hold for at least one subset of the distinct data abscissae. That is, there is no subset of ncap74ncap7-4 strictly increasing values, x(R(1)),x(R(2)),,x(R(ncap74))x(R(1)),x(R(2)),,x(R(ncap7-4)), among the abscissae such that
  • x(R(1)) < lamda(1) < x(R(5))x(R(1))<lamda1<x(R(5)),
  • x(R(2)) < lamda(2) < x(R(6))x(R(2))<lamda2<x(R(6)),
  • x(R(ncap78)) < lamda(ncap78) < x(R(ncap74))x(R(ncap7-8))<lamdancap7-8<x(R(ncap7-4)).
This means that there is no unique solution: there are regions containing too many knots compared with the number of data points.

Accuracy

The rounding errors committed are such that the computed coefficients are exact for a slightly perturbed set of ordinates yr + δyryr+δyr. The ratio of the root-mean-square value for the δyrδyr to the root-mean-square value of the yryr can be expected to be less than a small multiple of κ × m × machine precisionκ×m×machine precision, where κκ is a condition number for the problem. Values of κκ for 20203030 practical datasets all proved to lie between 4.54.5 and 7.87.8 (see Cox (1975)). (Note that for these datasets, replacing the coincident end knots at the end points x1x1 and xmxm used in the function by various choices of non-coincident exterior knots gave values of κκ between 1616 and 180180. 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.

Further Comments

The time taken is approximately C × (2m + n + 7)C×(2m+n-+7) seconds, where CC is a machine-dependent constant.
Multiple knots are permitted as long as their multiplicity does not exceed 44, i.e., the complete set of knots must satisfy λi < λi + 4λi<λi+4, for i = 1,2,,n + 3i=1,2,,n-+3, (see Section [Error Indicators and Warnings]). At a knot of multiplicity one (the usual case), s(x)s(x) and its first two derivatives are continuous. At a knot of multiplicity two, s(x)s(x) and its first derivative are continuous. At a knot of multiplicity three, s(x)s(x) is continuous, and at a knot of multiplicity four, s(x)s(x) is generally discontinuous.
The function can be used efficiently for cubic spline interpolation, i.e., if m = n + 3m=n-+3. The abscissae must then of course satisfy x1 < x2 < < xmx1<x2<<xm. Recommended values for the knots in this case are λi = xi2λi=xi-2, for i = 5,6,,n + 3i=5,6,,n-+3.

Example

function nag_fit_1dspline_knots_example
x = [0.2;
     0.47;
     0.74;
     1.09;
     1.6;
     1.9;
     2.6;
     3.1;
     4;
     5.15;
     6.17;
     8;
     10;
     12];
y = [0;
     2;
     4;
     6;
     8;
     8.62;
     9.1;
     8.9;
     8.15;
     7;
     6;
     4.54;
     3.39;
     2.56];
w = [0.2;
     0.2;
     0.3;
     0.7;
     0.9;
     1;
     1;
     1;
     0.8;
     0.5;
     0.7;
     1;
     1;
     1];
lamda = [0;
     0;
     0;
     0;
     1.5;
     2.6;
     4;
     8;
     0;
     0;
     0;
     0];
[lamdaOut, c, ss, ifail] = nag_fit_1dspline_knots(x, y, w, lamda)
 

lamdaOut =

    0.2000
    0.2000
    0.2000
    0.2000
    1.5000
    2.6000
    4.0000
    8.0000
   12.0000
   12.0000
   12.0000
   12.0000


c =

   -0.0465
    3.6150
    8.5724
    9.4261
    7.2716
    4.1207
    3.0822
    2.5597
         0
         0
         0
         0


ss =

    0.0018


ifail =

                    0


function e02ba_example
x = [0.2;
     0.47;
     0.74;
     1.09;
     1.6;
     1.9;
     2.6;
     3.1;
     4;
     5.15;
     6.17;
     8;
     10;
     12];
y = [0;
     2;
     4;
     6;
     8;
     8.62;
     9.1;
     8.9;
     8.15;
     7;
     6;
     4.54;
     3.39;
     2.56];
w = [0.2;
     0.2;
     0.3;
     0.7;
     0.9;
     1;
     1;
     1;
     0.8;
     0.5;
     0.7;
     1;
     1;
     1];
lamda = [0;
     0;
     0;
     0;
     1.5;
     2.6;
     4;
     8;
     0;
     0;
     0;
     0];
[lamdaOut, c, ss, ifail] = e02ba(x, y, w, lamda)
 

lamdaOut =

    0.2000
    0.2000
    0.2000
    0.2000
    1.5000
    2.6000
    4.0000
    8.0000
   12.0000
   12.0000
   12.0000
   12.0000


c =

   -0.0465
    3.6150
    8.5724
    9.4261
    7.2716
    4.1207
    3.0822
    2.5597
         0
         0
         0
         0


ss =

    0.0018


ifail =

                    0



PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013