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_stat_moving_average (g01wa)

## Purpose

nag_stat_moving_average (g01wa) calculates the mean and, optionally, the standard deviation using a rolling window for an arbitrary sized data stream.

## Syntax

[pn, rmean, rsd, rcomm, ifail] = g01wa(m, x, 'nb', nb, 'iwt', iwt, 'wt', wt, 'pn', pn, 'wantsd', wantsd, 'rcomm', rcomm)
[pn, rmean, rsd, rcomm, ifail] = nag_stat_moving_average(m, x, 'nb', nb, 'iwt', iwt, 'wt', wt, 'pn', pn, 'wantsd', wantsd, 'rcomm', rcomm)

## Description

Given a sample of n$n$ observations, denoted by x = {xi : i = 1,2,,n} $x=\left\{{x}_{i}:i=1,2,\dots ,n\right\}$ and a set of weights, w = {wj : j = 1,2,,m} $w=\left\{{w}_{j}:j=1,2,\dots ,m\right\}$, nag_stat_moving_average (g01wa) calculates the mean and, optionally, the standard deviation, in a rolling window of length m$m$.
For the i$i$th window the mean is defined as
 μi = ( ∑ j = 1m wj xi + j − 1 )/W $μi = ∑ j=1 m wj ⁢ xi+j-1 W$ (1)
and the standard deviation as
 σi = sqrt( ( ∑ j = 1m wj (xi + j − 1 − μi)2 )/( W − ( ∑ j = 1m wj2 )/W ) ) $σi = ∑ j=1 m wj ⁢ ( xi+j-1 - μi ) 2 W - ∑ j=1 m wj2 W$ (2)
with W = j = 1m wj $W=\sum _{j=1}^{m}{w}_{j}$.
Four different types of weighting are possible:
(i) No weights (wj = 1${w}_{j}=1$)
When no weights are required both the mean and standard deviations can be calculated in an iterative manner, with
 μi + 1 = μi + ((xi + m − xi))/m σi + 12 = (m − 1) σi2 + (xi + m − μi)2 − (xi − μi)2 − ((xi + m − xi)2)/m
$μi+1= μi + ( xi+m - xi ) m σi+12 = (m-1) ⁢ σi2 + ( xi+m - μi ) 2 - ( xi - μi ) 2 - ( xi+m - xi ) 2 m$
where the initial values μ1${\mu }_{1}$ and σ1${\sigma }_{1}$ are obtained using the one pass algorithm of West (1979).
(ii) Each observation has its own weight
In this case, rather than supplying a vector of m$m$ weights a vector of n$n$ weights is supplied instead, v = {vj : j = 1,2,,n}$v=\left\{{v}_{j}:j=1,2,\dots ,n\right\}$ and wj = vi + j1${w}_{j}={v}_{i+j-1}$ in (1) and (2).
If the standard deviations are not required then the mean is calculated using the iterative formula:
 Wi + 1 = Wi + (vi + m − vi) μi + 1 = μi + Wi − 1 (vi + mxi + m − vixi)
$Wi+1= Wi+ ( vi+m - vi ) μi+1= μi + Wi-1 ⁢ ( vi+m ⁢ xi+m - vi ⁢ xi )$
where W1 = i = 1m vi ${W}_{1}=\sum _{i=1}^{m}{v}_{i}$ and μ1 = W11 i = 1m vi xi ${\mu }_{1}={W}_{1}^{-1}\sum _{i=1}^{m}{v}_{i}{x}_{i}$.
If both the mean and standard deviation are required then the one pass algorithm of West (1979) is used in each window.
(iii) Each position in the window has its own weight
This is the case as described in (1) and (2), where the weight given to each observation differs depending on which summary is being produced. When these types of weights are specified both the mean and standard deviation are calculated by applying the one pass algorithm of West (1979) multiple times.
(iv) Each position in the window has a weight equal to its position number (wj = j${w}_{j}=j$)
This is a special case of (iii).
If the standard deviations are not required then the mean is calculated using the iterative formula:
 Si + 1 = Si + (xi + m − xi) μi + 1 = μi + ( 2 (mxi + m − Si) )/( m (m + 1) )
$Si+1= Si+ ( xi+m - xi ) μi+1= μi + 2 ( m ⁢ xi+m - Si ) m ⁢ (m+1)$
where S1 = i = 1m xi ${S}_{1}=\sum _{i=1}^{m}{x}_{i}$ and μ1 = 2 (m2 + m)1 S1 ${\mu }_{1}=2{\left({m}^{2}+m\right)}^{-1}{S}_{1}$.
If both the mean and standard deviation are required then the one pass algorithm of West is applied multiple times.
For large datasets, or where all the data is not available at the same time, x$x$ (and if each observation has its own weight, v$v$) can be split into arbitrary sized blocks and nag_stat_moving_average (g01wa) called multiple times.

