NAG Library Function Document
nag_moving_average (g01wac)
1 Purpose
nag_moving_average (g01wac) calculates the mean and, optionally, the standard deviation using a rolling window for an arbitrary sized data stream.
2 Specification
#include <nag.h> 
#include <nagg01.h> 
void 
nag_moving_average (Integer m,
Integer nb,
const double x[],
Nag_Weightstype iwt,
const double wt[],
Integer *pn,
double rmean[],
double rsd[],
double rcomm[],
NagError *fail) 

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\}$, nag_moving_average (g01wac) calculates the mean and, optionally, the standard deviation, in a rolling window of length $m$.
For the
$i$th window 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 (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 (${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 nag_moving_average (g01wac) called multiple times.
4 References
Chan T F, Golub G H and Leveque R J (1982) Updating Formulae and a Pairwise Algorithm for Computing Sample Variances Compstat, PhysicaVerlag
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555
5 Arguments
 1:
$\mathbf{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 nag_moving_average (g01wac).
Constraint:
${\mathbf{m}}\ge 1$.
 2:
$\mathbf{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}}=\mathrm{Nag\_WeightObs}$,
wt) can vary; therefore
nb can change between calls to nag_moving_average (g01wac).
Constraints:
 ${\mathbf{nb}}\ge 0$;
 if ${\mathbf{rcomm}}\phantom{\rule{0.25em}{0ex}}\text{is}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$, ${\mathbf{nb}}\ge {\mathbf{m}}$.
 3:
$\mathbf{x}\left[{\mathbf{nb}}\right]$ – const doubleInput

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:
$\mathbf{iwt}$ – Nag_WeightstypeInput

On entry: the type of weighting to use.
 ${\mathbf{iwt}}=\mathrm{Nag\_NoWeights}$
 No weights are used.
 ${\mathbf{iwt}}=\mathrm{Nag\_WeightObs}$
 Each observation has its own weight.
 ${\mathbf{iwt}}=\mathrm{Nag\_WeightWindow}$
 Each position in the window has its own weight.
 ${\mathbf{iwt}}=\mathrm{Nag\_WeightWindowPos}$
 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 nag_moving_average (g01wac).
Constraint:
${\mathbf{iwt}}=\mathrm{Nag\_NoWeights}$, $\mathrm{Nag\_WeightObs}$, $\mathrm{Nag\_WeightWindow}$ or $\mathrm{Nag\_WeightWindowPos}$.
 5:
$\mathbf{wt}\left[\mathit{dim}\right]$ – const doubleInput

Note: the dimension,
dim, of the array
wt
must be at least
 ${\mathbf{nb}}$ when ${\mathbf{iwt}}=\mathrm{Nag\_WeightObs}$;
 ${\mathbf{m}}$ when ${\mathbf{iwt}}=\mathrm{Nag\_WeightWindow}$;
 otherwise wt may be NULL.
On entry: the usersupplied weights.
If ${\mathbf{iwt}}=\mathrm{Nag\_WeightObs}$,
${\mathbf{wt}}\left[\mathit{i}1\right]={\nu}_{\mathit{i}+k}$, for $\mathit{i}=1,2,\dots ,b$.
If ${\mathbf{iwt}}=\mathrm{Nag\_WeightWindow}$,
${\mathbf{wt}}\left[\mathit{j}1\right]={w}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
Otherwise,
wt is not referenced and may be
NULL.
Constraints:
 if ${\mathbf{iwt}}=\mathrm{Nag\_WeightObs}$, ${\mathbf{wt}}\left[\mathit{i}1\right]\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{nb}}$;
 if ${\mathbf{iwt}}=\mathrm{Nag\_WeightWindow}$, ${\mathbf{wt}}\left[0\right]\ne 0$ and ${\sum}_{\mathit{j}=1}^{m}{\mathbf{wt}}\left[\mathit{j}1\right]>0$;
 if ${\mathbf{iwt}}=\mathrm{Nag\_WeightWindow}$ and ${\mathbf{rsd}}\phantom{\rule{0.25em}{0ex}}\text{is not}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$, ${\mathbf{wt}}\left[\mathit{j}1\right]\ge 0$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
 6:
$\mathbf{pn}$ – Integer *Input/Output

On entry:
$k$, the number of observations processed so far. On the first call to nag_moving_average (g01wac), 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 nag_moving_average (g01wac).
On exit: $k+b$, the updated number of observations processed so far.
Constraint:
${\mathbf{pn}}\ge 0$.
 7:
$\mathbf{rmean}\left[\mathit{dim}\right]$ – doubleOutput

Note: the dimension,
dim, of the array
rmean
must be at least
$\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)$.
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)$. Therefore,
${\mu}_{l}$ is the mean of the data in the window that ends on
${\mathbf{x}}\left[l+m\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(k,m1\right)2\right]$.
If, on entry, ${\mathbf{pn}}\ge {\mathbf{m}}1$, i.e., at least one windows worth of data has been previously processed, then ${\mathbf{rmean}}\left[l1\right]$ is the summary corresponding to the window that ends on ${\mathbf{x}}\left[l1\right]$. On the other hand, if, on entry, ${\mathbf{pn}}=0$, i.e., no data has been previously processed, then ${\mathbf{rmean}}\left[l1\right]$ is the summary corresponding to the window that ends on ${\mathbf{x}}\left[{\mathbf{m}}+l2\right]$ (or, equivalently, starts on ${\mathbf{x}}\left[l1\right]$).
 8:
