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_tsa_uni_smooth_exp (g13am)

## Purpose

nag_tsa_uni_smooth_exp (g13am) performs exponential smoothing using either single exponential, double exponential or a Holt–Winters method.

## Syntax

[init, fv, fse, yhat, res, dv, ad, r, ifail] = g13am(mode, itype, p, param, y, k, init, nf, r, 'n', n)
[init, fv, fse, yhat, res, dv, ad, r, ifail] = nag_tsa_uni_smooth_exp(mode, itype, p, param, y, k, init, nf, r, 'n', n)

## Description

Exponential smoothing is a relatively simple method of short term forecasting for a time series. nag_tsa_uni_smooth_exp (g13am) provides five types of exponential smoothing; single exponential, Brown's double exponential, linear Holt (also called double exponential smoothing in some references), additive Holt–Winters and multiplicative Holt–Winters. The choice of smoothing method used depends on the characteristics of the time series. If the mean of the series is only slowly changing then single exponential smoothing may be suitable. If there is a trend in the time series, which itself may be slowly changing, then double exponential smoothing may be suitable. If there is a seasonal component to the time series, e.g., daily or monthly data, then one of the two Holt–Winters methods may be suitable.
For a time series ${y}_{\mathit{t}}$, for $\mathit{t}=1,2,\dots ,n$, the five smoothing functions are defined by the following:
• Single Exponential Smoothing
 $mt = α yt + 1-α mt-1 y^t+f = mt var y^t+f = varεt 1+ f-1 α2$
• Brown Double Exponential Smoothing
 $mt = α yt + 1-α mt-1 rt = α mt - mt-1 + 1-α rt-1 y^ t+f = mt + f-1 + 1 / α rt var y^ t+f = varεt 1+ ∑ i=1 f-1 2α+ i-1 α2 2$
• Linear Holt Smoothing
 $mt = α yt + 1-α mt-1 + ϕ rt-1 rt = γ mt - mt-1 + 1-γ ϕ rt-1 y^ t+f = mt + ∑ i=1 f ϕi rt var y^ t+f = var εt 1+ ∑ i=1 f-1 α + α γ ϕ ϕi-1 ϕ-1 2$
• Additive Holt–Winters Smoothing
• Multiplicative Holt–Winters Smoothing
 $mt = α yt / s t-p + 1-α m t-1 +ϕ r t-1 rt = γ mt - m t-1 + 1-γ ϕ r t-1 st = β yt / mt + 1-β s t-p y^ t+f = mt + ∑ i=1 f ϕi rt × s t-p var y^ t+f = var εt ∑ i=0 ∞ ∑ j=0 p-1 ψ j+ip s t+f s t+f-j 2$