## References

Chan T F, Golub G H and Leveque R J (1982) Updating Formulae and a Pairwise Algorithm for Computing Sample Variances Compstat, Physica-Verlag
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555

## Parameters

### Compulsory Input Parameters

1:     m – int64int32nag_int scalar
m$m$, the length of the rolling window.
If pn0${\mathbf{pn}}\ne 0$, m must be unchanged since the last call to nag_stat_moving_average (g01wa).
Constraint: m1${\mathbf{m}}\ge 1$.
2:     x(nb) – double array
nb, the dimension of the array, must satisfy the constraint
• nb0${\mathbf{nb}}\ge 0$
• if rcomm is not provided, nbm${\mathbf{nb}}\ge {\mathbf{m}}$
• .
The current block of observations, corresponding to xi${x}_{\mathit{i}}$, for i = k + 1,,k + b$\mathit{i}=k+1,\dots ,k+b$, where k$k$ is the number of observations processed so far and b$b$ is the size of the current block of data.

### Optional Input Parameters

1:     nb – int64int32nag_int scalar
Default: The dimension of the array x.
b$b$, the number of observations in the current block of data. The size of the block of data supplied in x (and when iwt = 1${\mathbf{iwt}}=1$, wt) can vary; therefore nb can change between calls to nag_stat_moving_average (g01wa).
Constraints:
• nb0${\mathbf{nb}}\ge 0$;
• if rcomm is not provided, nbm${\mathbf{nb}}\ge {\mathbf{m}}$.
2:     iwt – int64int32nag_int scalar
The type of weighting to use.
iwt = 0${\mathbf{iwt}}=0$
No weights are used.
iwt = 1${\mathbf{iwt}}=1$
Each observation has its own weight.
iwt = 2${\mathbf{iwt}}=2$
Each position in the window has its own weight.
iwt = 3${\mathbf{iwt}}=3$
Each position in the window has a weight equal to its position number.
If pn0${\mathbf{pn}}\ne 0$, iwt must be unchanged since the last call to nag_stat_moving_average (g01wa).
Default: 0$0$
Constraint: iwt = 0${\mathbf{iwt}}=0$, 1$1$, 2$2$ or 3$3$.
3:     wt( : $:$) – double array
Note: the dimension of the array wt must be at least nb${\mathbf{nb}}$ if iwt = 1${\mathbf{iwt}}=1$ and at least m${\mathbf{m}}$ if iwt = 2${\mathbf{iwt}}=2$.
The user-supplied weights.
If iwt = 1${\mathbf{iwt}}=1$, wt(i) = νi + k${\mathbf{wt}}\left(\mathit{i}\right)={\nu }_{\mathit{i}+k}$, for i = 1,2,,b$\mathit{i}=1,2,\dots ,b$.
If iwt = 2${\mathbf{iwt}}=2$, wt(j) = wj${\mathbf{wt}}\left(\mathit{j}\right)={w}_{\mathit{j}}$, for j = 1,2,,m$\mathit{j}=1,2,\dots ,m$.
Otherwise, wt is not referenced.
Constraints:
• if iwt = 1${\mathbf{iwt}}=1$, wt(i)0${\mathbf{wt}}\left(\mathit{i}\right)\ge 0$, for i = 1,2,,nb$\mathit{i}=1,2,\dots ,{\mathbf{nb}}$;
• if iwt = 2${\mathbf{iwt}}=2$, wt(1)0${\mathbf{wt}}\left(1\right)\ne 0$ and j = 1mwt(j) > 0${\sum }_{\mathit{j}=1}^{m}{\mathbf{wt}}\left(\mathit{j}\right)>0$;
• if iwt = 2${\mathbf{iwt}}=2$ and lrsd0$\mathbf{lrsd}\ne 0$, wt(j)0${\mathbf{wt}}\left(\mathit{j}\right)\ge 0$, for j = 1,2,,m$\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
4:     pn – int64int32nag_int scalar
k$k$, the number of observations processed so far. On the first call to nag_stat_moving_average (g01wa), or when starting to summarise a new dataset, pn must be set to 0$0$.
If pn0${\mathbf{pn}}\ne 0$, it must be the same value as returned by the last call to nag_stat_moving_average (g01wa).
Default: 0$0$
Constraint: pn0${\mathbf{pn}}\ge 0$.
5:     wantsd – logical scalar
If the standard deviations are required then wantsd should be set to true.
Default: false$\mathbf{false}$
6:     rcomm(2m + 20$2{\mathbf{m}}+20$) – double array
Communication array, used to store information between calls to nag_stat_moving_average (g01wa). If lrcomm = 0$\mathbf{lrcomm}=0$, rcomm is not referenced and all the data must be supplied in one go.

