NAG Library Function Document
nag_durbin_watson_stat (g02fcc)
1 Purpose
nag_durbin_watson_stat (g02fcc) calculates the Durbin–Watson statistic, for a set of residuals, and the upper and lower bounds for its significance.
2 Specification
#include <nag.h> 
#include <nagg02.h> 
void 
nag_durbin_watson_stat (Integer n,
Integer p,
const double res[],
double *d,
double *pdl,
double *pdu,
NagError *fail) 

3 Description
For the general linear regression model
where 
$y$ is a vector of length $n$ of the dependent variable,
$X$ is a $n$ by $p$ matrix of the independent variables,
$\beta $ is a vector of length $p$ of unknown arguments, 
and 
$\epsilon $ is a vector of length $n$ of unknown random errors. 
The residuals are given by
and the fitted values,
$\hat{y}=X\hat{\beta}$, can be written as
$Hy$ for a
$n$ by
$n$ matrix
$H$. Note that when a mean term is included in the model the sum of the residuals is zero. If the observations have been taken serially, that is
${y}_{1},{y}_{2},\dots ,{y}_{n}$ can be considered as a time series, the Durbin–Watson test can be used to test for serial correlation in the
${\epsilon}_{i}$, see
Durbin and Watson (1950),
Durbin and Watson (1951) and
Durbin and Watson (1971).
The Durbin–Watson statistic is
Positive serial correlation in the
${\epsilon}_{i}$ will lead to a small value of
$d$ while for independent errors
$d$ will be close to
$2$. Durbin and Watson show that the exact distribution of
$d$ depends on the eigenvalues of the matrix
$HA$ where the matrix
$A$ is such that
$d$ can be written as
and the eigenvalues of the matrix
$A$ are
${\lambda}_{j}=\left(1\mathrm{cos}\left(\pi j/n\right)\right)$, for
$j=1,2,\dots ,n1$.
However bounds on the distribution can be obtained, the lower bound being
and the upper bound being
where the
${u}_{i}$ are independent standard Normal variables. The lower tail probabilities associated with these bounds,
${p}_{\mathrm{l}}$ and
${p}_{\mathrm{u}}$, are computed by
nag_prob_durbin_watson (g01epc). The interpretation of the bounds is that, for a test of size (significance)
$\alpha $, if
${p}_{l}\le \alpha $ the test is significant, if
${p}_{u}>\alpha $ the test is not significant, while if
${p}_{\mathrm{l}}>\alpha $ and
${p}_{\mathrm{u}}\le \alpha $ no conclusion can be reached.
The above probabilities are for the usual test of positive autocorrelation. If the alternative of negative autocorrelation is required, then a call to
nag_prob_durbin_watson (g01epc) should be made with the argument
d taking the value of
$4d$; see
Newbold (1988).
4 References
Durbin J and Watson G S (1950) Testing for serial correlation in leastsquares regression. I Biometrika 37 409–428
Durbin J and Watson G S (1951) Testing for serial correlation in leastsquares regression. II Biometrika 38 159–178
Durbin J and Watson G S (1971) Testing for serial correlation in leastsquares regression. III Biometrika 58 1–19
Granger C W J and Newbold P (1986) Forecasting Economic Time Series (2nd Edition) Academic Press
Newbold P (1988) Statistics for Business and Economics Prentice–Hall
5 Arguments
 1:
n – IntegerInput
On entry:
$n$, the number of residuals.
Constraint:
${\mathbf{n}}>{\mathbf{p}}$.
 2:
p – IntegerInput
On entry: $p$, the number of independent variables in the regression model, including the mean.
Constraint:
${\mathbf{p}}\ge 1$.
 3:
res[n] – const doubleInput
On entry: the residuals, ${r}_{1},{r}_{2},\dots ,{r}_{n}$.
Constraint:
the mean of the residuals $\text{}\le \sqrt{\epsilon}$, where $\epsilon =\mathit{machineprecision}$.
 4:
d – double *Output
On exit: the Durbin–Watson statistic, $d$.
 5:
pdl – double *Output
On exit: lower bound for the significance of the Durbin–Watson statistic, ${p}_{\mathrm{l}}$.
 6:
pdu – double *Output
On exit: upper bound for the significance of the Durbin–Watson statistic, ${p}_{\mathrm{u}}$.
 7:
fail – NagError *Input/Output

The NAG error argument (see
Section 3.6 in the Essential Introduction).
6 Error Indicators and Warnings
 NE_ALLOC_FAIL
Dynamic memory allocation failed.
 NE_BAD_PARAM
On entry, argument $\u2329\mathit{\text{value}}\u232a$ had an illegal value.
 NE_INT
On entry, ${\mathbf{p}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{p}}\ge 1$.
 NE_INT_2
On entry, ${\mathbf{n}}=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{p}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{n}}>{\mathbf{p}}$.
 NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact
NAG for assistance.
 NE_RESID_IDEN
On entry, all residuals are identical.
 NE_RESID_MEAN
On entry, the mean of
res is not approximately
$0.0$,
$\text{mean}=\u2329\mathit{\text{value}}\u232a$.
7 Accuracy
The probabilities are computed to an accuracy of at least $4$ decimal places.
If the exact probabilities are required, then the first
$np$ eigenvalues of
$HA$ can be computed and
nag_prob_lin_chi_sq (g01jdc) used to compute the required probabilities with the argument
c set to
$0.0$ and the argument
d set to the Durbin–Watson statistic
$d$.
9 Example
A set of $10$ residuals are read in and the Durbin–Watson statistic along with the probability bounds are computed and printed.
9.1 Program Text
Program Text (g02fcce.c)
9.2 Program Data
Program Data (g02fcce.d)
9.3 Program Results
Program Results (g02fcce.r)