nag_tsa_auto_corr (g13abc) (PDF version)
g13 Chapter Contents
g13 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_tsa_auto_corr (g13abc)

## 1  Purpose

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.

## 2  Specification

 #include #include
 void nag_tsa_auto_corr (const double x[], Integer nx, Integer nk, double *mean, double *var, double r[], double *stat, NagError *fail)

## 3  Description

The data consist of $n$ observations ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,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 $n\ge 2$)
 $s 2 = ∑ i=1 n x i - x - 2 n-1$
(c) The sample autocorrelation coefficients of lags $k=1,2,\dots ,K$, where $K$ is a user-specified maximum lag, and $K, $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 ${\chi }_{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

## 5  Arguments

1:     x[nx]const doubleInput
On entry: the time series, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
2:     nxIntegerInput
On entry: the number of values, $n$, in the time series.
Constraint: ${\mathbf{nx}}>1$.
3:     nkIntegerInput
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<{\mathbf{nk}}<{\mathbf{nx}}$.
4:     meandouble *Output
On exit: the sample mean of the input time series.
5:     vardouble *Output
On exit: the sample variance of the input time series.
6:     r[nk]doubleOutput
On exit: the sample autocorrelation coefficient relating to lag $\mathit{k}$, for $\mathit{k}=1,2,\dots ,K$.
7:     statdouble *Output
On exit: the statistic used to test the hypothesis that the true autocorrelation function of the time series is identically zero.
8:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_2_INT_ARG_LE
On entry, ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{nk}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{nx}}>{\mathbf{nk}}$.
NE_INT_ARG_LE
On entry, ${\mathbf{nk}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nk}}>0$.
On entry, ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nx}}>1$.
NE_TIME_SERIES_IDEN
On entry, all values of x are practically identical, giving zero variance. In this case r and stat are undefined on exit.

## 7  Accuracy

The computations are believed to be stable.

Not applicable.

## 9  Further Comments

The time taken by nag_tsa_auto_corr (g13abc) is approximately proportional to ${\mathbf{nx}}×{\mathbf{nk}}$.

## 10  Example

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

### 10.1  Program Text

Program Text (g13abce.c)

### 10.2  Program Data

Program Data (g13abce.d)

### 10.3  Program Results

Program Results (g13abce.r)

nag_tsa_auto_corr (g13abc) (PDF version)
g13 Chapter Contents
g13 Chapter Introduction
NAG Library Manual