None.

### Output Parameters

1:     pn – int64int32nag_int scalar
Default: 0$0$
k + b$k+b$, the updated number of observations processed so far.
2:     rmean(max (0,nb + min (0,pnm + 1))$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(0,{\mathbf{nb}}+\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(0,{\mathbf{pn}}-{\mathbf{m}}+1\right)\right)$) – double array
μl${\mu }_{\mathit{l}}$, the (weighted) moving averages, for l = 1,2,,b + min (0,km + 1)$\mathit{l}=1,2,\dots ,b+\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(0,k-m+1\right)$. Therefore, μl${\mu }_{l}$ is the mean of the data in the window that ends on x(l + mmin (k,m1)1)${\mathbf{x}}\left(l+m-\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(k,m-1\right)-1\right)$.
If, on entry, pnm1${\mathbf{pn}}\ge {\mathbf{m}}-1$, i.e., at least one windows worth of data has been previously processed, then rmean(l)${\mathbf{rmean}}\left(l\right)$ is the summary corresponding to the window that ends on x(l)${\mathbf{x}}\left(l\right)$. On the other hand, if, on entry, pn = 0${\mathbf{pn}}=0$, i.e., no data has been previously processed, then rmean(l)${\mathbf{rmean}}\left(l\right)$ is the summary corresponding to the window that ends on x(m + l1)${\mathbf{x}}\left({\mathbf{m}}+l-1\right)$ (or, equivalently, starts on x(l)${\mathbf{x}}\left(l\right)$).
3:     rsd(lrsd) – double array
If sdp was set to true, then σl${\sigma }_{l}$, the (weighted) standard deviation. The ordering of rsd is the same as the ordering of rmean.
4:     rcomm(2m + 20$2{\mathbf{m}}+20$) – double array
Communication array, used to store information between calls to nag_stat_moving_average (g01wa).
5:     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:

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

