Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

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 yt${y}_{\mathit{t}}$, for t = 1,2,,n$\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)
$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 )$
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) ( f − 1 )1 + ∑ (2α + (i − 1)α2)2 i = 1
• 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 )$
mt = α yt + (1 − α) (mt − 1 + φrt − 1) rt = γ (mt − mt − 1) + (1 − γ) φ rt − 1 ŷt + f = f mt + ∑ φirt i = 1 var(ŷt + f) =
 var(εt) ( f − 1 )1 + ∑ (α + ( α γ φ (φi − 1) )/((φ − 1)))2 i = 1
var(ŷt + f) = ψi =
mt = α (yt − st − p) + (1 − α) (mt − 1 + φrt − 1) rt = γ (mt − mt − 1) + (1 − γ) φ rt − 1 st = β (yt − mt) + (1 − β) st − p ŷt + f =
 mt + (f ) ∑ φirti = 1 + st − p
 var(εt) ( f − 1 )1 + ∑ ψi2 i = 1
 { 0 if ​i ≥ f α + ( αγφ (φi − 1) )/( (φ − 1) ) if ​ i  mod  p ≠ 0 α + ( α γ φ (φi − 1) )/((φ − 1)) + β (1 − α) otherwise
• Multiplicative Holt–Winters Smoothing
var(ŷt + f) =
mt = α yt / st − p + (1 − α) (m t − 1 + φrt − 1) rt = γ (mt − mt − 1) + (1 − γ) φ rt − 1 st = β yt / mt + (1 − β) st − p ŷt + f =
 ( f )mt + ∑ φirt i = 1 × st − p
 var(εt) (∞ ) ∑ ∑ j = 0p − 1 (ψj + ip( st + f )/( st + f − j ))2 i = 0
$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 mt${m}_{t}$ is the mean, rt${r}_{t}$ is the trend and st${s}_{t}$ is the seasonal component at time t$t$ with p$p$ being the seasonal order. The f$f$-step ahead forecasts are given by t + f${\stackrel{^}{y}}_{t+f}$ and their variances by var(t + f) $\mathrm{var}\left({\stackrel{^}{y}}_{t+f}\right)$. The term var(εt) $\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$0.1$ to 0.3$0.3$. The linear Holt and two Holt–Winters smoothers include an additional parameter, φ$\varphi$, which acts as a trend dampener. For 0.0 < φ < 1.0$0.0<\varphi <1.0$ the trend is dampened and for φ > 1.0$\varphi >1.0$ the forecast function has an exponential trend, φ = 0.0$\varphi =0.0$ removes the trend term from the forecast function and φ = 1.0$\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 ARIMA(0,1,1)$\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., φ = 1.0$\varphi =1.0$), suitable values for α$\alpha$ and γ$\gamma$ can be obtained by fitting an ARIMA(0,2,2)$\mathrm{ARIMA}\left(0,2,2\right)$ model. Similarly, the linear Holt method, with φ1.0$\varphi \ne 1.0$, can be expressed as an ARIMA(1,2,2)$\mathrm{ARIMA}\left(1,2,2\right)$ model and the additive Holt–Winters, with no dampening, (φ = 1.0$\varphi =1.0$), can be expressed as a seasonal ARIMA model with order p$p$ of the form ARIMA(0,1,p + 1)(0,1,0)$\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 φ1.0$\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, m0${m}_{0}$, r0${r}_{0}$ and sj${s}_{-\mathit{j}}$, for j = 0,1,,p1$\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$k$ observations. For single exponential smoothing the mean of the observations is used to estimate m0${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,,k$1,2,\dots ,k$ as the independent variable. The intercept is then used to estimate m0${m}_{0}$ and the slope to estimate r0${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$p$ seasonal groupings. The slope gives an estimate for r0${r}_{0}$ and the mean of the p$p$ intercepts is used as the estimate of m0${m}_{0}$. The seasonal parameters sj${s}_{-\mathit{j}}$, for j = 0,1,,p1$\mathit{j}=0,1,\dots ,p-1$, are estimated as the p$p$ intercepts – m0${m}_{0}$. A similar approach is adopted for the multiplicative Holt–Winter's method.
One step ahead forecasts, t + 1 ${\stackrel{^}{y}}_{t+1}$ are supplied along with the residuals computed as (yt + 1t + 1) $\left({y}_{t+1}-{\stackrel{^}{y}}_{t+1}\right)$. In addition, two measures of fit are provided. The mean absolute deviation,
 n 1/n ∑ |yt − ŷt| t = 1
$1 n ∑ t=1 n | yt - y^ t |$
and the square root of the mean deviation
 sqrt( 1/n ∑ t = 1n (yt − ŷt)2 ) . $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:     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.
mode = 0${\mathbf{mode}}=0$
Required values for m0${m}_{0}$, r0${r}_{0}$ and sj${s}_{-\mathit{j}}$, for j = 0,1,,p1$\mathit{j}=0,1,\dots ,p-1$, are supplied in init.
mode = 1${\mathbf{mode}}=1$
nag_tsa_uni_smooth_exp (g13am) continues from a previous call using values that are supplied in r.
mode = 2${\mathbf{mode}}=2$
Required values for m0${m}_{0}$, r0${r}_{0}$ and sj${s}_{-\mathit{j}}$, for j = 0,1,,p1$\mathit{j}=0,1,\dots ,p-1$, are estimated using the first k$k$ observations.
Constraint: mode = 0${\mathbf{mode}}=0$, 1$1$ or 2$2$.
2:     itype – int64int32nag_int scalar
The smoothing function.
itype = 1${\mathbf{itype}}=1$
Single exponential.
itype = 2${\mathbf{itype}}=2$
Brown double exponential.
itype = 3${\mathbf{itype}}=3$
Linear Holt.
itype = 4${\mathbf{itype}}=4$
itype = 5${\mathbf{itype}}=5$
Multiplicative Holt–Winters.
Constraint: itype = 1${\mathbf{itype}}=1$, 2$2$, 3$3$, 4$4$ or 5$5$.
3:     p – int64int32nag_int scalar
If itype = 4${\mathbf{itype}}=4$ or 5$5$, the seasonal order, p$p$, otherwise p is not referenced.
Constraint: if itype = 4${\mathbf{itype}}=4$ or 5$5$, p > 1${\mathbf{p}}>1$.
4:     param( : $:$) – double array
Note: the dimension of the array param must be at least 1$1$ if itype = 1${\mathbf{itype}}=1$ or 2$2$, 3$3$ if itype = 3${\mathbf{itype}}=3$ and at least 4$4$ if itype = 4${\mathbf{itype}}=4$ or 5$5$.
The smoothing parameters.
If itype = 1${\mathbf{itype}}=1$ or 2$2$, param(1) = α${\mathbf{param}}\left(1\right)=\alpha$ and any remaining elements of param are not referenced.
If itype = 3${\mathbf{itype}}=3$, param(1) = α${\mathbf{param}}\left(1\right)=\alpha$, param(2) = γ${\mathbf{param}}\left(2\right)=\gamma$, param(3) = φ${\mathbf{param}}\left(3\right)=\varphi$ and any remaining elements of param are not referenced.
If itype = 4${\mathbf{itype}}=4$ or 5$5$, param(1) = α${\mathbf{param}}\left(1\right)=\alpha$, param(2) = γ${\mathbf{param}}\left(2\right)=\gamma$, param(3) = β${\mathbf{param}}\left(3\right)=\beta$ and param(4) = φ${\mathbf{param}}\left(4\right)=\varphi$.
Constraints:
• if itype = 1${\mathbf{itype}}=1$, 0.0α1.0$0.0\le \alpha \le 1.0$;
• if itype = 2${\mathbf{itype}}=2$, 0.0 < α1.0$0.0<\alpha \le 1.0$;
• if itype = 3${\mathbf{itype}}=3$, 0.0α1.0$0.0\le \alpha \le 1.0$ and 0.0γ1.0$0.0\le \gamma \le 1.0$ and φ0.0$\varphi \ge 0.0$;
• if itype = 4${\mathbf{itype}}=4$ or 5$5$, 0.0α1.0$0.0\le \alpha \le 1.0$ and 0.0γ1.0$0.0\le \gamma \le 1.0$ and 0.0β1.0$0.0\le \beta \le 1.0$ and φ0.0$\varphi \ge 0.0$.
5:     y(n) – double array
n, the dimension of the array, must satisfy the constraint n0${\mathbf{n}}\ge 0$.
The time series.
6:     k – int64int32nag_int scalar
If mode = 2${\mathbf{mode}}=2$, the number of observations used to initialize the smoothing.
If mode2${\mathbf{mode}}\ne 2$, k is not referenced.
Constraints:
• if mode = 2${\mathbf{mode}}=2$ and itype = 4${\mathbf{itype}}=4$ or 5$5$, 2 × pkn$2×{\mathbf{p}}\le {\mathbf{k}}\le {\mathbf{n}}$;
• if mode = 2${\mathbf{mode}}=2$ and itype = 1${\mathbf{itype}}=1$, 2$2$ or 3$3$, 1kn$1\le {\mathbf{k}}\le {\mathbf{n}}$.
7:     init( : $:$) – double array
Note: the dimension of the array init must be at least 1$1$ if itype = 1${\mathbf{itype}}=1$, 2$2$ if itype = 2${\mathbf{itype}}=2$ or 3$3$ and at least 2 + p$2+{\mathbf{p}}$ if itype = 4${\mathbf{itype}}=4$ or 5$5$.
If mode = 0${\mathbf{mode}}=0$, the initial values for m0${m}_{0}$, r0${r}_{0}$ and sj${s}_{-\mathit{j}}$, for j = 0,1,,p1$\mathit{j}=0,1,\dots ,p-1$, used to initialize the smoothing.
If itype = 1${\mathbf{itype}}=1$, init(1) = m0${\mathbf{init}}\left(1\right)={m}_{0}$ and the remaining elements of init are not referenced.
If itype = 2${\mathbf{itype}}=2$ or 3$3$, init(1) = m0${\mathbf{init}}\left(1\right)={m}_{0}$ and init(2) = r0${\mathbf{init}}\left(2\right)={r}_{0}$ and the remaining elements of init are not referenced.
If itype = 4${\mathbf{itype}}=4$ or 5$5$, init(1) = m0${\mathbf{init}}\left(1\right)={m}_{0}$, init(2) = r0${\mathbf{init}}\left(2\right)={r}_{0}$ and init(3)${\mathbf{init}}\left(3\right)$ to init(p + 2)${\mathbf{init}}\left(p+2\right)$ hold the values for sj${s}_{-\mathit{j}}$, for j = 0,1,,p1$\mathit{j}=0,1,\dots ,p-1$. The remaining elements of init are not referenced.
8:     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: nf0${\mathbf{nf}}\ge 0$.
9:     r( : $:$) – double array
Note: the dimension of the array r must be at least 13$13$ if itype = 1${\mathbf{itype}}=1$, 2$2$ or 3$3$ and at least 13 + p$13+{\mathbf{p}}$ if itype = 4${\mathbf{itype}}=4$ or 5$5$.
If mode = 1${\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 itype = 1${\mathbf{itype}}=1$, 2$2$ or 3$3$, only the first 13$13$ elements of r are referenced, otherwise the first 13 + p$13+p$ elements are referenced.
Constraint: if mode = 1${\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 mode1${\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:     n – int64int32nag_int scalar
Default: The dimension of the array y.
The number of observations in the series.
Constraint: n0${\mathbf{n}}\ge 0$.

None.

### Output Parameters

1:     init( : $:$) – double array
Note: the dimension of the array init must be at least 1$1$ if itype = 1${\mathbf{itype}}=1$, 2$2$ if itype = 2${\mathbf{itype}}=2$ or 3$3$ and at least 2 + p$2+{\mathbf{p}}$ if itype = 4${\mathbf{itype}}=4$ or 5$5$.
If mode1${\mathbf{mode}}\ne 1$, the values used to initialize the smoothing. These are in the same order as described above.
2:     fv(nf) – double array
t + f${\stackrel{^}{y}}_{t+\mathit{f}}$, for f = 1,2,,nf$\mathit{f}=1,2,\dots ,{\mathbf{nf}}$, the next nf step forecasts. Where t = n$t={\mathbf{n}}$, if mode1${\mathbf{mode}}\ne 1$, else t$t$ is the total number of smoothed and forecast values already produced.
3:     fse(nf) – double array
The forecast standard errors for the values given in fv.
4:     yhat(n) – double array
t + 1${\stackrel{^}{y}}_{\mathit{t}+1}$, for t = 1,2,,n$\mathit{t}=1,2,\dots ,{\mathbf{n}}$, the one step ahead forecast values, with yhat(i)${\mathbf{yhat}}\left(i\right)$ being the one step ahead forecast of y(i1)${\mathbf{y}}\left(i-1\right)$.
5:     res(n) – double array
The residuals, (yt + 1t + 1) $\left({y}_{\mathit{t}+1}-{\stackrel{^}{y}}_{\mathit{t}+1}\right)$, for t = 1,2,,n$\mathit{t}=1,2,\dots ,{\mathbf{n}}$.
6:     dv – double scalar
The square root of the mean deviation.
The mean absolute deviation.
8:     r( : $:$) – double array
Note: the dimension of the array r must be at least 13$13$ if itype = 1${\mathbf{itype}}=1$, 2$2$ or 3$3$ and at least 13 + p$13+{\mathbf{p}}$ if itype = 4${\mathbf{itype}}=4$ or 5$5$.
The information on the current state of the smoothing.
9:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
 On entry, mode < 0${\mathbf{mode}}<0$, or mode > 2${\mathbf{mode}}>2$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, itype < 1${\mathbf{itype}}<1$, or itype > 5${\mathbf{itype}}>5$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, itype = 4${\mathbf{itype}}=4$ or 5$5$ and p < 2${\mathbf{p}}<2$.
ifail = 4${\mathbf{ifail}}=4$
On entry, at least one of α$\alpha$, β$\beta$ or γ < 0.0$\gamma <0.0$ or > 1.0$>1.0$.
On entry, itype = 2${\mathbf{itype}}=2$ and α = 0.0$\alpha =0.0$.
On entry, φ < 0.0$\varphi <0.0$.
ifail = 5${\mathbf{ifail}}=5$
 On entry, n < 0${\mathbf{n}}<0$.
ifail = 6${\mathbf{ifail}}=6$
A multiplicative Holt–Winters model cannot be used with the supplied data.
ifail = 7${\mathbf{ifail}}=7$
On entry, mode = 2${\mathbf{mode}}=2$ and k < 1${\mathbf{k}}<1$ or k > n${\mathbf{k}}>{\mathbf{n}}$.
On entry, mode = 2${\mathbf{mode}}=2$, itype = 4${\mathbf{itype}}=4$ or 5$5$ and k < 2 × p${\mathbf{k}}<2×{\mathbf{p}}$.
ifail = 9${\mathbf{ifail}}=9$
 On entry, nf < 0${\mathbf{nf}}<0$.
ifail = 16${\mathbf{ifail}}=16$
On entry, mode = 1${\mathbf{mode}}=1$ and the array r has not been initialized correctly.

## Accuracy

Not applicable.

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.

## Example

```function nag_tsa_uni_smooth_exp_example
mode = int64(2);
itype = int64(3);
p = int64(0);
param = [0.01;
1;
1];
y = [180;
135;
213;
181;
148;
204;
228;
225;
198;
200;
187];
k = int64(11);
init = [-1.666051209955092e-47;
4.853144920788553e-270];
nf = int64(5);
r = [1.910201800633982e-313;
-1.601103737937077e-47;
-1.678629904873684e-47;
-4.300360784598706e-39;
-0.09138826798201372;
-1.668172686220112e-47;
2.572155976556066e-306;
-1.601102994801293e-47;
4.940656458412465e-324;
2.514055054265363e-306;
-4.296977237061931e-39;
2.514055056853075e-306;
-4.300811189660317e-39];
[initOut, fv, fse, yhat, res, dv, ad, rOut, ifail] = ...
nag_tsa_uni_smooth_exp(mode, itype, p, param, y, k, init, nf, r)
```
```

initOut =

168.0182
3.8000

fv =

213.8545
217.6851
221.5157
225.3462
229.1768

fse =

25.4733
25.4784
25.4899
25.5102
25.5420

yhat =

171.8182
175.7818
178.8480
183.0050
186.7805
189.8003
193.4920
197.7318
202.1719
206.2559
210.2565

res =

8.1818
-40.7818
34.1520
-2.0050
-38.7805
14.1997
34.5080
27.2682
-4.1719
-6.2559
-23.2565

dv =

25.4733

21.2328

rOut =

1.0e+03 *

2.4680
0
0.0000
0.0010
0
0.0010
0
7.1378
0.2336
0.2100
0.0038
0.0110
0

ifail =

0

```
```function g13am_example
mode = int64(2);
itype = int64(3);
p = int64(0);
param = [0.01;
1;
1];
y = [180;
135;
213;
181;
148;
204;
228;
225;
198;
200;
187];
k = int64(11);
init = [-1.666051209955092e-47;
4.853144920788553e-270];
nf = int64(5);
r = [1.910201800633982e-313;
-1.601103737937077e-47;
-1.678629904873684e-47;
-4.300360784598706e-39;
-0.09138826798201372;
-1.668172686220112e-47;
2.572155976556066e-306;
-1.601102994801293e-47;
4.940656458412465e-324;
2.514055054265363e-306;
-4.296977237061931e-39;
2.514055056853075e-306;
-4.300811189660317e-39];
[initOut, fv, fse, yhat, res, dv, ad, rOut, ifail] = g13am(mode, itype, p, param, y, k, init, nf, r)
```
```

initOut =

168.0182
3.8000

fv =

213.8545
217.6851
221.5157
225.3462
229.1768

fse =

25.4733
25.4784
25.4899
25.5102
25.5420

yhat =

171.8182
175.7818
178.8480
183.0050
186.7805
189.8003
193.4920
197.7318
202.1719
206.2559
210.2565

res =

8.1818
-40.7818
34.1520
-2.0050
-38.7805
14.1997
34.5080
27.2682
-4.1719
-6.2559
-23.2565

dv =

25.4733

21.2328

rOut =

1.0e+03 *

2.4680
0
0.0000
0.0010
0
0.0010
0
7.1378
0.2336
0.2100
0.0038
0.0110
0

ifail =

0

```