and $\psi$ is defined as in the additive Holt–Winters smoothing,
where ${m}_{t}$ is the mean, ${r}_{t}$ is the trend and ${s}_{t}$ is the seasonal component at time $t$ with $p$ being the seasonal order. The $f$-step ahead forecasts are given by ${\stackrel{^}{y}}_{t+f}$ and their variances by $\mathrm{var}\left({\stackrel{^}{y}}_{t+f}\right)$. The term $\mathrm{var}\left({\epsilon }_{t}\right)$ is estimated as the mean deviation.
The parameters, $\alpha$, $\beta$ and $\gamma$ control the amount of smoothing. The nearer these parameters are to one, the greater the emphasis on the current data point. Generally these parameters take values in the range $0.1$ to $0.3$. The linear Holt and two Holt–Winters smoothers include an additional parameter, $\varphi$, which acts as a trend dampener. For $0.0<\varphi <1.0$ the trend is dampened and for $\varphi >1.0$ the forecast function has an exponential trend, $\varphi =0.0$ removes the trend term from the forecast function and $\varphi =1.0$ does not dampen the trend.
For all methods, values for $\alpha$, $\beta$, $\gamma$ and $\psi$ can be chosen by trying different values and then visually comparing the results by plotting the fitted values along side the original data. Alternatively, for single exponential smoothing a suitable value for $\alpha$ can be obtained by fitting an $\mathrm{ARIMA}\left(0,1,1\right)$ model (see nag_tsa_multi_inputmod_estim (g13be)). For Brown's double exponential smoothing and linear Holt smoothing with no dampening, (i.e., $\varphi =1.0$), suitable values for $\alpha$ and $\gamma$ can be obtained by fitting an $\mathrm{ARIMA}\left(0,2,2\right)$ model. Similarly, the linear Holt method, with $\varphi \ne 1.0$, can be expressed as an $\mathrm{ARIMA}\left(1,2,2\right)$ model and the additive Holt–Winters, with no dampening, ($\varphi =1.0$), can be expressed as a seasonal ARIMA model with order $p$ of the form $\mathrm{ARIMA}\left(0,1,p+1\right)\left(0,1,0\right)$. There is no similar procedure for obtaining parameter values for the multiplicative Holt–Winters method, or the additive Holt–Winters method with $\varphi \ne 1.0$. In these cases parameters could be selected by minimizing a measure of fit using one of the nonlinear optimization functions in Chapter E04.
In addition to values for $\alpha$, $\beta$, $\gamma$ and $\psi$, initial values, ${m}_{0}$, ${r}_{0}$ and ${s}_{-\mathit{j}}$, for $\mathit{j}=0,1,\dots ,p-1$, are required to start the smoothing process. You can either supply these or they can be calculated by nag_tsa_uni_smooth_exp (g13am) from the first $k$ observations. For single exponential smoothing the mean of the observations is used to estimate ${m}_{0}$. For Brown double exponential smoothing and linear Holt smoothing, a simple linear regression is carried out with the series as the dependent variable and the sequence $1,2,\dots ,k$ as the independent variable. The intercept is then used to estimate ${m}_{0}$ and the slope to estimate ${r}_{0}$. In the case of the additive Holt–Winters method, the same regression is carried out, but a separate intercept is used for each of the $p$ seasonal groupings. The slope gives an estimate for ${r}_{0}$ and the mean of the $p$ intercepts is used as the estimate of ${m}_{0}$. The seasonal parameters ${s}_{-\mathit{j}}$, for $\mathit{j}=0,1,\dots ,p-1$, are estimated as the $p$ intercepts – ${m}_{0}$. A similar approach is adopted for the multiplicative Holt–Winter's method.
One step ahead forecasts, ${\stackrel{^}{y}}_{t+1}$ are supplied along with the residuals computed as $\left({y}_{t+1}-{\stackrel{^}{y}}_{t+1}\right)$. In addition, two measures of fit are provided. The mean absolute deviation,
 $1 n ∑ t=1 n yt - y^ t$
and the square root of the mean deviation
 $1 n ∑ t=1 n yt - y^ t 2 .$

## References

