Calculates and plots the moving average of a time series (R.P. Littlejohn, G. Tunnicliffe Wilson & D.B. Baird).

### Options

`PRINT` = string token |
What to print (`parameters` ); default `*` i.e. nothing |
---|---|

`NSAMPLES` = scalar |
Number of samples used to calculate each moving average |

`METHOD` = string token |
How to calculate the averages (`past` , `centred` , `exponential` , `filter` , `holtwinters` ) default `past` |

`ORDER` = scalars |
Order for polynomial smoothing (0, 1, 2, 3, 4); default 0 i.e. ordinary moving-averages calculated from means |

`TRIM` = string token |
Whether to trim transients with `METHOD` settings `past` or `centre` when `ORDER` =0 (`yes` , `no` ); default `no` |

`PLOT` = string token |
What to plot (`components` , `movingaverages` , `predictions` ); default `*` i.e. nothing |

`ALPHA` = scalar |
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 |

`BETA` = scalar |
Allows the smoothing parameter for the trend to be specified for the Holt-Winters method |

`GAMMA` = scalar |
Allows the smoothing parameter for the seasonal component to be specified for the Holt-Winters method |

`MULTIPLICATIVE` = string token |
Controls whether the seasonal component is multiplicative in the Holt-Winters method (`yes` , `no` ); default `no` |

`NPREDICTIONS` = scalar |
Number of predicted values to form for the Holt-Winters method; default is twice the number of levels of the `SEASONAL` factor, or 2 if `SEASONAL` is not set |

### Parameters

`SERIES` = variates |
Time series whose moving averages are required |
---|---|

`MASERIES` = pointers |
Saves the moving averages for the defined `ORDER` settings |

`TITLE` = texts |
Title for the graph |

`SEASONAL` = factors |
Factor for seasonal adjustment |

`SAVE` = pointers |
Saves results from the Holt-Winters method or from seasonal adjustment |

### Description

`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:

`past` |
takes an unweighted average of past values (default); |
---|---|

`centred` |
takes an average centred on the current value with the first and last values receiving weights of 0.5 when `NSAMPLES` is even; |

`exponential` |
takes an exponentially weighted average of past values; |

`filter` |
uses `TFILTER` to smooth the data, using a specially constructed ARIMA model; and |

`holtwinters` |
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 `ORDER`

option.

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.

The `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).

In the `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.

With `METHOD`

settings `past`

or `centre`

, the `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 `TRIM=yes`

.

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 `ALPHA`

and `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.

When `METHOD=holtwinters`

, the `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:

`components` |
to plot the separate components (trend, level and season) of the estimate from a Holt-Winters or a seasonal model, |
---|---|

`movingaverages` |
to plot the moving averages, together with the original series, and |

`predictions` |
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.

The `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 `'Level'`

, `'Trend'`

, `'Season'`

(if `SEASONAL`

is set), `'Parameters'`

and, if `NPREDICTIONS`

is greater than zero, `'Lower'`

, and `'Upper'`

. With other seasonal adjustment models, it has elements for `'Trend'`

, `'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.

Options: `PRINT`

, `NSAMPLES`

, `METHOD`

, `ORDER`

, `TRIM`

, `PLOT`

, `ALPHA`

, `BETA`

, `GAMMA`

, `MULTIPLICATIVE`

, `NPREDICTIONS`

.

Parameters: `SERIES`

, `MASERIES`

, `TITLE`

, `SEASONAL`

, `SAVE`

.

### Method

The procedure uses `MODEL`

and `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 `NSAMPLES`

.

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 `_HWNFUNCTION`

, `_HWAFUNCTION`

and `_HWMFUNCTION`

, that are subsidiary procedures of `MOVINGAVERAGE`

### Action with `RESTRICT`

Restrictions are not permitted.

### References

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.

### See also

Commands for: Time series.

### Example

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[]