hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_tsa_uni_autocorr (g13ab)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_tsa_uni_autocorr (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.

Syntax

[xm, xv, r, stat, ifail] = g13ab(x, nk, 'nx', nx)
[xm, xv, r, stat, ifail] = nag_tsa_uni_autocorr(x, nk, 'nx', nx)

Description

The data consists of n observations xi, for i=1,2,,n from a time series.
The quantities calculated are
(a) The sample mean
x-=i=1nxin.  
(b) The sample variance (for n2)
s2=i=1n xi-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.
The coefficient of lag k is defined as
rk=i=1 n-kxi-x-xi+k-x- i=1n xi-x- 2 .  
See page 496 of Box and Jenkins (1976) for further details.
(d) A test statistic defined as
stat=nk= 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 χ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; nag_stat_prob_chisq (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.

References

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

Parameters

Compulsory Input Parameters

1:     xnx – double array
The time series, xi, for i=1,2,,n.
2:     nk int64int32nag_int scalar
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.

Optional Input Parameters

1:     nx int64int32nag_int scalar
Default: the dimension of the array x.
n, the number of values in the time series.
Constraint: nx>1.

Output Parameters

1:     xm – double scalar
The sample mean of the input time series.
2:     xv – double scalar
The sample variance of the input time series.
3:     rnk – double array
The sample autocorrelation coefficient relating to lag k, for k=1,2,,K.
4:     stat – double scalar
The statistic used to test the hypothesis that the true autocorrelation function of the time series is identically zero.
5:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

   ifail=1
On entry,nxnk,
ornx1,
ornk0.
W  ifail=2
On entry, all values of x are practically identical, giving zero variance. In this case r and stat are undefined on exit.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

The computations are believed to be stable.

Further Comments

If n<100, or K<10logn then the autocorrelations are calculated directly and the time taken by nag_tsa_uni_autocorr (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 nag_tsa_uni_autocorr (g13ab) internally allocates approximately 4n real elements.
If the input series for nag_tsa_uni_autocorr (g13ab) was generated by differencing using nag_tsa_uni_diff (g13aa), ensure that only the differenced values are input to nag_tsa_uni_autocorr (g13ab), and not the reconstituting information.

Example

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


fprintf('g13ab example results\n\n');

x = [ 5;  11;  16;  23;  36;  58;  29;  20;  10;   8;
      3;   0;   0;   2;  11;  27;  47;  63;  60;  39;
     28;  26;  22;  11;  21;  40;  78; 122; 103;  73;
     47;  35;  11;   5;  16;  34;  70;  81; 111; 101;
     73;  40;  20;  16;   5;  11;  22;  40;  60;  80.9];

nk = int64(10);
% Compute autocorrelation
[xm, xv, r, stat, ifail] = g13ab( ...
                                  x, nk);

% Display results
fprintf('The first %4d coefficients are required\n',nk);
fprintf('The input array has sample mean     %12.4f\n', xm);
fprintf('The input array has sample variance %12.4f\n', xv);
fprintf('The sample autocorrelation coefficients are\n\n');
fprintf('   Lag    Coeff      Lag    Coeff\n');
ivar = double([1:nk]);
fprintf('%6d%10.4f%8d%10.4f\n',[ivar; r(1:nk)']);
fprintf('\nThe value of stat is %12.4f\n', stat);

% For plotting use all posible lags
nk = int64(numel(x)-1);
[xm, xv, r, stat, ifail] = g13ab( ...
                                  x, nk);

fig1 = figure;
refline = 1.96/sqrt(numel(x));
hold on
h = bar(r,0.1);
h.FaceColor = [1 0 0];
h.EdgeColor = [1 0 0];
h.ShowBaseLine = 'off';
plot([0 nk+1],[refline refline],'green');
plot([0 nk+1],[-refline -refline],'green');
axis([0 50 -0.6 1]);
ax = gca;
ax.YTick = [-0.6:0.2:1];
ax.XTick = [0:10:50];
xlabel('Lag');
ylabel('ACF');
title('Sample autocorrelation coefficients');
hold off

g13ab example results

The first   10 coefficients are required
The input array has sample mean          37.4180
The input array has sample variance    1002.0301
The sample autocorrelation coefficients are

   Lag    Coeff      Lag    Coeff
     1    0.8004       2    0.4355
     3    0.0328       4   -0.2835
     5   -0.4505       6   -0.4242
     7   -0.2419       8    0.0550
     9    0.3783      10    0.5857

The value of stat is      92.1231
g13ab_fig1.png
This plot shows the autocorrelations for all possible lag values. Reference lines are given at ±z0.975/n.

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015