PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_stat_moving_average (g01wa)
Purpose
nag_stat_moving_average (g01wa) calculates the mean and, optionally, the standard deviation using a rolling window for an arbitrary sized data stream.
Syntax
[
pn,
rmean,
rsd,
rcomm,
ifail] = g01wa(
m,
x, 'nb',
nb, 'iwt',
iwt, 'wt',
wt, 'pn',
pn, 'wantsd',
wantsd, 'rcomm',
rcomm)
[
pn,
rmean,
rsd,
rcomm,
ifail] = nag_stat_moving_average(
m,
x, 'nb',
nb, 'iwt',
iwt, 'wt',
wt, 'pn',
pn, 'wantsd',
wantsd, 'rcomm',
rcomm)
Description
Given a sample of observations, denoted by and a set of weights, , nag_stat_moving_average (g01wa) 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 nag_stat_moving_average (g01wa) 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
Parameters
Compulsory Input Parameters
- 1:
– int64int32nag_int scalar
-
, the length of the rolling window.
If
,
m must be unchanged since the last call to
nag_stat_moving_average (g01wa).
Constraint:
.
- 2:
– double array
-
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.
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the array
x.
, 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
nag_stat_moving_average (g01wa).
Constraints:
- ;
- if rcomm is not provided, .
- 2:
– int64int32nag_int scalar
Default:
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
nag_stat_moving_average (g01wa).
Constraint:
, , or .
- 3:
– double array
-
The dimension of the array
wt
must be at least
if
and at least
if
The user-supplied weights.
If ,
, for .
If ,
, for .
Otherwise,
wt is not referenced.
Constraints:
- if , , for ;
- if , and ;
- if and , , for .
- 4:
– int64int32nag_int scalar
Default:
, the number of observations processed so far. On the first call to
nag_stat_moving_average (g01wa), 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 nag_stat_moving_average (g01wa).
Constraint:
.
- 5:
– logical scalar
Default:
If the standard deviations are required then
wantsd should be set to
true.
- 6:
– double array
Communication array, used to store information between calls to
nag_stat_moving_average (g01wa). If
,
rcomm is not referenced
and all the data must be supplied in one go.
Output Parameters
- 1:
– int64int32nag_int scalar
Default:
, the updated number of observations processed so far.
- 2:
– double array
-
, 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 ).
- 3:
– double array
-
If
sdp was set to
true, then
, the (weighted) standard deviation. The ordering of
rsd is the same as the ordering of
rmean.
- 4:
– double array
Communication array, used to store information between calls to nag_stat_moving_average (g01wa).
- 5:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
Cases prefixed with W are classified as warnings and
do not generate an error of type NAG:error_n. See nag_issue_warnings.
-
-
Constraint: .
-
-
Constraint: if
,
m must be unchanged since previous call.
-
-
Constraint: .
-
-
Constraint: if , .
-
-
Constraint: , , or .
-
-
Constraint: if
,
iwt must be unchanged since previous call.
-
-
Constraint: .
-
-
Constraint: if , .
- W
-
On entry, at least one window had all zero weights.
- W
-
On entry, unable to calculate at least one standard deviation due to the weights supplied.
-
-
Constraint: if , the sum of the weights .
-
-
Constraint: .
-
-
Constraint: if
,
pn must be unchanged since previous call.
-
-
Constraint: or .
-
-
rcomm has been corrupted between calls.
-
-
lrcomm is too small.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
Not applicable.
Further Comments
The more data that is supplied to
nag_stat_moving_average (g01wa) in one call, i.e., the larger
nb is, the more efficient the function will be.
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.
Open in the MATLAB editor:
g01wa_example
function g01wa_example
fprintf('g01wa example results\n\n');
m = int64([5,10,15]);
iwt = int64([0, 0, 2]);
wt{1} = [1:double(m(1))];
wt{2} = [1:double(m(2))];
wt{3} = [ -3.0; -6.0; -5.0; 3.0; 21.0; 46.0; 67.0; 74.0;
67.0; 46.0; 21.0; 3.0; -5.0; -6.0; -3.0];
data{1} = [-2170; -1770; -1660; -1360; -1100];
data{2} = [ -950; -640; -370; -140; -250;
-510; -620; -730; -880; -1130];
data{3} = [-1200; -830; -330; -190; 210;
170; 440; 440; 780; 880;
1220; 1260; 1140; 850; 640];
n = 30;
fprintf('\n Interval Mean\n');
fprintf('------------------------\n');
for im = 1:numel(m);
pn = int64(0);
x{im} = 1820 + m(im)/2 + [1:n-m(im)+1];
for b=1:3
nb = numel(data{b});
nsummaries = max(0, nb + min(0, pn-m(im)+1));
if b==1
[pn, rmean, rsd, rcomm, ifail] = ...
g01wa( ...
m(im), data{b}, 'iwt', iwt(im), 'wt', wt{im}, 'wantsd', im<3);
else
[pn, rmean, rsd, rcomm, ifail] = ...
g01wa( ...
m(im), data{b}, 'iwt', iwt(im), 'wt', wt{im}, 'pn', pn, ...
'rcomm', rcomm, 'wantsd', im<3);
end
offset = max(0, pn-nb-m(im)+1);
for i=1:nsummaries
if im==3
fprintf(' [%2d,%3d]%14.1f\n', i+offset, i+m(im)-1+offset, rmean(i));
else
y{im}(i+offset) = rmean(i);
z{im}(i+offset) = rsd(i);
end
end
end
end
fprintf('\nTotal number of observations : %d\n', pn);
fprintf('Length of window : %d\n', m(3));
g01wa_plot(x,y,z,data);
function g01wa_plot(x, y, z, data)
d = [data{1}; data{2}; data{3}];
fig1 = figure;
plot(x{1},y{1},x{2},y{2},[1821:1850],d,'*');
title({'Raw data and mean from a rolling window of', ...
'changes in rate of Earth''s rotation (μs)'});
xlabel('Year');
ylabel('Change in day length');
legend('m = 5', 'm = 10', 'Location', 'SouthEast');
set(gca,'xtick',[1820:5:1850])
set(gca,'ytick',[-2500:500:1500])
set(gca, 'PlotBoxAspectRatio', [1,1/2,1])
fig2 = figure;
plot(x{1},z{1},x{2},z{2});
title({'Standard deviation from a rolling window of', ...
'changes in rate of Earth''s rotation (μs)'});
ylabel('Standard deviation');
xlabel('Year');
set(gca,'ytick',[100:100:700])
set(gca, 'PlotBoxAspectRatio', [1,1/3,1])
g01wa example results
Interval Mean
------------------------
[ 1, 15] -427.6
[ 2, 16] -332.5
[ 3, 17] -337.1
[ 4, 18] -438.2
[ 5, 19] -604.4
[ 6, 20] -789.4
[ 7, 21] -935.4
[ 8, 22] -990.6
[ 9, 23] -927.1
[10, 24] -752.1
[11, 25] -501.3
[12, 26] -227.2
[13, 27] 23.2
[14, 28] 236.2
[15, 29] 422.4
[16, 30] 604.2
Total number of observations : 30
Length of window : 15
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.
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015