# NAG FL Interfaceg13abf (uni_​autocorr)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g13abf 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

Fortran Interface
 Subroutine g13abf ( x, nx, nk, xm, xv, r, stat,
 Integer, Intent (In) :: nx, nk Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(nx) Real (Kind=nag_wp), Intent (Out) :: xm, xv, r(nk), stat
#include <nag.h>
 void g13abf_ (const double x[], const Integer *nx, const Integer *nk, double *xm, double *xv, double r[], double *stat, Integer *ifail)
The routine may be called by the names g13abf or nagf_tsa_uni_autocorr.

## 3Description

The data consists of $n$ observations ${x}_{i}$, for $\mathit{i}=1,2,\dots ,n$ from a time series.
The quantities calculated are
1. (a)The sample mean
 $x¯=∑i=1nxin.$
2. (b)The sample variance (for $n\ge 2$)
 $s2=∑i=1n (xi-x¯) 2 (n-1) .$
3. (c)The sample autocorrelation coefficients of lags $k=1,2,\dots ,K$, where $K$ is a user-specified maximum lag, and $K, $n>1$.
The coefficient of lag $k$ is defined as
 $rk=∑i=1 n-k(xi-x¯)(xi+k-x¯) ∑i=1n (xi-x¯) 2 .$
See page 496 of Box and Jenkins (1976) for further details.
4. (d)A test statistic defined as
 $stat=n∑k= 1Krk2,$
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; g01ecf 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.

## 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)$Real (Kind=nag_wp) array Input
On entry: the time series, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
2: $\mathbf{nx}$Integer Input
On entry: $n$, the number of values in the time series.
Constraint: ${\mathbf{nx}}>1$.
3: $\mathbf{nk}$Integer Input
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<{\mathbf{nk}}<{\mathbf{nx}}$.
4: $\mathbf{xm}$Real (Kind=nag_wp) Output
On exit: the sample mean of the input time series.
5: $\mathbf{xv}$Real (Kind=nag_wp) Output
On exit: the sample variance of the input time series.
6: $\mathbf{r}\left({\mathbf{nk}}\right)$Real (Kind=nag_wp) array Output
On exit: the sample autocorrelation coefficient relating to lag $\mathit{k}$, for $\mathit{k}=1,2,\dots ,K$.
7: $\mathbf{stat}$Real (Kind=nag_wp) Output
On exit: the statistic used to test the hypothesis that the true autocorrelation function of the time series is identically zero.
8: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{nk}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nk}}>0$.
On entry, ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nx}}>1$.
On entry, ${\mathbf{nx}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{nk}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nx}}>{\mathbf{nk}}$.
${\mathbf{ifail}}=2$
On entry, all values of x are practically identical. This gives zero variance and means that r and stat are undefined on exit.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

The computations are believed to be stable.

## 8Parallelism and Performance

g13abf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13abf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

If $n<100$, or $K<10\mathrm{log}\left(n\right)$ then the autocorrelations are calculated directly and the time taken by g13abf is approximately proportional to $nK$, otherwise the autocorrelations are calculated by utilizing fast fourier transforms (FFTs) and the time taken is approximately proportional to $n\mathrm{log}\left(n\right)$. If FFTs are used then g13abf internally allocates approximately $4n$ real elements.
If the input series for g13abf was generated by differencing using g13aaf, ensure that only the differenced values are input to g13abf, and not the reconstituting information.

## 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 (g13abfe.f90)

### 10.2Program Data

Program Data (g13abfe.d)

### 10.3Program Results

Program Results (g13abfe.r)
This plot shows the autocorrelations for all possible lag values. Reference lines are given at $±{z}_{0.975}/\sqrt{n}$.