Chatfield C (1980) The Analysis of Time Series Chapman and Hall

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{mode}$int64int32nag_int scalar
Indicates if nag_tsa_uni_smooth_exp (g13am) is continuing from a previous call or, if not, how the initial values are computed.
${\mathbf{mode}}=0$
Required values for ${m}_{0}$, ${r}_{0}$ and ${s}_{-\mathit{j}}$, for $\mathit{j}=0,1,\dots ,p-1$, are supplied in init.
${\mathbf{mode}}=1$
nag_tsa_uni_smooth_exp (g13am) continues from a previous call using values that are supplied in r.
${\mathbf{mode}}=2$
Required values for ${m}_{0}$, ${r}_{0}$ and ${s}_{-\mathit{j}}$, for $\mathit{j}=0,1,\dots ,p-1$, are estimated using the first $k$ observations.
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
2:     $\mathrm{itype}$int64int32nag_int scalar
The smoothing function.
${\mathbf{itype}}=1$
Single exponential.
${\mathbf{itype}}=2$
Brown double exponential.
${\mathbf{itype}}=3$
Linear Holt.
${\mathbf{itype}}=4$
Additive Holt–Winters.
${\mathbf{itype}}=5$
Multiplicative Holt–Winters.
Constraint: ${\mathbf{itype}}=1$, $2$, $3$, $4$ or $5$.
3:     $\mathrm{p}$int64int32nag_int scalar
If ${\mathbf{itype}}=4$ or $5$, the seasonal order, $p$, otherwise p is not referenced.
Constraint: if ${\mathbf{itype}}=4$ or $5$, ${\mathbf{p}}>1$.
4:     $\mathrm{param}\left(:\right)$ – double array
The dimension of the array param must be at least $1$ if ${\mathbf{itype}}=1$ or $2$, $3$ if ${\mathbf{itype}}=3$ and at least $4$ if ${\mathbf{itype}}=4$ or $5$
The smoothing parameters.
If ${\mathbf{itype}}=1$ or $2$, ${\mathbf{param}}\left(1\right)=\alpha$ and any remaining elements of param are not referenced.
If ${\mathbf{itype}}=3$, ${\mathbf{param}}\left(1\right)=\alpha$, ${\mathbf{param}}\left(2\right)=\gamma$, ${\mathbf{param}}\left(3\right)=\varphi$ and any remaining elements of param are not referenced.
If ${\mathbf{itype}}=4$ or $5$, ${\mathbf{param}}\left(1\right)=\alpha$, ${\mathbf{param}}\left(2\right)=\gamma$, ${\mathbf{param}}\left(3\right)=\beta$ and ${\mathbf{param}}\left(4\right)=\varphi$.
Constraints:
• if ${\mathbf{itype}}=1$, $0.0\le \alpha \le 1.0$;
• if ${\mathbf{itype}}=2$, $0.0<\alpha \le 1.0$;
• if ${\mathbf{itype}}=3$, $0.0\le \alpha \le 1.0$ and $0.0\le \gamma \le 1.0$ and $\varphi \ge 0.0$;
• if ${\mathbf{itype}}=4$ or $5$, $0.0\le \alpha \le 1.0$ and $0.0\le \gamma \le 1.0$ and $0.0\le \beta \le 1.0$ and $\varphi \ge 0.0$.
5:     $\mathrm{y}\left({\mathbf{n}}\right)$ – double array
The time series.
6:     $\mathrm{k}$int64int32nag_int scalar
If ${\mathbf{mode}}=2$, the number of observations used to initialize the smoothing.
If ${\mathbf{mode}}\ne 2$, k is not referenced.
Constraints:
• if ${\mathbf{mode}}=2$ and ${\mathbf{itype}}=4$ or $5$, $2×{\mathbf{p}}\le {\mathbf{k}}\le {\mathbf{n}}$;
• if ${\mathbf{mode}}=2$ and ${\mathbf{itype}}=1$, $2$ or $3$, $1\le {\mathbf{k}}\le {\mathbf{n}}$.
7:     $\mathrm{init}\left(:\right)$ – double array
The dimension of the array init must be at least $1$ if ${\mathbf{itype}}=1$, $2$ if ${\mathbf{itype}}=2$ or $3$ and at least $2+{\mathbf{p}}$ if ${\mathbf{itype}}=4$ or $5$
If ${\mathbf{mode}}=0$, the initial values for ${m}_{0}$, ${r}_{0}$ and ${s}_{-\mathit{j}}$, for $\mathit{j}=0,1,\dots ,p-1$, used to initialize the smoothing.
If ${\mathbf{itype}}=1$, ${\mathbf{init}}\left(1\right)={m}_{0}$ and the remaining elements of init are not referenced.
If ${\mathbf{itype}}=2$ or $3$, ${\mathbf{init}}\left(1\right)={m}_{0}$ and ${\mathbf{init}}\left(2\right)={r}_{0}$ and the remaining elements of init are not referenced.
If ${\mathbf{itype}}=4$ or $5$, ${\mathbf{init}}\left(1\right)={m}_{0}$, ${\mathbf{init}}\left(2\right)={r}_{0}$ and ${\mathbf{init}}\left(3\right)$ to ${\mathbf{init}}\left(p+2\right)$ hold the values for ${s}_{-\mathit{j}}$, for $\mathit{j}=0,1,\dots ,p-1$. The remaining elements of init are not referenced.
8:     $\mathrm{nf}$int64int32nag_int scalar
The number of forecasts required beyond the end of the series. Note, the one step ahead forecast is always produced.
Constraint: ${\mathbf{nf}}\ge 0$.
9:     $\mathrm{r}\left(:\right)$ – double array
The dimension of the array r must be at least $13$ if ${\mathbf{itype}}=1$, $2$ or $3$ and at least $13+{\mathbf{p}}$ if ${\mathbf{itype}}=4$ or $5$
If ${\mathbf{mode}}=1$, r must contain the values as returned by a previous call to nag_rand_times_smooth_exp (g05pm) or nag_tsa_uni_smooth_exp (g13am), r need not be set otherwise.
If ${\mathbf{itype}}=1$, $2$ or $3$, only the first $13$ elements of r are referenced, otherwise the first $13+p$ elements are referenced.
Constraint: if ${\mathbf{mode}}=1$, r must have been initialized by at least one previous call to nag_rand_times_smooth_exp (g05pm) or nag_tsa_uni_smooth_exp (g13am) with ${\mathbf{mode}}\ne 1$, and r should not have been changed since the last call to nag_rand_times_smooth_exp (g05pm) or nag_tsa_uni_smooth_exp (g13am).

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array y.
The number of observations in the series.
Constraint: ${\mathbf{n}}\ge 0$.

