# NAG Library Function Document

## 1Purpose

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.

## 2Specification

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

## 3Description

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.

## 4References

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

## 5Arguments

1:    $\mathbf{x}\left[{\mathbf{nx}}\right]$const doubleInput
On entry: the time series, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
2:    $\mathbf{nx}$IntegerInput
On entry: the number of values, $n$, in the time series.
Constraint: ${\mathbf{nx}}>1$.
3:    $\mathbf{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<{\mathbf{nk}}<{\mathbf{nx}}$.
4:    $\mathbf{mean}$double *Output
On exit: the sample mean of the input time series.
5:    $\mathbf{var}$double *Output
On exit: the sample variance of the input time series.
6:    $\mathbf{r}\left[{\mathbf{nk}}\right]$doubleOutput
On exit: the sample autocorrelation coefficient relating to lag $\mathit{k}$, for $\mathit{k}=1,2,\dots ,K$.
7:    $\mathbf{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:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error 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.

## 7Accuracy

The computations are believed to be stable.

## 8Parallelism and Performance

nag_tsa_auto_corr (g13abc) is not threaded in any implementation.

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

## 10Example

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

### 10.1Program Text

Program Text (g13abce.c)

### 10.2Program Data

Program Data (g13abce.d)

### 10.3Program Results

Program Results (g13abce.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017