NAG C Library Function Document

nag_tsa_auto_corr (g13abc)


nag_tsa_auto_corr (g13abc) computes the sample autocorrelation function of a time series. It also computes the sample mean, the sample variance and a statistic which may be used to test the hypothesis that the true autocorrelation function is zero.


#include <nag.h>
#include <nagg13.h>
void  nag_tsa_auto_corr (const double x[], Integer nx, Integer nk, double *mean, double *var, double r[], double *stat, NagError *fail)


The data consist of n  observations x i , for i=1,2,,n, from a time series.
The quantities calculated are:
(a) The sample mean
x - = i=1 n x i n  
(b) The sample variance (for n2 )
s 2 = i=1 n x i - x - 2 n-1  
(c) The sample autocorrelation coefficients of lags k = 1 , 2 , , K , where K  is a user-specified maximum lag, and K<n , n>1 .
(d) The coefficient of lag k  is defined as
r k = i=1 n-k x i - x - x i+k - x - i=1 n x i - x - 2  
(e) See page 496 et seq. of Box and Jenkins (1976) for further details.
(f) A test statistic defined as
stat = n k=1 K r k 2 ,  
which can be used to test the hypothesis that the true autocorrelation function is identically zero.
If n  is large and K  is much smaller than n , stat has a χ K 2  distribution under the hypothesis of a zero autocorrelation function. Values of stat in the upper tail of the distribution provide evidence against the hypothesis.
Section 8.2.2 of Box and Jenkins (1976) provides further details of the use of stat.


Box G E P and Jenkins G M (1976) Time Series Analysis: Forecasting and Control (Revised Edition) Holden–Day


1:     x[nx] const doubleInput
On entry: the time series, x i , for i=1,2,,n.
2:     nx IntegerInput
On entry: the number of values, n , in the time series.
Constraint: nx>1 .
3:     nk IntegerInput
On entry: the number of lags, K , for which the autocorrelations are required. The lags range from 1 to K  and do not include zero.
Constraint: 0 < nk < nx .
4:     mean double *Output
On exit: the sample mean of the input time series.
5:     var double *Output
On exit: the sample variance of the input time series.
6:     r[nk] doubleOutput
On exit: the sample autocorrelation coefficient relating to lag k , for k=1,2,,K.
7:     stat double *Output
On exit: the statistic used to test the hypothesis that the true autocorrelation function of the time series is identically zero.
8:     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, nx=value  while nk=value . These arguments must satisfy nx>nk .
On entry, nk=value.
Constraint: nk>0.
On entry, nx=value.
Constraint: nx>1.
On entry, all values of x are practically identical, giving zero variance. In this case r and stat are undefined on exit.


The computations are believed to be stable.

Parallelism and Performance

nag_tsa_auto_corr (g13abc) is not threaded in any implementation.

Further Comments

The time taken by nag_tsa_auto_corr (g13abc) is approximately proportional to nx×nk .


In the example below, a set of 50 values of sunspot counts is used as input. The first 10 autocorrelations are computed.

Program Text

Program Text (g13abce.c)

Program Data

Program Data (g13abce.d)

Program Results

Program Results (g13abce.r)