### Output Parameters

1:     $\mathrm{init}\left(:\right)$ – double array
The dimension of the array init will be $1$ if ${\mathbf{itype}}=1$, $2$ if ${\mathbf{itype}}=2$ or $3$ and at least $2+{\mathbf{p}}$ if ${\mathbf{itype}}=4$ or $5$
If ${\mathbf{mode}}\ne 1$, the values used to initialize the smoothing. These are in the same order as described above.
2:     $\mathrm{fv}\left({\mathbf{nf}}\right)$ – double array
${\stackrel{^}{y}}_{t+\mathit{f}}$, for $\mathit{f}=1,2,\dots ,{\mathbf{nf}}$, the next nf step forecasts. Where $t={\mathbf{n}}$, if ${\mathbf{mode}}\ne 1$, else $t$ is the total number of smoothed and forecast values already produced.
3:     $\mathrm{fse}\left({\mathbf{nf}}\right)$ – double array
The forecast standard errors for the values given in fv.
4:     $\mathrm{yhat}\left({\mathbf{n}}\right)$ – double array
${\stackrel{^}{y}}_{\mathit{t}+1}$, for $\mathit{t}=1,2,\dots ,{\mathbf{n}}$, the one step ahead forecast values, with ${\mathbf{yhat}}\left(i\right)$ being the one step ahead forecast of ${\mathbf{y}}\left(i-1\right)$.
5:     $\mathrm{res}\left({\mathbf{n}}\right)$ – double array
The residuals, $\left({y}_{\mathit{t}+1}-{\stackrel{^}{y}}_{\mathit{t}+1}\right)$, for $\mathit{t}=1,2,\dots ,{\mathbf{n}}$.
6:     $\mathrm{dv}$ – double scalar
The square root of the mean deviation.
7:     $\mathrm{ad}$ – double scalar
The mean absolute deviation.
8:     $\mathrm{r}\left(:\right)$ – double array
The dimension of the array r will be $13$ if ${\mathbf{itype}}=1$, $2$ or $3$ and at least $13+{\mathbf{p}}$ if ${\mathbf{itype}}=4$ or $5$
The information on the current state of the smoothing.
9:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{itype}}=1$, $2$, $3$, $4$ or $5$.
${\mathbf{ifail}}=3$
Constraint: if ${\mathbf{itype}}=4$ or $5$, ${\mathbf{p}}>1$.
${\mathbf{ifail}}=4$
Constraint: $0.0\le {\mathbf{param}}\left(_\right)\le 1.0$.
Constraint: if ${\mathbf{itype}}=2$, $0.0<{\mathbf{param}}\left(_\right)\le 1.0$.
Constraint: ${\mathbf{param}}\left(_\right)\ge 0.0$.
${\mathbf{ifail}}=5$
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=6$
A multiplicative Holt–Winters model cannot be used with the supplied data.
${\mathbf{ifail}}=7$
Constraint: if ${\mathbf{mode}}=2$ and ${\mathbf{itype}}=4$ or $5$, $1\le {\mathbf{k}}\le {\mathbf{n}}$.
Constraint: if ${\mathbf{mode}}=2$ and ${\mathbf{itype}}=4$ or $5$, $2×{\mathbf{p}}\le {\mathbf{k}}$.
${\mathbf{ifail}}=9$
Constraint: ${\mathbf{nf}}\ge 0$.
${\mathbf{ifail}}=16$
On entry, the array r has not been initialized correctly.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

