e02dcc computes a bicubic spline approximation to a set of data values, given on a rectangular grid in the $x$-$y$ plane. 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.
The function may be called by the names: e02dcc, nag_fit_dim2_spline_grid or nag_2d_spline_fit_grid.
3Description
e02dcc determines a smooth bicubic spline approximation $s(x,y)$ to the set of data points $({x}_{\mathit{q}},{y}_{\mathit{r}},{f}_{\mathit{q},\mathit{r}})$, for $\mathit{q}=1,2,\dots ,{m}_{x}$ and $\mathit{r}=1,2,\dots ,{m}_{y}$.
The spline is given in the B-spline representation
where ${M}_{i}\left(x\right)$ and ${N}_{j}\left(y\right)$ denote normalized cubic B-splines, the former defined on the knots ${\lambda}_{i}$ to ${\lambda}_{i+4}$ and the latter on the knots ${\mu}_{j}$ to ${\mu}_{j+4}$. For further details, see Hayes and Halliday (1974) for bicubic splines and de Boor (1972) for normalized B-splines.
The total numbers ${n}_{x}$ and ${n}_{y}$ of these knots and their values ${\lambda}_{1},\dots ,{\lambda}_{{n}_{x}}$ and ${\mu}_{1},\dots ,{\mu}_{{n}_{y}}$ are chosen automatically by the function. The knots ${\lambda}_{5},\dots ,{\lambda}_{{n}_{x}-4}$ and ${\mu}_{5},\dots ,{\mu}_{{n}_{y}-4}$ are the interior knots; they divide the approximation domain $[{x}_{1},{x}_{{m}_{x}}]\times [{y}_{1},{y}_{{m}_{y}}]$ into $({n}_{x}-7)\times ({n}_{y}-7)$ subpanels $[{\lambda}_{\mathit{i}},{\lambda}_{\mathit{i}+1}]\times [{\mu}_{\mathit{i}},{\mu}_{\mathit{i}+1}]$, for $\mathit{i}=4,5,\dots ,{n}_{x}-4$ and $\mathit{j}=4,5,\dots ,{n}_{y}-4$. Then, much as in the curve case (see e02bec), the coefficients ${c}_{ij}$ are determined as the solution of the following constrained minimization problem:
where $\eta $ is a measure of the (lack of) smoothness of $s(x,y)$. Its value depends on the discontinuity jumps in $s(x,y)$ across the boundaries of the subpanels. It is zero only when there are no discontinuities and is positive otherwise, increasing with the size of the jumps (see Dierckx (1982) for details). ${\epsilon}_{q,r}$ denotes the residual ${f}_{q,r}-s({x}_{q},{y}_{r})$, and $S$ is a non-negative number to be specified.
By means of the argument $S$, ‘the smoothing factor’, you will then control the balance between smoothness and closeness of fit, as measured by the sum of squares of residuals in (3). 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 function will return an interpolating spline $(\theta =0)$ if $S$ is set to zero, and the least squares bicubic polynomial ($\eta =0$) if $S$ is set very large. Experimenting with $S$ values between these two extremes should result in a good compromise. (See Section 9.3 for advice on choice of $S$.)
The method employed is outlined in Section 9.5 and fully described in Dierckx (1981) and Dierckx (1982). It involves an adaptive strategy for locating the knots of the bicubic 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 and derivatives of the computed spline can subsequently be computed by calling e02dec,e02dfcande02dhc as described in Section 9.6.
4References
de Boor C (1972) On calculating with B-splines J. Approx. Theory6 50–62
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
Hayes J G and Halliday J (1974) The least squares fitting of cubic spline surfaces to general data sets J. Inst. Math. Appl.14 89–103
Reinsch C H (1967) Smoothing by spline functions Numer. Math.10 177–183
5Arguments
1: $\mathbf{start}$ – Nag_StartInput
On entry: start must be set to ${\mathbf{start}}=\mathrm{Nag\_Cold}$ or $\mathrm{Nag\_Warm}$.
The function will build up the knot set starting with no interior knots. No values need be assigned to $\mathbf{spline}\mathbf{\to}\mathbf{nx}$ and $\mathbf{spline}\mathbf{\to}\mathbf{ny}$ and memory will be internally allocated to $\mathbf{spline}\mathbf{\to}\mathbf{lamda}$, $\mathbf{spline}\mathbf{\to}\mathbf{mu}$, $\mathbf{spline}\mathbf{\to}\mathbf{c}$, $\mathbf{warmstartinf}\mathbf{\to}\mathbf{nag\_w}$ and $\mathbf{warmstartinf}\mathbf{\to}\mathbf{nag\_iw}$.
The function will restart the knot-placing strategy using the knots found in a previous call of the function. In this case, all arguments except s must be unchanged from that previous call. This warm start can save much time in searching for a satisfactory value of $S$.
Constraint:
${\mathbf{start}}=\mathrm{Nag\_Cold}$ or $\mathrm{Nag\_Warm}$.
2: $\mathbf{mx}$ – IntegerInput
On entry: ${m}_{x}$, the number of grid points along the $x$ axis.
On entry: ${\mathbf{x}}\left[\mathit{q}-1\right]$ must be set to ${x}_{\mathit{q}}$, the $x$ coordinate of the $\mathit{q}$th grid point along the $x$ axis, for $\mathit{q}=1,2,\dots ,{m}_{x}$.
On entry: ${\mathbf{y}}\left[\mathit{r}-1\right]$ must be set to ${y}_{\mathit{r}}$, the $y$ coordinate of the $\mathit{r}$th grid point along the $y$ axis, for $\mathit{r}=1,2,\dots ,{m}_{y}$.
On entry: ${\mathbf{f}}\left[{m}_{y}\times (\mathit{q}-1)+\mathit{r}-1\right]$ must contain the data value ${f}_{\mathit{q},\mathit{r}}$, for $\mathit{q}=1,2,\dots ,{m}_{x}$ and $\mathit{r}=1,2,\dots ,{m}_{y}$.
7: $\mathbf{s}$ – doubleInput
On entry: the smoothing factor, $S$.
If $S=0.0$, the function returns an interpolating spline.
If $S$ is smaller than machine precision, it is assumed equal to zero.
On entry: an upper bound for the number of knots ${n}_{x}$ and ${n}_{y}$ required in the $x$ and $y$ directions respectively.
In most practical situations, ${\mathbf{nxest}}={m}_{x}/2$ and ${\mathbf{nyest}}={m}_{y}/2$ is sufficient. nxest and nyest never need to be larger than ${m}_{x}+4$ and ${m}_{y}+4$ respectively, the numbers of knots needed for interpolation ($S=0.0$). See also Section 9.4.
Constraint:
${\mathbf{nxest}}\ge 8$ and ${\mathbf{nyest}}\ge 8$.
10: $\mathbf{fp}$ – double *Output
On exit: the sum of squared residuals, $\theta $, of the computed spline approximation.
If ${\mathbf{fp}}=0.0$, this is an interpolating spline. fp should equal $S$ within a relative tolerance of $0.001$ unless $\mathbf{spline}\mathbf{\to}\mathbf{nx}=\mathbf{spline}\mathbf{\to}\mathbf{ny}=8$, when the spline has no interior knots and so is simply a bicubic polynomial. For knots to be inserted, $S$ must be set to a value below the value of fp produced in this case.
11: $\mathbf{warmstartinf}$ – Nag_Comm *
Pointer to structure of type Nag_Comm with the following members:
nag_w – double *Input
On entry: if the warm start option is used, the values $\mathbf{nag\_w}\left[0\right],\dots ,\mathbf{nag\_w}\left[3\right]$ must be left unchanged from the previous call.
nag_iw – Integer *Input
On entry: if the warm start option is used, the values $\mathbf{nag\_iw}\left[0\right],\dots ,\mathbf{nag\_iw}\left[2\right]$ must be left unchanged from the previous call.
Note that when the information contained in the pointers $\mathbf{nag\_w}$ and $\mathbf{nag\_iw}$ is no longer of use, or before a new call to e02dcc with the same warmstartinf, you should free this storage using the NAG macros NAG_FREE. This storage will have been allocated only if this function returns with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=\mathrm{NE\_NOERROR}$, NE_SPLINE_COEFF_CONV, or NE_NUM_KNOTS_2D_GT_RECT.
12: $\mathbf{spline}$ – Nag_2dSpline *
Pointer to structure of type Nag_2dSpline with the following members:
nx – IntegerInput/Output
On entry: if the warm start option is used, the value of $\mathbf{nx}$ must be left unchanged from the previous call.
On exit: the total number of knots, ${n}_{x}$, of the computed spline with respect to the $x$ variable.
lamda – double *Input/Output
On entry: a pointer to which if ${\mathbf{start}}=\mathrm{Nag\_Cold}$, memory of size nxest is internally allocated. If the warm start option is used, the values $\mathbf{lamda}\left[0\right],\mathbf{lamda}\left[1\right],\dots ,\mathbf{lamda}\left[\mathbf{nx}-1\right]$ must be left unchanged from the previous call.
On exit: $\mathbf{lamda}$ contains the complete set of knots ${\lambda}_{i}$ associated with the $x$ variable, i.e., the interior knots $\mathbf{lamda}\left[4\right],\mathbf{lamda}\left[5\right],\dots ,\mathbf{lamda}\left[\mathbf{nx}-5\right]$ as well as the additional knots $\mathbf{lamda}\left[0\right]=\mathbf{lamda}\left[1\right]=\mathbf{lamda}\left[2\right]=\mathbf{lamda}\left[3\right]={\mathbf{x}}\left[0\right]$ and $\mathbf{lamda}\left[\mathbf{nx}-4\right]=\mathbf{lamda}\left[\mathbf{nx}-3\right]=\mathbf{lamda}\left[\mathbf{nx}-2\right]=\mathbf{lamda}\left[\mathbf{nx}-1\right]={\mathbf{x}}\left[{\mathbf{mx}}-1\right]$ needed for the B-spline representation.
ny – IntegerInput/Output
On entry: if the warm start option is used, the value of $\mathbf{ny}$ must be left unchanged from the previous call.
On exit: the total number of knots, ${n}_{y}$, of the computed spline with respect to the $y$ variable.
mu – double *Input/Output
On entry: a pointer to which if ${\mathbf{start}}=\mathrm{Nag\_Cold}$, memory of size nyest is internally allocated. If the warm start option is used, the values $\mathbf{mu}\left[0\right],\mathbf{mu}\left[1\right],\dots ,\mathbf{mu}\left[\mathbf{ny}-1\right]$ must be left unchanged from the previous call.
On exit: $\mathbf{mu}$ contains the complete set of knots ${\mu}_{i}$ associated with the $y$ variable, i.e., the interior knots $\mathbf{mu}\left[4\right]$, $\mathbf{mu}\left[5\right]$, $\dots $, $\mathbf{mu}\left[\mathbf{ny}-5\right]$ as well as the additional knots $\mathbf{mu}\left[0\right]=\mathbf{mu}\left[1\right]=\mathbf{mu}\left[2\right]=\mathbf{mu}\left[3\right]={\mathbf{y}}\left[0\right]$ and $\mathbf{mu}\left[\mathbf{ny}-4\right]=\mathbf{mu}\left[\mathbf{ny}-3\right]=\mathbf{mu}\left[\mathbf{ny}-2\right]=\mathbf{mu}\left[\mathbf{ny}-1\right]={\mathbf{y}}\left[{\mathbf{my}}-1\right]$ needed for the B-spline representation.
c – double *Output
On exit: a pointer to which if ${\mathbf{start}}=\mathrm{Nag\_Cold}$, memory of size $({\mathbf{nxest}}-4)\times ({\mathbf{nyest}}-4)$ is internally allocated. $\mathbf{c}\left[({n}_{y}-4)\times (i-1)+j-1\right]$ is the coefficient ${c}_{ij}$ defined in Section 3.
Note that when the information contained in the pointers $\mathbf{lamda}$, $\mathbf{mu}$ and $\mathbf{c}$ is no longer of use, or before a new call to e02dcc with the same spline, you should free this storage using the NAG macro NAG_FREE. This storage will have been allocated only if this function returns with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=\mathrm{NE\_NOERROR}$, NE_SPLINE_COEFF_CONV, or NE_NUM_KNOTS_2D_GT_RECT.
13: $\mathbf{fail}$ – NagError *Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).
${\mathbf{start}}=\mathrm{Nag\_Warm}$ at the first call of this function. start must be set to${\mathbf{start}}=\mathrm{Nag\_Cold}$ at the first call.
NE_INT_ARG_LT
On entry, ${\mathbf{mx}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{mx}}\ge 4$.
On entry, ${\mathbf{my}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{my}}\ge 4$.
On entry, ${\mathbf{nxest}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{nxest}}\ge 8$.
On entry, ${\mathbf{nyest}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{nyest}}\ge 8$.
NE_NOT_STRICTLY_INCREASING
The sequence x is not strictly increasing: ${\mathbf{x}}\left[\u27e8\mathit{\text{value}}\u27e9\right]=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{x}}\left[\u27e8\mathit{\text{value}}\u27e9\right]=\u27e8\mathit{\text{value}}\u27e9$.
The sequence y is not strictly increasing: ${\mathbf{y}}\left[\u27e8\mathit{\text{value}}\u27e9\right]=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{y}}\left[\u27e8\mathit{\text{value}}\u27e9\right]=\u27e8\mathit{\text{value}}\u27e9$.
NE_NUM_KNOTS_2D_GT_RECT
The number of knots required is greater than allowed by nxest or nyest, ${\mathbf{nxest}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{nyest}}=\u27e8\mathit{\text{value}}\u27e9$. Possibly s is too small, especially if nxest, ${\mathbf{nyest}}>{\mathbf{mx}}/2$, ${\mathbf{my}}/2$. ${\mathbf{s}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{mx}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{my}}=\u27e8\mathit{\text{value}}\u27e9$. A spline approximation is returned, but it fails to satisfy the fitting criterion (see (2) and (3)) – perhaps by only a small amount, however.
NE_REAL_ARG_LT
On entry, s must not be less than 0.0: ${\mathbf{s}}=\u27e8\mathit{\text{value}}\u27e9$.
NE_SF_D_K_CONS
On entry, ${\mathbf{s}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{nxest}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{mx}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{nxest}}\ge {\mathbf{mx}}+4$ when ${\mathbf{s}}=0.0$.
On entry, ${\mathbf{s}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{nyest}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{my}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{nyest}}\ge {\mathbf{mx}}+4$ when ${\mathbf{s}}=0.0$.
NE_SPLINE_COEFF_CONV
The iterative process has failed to converge. Possibly s is too small: ${\mathbf{s}}=\u27e8\mathit{\text{value}}\u27e9$. A spline approximation is returned, but it fails to satisfy the fitting criterion (see (2) and (3)) – perhaps by only a small amount, however.
7Accuracy
On successful exit, the approximation returned is such that its sum of squared residuals fp is equal to the smoothing factor $S$, up to a specified relative tolerance of $0.001$ – except that if ${n}_{x}=8$ and ${n}_{y}=8$, fp may be significantly less than $S$: in this case the computed spline is simply the least squares bicubic polynomial approximation of degree $3$, i.e., a spline with no interior knots.
8Parallelism and Performance
Background information to multithreading can be found in the Multithreading documentation.
e02dcc is not threaded in any implementation.
9Further Comments
9.1Timing
The time taken for a call of e02dcc depends on the complexity of the shape of the data, the value of the smoothing factor $S$, and the number of data points. If e02dcc is to be called for different values of $S$, much time can be saved by setting ${\mathbf{start}}=\mathrm{Nag\_Warm}$ after the first call.
9.2Weighting of Data Points
e02dcc does not allow individual weighting of the data values. If these were determined to widely differing accuracies, it may be better to use e02ddc. The computation time would be very much longer, however.
If the standard deviation of ${f}_{q,r}$ is the same for all $q$ and $r$ (the case for which this function is designed – see Section 9.2) and known to be equal, at least approximately, to $\sigma $, say, then following Reinsch (1967) and choosing the smoothing factor $S$ in the range ${\sigma}^{2}(m\pm \sqrt{2m})$, where $m={m}_{x}{m}_{y}$, is likely to give a good start in the search for a satisfactory value. If the standard deviations vary, the sum of their squares over all the data points could be used. 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 bicubic polynomial; the value returned for fp, call it ${{\mathbf{fp}}}_{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={{\mathbf{fp}}}_{0}/10$, $S={{\mathbf{fp}}}_{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 e02dcc is called with ${\mathbf{start}}=\mathrm{Nag\_Warm}$, 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 ${\mathbf{start}}=\mathrm{Nag\_Warm}$, a fit can finally be accepted as satisfactory, it may be worthwhile to call e02dcc once more with the selected value for $S$ but now using ${\mathbf{start}}=\mathrm{Nag\_Cold}$. Often, e02dcc then returns an approximation with the same quality of fit but with fewer knots, which is, therefore, better if data reduction is also important.
The number of knots may also depend on the upper bounds nxest and nyest. Indeed, if at a certain stage in e02dcc the number of knots in one direction (say ${n}_{x}$) has reached the value of its upper bound (nxest), then from that moment on all subsequent knots are added in the other $\left(y\right)$ direction. Therefore, you have the option of limiting the number of knots the function locates in any direction. For example, by setting ${\mathbf{nxest}}=8$ (the lowest allowable value for nxest), you can indicate that you want an approximation which is a simple cubic polynomial in the variable $x$.
9.5Outline of Method Used
If $S=0$, the requisite number of knots is known in advance, i.e., ${n}_{x}={m}_{x}+4$ and ${n}_{y}={m}_{y}+4$; the interior knots are located immediately as ${\lambda}_{\mathit{i}}={x}_{\mathit{i}-2}$ and ${\mu}_{\mathit{j}}={y}_{\mathit{j}-2}$, for $\mathit{i}=5,6,\dots ,{n}_{x}-4$ and $\mathit{j}=5,6,\dots ,{n}_{y}-4$. The corresponding least squares spline is then an interpolating spline and, therefore, a solution of the problem.
If $S>0$, suitable knot sets are 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 bicubic spline is fitted to the data by least squares, and $\theta $, the sum of squares of residuals, is computed. If $\theta >S$, new knots are added to one knot set or the other so as to reduce $\theta $ 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 $\theta $. Sooner or later, we find that $\theta \le S$ and at that point the knot sets are accepted. The function then goes on to compute the (unique) spline which has these knot sets and which satisfies the full fitting criterion specified by (2) and (3). The theoretical solution has $\theta =S$. The function computes the spline by an iterative scheme which is ended when $\theta =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 e02bac for least squares curve fitting.
An exception occurs when the function finds at the start that, even with no interior knots $({n}_{x}={n}_{y}=8)$, the least squares spline already has its sum of residuals $\le S$. In this case, since this spline (which is simply a bicubic polynomial) also has an optimal value for the smoothness measure $\eta $, 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 (1982).
9.6Evaluation of Computed Spline
The values of the computed spline at the points $(\mathbf{tx}(\mathit{r}-1),\mathbf{ty}(\mathit{r}-1))$, for $\mathit{r}=1,2,\dots ,\mathbf{n}$, may be obtained in the array ff, of length at least $\mathbf{n}$, by the following code:
e02dec(n, tx, ty, ff, &spline, &fail)
where spline is a structure of type Nag_2dSpline which is an output argument of e02dcc.
To evaluate the computed spline on a kx by ky rectangular grid of points in the $x$-$y$ plane, which is defined by the $x$ coordinates stored in $\mathbf{tx}(\mathit{q}-1)$, for $\mathit{q}=1,2,\dots ,\mathbf{kx}$, and the $y$ coordinates stored in $\mathbf{ty}(\mathit{r}-1)$, for $\mathit{r}=1,2,\dots ,\mathbf{ky}$, returning the results in the array fg which is of length at least $\mathbf{kx}\times \mathbf{ky}$, the following call may be used:
e02dfc(kx, ky, tx, ty, fg, &spline, &fail)
where spline is a structure of type Nag_2dSpline which is an output argument of e02dcc. The result of the spline evaluated at grid point $(q,r)$ is returned in element $[\mathbf{ky}\times (q-1)+r-1]$ of the array fg.
10Example
This example program reads in values of mx, my, ${x}_{\mathit{q}}$, for $\mathit{q}=1,2,\dots ,{\mathbf{mx}}$, and ${y}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,{\mathbf{my}}$, followed by values of the ordinates ${f}_{q,r}$ defined at the grid points $({x}_{q},{y}_{r})$. It then calls e02dcc to compute a bicubic spline approximation for one specified value of s, and prints the values of the computed knots and B-spline coefficients. Finally it evaluates the spline at a small sample of points on a rectangular grid.