NAG Library Routine Document
G01WAF
1 Purpose
G01WAF calculates the mean and, optionally, the standard deviation using a rolling window for an arbitrarysized data stream.
2 Specification
SUBROUTINE G01WAF ( 
M, NB, X, IWT, WT, PN, RMEAN, RSD, LRSD, RCOMM, LRCOMM, IFAIL) 
INTEGER 
M, NB, IWT, PN, LRSD, LRCOMM, IFAIL 
REAL (KIND=nag_wp) 
X(NB), WT(*), RMEAN(max(0,NB+min(0,PNM+1))), RSD(LRSD), RCOMM(LRCOMM) 

3 Description
Given a sample of $n$ observations, denoted by $x=\left\{{x}_{i}:i=1,2,\dots ,n\right\}$ and a set of weights, $w=\left\{{w}_{j}:j=1,2,\dots ,m\right\}$, G01WAF calculates the mean and, optionally, the standard deviation, in a rolling window of length $m$.
The mean is defined as
and the standard deviation as
with
$W={\displaystyle \sum _{j=1}^{m}}{w}_{j}$.
Four different types of weighting are possible:
(i) 
No weights (${w}_{j}=1$)
When no weights are required both the mean and standard deviations can be calculated in an iterative manner, with
where the initial values ${\mu}_{1}$ and ${\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$ weights a vector of $n$ weights is supplied instead, $v=\left\{{v}_{j}:j=1,2,\dots ,n\right\}$ and ${w}_{j}={v}_{i+j1}$ in (1) and (2).
If the standard deviations are not required then the mean is calculated using the iterative formula:
where
${W}_{1}={\displaystyle \sum _{i=1}^{m}}{v}_{i}$ and
${\mu}_{1}={W}_{1}^{1}{\displaystyle \sum _{i=1}^{m}}{v}_{i}{x}_{i}$.
If both the mean and standard deviation are required then the one pass algorithm of West is applied multiple times. 
(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 multiple times. 
(iv) 
Each position in the window has a weight equal to its position number (${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:
where
${S}_{1}={\displaystyle \sum _{i=1}^{m}}{x}_{i}$ and
${\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$ (and if each observation has its own weight, $v$) can be split into arbitrary sized blocks and G01WAF called multiple times.
4 References
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555
5 Parameters
 1: M – INTEGERInput
On entry:
$m$, the length of the rolling window.
If
${\mathbf{PN}}\ne 0$,
M must be unchanged since the last call to G01WAF.
Constraint:
${\mathbf{M}}\ge 1$.
 2: NB – INTEGERInput
On entry:
$b$, the number of observations in the current block of data. The size of the block of data supplied in
X (and when
${\mathbf{IWT}}=1$,
WT) can vary; therefore
NB can change between calls to G01WAF.
Constraints:
 ${\mathbf{NB}}\ge 0$;
 if ${\mathbf{LRCOMM}}=0$, ${\mathbf{NB}}\ge {\mathbf{M}}$.
 3: X(NB) – REAL (KIND=nag_wp) arrayInput
On entry: the current block of observations, corresponding to
${x}_{\mathit{i}}$, for $\mathit{i}=k+1,\dots ,k+b$, where $k$ is the number of observations processed so far and $b$ is the size of the current block of data.
 4: IWT – INTEGERInput
On entry: the type of weighting to use.
 ${\mathbf{IWT}}=0$
 No weights are used.
 ${\mathbf{IWT}}=1$
 Each observation has its own weight.
 ${\mathbf{IWT}}=2$
 Each position in the window has its own weight.
 ${\mathbf{IWT}}=3$
 Each position in the window has a weight equal to its position number.
If
${\mathbf{PN}}\ne 0$,
IWT must be unchanged since the last call to G01WAF.
Constraint:
${\mathbf{IWT}}=0$, $1$, $2$ or $3$.
 5: WT($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array
WT
must be at least
${\mathbf{NB}}$ if
${\mathbf{IWT}}=1$ and at least
${\mathbf{M}}$ if
${\mathbf{IWT}}=2$.
On entry: the usersupplied weights.
If ${\mathbf{IWT}}=1$,
${\mathbf{WT}}\left(\mathit{i}\right)={v}_{i}$, for $\mathit{i}=1,2,\dots ,n$.
If ${\mathbf{IWT}}=2$,
${\mathbf{WT}}\left(\mathit{j}\right)={w}_{j}$, for $\mathit{j}=1,2,\dots ,m$.
Otherwise,
WT is not referenced.
Constraints:
 if ${\mathbf{IWT}}=1$, ${\mathbf{WT}}\left(\mathit{i}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{NB}}$;
 if ${\mathbf{IWT}}=2$, ${\mathbf{WT}}\left(1\right)\ne 0$ and ${\sum}_{i=1}^{m}{\mathbf{WT}}\left(i\right)>0$;
 if ${\mathbf{IWT}}=2$ and ${\mathbf{LRSD}}\ne 0$, ${\mathbf{WT}}\left(\mathit{i}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{M}}$.
 6: PN – INTEGERInput/Output
On entry:
$k$, the number of observations processed so far. On the first call to G01WAF, or when starting to summarise a new dataset,
PN must be set to
$0$.
If ${\mathbf{PN}}\ne 0$, it must be the same value as returned by the last call to G01WAF.
On exit: $k+b$, the updated number of observations processed so far.
Constraint:
${\mathbf{PN}}\ge 0$.
 7: RMEAN($\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)$) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mu}_{\mathit{l}}$, the (weighted) moving averages, for $\mathit{l}=1,2,\dots ,b+\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(0,km+1\right)$. Where ${\mu}_{l}$ is the summary to the window that ends on ${\mathbf{X}}\left(l+m\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(k,m1\right)1\right)$. Therefore, if, on entry, ${\mathbf{PN}}\ge {\mathbf{M}}1$, ${\mathbf{RMEAN}}\left(l\right)$ is the summary corresponding to the window that ends on ${\mathbf{X}}\left(l\right)$ and if, on entry, ${\mathbf{PN}}=0$, ${\mathbf{RMEAN}}\left(l\right)$ is the summary corresponding to the window that ends on ${\mathbf{X}}\left({\mathbf{M}}+l1\right)$ (or, equivalently, starts on ${\mathbf{X}}\left(l\right)$).
 8: RSD(LRSD) – REAL (KIND=nag_wp) arrayOutput
On exit: if
${\mathbf{LRSD}}\ne 0$ then
${\sigma}_{l}$, the (weighted) standard deviation. The ordering of
RSD is the same as the ordering of
RMEAN.
If
${\mathbf{LRSD}}=0$,
RSD is not referenced.
 9: LRSD – INTEGERInput
On entry: the dimension of the array
RSD as declared in the (sub)program from which G01WAF is called. If the standard deviations are not required then
LRSD should be set to zero.
Constraint:
${\mathbf{LRSD}}=0$ or ${\mathbf{LRSD}}\ge \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)$.
 10: RCOMM(LRCOMM) – REAL (KIND=nag_wp) arrayCommunication Array
On entry: communication array, used to store information between calls to G01WAF. If
${\mathbf{LRCOMM}}=0$,
RCOMM is not referenced and all the data must be supplied in one go.
 11: LRCOMM – INTEGERInput
On entry: the dimension of the array
RCOMM as declared in the (sub)program from which G01WAF is called.
Constraint:
${\mathbf{LRCOMM}}=0$ or ${\mathbf{LRCOMM}}\ge 2{\mathbf{M}}+20$.
 12: IFAIL – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this parameter you should refer to
Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
$1\text{ or}1$ is recommended. If the output of error messages is undesirable, then the value
$1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is
$0$.
When the value $\mathbf{1}\text{ 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).
6 Error Indicators and Warnings
If on entry
${\mathbf{IFAIL}}={\mathbf{0}}$ or
${{\mathbf{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}}=11$

On entry, ${\mathbf{M}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{M}}\ge 1$.
 ${\mathbf{IFAIL}}=12$

On entry,
${\mathbf{M}}=\u27e8\mathit{\text{value}}\u27e9$.
On entry at previous call,
${\mathbf{M}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: if
${\mathbf{PN}}>0$,
M must be unchanged since previous call.
 ${\mathbf{IFAIL}}=21$

On entry, ${\mathbf{NB}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{NB}}\ge 0$.
 ${\mathbf{IFAIL}}=22$

On entry, ${\mathbf{NB}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{M}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: if ${\mathbf{LRCOMM}}=0$, ${\mathbf{NB}}\ge {\mathbf{M}}$.
 ${\mathbf{IFAIL}}=41$

On entry, ${\mathbf{IWT}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{IWT}}=0$, $1$, $2$ or $3$.
 ${\mathbf{IFAIL}}=42$

On entry,
${\mathbf{IWT}}=\u27e8\mathit{\text{value}}\u27e9$.
On entry at previous call,
${\mathbf{IWT}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: if
${\mathbf{PN}}>0$,
IWT must be unchanged since previous call.
 ${\mathbf{IFAIL}}=51$

On entry, ${\mathbf{WT}}\left(\u27e8\mathit{\text{value}}\u27e9\right)=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{WT}}\left(i\right)\ge 0$.
 ${\mathbf{IFAIL}}=52$

On entry, ${\mathbf{WT}}\left(1\right)=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: if ${\mathbf{IWT}}=2$, ${\mathbf{WT}}\left(1\right)>0$.
 ${\mathbf{IFAIL}}=53$

On entry, at least one window had all zero weights.
 ${\mathbf{IFAIL}}=54$

On entry, unable to calculate at least one standard deviation due to the weights supplied.
 ${\mathbf{IFAIL}}=55$

On entry, sum of weights supplied in
WT is
$\u27e8\mathit{\text{value}}\u27e9$.
Constraint: if
${\mathbf{IWT}}=2$, the sum of the weights
$>0$.
 ${\mathbf{IFAIL}}=61$

On entry, ${\mathbf{PN}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{PN}}\ge 0$.
 ${\mathbf{IFAIL}}=62$

On entry,
${\mathbf{PN}}=\u27e8\mathit{\text{value}}\u27e9$.
On exit from previous call,
${\mathbf{PN}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: if
${\mathbf{PN}}>0$,
PN must be unchanged since previous call.
 ${\mathbf{IFAIL}}=91$

On entry, ${\mathbf{LRSD}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{LRSD}}=0$ or ${\mathbf{LRSD}}\ge \u27e8\mathit{\text{value}}\u27e9$.
 ${\mathbf{IFAIL}}=101$

RCOMM has been corrupted between calls.
 ${\mathbf{IFAIL}}=111$

On entry, ${\mathbf{LRCOMM}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{LRCOMM}}\ge \u27e8\mathit{\text{value}}\u27e9$.
On entry, ${\mathbf{LRCOMM}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{LRCOMM}}\ge \u27e8\mathit{\text{value}}\u27e9$.
 ${\mathbf{IFAIL}}=999$

Dynamic memory allocation failed.
7 Accuracy
Not applicable.
The more data that is supplied to G01WAF in one call, i.e., the larger
NB is, the more efficient the routine will be. In addition, where possible, the input parameters should be chosen so that G01WAF can use the iterative formula as described in
Section 3.
9 Example
This example calculates Spencer's $15$point moving average for the change in rate of the Earth's rotation between $1821$ and $1850$. The data is supplied in three chunks, the first consisting of five observations, the second $10$ observations and the last $15$ observations.
9.1 Program Text
Program Text (g01wafe.f90)
9.2 Program Data
Program Data (g01wafe.d)
9.3 Program Results
Program Results (g01wafe.r)
This example plot shows the smoothing effect of using different length rolling windows on the mean and standard deviation. Two different window lengths, $m=5$ and $10$, are used to produce the unweighted rolling mean and standard deviations for the change in rate of the Earth's rotation between $1821$ and $1850$.