Not applicable.

## Further Comments

Single exponential, Brown's double exponential and linear Holt smoothing methods are stable, whereas the two Holt–Winters methods can be affected by poor initial values for the seasonal components.
See also the function document for nag_rand_times_smooth_exp (g05pm).

## Example

This example smooths a time series relating to the rate of the earth's rotation about its polar axis.
```function g13am_example

fprintf('g13am example results\n\n');

% data
y = [180;     135;     213;     181;     148;     204;
228;     225;     198;     200;     187];
n = numel(y);

% Linear Holt smoothing (3 params)
itype = int64(3);
p     = int64(0);
param = [0.01;    1;     1];
init  = [0; 0];
r     = zeros(p+13,1);

% Initial r values calculated from data
mode  = int64(2);
k     = int64(n);

nf    = int64(5);

% Perform exponential smoothing
[init, fv, fse, yhat, res, dv, ad, r, ifail] = ...
g13am( ...
mode, itype, p, param, y, k, init, nf, r);

% Display output
fprintf('Initial values used:\n');
ival = [1:numel(init)]';
fprintf('%4d%12.3f\n',[ival init]');
fprintf('\nMean Deviation     = %12.4e\n', dv);
fprintf('Absolute Deviation = %12.4e\n', ad);
fprintf('\n         Observed      1-Step\n');
fprintf(' Period   Values      Forecast      Residual\n\n');
ival = [1:n]';
fprintf('%4d %12.3f %12.3f %12.3f\n', [ival y yhat res]');
fprintf('\n         Forecast     Standard\n');
fprintf(' Period   Values       Errors\n\n');
ival = double([n+1:n+nf]');
fprintf('%4d %12.3f %12.3f\n', [ival fv fse]');

```
```g13am example results

Initial values used:
1     168.018
2       3.800

Mean Deviation     =   2.5473e+01
Absolute Deviation =   2.1233e+01

Observed      1-Step
Period   Values      Forecast      Residual

1      180.000      171.818        8.182
2      135.000      175.782      -40.782
3      213.000      178.848       34.152
4      181.000      183.005       -2.005
5      148.000      186.780      -38.780
6      204.000      189.800       14.200
7      228.000      193.492       34.508
8      225.000      197.732       27.268
9      198.000      202.172       -4.172
10      200.000      206.256       -6.256
11      187.000      210.256      -23.256

Forecast     Standard
Period   Values       Errors

12      213.854       25.473
13      217.685       25.478
14      221.516       25.490
15      225.346       25.510
16      229.177       25.542
```

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–2015