NAG C Library Function Document

nag_tsa_resid_corr (g13asc)


nag_tsa_resid_corr (g13asc) is a diagnostic checking function suitable for use after fitting a Box–Jenkins ARMA model to a univariate time series using nag_tsa_multi_inp_model_estim (g13bec). The residual autocorrelation function is returned along with an estimate of its asymptotic standard errors and correlations. Also, nag_tsa_resid_corr (g13asc) calculates the Box–Ljung portmanteau statistic and its significance level for testing model adequacy.


#include <nag.h>
#include <nagg13.h>
void  nag_tsa_resid_corr (Nag_ArimaOrder *arimav, Integer n, const double v[], Integer m, const double par[], Integer narma, double r[], double rc[], Integer tdrc, double *chi, Integer *df, double *siglev, NagError *fail)


Consider the univariate multiplicative autoregressive-moving average model
ϕ B Φ B s W t - μ = θ B Θ B s ε t (1)
where W t , for t=1,2,,n, denotes a time series and ε t , for t=1,2,,n, is a residual series assumed to be Normally distributed with zero mean and variance σ 2 >0 . The ε t 's are also assumed to be uncorrelated. Here μ  is the overall mean term, s  is the seasonal period and B  is the backward shift operator such that B r W t = W t-r . The polynomials in (1) are defined as follows:
ϕ B = 1 - ϕ 1 B - ϕ 2 B 2 - - ϕ p B p  
is the non-seasonal autoregressive (AR) operator;
θ B = 1 - θ 1 B - θ 2 B 2 - - θ q B q  
is the non-seasonal moving average (MA) operator;
Φ B s = 1 - Φ 1 B s - Φ 2 B 2s - - Φ P B Ps  
is the seasonal AR operator; and
Θ B s = 1 - Θ 1 B s - Θ 2 B 2s - - Θ Q B Qs  
is the seasonal MA operator. The model (1) is assumed to be stationary, that is the zeros of ϕ B  and Φ B s  are assumed to lie outside the unit circle. The model (1) is also assumed to be invertible, that is the zeros of θ B  and Θ B s  are assumed to lie outside the unit circle. When both Φ B s  and Θ B s  are absent from the model, that is when P = Q = 0 , then the model is said to be non-seasonal.
The estimated residual autocorrelation coefficient at lag l , r ^ l , is computed as:
r ^ l = t = l + 1 n ε ^ t-l - ε - ε ^ t - ε - t=1 n ε ^ t - ε - 2 ,   l = 1 , 2 ,  
where ε ^ t  denotes an estimate of the t th residual, ε t , and ε - = t=1 n ε ^ t / n . A portmanteau statistic, Q m , is calculated from the formula (see Box and Ljung (1978)):
Q m = n n+2 l=1 m r ^ l 2 / n-l  
where m  denotes the number of residual autocorrelations computed. (Advice on the choice of m  is given in Section 9.) Under the hypothesis of model adequacy, Q m  has an asymptotic χ 2  distribution on m - p - q - P - Q  degrees of freedom. Let r ^T = r ^ 1 , r ^ 2 , , r ^ m  then the variance-covariance matrix of r ^  is given by:
Var r ^ = I m - X XT X -1 XT / n .  
The construction of the matrix X  is discussed in McLeod (1978). (Note that the mean, μ , and the residual variance, σ 2 , play no part in calculating Var r ^  and therefore are not required as input to nag_tsa_resid_corr (g13asc).)


Box G E P and Ljung G M (1978) On a measure of lack of fit in time series models Biometrika 65 297–303
McLeod A I (1978) On the distribution of the residual autocorrelations in Box–Jenkins models J. Roy. Statist. Soc. Ser. B 40 296–302


1:     arimav Nag_ArimaOrder *
Pointer to structure of type Nag_ArimaOrder with the following members:
On entry: these seven members of arimav must specify the orders vector p,d,q,P,D,Q,s , respectively, of the ARIMA model for the output noise component.
p , q , P  and Q  refer, respectively, to the number of autoregressive ϕ , moving average θ , seasonal autoregressive Φ  and seasonal moving average Θ  arguments.
d , D  and s  refer, respectively, to the order of non-seasonal differencing, the order of seasonal differencing and the seasonal period.
  • arimavp, arimavq, arimavbigp, arimavbigq, arimavs0,
  • arimavp + arimavq + arimavbigp + arimavbigq > 0 ,
  • if arimavs=0 , then arimavbigp=0  and arimavbigq=0 .
