The function may be called by the names: g01wac, nag_stat_moving_average or nag_moving_average.
3Description
Given a sample of observations, denoted by and a set of weights, , g01wac calculates the mean and, optionally, the standard deviation, in a rolling window of length .
For the th window the mean is defined as
(1)
and the standard deviation as
(2)
with
.
Four different types of weighting are possible:
(i)No weights ()
When no weights are required both the mean and standard deviations can be calculated in an iterative manner, with
where the initial values and 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 weights a vector of weights is supplied instead, and in (1) and (2).
If the standard deviations are not required then the mean is calculated using the iterative formula:
where
and
.
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 ()
If the standard deviations are not required then the mean is calculated using the iterative formula:
where
and
.
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, (and if each observation has its own weight, ) can be split into arbitrary sized blocks and g01wac called multiple times.
4References
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. ACM22 532–555
5Arguments
1: – IntegerInput
On entry: , the length of the rolling window.
If , m must be unchanged since the last call to g01wac.
Constraint:
.
2: – IntegerInput
On entry: , the number of observations in the current block of data. The size of the block of data supplied in x (and when , wt) can vary;, therefore, nb can change between calls to g01wac.
Constraints:
;
if , .
3: – const doubleInput
On entry: the current block of observations, corresponding to
, for , where is the number of observations processed so far and is the size of the current block of data.
4: – Nag_WeightstypeInput
On entry: the type of weighting to use.
No weights are used.
Each observation has its own weight.
Each position in the window has its own weight.
Each position in the window has a weight equal to its position number.
If , iwt must be unchanged since the last call to g01wac.
Constraint:
, , or .
5: – const doubleInput
Note: the dimension, dim, of the array wt
must be at least
, when ;
, when ;
otherwise is not referenced and may be NULL.
On entry: the user-supplied weights.
If ,
, for .
If ,
, for .
Constraints:
if , , for ;
if , and ;
if and , , for .
6: – Integer *Input/Output
On entry: , the number of observations processed so far. On the first call to g01wac, or when starting to summarise a new dataset, pn must be set to .
If , it must be the same value as returned by the last call to g01wac.
On exit: , the updated number of observations processed so far.
Constraint:
.
7: – doubleOutput
Note: the dimension, dim, of the array rmean
must be at least
.
On exit: , the (weighted) moving averages, for . Therefore, is the mean of the data in the window that ends on .
If, on entry, , i.e., at least one windows worth of data has been previously processed, then is the summary corresponding to the window that ends on . On the other hand, if, on entry, , i.e., no data has been previously processed, then is the summary corresponding to the window that ends on (or, equivalently, starts on ).
8: – doubleOutput
Note: the dimension, dim, of the array rsd
must be at least
, when ;
otherwise is not referenced and may be NULL.
if standard deviations are not required then rsd must be NULL.
On exit: if on entry then , the (weighted) standard deviation. The ordering of rsd is the same as the ordering of rmean.
9: – doubleCommunication Array
Note: the dimension, dim, of the array rcomm
must be at least
, when ;
, otherwise.
On entry: communication array, used to store information between calls to g01wac. If then pn must be set to zero and all the data must be supplied in one go.
10: – NagError *Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).
6Error Indicators and Warnings
NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
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.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NEG_WEIGHT
On entry, .
Constraint: .
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
On entry, .
On entry at previous call, .
Constraint: if , m must be unchanged since previous call.
On entry, .
On exit from previous call, .
Constraint: if , pn must be unchanged since previous call.
NE_SUM_WEIGHT
On entry, sum of weights supplied in wt is .
Constraint: if , the sum of the weights .
NE_WEIGHT_ZERO
On entry, .
Constraint: if , .
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.
7Accuracy
Not applicable.
8Parallelism and Performance
Background information to multithreading can be found in the Multithreading documentation.
g01wac is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
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 implementation-specific information.
9Further Comments
The more data that is supplied to g01wac in one call, i.e., the larger nb is, the more efficient the function will be.
10Example
This example calculates Spencer's -point moving average for the change in rate of the Earth's rotation between and . The data is supplied in three chunks, the first consisting of five observations, the second observations and the last observations.
This example plot shows the smoothing effect of using different length rolling windows on the mean and standard deviation. Two different window lengths, and , are used to produce the unweighted rolling mean and standard deviations for the change in rate of the Earth's rotation between and . The values of the rolling mean and standard deviations are plotted at the centre points of their respective windows.