$\mathbf{rsd}\left[\mathit{dim}\right]$ – doubleOutput

Note: the dimension,
dim, of the array
rsd
must be at least
$\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)$.
If standard deviations are not required then
rsd must be
NULL.
On exit: if
${\mathbf{rsd}}\phantom{\rule{0.25em}{0ex}}\text{is not}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$ then
${\sigma}_{l}$, the (weighted) standard deviation. The ordering of
rsd is the same as the ordering of
rmean.
 9:
$\mathbf{rcomm}\left[2{\mathbf{m}}+20\right]$ – doubleCommunication Array

On entry: communication array, used to store information between calls to nag_moving_average (g01wac). If
${\mathbf{rcomm}}\phantom{\rule{0.25em}{0ex}}\text{is}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$ then
pn must be set to zero
and all the data must be supplied in one go.
 10:
$\mathbf{fail}$ – NagError *Input/Output

The NAG error argument (see
Section 3.6 in the Essential Introduction).
6 Error Indicators and Warnings
 NE_ALLOC_FAIL

Dynamic memory allocation failed.
See
Section 3.2.1.2 in the Essential Introduction for further information.
 NE_BAD_PARAM

On entry, argument $\u2329\mathit{\text{value}}\u232a$ had an illegal value.
 NE_ILLEGAL_COMM

rcomm has been corrupted between calls.
 NE_INT

On entry, ${\mathbf{m}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{m}}\ge 1$.
On entry, ${\mathbf{nb}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{nb}}\ge 0$.
On entry, ${\mathbf{nb}}=\u2329\mathit{\text{value}}\u232a$, ${\mathbf{m}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: if ${\mathbf{rcomm}}\phantom{\rule{0.25em}{0ex}}\text{is}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$, ${\mathbf{nb}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{pn}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{pn}}\ge 0$.
 NE_INTERNAL_ERROR

An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact
NAG for assistance.
An unexpected error has been triggered by this function. Please contact
NAG.
See
Section 3.6.6 in the Essential Introduction for further information.
 NE_NEG_WEIGHT

On entry, ${\mathbf{wt}}\left[\u2329\mathit{\text{value}}\u232a\right]=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{wt}}\left[i1\right]\ge 0$.
 NE_NO_LICENCE

Your licence key may have expired or may not have been installed correctly.
See
Section 3.6.5 in the Essential Introduction for further information.
 NE_PREV_CALL

if
${\mathbf{pn}}>0$,
iwt must be unchanged since previous call.
On entry,
${\mathbf{m}}=\u2329\mathit{\text{value}}\u232a$.
On entry at previous call,
${\mathbf{m}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: if
${\mathbf{pn}}>0$,
m must be unchanged since previous call.
On entry,
${\mathbf{pn}}=\u2329\mathit{\text{value}}\u232a$.
On exit from previous call,
${\mathbf{pn}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: if
${\mathbf{pn}}>0$,
pn must be unchanged since previous call.
 NE_SUM_WEIGHT

On entry, sum of weights supplied in
wt is
$\u2329\mathit{\text{value}}\u232a$.
Constraint: if
${\mathbf{iwt}}=\mathrm{Nag\_WeightWindow}$, the sum of the weights
$>0$.
 NE_WEIGHT_ZERO

On entry, ${\mathbf{wt}}\left[0\right]=\u2329\mathit{\text{value}}\u232a$.
Constraint: if ${\mathbf{iwt}}=\mathrm{Nag\_WeightWindow}$, ${\mathbf{wt}}\left[0\right]>0$.
 NW_POTENTIAL_PROBLEM

On entry, at least one window had all zero weights.
On entry, unable to calculate at least one standard deviation due to the weights supplied.
7 Accuracy
Not applicable.
8 Parallelism and Performance
nag_moving_average (g01wac) is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
nag_moving_average (g01wac) makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the
X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the
Users' Note for your implementation for any additional implementationspecific information.
The more data that is supplied to nag_moving_average (g01wac) in one call, i.e., the larger
nb is, the more efficient the function will be.
10 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.
10.1 Program Text
Program Text (g01wace.c)
10.2 Program Data
Program Data (g01wace.d)
10.3 Program Results
Program Results (g01wace.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$. The values of the rolling mean and standard deviations are plotted at the centre points of their respective windows.