Calculates and plots the moving average of a time series (R.P. Littlejohn, G. Tunnicliffe Wilson & D.B. Baird).
|What to print (
||Number of samples used to calculate each moving average|
||How to calculate the averages (
||Order for polynomial smoothing (0, 1, 2, 3, 4); default 0 i.e. ordinary moving-averages calculated from means|
||Whether to trim transients with
||What to plot (
||Allows the smoothing parameter for the contribution of the last value in the series to the moving average to be specified for the exponential or Holt-Winters methods|
||Allows the smoothing parameter for the trend to be specified for the Holt-Winters method|
||Allows the smoothing parameter for the seasonal component to be specified for the Holt-Winters method|
||Controls whether the seasonal component is multiplicative in the Holt-Winters method (
||Number of predicted values to form for the Holt-Winters method; default is twice the number of levels of the
||Time series whose moving averages are required|
||Saves the moving averages for the defined
||Title for the graph|
||Factor for seasonal adjustment|
||Saves results from the Holt-Winters method or from seasonal adjustment|
MOVINGAVERAGE calculates and plots an unweighted or exponentially weighted moving average of a time series, or uses
TFILTER with two-sided ARIMA smoothing of transients. This allows you to smooth out short-term volatility and assess longer-term trends or cycles in the data.
The method of averaging is specified by the
METHOD option, with settings:
||takes an unweighted average of past values (default);|
||takes an average centred on the current value with the first and last values receiving weights of 0.5 when
||takes an exponentially weighted average of past values;|
||uses the Holt-Winters method; see Holt (1957) and Winters (1960).|
The time series is specified, in a variate, using the
SERIES parameter. The moving averages can be saved using the
MASERIES parameter. They are saved in a pointer with a suffix for each setting of the
SEASONAL factor can specify a factor to perform a seasonal adjustment of the moving average. The residuals (the observed values minus the moving average) are calculated and averaged for each level of the factor. These averages for each level are then subtracted from the corresponding units of the moving average, so that the mean residual for each level is now zero.
NSAMPLES option specifies the number of data points that are used to calculate the moving average. When
METHOD=exponential the weighting parameter can be specified by the
ALPHA option. If this is unset, the default is calculated as
α = 2 / (
NSAMPLES + 1).
filter method, most of the weight is spread over an
NSAMPLES range centred upon each point. Outside this range, the weights go slightly negative before dying away.
ORDER option can be used to request polynomial smoothing. The default is
ORDER=0, which gives ordinary moving averages calculated from the means of the defined range of values. Alternatively, you can set
ORDER to values in the range 1-4 to calculate the averages by fitting polynomials of those orders to the data values in the defined range.
NSAMPLES should then be an integer greater than the requested
ORDER. With these
METHOD settings, the transients at either end of the series are trimmed by default, but they can be evaluated by setting option
The Holt-Winters method uses a weighted average of past estimates of the level and trend in estimating the current value. The smoothing parameters can be specified by the
BETA options. These control the balance between past and current contributions to the estimates of the level and trend respectively. They must take a value between 0 and 1, and the closer they are to zero, the less the current value contributes to the estimate, giving greater smoothing of the series. When the
SEASONAL parameter is set, the Holt-Winters moving average uses a weighted estimate of the seasonal effects. The smoothing parameter that controls the balance between past and current contributions to the seasonal effects can be specified by the
GAMMA option. If the parameters are not specified, their values are estimated by minimizing the prediction sums of squares. You can set option
PRINT=parameters to print the values of the parameters. By default, the seasonal component is used in an additive model
estimate = level + trend + season
but you can set option
MULTIPLICATIVE=yes to use a multiplicative model
estimate = (level + trend) × season.
NPREDICTIONS option specifies the number of predicted points to form at the end of the original series. The default is twice the number of levels of the
SEASONAL factor, or two if
SEASONAL is not set.
The graphs that are produced by
MOVINGAVERAGE are controlled by the
PLOT option, with settings:
||to plot the separate components (trend, level and season) of the estimate from a Holt-Winters or a seasonal model,|
||to plot the moving averages, together with the original series, and|
||to plot the predicted values with 95% confidence limits at the end of the series for a Holt-Winters model.|
By default nothing is plotted. The
TITLE parameter can supply a title for the graphs. The default is to construct a title automatically from the name of the series variate and the type of moving average.
SAVE parameter allows you to save a pointer with results from the Holt-Winters method or from seasonal adjustment. For Holt-Winters, the pointer has elements
SEASONAL is set),
'Parameters' and, if
NPREDICTIONS is greater than zero,
'Upper'. With other seasonal adjustment models, it has elements for
'Season'. However, if polynomial smoothing is being used, the pointer has two levels of suffix, with these at the second level, and the polynomial components as the first level.
The procedure uses
FIT to do polynomial smoothing. The
filter setting uses an ARIMA model with forward and backward filtering, to implement a Wiener signal extraction procedure in a similar manner to the example of the
TFILTER directive illustrated in Example 7.6.1b of the Guide to the Genstat Command Language, Part 2 Statistics.
The filter is designed to estimate a regularly sampled continuous time integrated random walk (the signal), to which random noise has been added. At the sampled points the signal follows an ARIMA(0,2,1) process, and the observations follow an ARIMA(0,2,2) process. The parameters of these processes depend on the signal to noise ratio, which is imputed from the specified value of
NSAMPLES. An important aspect of the implementation is that the assumed stochastic properties of the signal allow one to reduce the end effect (transients) by implicit forecasting and backforecasting, to get a fully optimal filter for the finite set of observations. The result is, in fact, exactly equivalent to fitting a cubic smoothing spline with knots at each observation point. Example 7.6.1b in the Guide implements a similar filter, but for a signal that follows the more simple random walk process, and the result is a two-sided exponential smoother that is equivalent to a linear smoothing spline. To be effective, the assumed model does not have to be correct. The procedure just removes high frequency variations with relatively small distortion of the lower frequency variations, the cut off between high and low frequencies being determined by the setting of
For the Holt-Winters model, the prediction sum of squares is minimized by
FITNONLINEAR using functions in
CurveFuncs.dll, if this is available in the version of Genstat that is being used. Otherwise, or if
FITNONLINEAR does not find a solution, it is minimized by the
MINIMIZE procedure. The calculations are then done by procedures
_HWMFUNCTION, that are subsidiary procedures of
Restrictions are not permitted.
Holt, C.C. (1957). Forecasting trends and seasonals by exponentially weighted moving averages. ONR Research Memorandum, 52.
Winters, P.R. (1960). Forecasting sales by exponentially weighted moving averages. Management Science, 6, 324–342.
Commands for: Time series.
CAPTION 'MOVINGAVERAGE example',\ !t('Annual measurements of Central England Average Temperature:',\ 'data from Manley, G. (1974), Central England temperatures:',\ 'monthly means 1659-1973, Quart.J.Met.Soc., 100, 378-405.',\ 'See the Guide to Genstat, Part 2 Statistics, Example 7.6.');\ STYLE=meta,plain VARIATE [NVALUES=315] Cetave OPEN '%GENDIR%/Examples/GuidePart2/Cetave.dat'; 3 READ [CHANNEL=3] Cetave CLOSE 3 MOVINGAVERAGE [NSAMPLES=12; METHOD=centre; PLOT=movingaverage] Cetave;\ MASERIES=Centre MOVINGAVERAGE [NSAMPLES=12; METHOD=exponential; PLOT=movingaverage] Cetave;\ MASERIES=Exponential MOVINGAVERAGE [NSAMPLES=12; METHOD=filter; PLOT=movingaverage] Cetave;\ MASERIES=Filter PRINT Cetave,Centre,Exponential,Filter