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, ifail] = g13au(z, m, rs, 'n', n)
[y, mean, ifail] = nag_tsa_uni_means(z, m, rs, 'n', n)

Description

Let Z1,Z2,,Zn${Z}_{1},{Z}_{2},\dots ,{Z}_{n}$ denote n$n$ successive observations in a time series. The series may be divided into groups of m$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$n$ is not a multiple of m$m$ then groups of equal size m$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$k$, is the integer part of n / m$n/m$. If you wish to ensure that no observations are ignored then the number of observations, n$n$, should be chosen so that n$n$ is divisible by m$m$.
The mean, Mi${M}_{i}$, the range, Ri${R}_{i}$, and the standard deviation, Si${S}_{i}$, for the i$i$th group are defined as
 Mi = 1/m ∑ j = 1mZl + m(i − 1) + j Ri = max1 ≤ j ≤ m{Zl + m(i − 1) + j} − min1 ≤ j ≤ m{Zl + m(i − 1) + j}
$Mi=1m∑j=1mZl+m(i-1)+j Ri=max1≤j≤m{Zl+m(i-1)+j}-min1≤j≤m{Zl+m(i-1)+j}$
and
 Si = sqrt( (1/(m − 1)) ∑ j = 1m(Zl + m(i − 1) + j − Mi)2 ) $Si= (1m- 1 ) ∑j= 1m(Zl+m(i- 1)+j-Mi)2$
where l = nkm$l=n-km$, the number of observations ignored.
For seasonal data it is recommended that m$m$ should be equal to the seasonal period. For non-seasonal data the recommended group size is 8$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$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:     z(n) – double array
n, the dimension of the array, must satisfy the constraint nm${\mathbf{n}}\ge {\mathbf{m}}$.
z(t)${\mathbf{z}}\left(\mathit{t}\right)$ must contain the t$\mathit{t}$th observation Zt${Z}_{\mathit{t}}$, for t = 1,2,,n$\mathit{t}=1,2,\dots ,n$.
2:     m – int64int32nag_int scalar
m$m$, the group size.
Constraint: m2${\mathbf{m}}\ge 2$.
3:     rs – string (length ≥ 1)
Indicates whether ranges or standard deviations are to be calculated.
rs = 'R'${\mathbf{rs}}=\text{'R'}$
Ranges are calculated.
rs = 'S'${\mathbf{rs}}=\text{'S'}$
Standard deviations are calculated.
Constraint: rs = 'R'${\mathbf{rs}}=\text{'R'}$ or 'S'$\text{'S'}$.

Optional Input Parameters

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

ngrps

Output Parameters

1:     y(ngrps) – double array
ngrps = int(n / m)$\mathit{ngrps}=\mathrm{int}\left({\mathbf{n}}/{\mathbf{m}}\right)$.
y(i)${\mathbf{y}}\left(\mathit{i}\right)$ contains the range or standard deviation, as determined by rs, of the i$\mathit{i}$th group of observations, for i = 1,2,,k$\mathit{i}=1,2,\dots ,k$.
2:     mean(ngrps) – double array
ngrps = int(n / m)$\mathit{ngrps}=\mathrm{int}\left({\mathbf{n}}/{\mathbf{m}}\right)$.
mean(i)${\mathbf{mean}}\left(\mathit{i}\right)$ contains the mean of the i$\mathit{i}$th group of observations, for i = 1,2,,k$\mathit{i}=1,2,\dots ,k$.
3:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
 On entry, n < m${\mathbf{n}}<{\mathbf{m}}$, or m < 2${\mathbf{m}}<2$, or ngrps ≠ $\mathit{ngrps}\ne \text{}$ integer part of n / m${\mathbf{n}}/{\mathbf{m}}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, rs is not equal to 'R' or 'S'.

Accuracy

The computations are believed to be stable.

The time taken by nag_tsa_uni_means (g13au) is approximately proportional to n$n$.
If you wish to obtain a plot of the group ranges or standard deviations against the group means then nag_stat_plot_scatter_2var (g01ag) may be used. The plot is output to the unit defined by nag_file_set_unit_advisory (x04ab). You should note that nag_stat_plot_scatter_2var (g01ag) sorts the data to be plotted on the y$y$ axis (in this case the ranges or standard deviations). If required you may use nag_sort_realvec_rank_rearrange (m01ea) to re-arrange the data into their original order.

Example

```function nag_tsa_uni_means_example
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];
m = int64(8);
rs = 'RANGE';
[y, mean_p, ifail] = nag_tsa_uni_means(z, m, rs)
```
```

y =

148
123
84
45
28
40
65
131
92
85
92
67

mean_p =

72.3750
70.0000
43.5000
29.7500
7.6250
26.7500
30.2500
61.0000
47.6250
75.2500
46.8750
39.2500

ifail =

0

```
```function g13au_example
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];
m = int64(8);
rs = 'RANGE';
[y, mean_p, ifail] = g13au(z, m, rs)
```
```

y =

148
123
84
45
28
40
65
131
92
85
92
67

mean_p =

72.3750
70.0000
43.5000
29.7500
7.6250
26.7500
30.2500
61.0000
47.6250
75.2500
46.8750
39.2500

ifail =

0

```