nag_moving_average (g01wac) (PDF version)
g01 Chapter Contents
g01 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_moving_average (g01wac)

 Contents

    1  Purpose
    7  Accuracy

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 = xi : i=1,2,,n  and a set of weights, w = wj : j=1,2,,m , nag_moving_average (g01wac) 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 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, Physica-Verlag
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555

5  Arguments

1:     m IntegerInput
On entry: m, the length of the rolling window.
If pn0, m must be unchanged since the last call to nag_moving_average (g01wac).
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=Nag_WeightObs, wt) can vary; therefore nb can change between calls to nag_moving_average (g01wac).
Constraints:
  • nb0;
  • if rcommisNULL, nbm.
3:     x[nb] const doubleInput
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 Nag_WeightstypeInput
On entry: the type of weighting to use.
iwt=Nag_NoWeights
No weights are used.
iwt=Nag_WeightObs
Each observation has its own weight.
iwt=Nag_WeightWindow
Each position in the window has its own weight.
iwt=Nag_WeightWindowPos
Each position in the window has a weight equal to its position number.
If pn0, iwt must be unchanged since the last call to nag_moving_average (g01wac).
Constraint: iwt=Nag_NoWeights, Nag_WeightObs, Nag_WeightWindow or Nag_WeightWindowPos.
5:     wt[dim] const doubleInput
Note: the dimension, dim, of the array wt must be at least
  • nb when iwt=Nag_WeightObs;
  • m when iwt=Nag_WeightWindow;
  • otherwise wt may be NULL.
On entry: the user-supplied weights.
If iwt=Nag_WeightObs, wt[i-1]=νi+k, for i=1,2,,b.
If iwt=Nag_WeightWindow, wt[j-1]=wj, for j=1,2,,m.
Otherwise, wt is not referenced and may be NULL.
Constraints:
  • if iwt=Nag_WeightObs, wt[i-1]0, for i=1,2,,nb;
  • if iwt=Nag_WeightWindow, wt[0]0 and j=1mwt[j-1]>0;
  • if iwt=Nag_WeightWindow and rsdis notNULL, wt[j-1]0, for j=1,2,,m.
6:     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 pn0, 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: pn0.
7:     rmean[dim] doubleOutput
Note: the dimension, dim, of the array rmean must be at least max0,nb+min0,pn-m+1.
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 x[l+m-mink,m-1-2].
If, on entry, pnm-1, i.e., at least one windows worth of data has been previously processed, then rmean[l-1] is the summary corresponding to the window that ends on x[l-1]. On the other hand, if, on entry, pn=0, i.e., no data has been previously processed, then rmean[l-1] is the summary corresponding to the window that ends on x[m+l-2] (or, equivalently, starts on x[l-1]).
8:     rsd[dim] doubleOutput
Note: the dimension, dim, of the array rsd must be at least max0,nb+min0,pn-m+1.
If standard deviations are not required then rsd must be NULL.
On exit: if rsdis notNULL then σl, the (weighted) standard deviation. The ordering of rsd is the same as the ordering of rmean.
9:     rcomm[2m+20] doubleCommunication Array
On entry: communication array, used to store information between calls to nag_moving_average (g01wac). If rcommisNULL then pn must be set to zero and all the data must be supplied in one go.
10:   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 value had an illegal value.
NE_ILLEGAL_COMM
rcomm has been corrupted between calls.
NE_INT
On entry, m=value.
Constraint: m1.
On entry, nb=value.
Constraint: nb0.
On entry, nb=value, m=value.
Constraint: if rcommisNULL, nbm.
On entry, pn=value.
Constraint: pn0.
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, wt[value]=value.
Constraint: wt[i-1]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 pn>0, iwt must be unchanged since previous call.
On entry, m=value.
On entry at previous call, m=value.
Constraint: if pn>0, m must be unchanged since previous call.
On entry, pn=value.
On exit from previous call, pn=value.
Constraint: if pn>0, pn must be unchanged since previous call.
NE_SUM_WEIGHT
On entry, sum of weights supplied in wt is value.
Constraint: if iwt=Nag_WeightWindow, the sum of the weights >0.
NE_WEIGHT_ZERO
On entry, wt[0]=value.
Constraint: if iwt=Nag_WeightWindow, wt[0]>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 implementation-specific information.

9  Further Comments

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.
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

nag_moving_average (g01wac) (PDF version)
g01 Chapter Contents
g01 Chapter Introduction
NAG Library Manual

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