Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

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\left(x\right)$ to the set of data points (xr,yr)$\left({x}_{\mathit{r}},{y}_{\mathit{r}}\right)$ with weights wr${w}_{\mathit{r}}$, for r = 1,2,,m$\mathit{r}=1,2,\dots ,m$. The value of ncap7 = n + 7${\mathbf{ncap7}}=\stackrel{-}{n}+7$, where n$\stackrel{-}{n}$ 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${\lambda }_{5},{\lambda }_{6},\dots ,{\lambda }_{\stackrel{-}{n}+3}$, interior to the data interval, are prescribed by you.
s(x)$s\left(x\right)$ has the property that it minimizes θ$\theta$, the sum of squares of the weighted residuals εr${\epsilon }_{\mathit{r}}$, for r = 1,2,,m$\mathit{r}=1,2,\dots ,m$, where
 εr = wr(yr − s(xr)). $εr=wr(yr-s(xr)).$
The function produces this minimizing value of θ$\theta$ and the coefficients c1,c2,,cq${c}_{1},{c}_{2},\dots ,{c}_{q}$, where q = n + 3$q=\stackrel{-}{n}+3$, in the B-spline representation
 q s(x) = ∑ ciNi(x). i = 1
$s(x)=∑i=1qciNi(x).$
Here Ni(x)${N}_{i}\left(x\right)$ denotes the normalized B-spline of degree 3$3$ defined upon the knots λi,λi + 1,,λi + 4${\lambda }_{i},{\lambda }_{i+1},\dots ,{\lambda }_{i+4}$.
In order to define the full set of B-splines required, eight additional knots λ1,λ2,λ3,λ4${\lambda }_{1},{\lambda }_{2},{\lambda }_{3},{\lambda }_{4}$ and λn + 4,λn + 5,λn + 6,λn + 7${\lambda }_{\stackrel{-}{n}+4},{\lambda }_{\stackrel{-}{n}+5},{\lambda }_{\stackrel{-}{n}+6},{\lambda }_{\stackrel{-}{n}+7}$ are inserted automatically by the function. The first four of these are set equal to the smallest xr${x}_{r}$ and the last four to the largest xr${x}_{r}$.
The representation of s(x)$s\left(x\right)$ in terms of B-splines is the most compact form possible in that only n + 3$\stackrel{-}{n}+3$ coefficients, in addition to the n + 7$\stackrel{-}{n}+7$ knots, fully define s(x)$s\left(x\right)$.
The method employed involves forming and then computing the least squares solution of a set of m$m$ linear equations in the coefficients ci${c}_{\mathit{i}}$, for i = 1,2,,n + 3$\mathit{i}=1,2,\dots ,\stackrel{-}{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)${N}_{i}\left(x\right)$ for any value of x$x$ 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\left(x\right)$ from its B-spline representation may be carried out using nag_fit_1dspline_eval (e02bb). If derivatives of s(x)$s\left(x\right)$ 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\left(x\right)$.

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 mmdist4${\mathbf{m}}\ge \mathit{mdist}\ge 4$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data.
The values xr${x}_{\mathit{r}}$ of the independent variable (abscissa), for r = 1,2,,m$\mathit{r}=1,2,\dots ,m$.
Constraint: x1x2xm${x}_{1}\le {x}_{2}\le \cdots \le {x}_{m}$.
2:     y(m) – double array
m, the dimension of the array, must satisfy the constraint mmdist4${\mathbf{m}}\ge \mathit{mdist}\ge 4$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data.
The values yr${y}_{\mathit{r}}$ of the dependent variable (ordinate), for r = 1,2,,m$\mathit{r}=1,2,\dots ,m$.
3:     w(m) – double array
m, the dimension of the array, must satisfy the constraint mmdist4${\mathbf{m}}\ge \mathit{mdist}\ge 4$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data.
The values wr${w}_{\mathit{r}}$ of the weights, for r = 1,2,,m$\mathit{r}=1,2,\dots ,m$. For advice on the choice of weights, see the E02 Chapter Introduction.
Constraint: w(r) > 0.0${\mathbf{w}}\left(\mathit{r}\right)>0.0$, for r = 1,2,,m$\mathit{r}=1,2,\dots ,m$.
4:     lamda(ncap7) – double array
ncap7, the dimension of the array, must satisfy the constraint 8ncap7mdist + 4$8\le {\mathbf{ncap7}}\le \mathit{mdist}+4$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data.
lamda(i)${\mathbf{lamda}}\left(\mathit{i}\right)$ must be set to the (i4)$\left(\mathit{i}-4\right)$th (interior) knot, λi${\lambda }_{\mathit{i}}$, for i = 5,6,,n + 3$\mathit{i}=5,6,\dots ,\stackrel{-}{n}+3$.
Constraint: x(1) < lamda(5)lamda(6)lamda(ncap74) < x(m)${\mathbf{x}}\left(1\right)<{\mathbf{lamda}}\left(5\right)\le {\mathbf{lamda}}\left(6\right)\le \cdots \le {\mathbf{lamda}}\left({\mathbf{ncap7}}-4\right)<{\mathbf{x}}\left({\mathbf{m}}\right)$.

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 m$m$ of data points.
Constraint: mmdist4${\mathbf{m}}\ge \mathit{mdist}\ge 4$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data.
2:     ncap7 – int64int32nag_int scalar
Default: The dimension of the array lamda.
n + 7$\stackrel{-}{n}+7$, where n$\stackrel{-}{n}$ 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 x1${x}_{1}$ to xm${x}_{m}$) over which the spline is defined.
Constraint: 8ncap7mdist + 4$8\le {\mathbf{ncap7}}\le \mathit{mdist}+4$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data.

