Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_tsa_uni_means (g13au)

## Purpose

nag_tsa_uni_means (g13au) calculates the range (or standard deviation) and the mean for groups of successive time series values. It is intended for use in the construction of range-mean plots.

## Syntax

[y, mean_p, ifail] = g13au(z, m, rs, 'n', n)
[y, mean_p, ifail] = nag_tsa_uni_means(z, m, rs, 'n', n)

## Description

Let ${Z}_{1},{Z}_{2},\dots ,{Z}_{n}$ denote $n$ successive observations in a time series. The series may be divided into groups of $m$ successive values and for each group the range or standard deviation (depending on a user-supplied option) and the mean are calculated. If $n$ is not a multiple of $m$ then groups of equal size $m$ are found starting from the end of the series of observations provided, and any remaining observations at the start of the series are ignored. The number of groups used, $k$, is the integer part of $n/m$. If you wish to ensure that no observations are ignored then the number of observations, $n$, should be chosen so that $n$ is divisible by $m$.
The mean, ${M}_{i}$, the range, ${R}_{i}$, and the standard deviation, ${S}_{i}$, for the $i$th group are defined as
 $Mi=1m∑j=1mZl+mi-1+j Ri=max1≤j≤mZl+mi-1+j-min1≤j≤mZl+mi-1+j$
and
 $Si= 1m- 1 ∑j= 1mZl+mi- 1+j-Mi2$
where $l=n-km$, the number of observations ignored.
For seasonal data it is recommended that $m$ should be equal to the seasonal period. For non-seasonal data the recommended group size is $8$.
A plot of range against mean or of standard deviation against mean is useful for finding a transformation of the series which makes the variance constant. If the plot appears random or the range (or standard deviation) seems to be constant irrespective of the mean level then this suggests that no transformation of the time series is called for. On the other hand an approximate linear relationship between range (or standard deviation) and mean would indicate that a log transformation is appropriate. Further details may be found in either Jenkins (1979) or McLeod (1982).
You have the choice of whether to use the range or the standard deviation as a measure of variability. If the group size is small they are both equally good but if the group size is fairly large (e.g., $m=12$ for monthly data) then the range may not be as good an estimate of variability as the standard deviation.

## References

Jenkins G M (1979) Practical Experiences with Modelling and Forecasting Time Series GJP Publications, Lancaster
McLeod G (1982) Box–Jenkins in Practice. 1: Univariate Stochastic and Single Output Transfer Function/Noise Analysis GJP Publications, Lancaster

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{z}\left({\mathbf{n}}\right)$ – double array
${\mathbf{z}}\left(\mathit{t}\right)$ must contain the $\mathit{t}$th observation ${Z}_{\mathit{t}}$, for $\mathit{t}=1,2,\dots ,n$.
2:     $\mathrm{m}$int64int32nag_int scalar
$m$, the group size.
Constraint: ${\mathbf{m}}\ge 2$.
3:     $\mathrm{rs}$ – string (length ≥ 1)
Indicates whether ranges or standard deviations are to be calculated.
${\mathbf{rs}}=\text{'R'}$
Ranges are calculated.
${\mathbf{rs}}=\text{'S'}$
Standard deviations are calculated.
Constraint: ${\mathbf{rs}}=\text{'R'}$ or $\text{'S'}$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array z.
$n$, the number of observations in the time series.
Constraint: ${\mathbf{n}}\ge {\mathbf{m}}$.

### Output Parameters

1:     $\mathrm{y}\left(\mathit{ngrps}\right)$ – double array
$\mathit{ngrps}=\mathrm{int}\left({\mathbf{n}}/{\mathbf{m}}\right)$.
${\mathbf{y}}\left(\mathit{i}\right)$ contains the range or standard deviation, as determined by rs, of the $\mathit{i}$th group of observations, for $\mathit{i}=1,2,\dots ,k$.
2:     $\mathrm{mean_p}\left(\mathit{ngrps}\right)$ – double array
$\mathit{ngrps}=\mathrm{int}\left({\mathbf{n}}/{\mathbf{m}}\right)$.
${\mathbf{mean_p}}\left(\mathit{i}\right)$ contains the mean of the $\mathit{i}$th group of observations, for $\mathit{i}=1,2,\dots ,k$.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<{\mathbf{m}}$, or ${\mathbf{m}}<2$, or $\mathit{ngrps}\ne \text{}$ integer part of ${\mathbf{n}}/{\mathbf{m}}$.
${\mathbf{ifail}}=2$
 On entry, rs is not equal to 'R' or 'S'.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

The computations are believed to be stable.

The time taken by nag_tsa_uni_means (g13au) is approximately proportional to $n$.

## Example

The following program produces the statistics for a range-mean plot for a series of $100$ observations divided into groups of $8$.
```function g13au_example

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

% Data
z = [101;  82;  66;  35;  31;   6;  20;  90; 154; 125;
85;  68;  38;  23;  10;  24;  83; 133; 131; 118;
90;  67;  60;  47;  41;  21;  16;   6;   4;   7;
14;  34;  45;  43;  49;  42;  28;  10;   5;   2;
0;   1;   3;  12;  14;  35;  47;  41;  30;  24;
16;   7;   4;   2;   8;  13;  36;  50;  62;  67;
72;  48;  29;   8;  13;  57; 122; 139; 103;  86;
63;  37;  26;  11;  15;  40;  62;  98; 124;  96;
65;  64;  54;  39;  21;   7;   4;  23;  53;  94;
96;  77;  59;  44;  47;  30;  16;   7;  37;  74];

% Number of groups
m = int64(8);

% Calculate summary statistic
rs = 'RANGE';
[y, mean_p, ifail] = g13au( ...
z, m, rs);

% Display results
fprintf('     Mean      Range\n');
fprintf('   -------------------\n');
fprintf('%11.3f%11.3f\n', [mean_p y]');

fig1 = figure;
plot(mean_p,y,'+','Color','Red');
xlabel('Mean');
ylabel('Range');
title('Plot of Range vs Mean (Y vs Mean)');

```
```g13au example results

Mean      Range
-------------------
72.375    148.000
70.000    123.000
43.500     84.000
29.750     45.000
7.625     28.000
26.750     40.000
30.250     65.000
61.000    131.000
47.625     92.000
75.250     85.000
46.875     92.000
39.250     67.000
```

Chapter Contents
Chapter Introduction
NAG Toolbox

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