2:     n IntegerInput
On entry: the number of observations in the residual series, n .
Constraint: n3 .
3:     v[n] const doubleInput
On entry: v[t-1] must contain an estimate of ε t , for t=1,2,,n.
Constraint: v must contain at least two distinct elements.
4:     m IntegerInput
On entry: the value of m , the number of residual autocorrelations to be computed. See Section 9 for advice on the value of m.
Constraint: narma < m < n .
5:     par[narma] const doubleInput
On entry: the parameter estimates in the order ϕ 1 , ϕ 2 , , ϕ p , θ 1 , θ 2 , , θ q , Φ 1 , Φ 2 , , Φ P , Θ 1 , Θ 2 , , Θ Q  only.
Constraint: the elements in par must satisfy the stationarity and invertibility conditions.
6:     narma IntegerInput
On entry: the number of ARMA arguments, ϕ , θ , Φ  and Θ  arguments, i.e., narma = p + q + P + Q .
Constraint: narma = arimavp + arimavq + arimavbigp + arimavbigq .
7:     r[m] doubleOutput
On exit: an estimate of the residual autocorrelation coefficient at lag l , for l=1,2,,m. If fail.code=NE_G13AS_ZERO_VAR  on exit then all elements of r are set to zero.
8:     rc[m×tdrc] doubleOutput
On exit: the estimated standard errors and correlations of the elements in the array r. The correlation between r[i-1]  and r[j-1]  is returned as rc[i-1×tdrc+j-1]  except that if i=j  then rc[i-1×tdrc+j-1]  contains the standard error of r[i-1] . If on exit, fail.code=NE_G13AS_FACT  or NE_G13AS_DIAG, then all off-diagonal elements of rc are set to zero and all diagonal elements are set to 1 / n .
9:     tdrc IntegerInput
On entry: the stride separating matrix column elements in the array rc.
Constraint: tdrcm .
10:   chi double *Output
On exit: the value of the portmanteau statistic, Q m . If fail.code=NE_G13AS_ZERO_VAR  on exit then chi is returned as zero.
11:   df Integer *Output
On exit: the number of degrees of freedom of chi.
12:   siglev double *Output
On exit: the significance level of chi based on df degrees of freedom. If fail.code=NE_G13AS_ZERO_VAR  on exit then siglev is returned as one.
13:   fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

On entry, tdrc=value  while m=value . These arguments must satisfy tdrcm .
Dynamic memory allocation failed.
On entry, arimavp=value , arimavd=value , arimavq=value , arimavbigp=value , arimavbigd=value , arimavbigq=value  and arimavs=value .
Constraints on the members of arimav are:
  • arimavp, arimavq, arimavbigp, arimavbigq, arimavs0 , arimavp + arimavq + arimavbigp + arimavbigq > 0 , if arimavs=0 , then arimavbigp=0  and arimavbigq=0 .
On entry, the autoregressive (or moving average) arguments are extremely close to or outside the stationarity (or invertibility) region. To proceed, you must supply different parameter estimates in the array par.
This is an unlikely exit. At least one of the diagonal elements of rc was found to be either negative or zero. In this case all off-diagonal elements of rc are returned as zero and all diagonal elements of rc set to 1 / n .
On entry, one or more of the AR operators has a factor in common with one or more of the MA operators. To proceed, this common factor must be deleted from the model. In this case, the off-diagonal elements of rc are returned as zero and the diagonal elements set to 1 / n . All other output quantities will be correct.
This is an unlikely exit brought about by an excessive number of iterations being needed to evaluate the zeros of the AR or MA polynomials. All output arguments are undefined.
On entry, the residuals are practically identical giving zero (or near zero) variance. In this case chi is set to zero, siglev to one and all the elements of r set to zero.
On entry, arimavp=value , arimavq=value , arimavbigp=value , arimavbigq=value  while narma=value .
Constraint: narma= arimavp+ arimavq+ arimavbigp+ arimavbigq.
On entry, m=value , n=value , narma=value .
Constraint: narma < m < n .
On entry, n=value.
Constraint: n3.
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.


The computations are believed to be stable.

Parallelism and Performance

nag_tsa_resid_corr (g13asc) is not threaded in any implementation.

Further Comments


The time taken by nag_tsa_resid_corr (g13asc) depends upon the number of residual autocorrelations to be computed, m .

Choice of m

The number of residual autocorrelations to be computed, m  should be chosen to ensure that when the ARMA model (1) is written as either an infinite order autoregressive process:
W t - μ = j=1 π j W t-j - μ + ε t  
or as an infinite order moving average process:
W t - μ = j=1 ψ j ε t-j + ε t  
then the two sequences π 1 , π 2 ,  and ψ 1 , ψ 2 ,  are such that π j  and ψ j  are approximately zero for j>m . An overestimate of m  is therefore preferable to an under-estimate of m . In many instances the choice m=10  will suffice. In practice, to be on the safe side, you should try setting m=20 .

Approximate Standard Errors

When fail.code = NE_G13AS_FACT ​ or ​ NE_G13AS_DIAG all the standard errors in rc are set to 1 / n . This is the asymptotic standard error of r ^ l  when all the autoregressive and moving average arguments are assumed to be known rather than estimated.


A program to fit an ARIMA(1,1,2) model to a series of 30 observations. 10 residual autocorrelations are computed.

Program Text

Program Text (g13asce.c)

Program Data

Program Data (g13asce.d)

Program Results

Program Results (g13asce.r)