work1 work2

Output Parameters

1:     lamda(ncap7) – double array
The input values are unchanged, and lamda(i)${\mathbf{lamda}}\left(\mathit{i}\right)$, for i = 1,2,3,4$\mathit{i}=1,2,3,4$, ncap73${\mathbf{ncap7}}-3$, ncap72${\mathbf{ncap7}}-2$, ncap71${\mathbf{ncap7}}-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 ci${c}_{\mathit{i}}$ of the B-spline Ni(x)${N}_{\mathit{i}}\left(x\right)$, for i = 1,2,,n + 3$\mathit{i}=1,2,\dots ,\stackrel{-}{n}+3$. The remaining elements of the array are not used.
3:     ss – double scalar
The residual sum of squares, θ$\theta$.
4:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
The knots fail to satisfy the condition
x(1) < lamda(5)lamda(6)lamda(ncap74) < x(m)${\mathbf{x}}\left(1\right)<{\mathbf{lamda}}\left(5\right)\le {\mathbf{lamda}}\left(6\right)\le \cdots \le {\mathbf{lamda}}\left({\mathbf{ncap7}}-4\right)<{\mathbf{x}}\left({\mathbf{m}}\right)$.
Thus the knots are not in correct order or are not interior to the data interval.
ifail = 2${\mathbf{ifail}}=2$
The weights are not all strictly positive.
ifail = 3${\mathbf{ifail}}=3$
The values of x(r)${\mathbf{x}}\left(\mathit{r}\right)$, for r = 1,2,,m$\mathit{r}=1,2,\dots ,{\mathbf{m}}$, are not in nondecreasing order.
ifail = 4${\mathbf{ifail}}=4$
ncap7 < 8${\mathbf{ncap7}}<8$ (so the number of interior knots is negative) or ncap7 > mdist + 4${\mathbf{ncap7}}>\mathit{mdist}+4$, where mdist$\mathit{mdist}$ is the number of distinct x$x$ values in the data (so there cannot be a unique solution).
ifail = 5${\mathbf{ifail}}=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 ncap74${\mathbf{ncap7}}-4$ strictly increasing values, x(R(1)),x(R(2)),,x(R(ncap74))${\mathbf{x}}\left(R\left(1\right)\right),{\mathbf{x}}\left(R\left(2\right)\right),\dots ,{\mathbf{x}}\left(R\left({\mathbf{ncap7}}-4\right)\right)$, among the abscissae such that
• x(R(1)) < lamda(1) < x(R(5))${\mathbf{x}}\left(R\left(1\right)\right)<{\mathbf{lamda}}\left(1\right)<{\mathbf{x}}\left(R\left(5\right)\right)$,
• x(R(2)) < lamda(2) < x(R(6))${\mathbf{x}}\left(R\left(2\right)\right)<{\mathbf{lamda}}\left(2\right)<{\mathbf{x}}\left(R\left(6\right)\right)$,
• $⋮$
• x(R(ncap78)) < lamda(ncap78) < x(R(ncap74))${\mathbf{x}}\left(R\left({\mathbf{ncap7}}-8\right)\right)<{\mathbf{lamda}}\left({\mathbf{ncap7}}-8\right)<{\mathbf{x}}\left(R\left({\mathbf{ncap7}}-4\right)\right)$.
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 + δyr${y}_{r}+\delta {y}_{r}$. The ratio of the root-mean-square value for the δyr$\delta {y}_{r}$ to the root-mean-square value of the yr${y}_{r}$ can be expected to be less than a small multiple of κ × m × machine precision, where κ$\kappa$ is a condition number for the problem. Values of κ$\kappa$ for 20$20$30$30$ practical datasets all proved to lie between 4.5$4.5$ and 7.8$7.8$ (see Cox (1975)). (Note that for these datasets, replacing the coincident end knots at the end points x1${x}_{1}$ and xm${x}_{m}$ used in the function by various choices of non-coincident exterior knots gave values of κ$\kappa$ between 16$16$ and 180$180$. Again see Cox (1975) for further details.) In general we would not expect κ$\kappa$ 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.

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

```