# NAG FL Interfaceg01waf (moving_​average)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g01waf ( m, nb, x, iwt, wt, pn, rsd, lrsd,
 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 <nag.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)
The routine may be called by the names g01waf or nagf_stat_moving_average.

## 3Description

Given a sample of $n$ observations, denoted by $x=\left\{{x}_{i}:i=1,2,\dots ,n\right\}$ and a set of weights, $w=\left\{{w}_{j}:j=1,2,\dots ,m\right\}$, g01waf calculates the mean and, optionally, the standard deviation, in a rolling window of length $m$.
For the $i$th 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=\sum _{j=1}^{m}{w}_{j}$.
Four different types of weighting are possible:
1. (i)No weights (${w}_{j}=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 ${\mu }_{1}$ and ${\sigma }_{1}$ are obtained using the one pass algorithm of West (1979).
2. (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=\left\{{v}_{j}:j=1,2,\dots ,n\right\}$ and ${w}_{j}={v}_{i+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 ${W}_{1}=\sum _{i=1}^{m}{v}_{i}$ and ${\mu }_{1}={W}_{1}^{-1}\sum _{i=1}^{m}{v}_{i}{x}_{i}$.
If both the mean and standard deviation are required then the one pass algorithm of West (1979) is used in each window.
3. (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.
4. (iv)Each position in the window has a weight equal to its position number (${w}_{j}=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 ${S}_{1}=\sum _{i=1}^{m}{x}_{i}$ and ${\mu }_{1}=2{\left({m}^{2}+m\right)}^{-1}{S}_{1}$.
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.

## 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. ACM 22 532–555

## 5Arguments

1: $\mathbf{m}$Integer Input
On entry: $m$, the length of the rolling window.
If ${\mathbf{pn}}\ne 0$, m must be unchanged since the last call to g01waf.
Constraint: ${\mathbf{m}}\ge 1$.
2: $\mathbf{nb}$Integer Input
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 ${\mathbf{iwt}}=1$, wt) can vary;, therefore, nb can change between calls to g01waf.
Constraints:
• ${\mathbf{nb}}\ge 0$;
• if ${\mathbf{lrcomm}}=0$, ${\mathbf{nb}}\ge {\mathbf{m}}$.
3: $\mathbf{x}\left({\mathbf{nb}}\right)$Real (Kind=nag_wp) array Input
On entry: the current block of observations, corresponding to ${x}_{\mathit{i}}$, for $\mathit{i}=k+1,\dots ,k+b$, where $k$ is the number of observations processed so far and $b$ is the size of the current block of data.
4: $\mathbf{iwt}$Integer Input
On entry: the type of weighting to use.
${\mathbf{iwt}}=0$
No weights are used.
${\mathbf{iwt}}=1$
Each observation has its own weight.
${\mathbf{iwt}}=2$
Each position in the window has its own weight.
${\mathbf{iwt}}=3$
Each position in the window has a weight equal to its position number.
If ${\mathbf{pn}}\ne 0$, iwt must be unchanged since the last call to g01waf.
Constraint: ${\mathbf{iwt}}=0$, $1$, $2$ or $3$.
5: $\mathbf{wt}\left(*\right)$Real (Kind=nag_wp) array Input
Note: the dimension of the array wt must be at least ${\mathbf{nb}}$ if ${\mathbf{iwt}}=1$, ${\mathbf{m}}$ if ${\mathbf{iwt}}=2$.
On entry: the user-supplied weights.
If ${\mathbf{iwt}}=1$, ${\mathbf{wt}}\left(\mathit{i}\right)={\nu }_{\mathit{i}+k}$, for $\mathit{i}=1,2,\dots ,b$.
If ${\mathbf{iwt}}=2$, ${\mathbf{wt}}\left(\mathit{j}\right)={w}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
Constraints:
• if ${\mathbf{iwt}}=1$, ${\mathbf{wt}}\left(\mathit{i}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{nb}}$;
• if ${\mathbf{iwt}}=2$, ${\mathbf{wt}}\left(1\right)\ne 0$ and ${\sum }_{\mathit{j}=1}^{m}{\mathbf{wt}}\left(\mathit{j}\right)>0$;
• if ${\mathbf{iwt}}=2$ and ${\mathbf{lrsd}}\ne 0$, ${\mathbf{wt}}\left(\mathit{j}\right)\ge 0$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
6: $\mathbf{pn}$Integer Input/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 ${\mathbf{pn}}\ne 0$, 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: ${\mathbf{pn}}\ge 0$.
7: $\mathbf{rmean}\left(\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(0,{\mathbf{nb}}+\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(0,{\mathbf{pn}}-{\mathbf{m}}+1\right)\right)\right)$Real (Kind=nag_wp) array Output
On exit: ${\mu }_{\mathit{l}}$, the (weighted) moving averages, for $\mathit{l}=1,2,\dots ,b+\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(0,k-m+1\right)$. Therefore, ${\mu }_{l}$ is the mean of the data in the window that ends on ${\mathbf{x}}\left(l+m-\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(k,m-1\right)-1\right)$.
If, on entry, ${\mathbf{pn}}\ge {\mathbf{m}}-1$, i.e., at least one windows worth of data has been previously processed, then ${\mathbf{rmean}}\left(l\right)$ is the summary corresponding to the window that ends on ${\mathbf{x}}\left(l\right)$. On the other hand, if, on entry, ${\mathbf{pn}}=0$, i.e., no data has been previously processed, then ${\mathbf{rmean}}\left(l\right)$ is the summary corresponding to the window that ends on ${\mathbf{x}}\left({\mathbf{m}}+l-1\right)$ (or, equivalently, starts on ${\mathbf{x}}\left(l\right)$).
8: $\mathbf{rsd}\left({\mathbf{lrsd}}\right)$Real (Kind=nag_wp) array Output
On exit: if ${\mathbf{lrsd}}\ne 0$ then ${\sigma }_{l}$, the (weighted) standard deviation. The ordering of rsd is the same as the ordering of rmean.
If ${\mathbf{lrsd}}=0$, rsd is not referenced.
9: $\mathbf{lrsd}$Integer Input
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: ${\mathbf{lrsd}}=0$ or ${\mathbf{lrsd}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(0,{\mathbf{nb}}+\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(0,{\mathbf{pn}}-{\mathbf{m}}+1\right)\right)$.
10: $\mathbf{rcomm}\left({\mathbf{lrcomm}}\right)$Real (Kind=nag_wp) array Communication Array
On entry: communication array, used to store information between calls to g01waf. If ${\mathbf{lrcomm}}=0$, rcomm is not referenced and all the data must be supplied in one go.
11: $\mathbf{lrcomm}$Integer Input
On entry: the dimension of the array rcomm as declared in the (sub)program from which g01waf is called.
Constraint: ${\mathbf{lrcomm}}=0$ or ${\mathbf{lrcomm}}\ge 2{\mathbf{m}}+20$.
12: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{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:
${\mathbf{ifail}}=11$
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\ge 1$.
${\mathbf{ifail}}=12$
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
On entry at previous call, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{pn}}>0$, m must be unchanged since previous call.
${\mathbf{ifail}}=21$
On entry, ${\mathbf{nb}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nb}}\ge 0$.
${\mathbf{ifail}}=22$
On entry, ${\mathbf{nb}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{lrcomm}}=0$, ${\mathbf{nb}}\ge {\mathbf{m}}$.
${\mathbf{ifail}}=41$
On entry, ${\mathbf{iwt}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{iwt}}=0$, $1$, $2$ or $3$.
${\mathbf{ifail}}=42$
On entry, ${\mathbf{iwt}}=⟨\mathit{\text{value}}⟩$.
On entry at previous call, ${\mathbf{iwt}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{pn}}>0$, iwt must be unchanged since previous call.
${\mathbf{ifail}}=51$
On entry, ${\mathbf{wt}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{wt}}\left(i\right)\ge 0$.
${\mathbf{ifail}}=52$
On entry, ${\mathbf{wt}}\left(1\right)=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{iwt}}=2$, ${\mathbf{wt}}\left(1\right)>0$.
${\mathbf{ifail}}=53$
On entry, at least one window had all zero weights.
${\mathbf{ifail}}=54$
On entry, unable to calculate at least one standard deviation due to the weights supplied.
${\mathbf{ifail}}=55$
On entry, sum of weights supplied in wt is $⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{iwt}}=2$, the sum of the weights $>0$.
${\mathbf{ifail}}=61$
On entry, ${\mathbf{pn}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{pn}}\ge 0$.
${\mathbf{ifail}}=62$
On entry, ${\mathbf{pn}}=⟨\mathit{\text{value}}⟩$.
On exit from previous call, ${\mathbf{pn}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{pn}}>0$, pn must be unchanged since previous call.
${\mathbf{ifail}}=91$
On entry, ${\mathbf{lrsd}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lrsd}}=0$ or ${\mathbf{lrsd}}\ge ⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=101$
rcomm has been corrupted between calls.
${\mathbf{ifail}}=111$
On entry, ${\mathbf{lrcomm}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lrcomm}}\ge ⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

Not applicable.

## 8Parallelism 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.

## 9Further 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.

## 10Example

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.1Program Text

Program Text (g01wafe.f90)

### 10.2Program Data

Program Data (g01wafe.d)

### 10.3Program 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.