hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

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 nn observations, denoted by x = {xi : i = 1,2,,n} x = { xi : i=1,2,,n }  and a set of weights, w = {wj : j = 1,2,,m} w = { wj : j=1,2,,m } , nag_stat_moving_average (g01wa) calculates the mean and, optionally, the standard deviation, in a rolling window of length mm.
For the iith window the mean is defined as
μi = ( j = 1m wj xi + j1 )/W
μi = j=1 m wj xi+j-1 W
(1)
and the standard deviation as
σi = sqrt( ( j = 1m wj (xi + j1μi)2 )/( W ( j = 1m wj2 )/W ) )
σi = j=1 m wj ( xi+j-1 - μi ) 2 W - j=1 m wj2 W
(2)
with W = j = 1m wj W = j=1 m wj .
Four different types of weighting are possible:
(i) No weights (wj = 1wj=1)
When no weights are required both the mean and standard deviations can be calculated in an iterative manner, with
μi + 1 = μi + ((xi + mxi))/m
σi + 12 = (m1) σi2 + (xi + mμi)2 (xiμi)2 ((xi + mxi)2)/m
μ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μ1 and σ1σ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 mm weights a vector of nn weights is supplied instead, v = {vj : j = 1,2,,n}v={vj:j=1,2,,n} and wj = vi + j1wj=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 + mvi)
μi + 1 = μi + Wi1 (vi + mxi + mvixi)
Wi+1= Wi+ ( vi+m - vi ) μi+1= μi + Wi-1 ( vi+m xi+m - vi xi )
where W1 = i = 1m vi W1= i=1 m vi  and μ1 = W11 i = 1m vi xi μ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 = jwj=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 + mxi)
μi + 1 = μi + ( 2 (mxi + mSi) )/( m (m + 1) )
Si+1= Si+ ( xi+m - xi ) μi+1= μi + 2 ( m xi+m - Si ) m (m+1)
where S1 = i = 1m xi S1= i=1 m xi  and μ1 = 2 (m2 + m)1 S1 μ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, xx (and if each observation has its own weight, vv) 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:     m – int64int32nag_int scalar
mm, the length of the rolling window.
If pn0pn0, m must be unchanged since the last call to nag_stat_moving_average (g01wa).
Constraint: m1m1.
2:     x(nb) – double array
nb, the dimension of the array, must satisfy the constraint
  • nb0nb0
  • if rcomm is not provided, nbmnbm
  • .
    The current block of observations, corresponding to xixi, for i = k + 1,,k + bi=k+1,,k+b, where kk is the number of observations processed so far and bb is the size of the current block of data.

    Optional Input Parameters

    1:     nb – int64int32nag_int scalar
    Default: The dimension of the array x.
    bb, the number of observations in the current block of data. The size of the block of data supplied in x (and when iwt = 1iwt=1, wt) can vary; therefore nb can change between calls to nag_stat_moving_average (g01wa).
    Constraints:
    • nb0nb0;
    • if rcomm is not provided, nbmnbm.
    2:     iwt – int64int32nag_int scalar
    The type of weighting to use.
    iwt = 0iwt=0
    No weights are used.
    iwt = 1iwt=1
    Each observation has its own weight.
    iwt = 2iwt=2
    Each position in the window has its own weight.
    iwt = 3iwt=3
    Each position in the window has a weight equal to its position number.
    If pn0pn0, iwt must be unchanged since the last call to nag_stat_moving_average (g01wa).
    Default: 00
    Constraint: iwt = 0iwt=0, 11, 22 or 33.
    3:     wt( : :) – double array
    Note: the dimension of the array wt must be at least nbnb if iwt = 1iwt=1 and at least mm if iwt = 2iwt=2.
    The user-supplied weights.
    If iwt = 1iwt=1, wt(i) = νi + kwti=νi+k, for i = 1,2,,bi=1,2,,b.
    If iwt = 2iwt=2, wt(j) = wjwtj=wj, for j = 1,2,,mj=1,2,,m.
    Otherwise, wt is not referenced.
    Constraints:
    • if iwt = 1iwt=1, wt(i)0wti0, for i = 1,2,,nbi=1,2,,nb;
    • if iwt = 2iwt=2, wt(1)0wt10 and j = 1mwt(j) > 0j=1mwtj>0;
    • if iwt = 2iwt=2 and lrsd0lrsd0, wt(j)0wtj0, for j = 1,2,,mj=1,2,,m.
    4:     pn – int64int32nag_int scalar
    kk, 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 00.
    If pn0pn0, it must be the same value as returned by the last call to nag_stat_moving_average (g01wa).
    Default: 00
    Constraint: pn0pn0.
    5:     wantsd – logical scalar
    If the standard deviations are required then wantsd should be set to true.
    Default: falsefalse
    6:     rcomm(2m + 202m+20) – double array
    Communication array, used to store information between calls to nag_stat_moving_average (g01wa). If lrcomm = 0lrcomm=0, rcomm is not referenced and all the data must be supplied in one go.

    Input Parameters Omitted from the MATLAB Interface

    None.

    Output Parameters

    1:     pn – int64int32nag_int scalar
    Default: 00
    k + bk+b, the updated number of observations processed so far.
    2:     rmean(max (0,nb + min (0,pnm + 1))max(0,nb+min(0,pn-m+1))) – double array
    μlμl, the (weighted) moving averages, for l = 1,2,,b + min (0,km + 1)l=1,2,,b+min(0,k-m+1). Therefore, μlμl is the mean of the data in the window that ends on x(l + mmin (k,m1)1)xl+m-min(k,m-1)-1.
    If, on entry, pnm1pnm-1, i.e., at least one windows worth of data has been previously processed, then rmean(l)rmeanl is the summary corresponding to the window that ends on x(l)xl. On the other hand, if, on entry, pn = 0pn=0, i.e., no data has been previously processed, then rmean(l)rmeanl is the summary corresponding to the window that ends on x(m + l1)xm+l-1 (or, equivalently, starts on x(l)xl).
    3:     rsd(lrsd) – double array
    If sdp was set to true, then σlσl, the (weighted) standard deviation. The ordering of rsd is the same as the ordering of rmean.
    4:     rcomm(2m + 202m+20) – double array
    Communication array, used to store information between calls to nag_stat_moving_average (g01wa).
    5:     ifail – int64int32nag_int scalar
    ifail = 0ifail=0 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.

      ifail = 11ifail=11
    Constraint: m1m1.
      ifail = 12ifail=12
    Constraint: if pn > 0pn>0, m must be unchanged since previous call.
      ifail = 21ifail=21
    Constraint: nb0nb0.
      ifail = 22ifail=22
    Constraint: if lrcomm = 0lrcomm=0, nbmnbm.
      ifail = 41ifail=41
    Constraint: iwt = 0iwt=0, 11, 22 or 33.
      ifail = 42ifail=42
    Constraint: if pn > 0pn>0, iwt must be unchanged since previous call.
      ifail = 51ifail=51
    Constraint: wt(i)0wti0.
      ifail = 52ifail=52
    Constraint: if iwt = 2iwt=2, wt(1) > 0wt1>0.
    W ifail = 53ifail=53
    On entry, at least one window had all zero weights.
    W ifail = 54ifail=54
    On entry, unable to calculate at least one standard deviation due to the weights supplied.
      ifail = 55ifail=55
    Constraint: if iwt = 2iwt=2, the sum of the weights > 0>0.
      ifail = 61ifail=61
    Constraint: pn0pn0.
      ifail = 62ifail=62
    Constraint: if pn > 0pn>0, pn must be unchanged since previous call.
      ifail = 91ifail=91
    Constraint: lrsd = 0lrsd=0 or .
      ifail = 101ifail=101
    rcomm has been corrupted between calls.
      ifail = 111ifail=111
    lrcomm is too small. lrcomm is too small.
      ifail = 999ifail=-999
    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

    function nag_stat_moving_average_example
    wt  = [-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];
    iwt = int64(2);
    m   = int64(15);
    x1 = [-2170; -1770; -1660; -1360; -1100];
    x2 = [-950; -640; -370; -140; -250; -510; -620; -730; -880; -1130];
    x3 = [-1200; -830; -330; -190; 210; 170; 440; 440; 780; 880; 1220; 1260; ...
           1140; 850; 640];
    data = {x1; x2; x3};
    
    
    % Initialise the number of valid observations processed so far
    pn = int64(0);
    
    fprintf('\n Interval          Mean\n');
    fprintf('------------------------\n');
    for b=1:3
      % Calculate the number of summaries we can produce
      nb = numel(data{b});
      nsummaries = max(0, nb + min(0, pn-m+1));
    
      % Calculate summary statistics for this block of data
      if b==1
        [pn, rmean, rsd, rcomm, ifail] = ...
          nag_stat_moving_average(m, data{b}, 'iwt', iwt, 'wt', wt);
      else
        [pn, rmean, rsd, rcomm, ifail] =  ...
          nag_stat_moving_average(m, data{b}, 'iwt', iwt, 'wt', wt, 'pn', pn, 'rcomm', rcomm);
      end
    
      % Number of results printed so far
      offset = max(0, pn-nb-m+1);
    
      % Display the results for this block of data
      for i=1:nsummaries
        fprintf(' [%2d, %2d]    %10.1f\n', i+offset, i+m-1+offset, rmean(i));
      end
    end
    
    fprintf('\nTotal number of observations : %d\n', pn);
    fprintf('Length of window             : %d\n', m);
    
     
    
     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
    
    
    function g01wa_example
    wt  = [-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];
    iwt = int64(2);
    m   = int64(15);
    x1 = [-2170; -1770; -1660; -1360; -1100];
    x2 = [-950; -640; -370; -140; -250; -510; -620; -730; -880; -1130];
    x3 = [-1200; -830; -330; -190; 210; 170; 440; 440; 780; 880; 1220; 1260; ...
           1140; 850; 640];
    data = {x1; x2; x3};
    
    
    % Initialise the number of valid observations processed so far
    pn = int64(0);
    
    fprintf('\n Interval          Mean\n');
    fprintf('------------------------\n');
    for b=1:3
      % Calculate the number of summaries we can produce
      nb = numel(data{b});
      nsummaries = max(0, nb + min(0, pn-m+1));
    
      % Calculate summary statistics for this block of data
      if b==1
        [pn, rmean, rsd, rcomm, ifail] = ...
          g01wa(m, data{b}, 'iwt', iwt, 'wt', wt);
      else
        [pn, rmean, rsd, rcomm, ifail] =  ...
          g01wa(m, data{b}, 'iwt', iwt, 'wt', wt, 'pn', pn, 'rcomm', rcomm);
      end
    
      % Number of results printed so far
      offset = max(0, pn-nb-m+1);
    
      % Display the results for this block of data
      for i=1:nsummaries
        fprintf(' [%2d, %2d]    %10.1f\n', i+offset, i+m-1+offset, rmean(i));
      end
    end
    
    fprintf('\nTotal number of observations : %d\n', pn);
    fprintf('Length of window             : %d\n', m);
    
     
    
     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
    
    

    PDF version (NAG web site, 64-bit version, 64-bit version)
    Chapter Contents
    Chapter Introduction
    NAG Toolbox

    © The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013