# NAG FL Interfaceg13amf (uni_​smooth_​exp)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g13amf performs exponential smoothing using either single exponential, double exponential or a Holt–Winters method.

## 2Specification

Fortran Interface
 Subroutine g13amf ( mode, p, n, y, k, init, nf, fv, fse, yhat, res, dv, ad, r,
 Integer, Intent (In) :: mode, itype, p, n, k, nf Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: param(*), y(n) Real (Kind=nag_wp), Intent (Inout) :: init(*), r(*) Real (Kind=nag_wp), Intent (Out) :: fv(nf), fse(nf), yhat(n), res(n), dv, ad
#include <nag.h>
 void g13amf_ (const Integer *mode, const Integer *itype, const Integer *p, const double param[], const Integer *n, const double y[], const Integer *k, double init[], const Integer *nf, double fv[], double fse[], double yhat[], double res[], double *dv, double *ad, double r[], Integer *ifail)
The routine may be called by the names g13amf or nagf_tsa_uni_smooth_exp.

## 3Description

Exponential smoothing is a relatively simple method of short term forecasting for a time series. g13amf 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 )$
• 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 ϕirt) × 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 g13bef). 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 routines 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 g13amf 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 .$
Chatfield C (1980) The Analysis of Time Series Chapman and Hall

## 5Arguments

1: $\mathbf{mode}$Integer Input
On entry: indicates if g13amf 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$
g13amf 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: $\mathbf{itype}$Integer Input
On entry: the smoothing function.
${\mathbf{itype}}=1$
Single exponential.
${\mathbf{itype}}=2$
Brown double exponential.
${\mathbf{itype}}=3$
Linear Holt.
${\mathbf{itype}}=4$
${\mathbf{itype}}=5$
Multiplicative Holt–Winters.
Constraint: ${\mathbf{itype}}=1$, $2$, $3$, $4$ or $5$.
3: $\mathbf{p}$Integer Input
On entry: 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: $\mathbf{param}\left(*\right)$Real (Kind=nag_wp) array Input
Note: 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$.
On entry: 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: $\mathbf{n}$Integer Input
On entry: the number of observations in the series.
Constraint: ${\mathbf{n}}\ge 0$.
6: $\mathbf{y}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the time series.
7: $\mathbf{k}$Integer Input
On entry: 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}}$.
8: $\mathbf{init}\left(*\right)$Real (Kind=nag_wp) array Input/Output
Note: 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$.
On entry: 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.
On exit: if ${\mathbf{mode}}\ne 1$, the values used to initialize the smoothing. These are in the same order as described above.
9: $\mathbf{nf}$Integer Input
On entry: 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$.
10: $\mathbf{fv}\left({\mathbf{nf}}\right)$Real (Kind=nag_wp) array Output
On exit: ${\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.
11: $\mathbf{fse}\left({\mathbf{nf}}\right)$Real (Kind=nag_wp) array Output
On exit: the forecast standard errors for the values given in fv.
12: $\mathbf{yhat}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: ${\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)$.
13: $\mathbf{res}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: the residuals, $\left({y}_{\mathit{t}+1}-{\stackrel{^}{y}}_{\mathit{t}+1}\right)$, for $\mathit{t}=1,2,\dots ,{\mathbf{n}}$.
14: $\mathbf{dv}$Real (Kind=nag_wp) Output
On exit: the square root of the mean deviation.
On exit: the mean absolute deviation.
16: $\mathbf{r}\left(*\right)$Real (Kind=nag_wp) array Communication Array
Note: 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$.
On entry: if ${\mathbf{mode}}=1$, r must contain the values as returned by a previous call to g05pmf or g13amf, 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.
On exit: the information on the current state of the smoothing.
Constraint: if ${\mathbf{mode}}=1$, r must have been initialized by at least one previous call to g05pmf or g13amf with ${\mathbf{mode}}\ne 1$, and r should not have been changed since the last call to g05pmf or g13amf.
17: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{mode}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{itype}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{itype}}=1$, $2$, $3$, $4$ or $5$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{itype}}=4$ or $5$, ${\mathbf{p}}>1$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{param}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: $0.0\le {\mathbf{param}}\left(⟨\mathit{\text{value}}⟩\right)\le 1.0$.
On entry, ${\mathbf{param}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{itype}}=2$, $0.0<{\mathbf{param}}\left(⟨\mathit{\text{value}}⟩\right)\le 1.0$.
On entry, ${\mathbf{param}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{param}}\left(⟨\mathit{\text{value}}⟩\right)\ge 0.0$.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=6$
A multiplicative Holt–Winters model cannot be used with the supplied data.
${\mathbf{ifail}}=7$
On entry, ${\mathbf{k}}=⟨\mathit{\text{value}}⟩$, $2×{\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{mode}}=2$ and ${\mathbf{itype}}=4$ or $5$, $2×{\mathbf{p}}\le {\mathbf{k}}$.
On entry, ${\mathbf{k}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{mode}}=2$ and ${\mathbf{itype}}=4$ or $5$, $1\le {\mathbf{k}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=9$
On entry, ${\mathbf{nf}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nf}}\ge 0$.
${\mathbf{ifail}}=16$
On entry, the array r has not been initialized correctly.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

Not applicable.

## 8Parallelism and Performance

g13amf is not threaded in any implementation.

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.

## 10Example

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

### 10.1Program Text

Program Text (g13amfe.f90)

### 10.2Program Data

Program Data (g13amfe.d)

### 10.3Program Results

Program Results (g13amfe.r)