g13ab 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.


public static void g13ab(
	double[] x,
	int nx,
	int nk,
	out double xm,
	out double xv,
	double[] r,
	out double stat,
	out int ifail
Visual Basic
Public Shared Sub g13ab ( _
	x As Double(), _
	nx As Integer, _
	nk As Integer, _
	<OutAttribute> ByRef xm As Double, _
	<OutAttribute> ByRef xv As Double, _
	r As Double(), _
	<OutAttribute> ByRef stat As Double, _
	<OutAttribute> ByRef ifail As Integer _
Visual C++
static void g13ab(
	array<double>^ x, 
	int nx, 
	int nk, 
	[OutAttribute] double% xm, 
	[OutAttribute] double% xv, 
	array<double>^ r, 
	[OutAttribute] double% stat, 
	[OutAttribute] int% ifail
static member g13ab : 
        x : float[] * 
        nx : int * 
        nk : int * 
        xm : float byref * 
        xv : float byref * 
        r : float[] * 
        stat : float byref * 
        ifail : int byref -> unit 


Type: array<System..::..Double>[]()[][]
An array of size [nx]
On entry: the time series, xi, for i=1,2,,n.
Type: System..::..Int32
On entry: n, the number of values in the time series.
Constraint: nx>1.
Type: System..::..Int32
On entry: K, the number of lags for which the autocorrelations are required. The lags range from 1 to K and do not include zero.
Constraint: 0<nk<nx.
Type: System..::..Double%
On exit: the sample mean of the input time series.
Type: System..::..Double%
On exit: the sample variance of the input time series.
Type: array<System..::..Double>[]()[][]
An array of size [nk]
On exit: the sample autocorrelation coefficient relating to lag k, for k=1,2,,K.
Type: System..::..Double%
On exit: the statistic used to test the hypothesis that the true autocorrelation function of the time series is identically zero.
Type: System..::..Int32%
On exit: ifail=0 unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).


The data consists of n observations xi, for i=1,2,,n from a time series.
The quantities calculated are
(a) The sample mean
(b) The sample variance (for n2)
(c) The sample autocorrelation coefficients of lags k=1,2,,K, where K is a user-specified maximum lag, and K<n, n>1.
The coefficient of lag k is defined as
See page 496 of Box and Jenkins (1976) for further details.
(d) A test statistic defined as
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 χK2 distribution under the hypothesis of a zero autocorrelation function. Values of stat in the upper tail of the distribution provide evidence against the hypothesis; g01ec can be used to compute the tail probability.
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

Error Indicators and Warnings

Errors or warnings detected by the method:
On entry,nxnk,
On entry, all values of x are practically identical, giving zero variance. In this case r and stat are undefined on exit.
An error occured, see message report.
Negative dimension for array value
Invalid Parameters value


The computations are believed to be stable.

Parallelism and Performance


Further Comments

If n<100, or K<10logn then the autocorrelations are calculated directly and the time taken by g13ab is approximately proportional to nK, otherwise the autocorrelations are calculated by utilizing fast fourier transforms (FFTs) and the time taken is approximately proportional to nlogn. If FFTs are used then g13ab internally allocates approximately 4n real elements.
If the input series for g13ab was generated by differencing using g13aa, ensure that only the differenced values are input to g13ab, and not the reconstituting information.


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

Example program (C#): g13abe.cs

Example program data: g13abe.d

Example program results: g13abe.r

See Also