naginterfaces.library.stat.moving_​average

naginterfaces.library.stat.moving_average(m, x, iwt=0, wt=None, pn=0, wantsd=False, comm=None)[source]

moving_average calculates the mean and, optionally, the standard deviation using a rolling window for an arbitrary sized data stream.

For full information please refer to the NAG Library document for g01wa

https://www.nag.com/numeric/nl/nagdoc_28.4/flhtml/g01/g01waf.html

Parameters
mint

, the length of the rolling window.

If , must be unchanged since the last call to moving_average.

xfloat, array-like, shape

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.

iwtint, optional

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 , must be unchanged since the last call to moving_average.

wtNone or float, array-like, shape , optional

Note: the required length for this argument is determined as follows: if : ; if : ; otherwise: .

The user-supplied weights.

If , , for .

If , , for .

pnint, optional

, the number of observations processed so far. On the first call to moving_average, or when starting to summarise a new dataset, must be set to .

If , it must be the same value as returned by the last call to moving_average.

wantsdbool, optional

If the standard deviations are required then should be set to .

commNone or dict, communication object, optional, modified in place

Communication structure.

If all the data must be supplied in one go, otherwise need not be set.

Returns
pnint

, the updated number of observations processed so far.

rmeanfloat, ndarray, shape

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

rsdNone or float, ndarray, shape

If then , the (weighted) standard deviation. The ordering of is the same as the ordering of .

Raises
NagValueError
(errno )

On entry, .

Constraint: .

(errno )

On entry, .

On entry at previous call, .

Constraint: if , must be unchanged since previous call.

(errno )

On entry, .

Constraint: .

(errno )

On entry, , .

Constraint: if is None, .

(errno )

On entry, .

Constraint: , , or .

(errno )

On entry, .

On entry at previous call, .

Constraint: if , must be unchanged since previous call.

(errno )

On entry, .

Constraint: .

(errno )

On entry, .

Constraint: if , .

(errno )

On entry, sum of weights supplied in is .

Constraint: if , the sum of the weights .

(errno )

On entry, .

Constraint: .

(errno )

On entry, .

On exit from previous call, .

Constraint: if , must be unchanged since previous call.

(errno )

[‘rcomm’] has been corrupted between calls.

Warns
NagAlgorithmicWarning
(errno )

On entry, at least one window had all zero weights.

(errno )

On entry, unable to calculate at least one standard deviation due to the weights supplied.

Notes

Given a sample of observations, denoted by and a set of weights, , moving_average 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:

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

  2. 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 [equation] and [equation].

    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.

  3. Each position in the window has its own weight

    This is the case as described in [equation] and [equation], 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.

  4. 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 moving_average called multiple times.

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