G01WAF (PDF version)
G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G01WAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

G01WAF calculates the mean and, optionally, the standard deviation using a rolling window for an arbitrary sized 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,PN-M+1))), RSD(LRSD), RCOMM(LRCOMM)

3  Description

Given a sample of n observations, denoted by x = xi : i=1,2,,n  and a set of weights, w = wj : j=1,2,,m , G01WAF calculates the mean and, optionally, the standard deviation, in a rolling window of length m.
For the ith window the mean is defined as
μi = j=1 m wj xi+j-1 W (1)
and the standard deviation as
σi = j=1 m wj xi+j-1 - μi 2 W - j=1 m wj2 W (2)
with W = j=1 m wj .
Four different types of weighting are possible:
(i) No weights (wj=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  
where the initial values μ1 and σ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=vj:j=1,2,,n and wj=vi+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+m xi+m - vi xi  
where W1= i=1 m vi  and μ1 = W1-1 i=1 m vi xi .
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)
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 m xi+m - Si m m+1  
where S1= i=1 m xi  and μ1 = 2 m2 + m -1 S1 .
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

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  Parameters

1:     M – INTEGERInput
On entry: m, the length of the rolling window.
If PN0, M must be unchanged since the last call to G01WAF.
Constraint: M1.
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 IWT=1, WT) can vary; therefore NB can change between calls to G01WAF.
Constraints:
  • NB0;
  • if LRCOMM=0, NBM.
3:     XNB – REAL (KIND=nag_wp) arrayInput
On entry: the current block of observations, corresponding to xi, for i=k+1,,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.
IWT=0
No weights are used.
IWT=1
Each observation has its own weight.
IWT=2
Each position in the window has its own weight.
IWT=3
Each position in the window has a weight equal to its position number.
If PN0, IWT must be unchanged since the last call to G01WAF.
Constraint: IWT=0, 1, 2 or 3.
5:     WT* – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least NB if IWT=1 and at least M if IWT=2.
On entry: the user-supplied weights.
If IWT=1, WTi=νi+k, for i=1,2,,b.
If IWT=2, WTj=wj, for j=1,2,,m.
Otherwise, WT is not referenced.
Constraints:
  • if IWT=1, WTi0, for i=1,2,,NB;
  • if IWT=2, WT10 and j=1mWTj>0;
  • if IWT=2 and LRSD0, WTj0, for j=1,2,,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 PN0, 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: PN0.
7:     RMEANmax0,NB+min0,PN-M+1 – REAL (KIND=nag_wp) arrayOutput
On exit: μl, the (weighted) moving averages, for l=1,2,,b+min0,k-m+1. Therefore, μl is the mean of the data in the window that ends on Xl+m-mink,m-1-1.
If, on entry, PNM-1, i.e., at least one windows worth of data has been previously processed, then RMEANl is the summary corresponding to the window that ends on Xl. On the other hand, if, on entry, PN=0, i.e., no data has been previously processed, then RMEANl is the summary corresponding to the window that ends on XM+l-1 (or, equivalently, starts on Xl).
8:     RSDLRSD – REAL (KIND=nag_wp) arrayOutput
On exit: if LRSD0 then σl, the (weighted) standard deviation. The ordering of RSD is the same as the ordering of RMEAN.
If 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: LRSD=0 or LRSDmax0,NB+min0,PN-M+1.
10:   RCOMMLRCOMM – REAL (KIND=nag_wp) arrayCommunication Array
On entry: communication array, used to store information between calls to G01WAF. If 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: LRCOMM=0 or LRCOMM2M+20.
12:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ 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​ 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 -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
IFAIL=11
On entry, M=value.
Constraint: M1.
IFAIL=12
On entry, M=value.
On entry at previous call, M=value.
Constraint: if PN>0, M must be unchanged since previous call.
IFAIL=21
On entry, NB=value.
Constraint: NB0.
IFAIL=22
On entry, NB=value, M=value.
Constraint: if LRCOMM=0, NBM.
IFAIL=41
On entry, IWT=value.
Constraint: IWT=0, 1, 2 or 3.
IFAIL=42
On entry, IWT=value.
On entry at previous call, IWT=value.
Constraint: if PN>0, IWT must be unchanged since previous call.
IFAIL=51
On entry, WTvalue=value.
Constraint: WTi0.
IFAIL=52
On entry, WT1=value.
Constraint: if IWT=2, WT1>0.
IFAIL=53
On entry, at least one window had all zero weights.
IFAIL=54
On entry, unable to calculate at least one standard deviation due to the weights supplied.
IFAIL=55
On entry, sum of weights supplied in WT is value.
Constraint: if IWT=2, the sum of the weights >0.
IFAIL=61
On entry, PN=value.
Constraint: PN0.
IFAIL=62
On entry, PN=value.
On exit from previous call, PN=value.
Constraint: if PN>0, PN must be unchanged since previous call.
IFAIL=91
On entry, LRSD=value.
Constraint: LRSD=0 or LRSDvalue.
IFAIL=101
RCOMM has been corrupted between calls.
IFAIL=111
On entry, LRCOMM=value.
Constraint: LRCOMMvalue.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction 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.

9  Further Comments

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 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 (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, 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.
GnuplotProduced by GNUPLOT 4.6 patchlevel 3 100 200 300 400 500 600 700 1820 1825 1830 1835 1840 1845 1850 Standard Deviation Year gnuplot_plot_1a gnuplot_plot_2a −2500 −2000 −1500 −1000 −500 0 500 1000 1500 Change in Day Length Example Program Raw data, mean and standard deviation from a rolling window of changes in rate of Earth's rotation (μs), 1821 to 1850 gnuplot_plot_1b window length (m) = 10 gnuplot_plot_2b window length (m) = 5 gnuplot_plot_3b

G01WAF (PDF version)
G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015