ifail = 11${\mathbf{ifail}}=11$
Constraint: m1${\mathbf{m}}\ge 1$.
ifail = 12${\mathbf{ifail}}=12$
Constraint: if pn > 0${\mathbf{pn}}>0$, m must be unchanged since previous call.
ifail = 21${\mathbf{ifail}}=21$
Constraint: nb0${\mathbf{nb}}\ge 0$.
ifail = 22${\mathbf{ifail}}=22$
Constraint: if lrcomm = 0$\mathbf{lrcomm}=0$, nbm${\mathbf{nb}}\ge {\mathbf{m}}$.
ifail = 41${\mathbf{ifail}}=41$
Constraint: iwt = 0${\mathbf{iwt}}=0$, 1$1$, 2$2$ or 3$3$.
ifail = 42${\mathbf{ifail}}=42$
Constraint: if pn > 0${\mathbf{pn}}>0$, iwt must be unchanged since previous call.
ifail = 51${\mathbf{ifail}}=51$
Constraint: wt(i)0${\mathbf{wt}}\left(i\right)\ge 0$.
ifail = 52${\mathbf{ifail}}=52$
Constraint: if iwt = 2${\mathbf{iwt}}=2$, wt(1) > 0${\mathbf{wt}}\left(1\right)>0$.
W ifail = 53${\mathbf{ifail}}=53$
On entry, at least one window had all zero weights.
W ifail = 54${\mathbf{ifail}}=54$
On entry, unable to calculate at least one standard deviation due to the weights supplied.
ifail = 55${\mathbf{ifail}}=55$
Constraint: if iwt = 2${\mathbf{iwt}}=2$, the sum of the weights > 0$>0$.
ifail = 61${\mathbf{ifail}}=61$
Constraint: pn0${\mathbf{pn}}\ge 0$.
ifail = 62${\mathbf{ifail}}=62$
Constraint: if pn > 0${\mathbf{pn}}>0$, pn must be unchanged since previous call.
ifail = 91${\mathbf{ifail}}=91$
Constraint: lrsd = 0$\mathbf{lrsd}=0$ or .
ifail = 101${\mathbf{ifail}}=101$
rcomm has been corrupted between calls.
ifail = 111${\mathbf{ifail}}=111$
lrcomm is too small. lrcomm is too small.
ifail = 999${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

Not applicable.

The more data that is supplied to nag_stat_moving_average (g01wa) in one call, i.e., the larger nb is, the more efficient the function will be.

## Example

```function nag_stat_moving_average_example
wt  = [-3.0; -6.0; -5.0; 3.0; 21.0; 46.0; 67.0; 74.0; 67.0; 46.0; 21.0; ...
3.0; -5.0; -6.0; -3.0];
iwt = int64(2);
m   = int64(15);
x1 = [-2170; -1770; -1660; -1360; -1100];
x2 = [-950; -640; -370; -140; -250; -510; -620; -730; -880; -1130];
x3 = [-1200; -830; -330; -190; 210; 170; 440; 440; 780; 880; 1220; 1260; ...
1140; 850; 640];
data = {x1; x2; x3};

% Initialise the number of valid observations processed so far
pn = int64(0);

fprintf('\n Interval          Mean\n');
fprintf('------------------------\n');
for b=1:3
% Calculate the number of summaries we can produce
nb = numel(data{b});
nsummaries = max(0, nb + min(0, pn-m+1));

% Calculate summary statistics for this block of data
if b==1
[pn, rmean, rsd, rcomm, ifail] = ...
nag_stat_moving_average(m, data{b}, 'iwt', iwt, 'wt', wt);
else
[pn, rmean, rsd, rcomm, ifail] =  ...
nag_stat_moving_average(m, data{b}, 'iwt', iwt, 'wt', wt, 'pn', pn, 'rcomm', rcomm);
end

% Number of results printed so far
offset = max(0, pn-nb-m+1);

% Display the results for this block of data
for i=1:nsummaries
fprintf(' [%2d, %2d]    %10.1f\n', i+offset, i+m-1+offset, rmean(i));
end
end

fprintf('\nTotal number of observations : %d\n', pn);
fprintf('Length of window             : %d\n', m);
```
```

Interval          Mean
------------------------
[ 1, 15]        -427.6
[ 2, 16]        -332.5
[ 3, 17]        -337.1
[ 4, 18]        -438.2
[ 5, 19]        -604.4
[ 6, 20]        -789.4
[ 7, 21]        -935.4
[ 8, 22]        -990.6
[ 9, 23]        -927.1
[10, 24]        -752.1
[11, 25]        -501.3
[12, 26]        -227.2
[13, 27]          23.2
[14, 28]         236.2
[15, 29]         422.4
[16, 30]         604.2

Total number of observations : 30
Length of window             : 15

```
```function g01wa_example
wt  = [-3.0; -6.0; -5.0; 3.0; 21.0; 46.0; 67.0; 74.0; 67.0; 46.0; 21.0; ...
3.0; -5.0; -6.0; -3.0];
iwt = int64(2);
m   = int64(15);
x1 = [-2170; -1770; -1660; -1360; -1100];
x2 = [-950; -640; -370; -140; -250; -510; -620; -730; -880; -1130];
x3 = [-1200; -830; -330; -190; 210; 170; 440; 440; 780; 880; 1220; 1260; ...
1140; 850; 640];
data = {x1; x2; x3};

% Initialise the number of valid observations processed so far
pn = int64(0);

fprintf('\n Interval          Mean\n');
fprintf('------------------------\n');
for b=1:3
% Calculate the number of summaries we can produce
nb = numel(data{b});
nsummaries = max(0, nb + min(0, pn-m+1));

% Calculate summary statistics for this block of data
if b==1
[pn, rmean, rsd, rcomm, ifail] = ...
g01wa(m, data{b}, 'iwt', iwt, 'wt', wt);
else
[pn, rmean, rsd, rcomm, ifail] =  ...
g01wa(m, data{b}, 'iwt', iwt, 'wt', wt, 'pn', pn, 'rcomm', rcomm);
end

% Number of results printed so far
offset = max(0, pn-nb-m+1);

% Display the results for this block of data
for i=1:nsummaries
fprintf(' [%2d, %2d]    %10.1f\n', i+offset, i+m-1+offset, rmean(i));
end
end

fprintf('\nTotal number of observations : %d\n', pn);
fprintf('Length of window             : %d\n', m);
```
```

Interval          Mean
------------------------
[ 1, 15]        -427.6
[ 2, 16]        -332.5
[ 3, 17]        -337.1
[ 4, 18]        -438.2
[ 5, 19]        -604.4
[ 6, 20]        -789.4
[ 7, 21]        -935.4
[ 8, 22]        -990.6
[ 9, 23]        -927.1
[10, 24]        -752.1
[11, 25]        -501.3
[12, 26]        -227.2
[13, 27]          23.2
[14, 28]         236.2
[15, 29]         422.4
[16, 30]         604.2

Total number of observations : 30
Length of window             : 15

```

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–2013