NAG Library Routine Document
g01waf
(moving_average)
1
Purpose
g01waf calculates the mean and, optionally, the standard deviation using a rolling window for an arbitrary sized data stream.
2
Specification
Fortran Interface
Subroutine g01waf ( |
m,
nb,
x,
iwt,
wt,
pn,
rmean,
rsd,
lrsd,
rcomm,
lrcomm,
ifail) |
Integer, Intent (In) | :: |
m,
nb,
iwt,
lrsd,
lrcomm | Integer, Intent (Inout) | :: |
pn,
ifail | Real (Kind=nag_wp), Intent (In) | :: |
x(nb),
wt(*) | Real (Kind=nag_wp), Intent (Inout) | :: |
rcomm(lrcomm) | Real (Kind=nag_wp), Intent (Out) | :: |
rmean(max(0,nb+min(0,pn-m+1))),
rsd(lrsd) |
|
C Header Interface
#include nagmk26.h
void |
g01waf_ (
const Integer *m,
const Integer *nb,
const double x[],
const Integer *iwt,
const double wt[],
Integer *pn,
double rmean[],
double rsd[],
const Integer *lrsd,
double rcomm[],
const Integer *lrcomm,
Integer *ifail) |
|
3
Description
Given a sample of observations, denoted by and a set of weights, , g01waf calculates the mean and, optionally, the standard deviation, in a rolling window of length .
For the
th window the mean is defined as
and the standard deviation as
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 ()
This is a special case of (iii).
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 g01waf 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, Physica-Verlag
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555
5
Arguments
- 1: – IntegerInput
-
On entry:
, the length of the rolling window.
If
,
m must be unchanged since the last call to
g01waf.
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
g01waf.
- 3: – Real (Kind=nag_wp) arrayInput
-
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: – IntegerInput
-
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
g01waf.
Constraint:
, , or .
- 5: – Real (Kind=nag_wp) arrayInput
-
Note: the dimension of the array
wt
must be at least
if
and at least
if
.
On entry: the user-supplied weights.
If ,
, for .
If ,
, for .
Otherwise,
wt is not referenced.
Constraints:
- if , , for ;
- if , and ;
- if and , , for .
- 6: – IntegerInput/Output
-
On entry:
, 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
.
If , it must be the same value as returned by the last call to g01waf.
On exit: , the updated number of observations processed so far.
Constraint:
.
- 7: – Real (Kind=nag_wp) arrayOutput
-
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: – Real (Kind=nag_wp) arrayOutput
-
On exit: if
then
, the (weighted) standard deviation. The ordering of
rsd is the same as the ordering of
rmean.
If
,
rsd is not referenced.
- 9: – 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:
or .
- 10: – Real (Kind=nag_wp) arrayCommunication Array
-
On entry: communication array, used to store information between calls to
g01waf. If
,
rcomm is not referenced
and all the data must be supplied in one go.
- 11: – IntegerInput
-
On entry: the dimension of the array
rcomm as declared in the (sub)program from which
g01waf is called.
Constraint:
or .
- 12: – IntegerInput/Output
-
On entry:
ifail must be set to
,
. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this argument, the recommended value is
.
When the value is used it is essential to test the value of ifail on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6
Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Errors or warnings detected by the routine:
-
On entry, .
Constraint: .
-
On entry,
.
On entry at previous call,
.
Constraint: if
,
m must be unchanged since previous call.
-
On entry, .
Constraint: .
-
On entry, , .
Constraint: if , .
-
On entry, .
Constraint: , , or .
-
On entry,
.
On entry at previous call,
.
Constraint: if
,
iwt must be unchanged since previous call.
-
On entry, .
Constraint: .
-
On entry, .
Constraint: if , .
-
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.
-
On entry, sum of weights supplied in
wt is
.
Constraint: if
, the sum of the weights
.
-
On entry, .
Constraint: .
-
On entry,
.
On exit from previous call,
.
Constraint: if
,
pn must be unchanged since previous call.
-
On entry, .
Constraint: or .
-
rcomm has been corrupted between calls.
-
On entry, .
Constraint: .
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
7
Accuracy
Not applicable.
8
Parallelism and Performance
g01waf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g01waf 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 routine. Please also consult the
Users' Note for your implementation for any additional implementation-specific information.
The more data that is supplied to
g01waf in one call, i.e., the larger
nb is, the more efficient the routine will be.
10
Example
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.
10.1
Program Text
Program Text (g01wafe.f90)
10.2
Program Data
Program Data (g01wafe.d)
10.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, 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.