g05 Chapter Contents
g05 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_rand_exp_smooth (g05pmc)

## 1  Purpose

nag_rand_exp_smooth (g05pmc) simulates from an exponential smoothing model, where the model uses either single exponential, double exponential or a Holt–Winters method.

## 2  Specification

 #include #include
 void nag_rand_exp_smooth (Nag_InitialValues mode, Integer n, Nag_ExpSmoothType itype, Integer p, const double param[], const double init[], double var, double r[], Integer state[], const double e[], Integer en, double x[], NagError *fail)

## 3  Description

nag_rand_exp_smooth (g05pmc) returns $\left\{{x}_{t}:t=1,2,\dots ,n\right\}$, a realization of a time series from an exponential smoothing model defined by one of five smoothing functions:
• Single Exponential Smoothing
 $xt = mt-1 + εt mt = α xt + 1-α mt-1$
• Brown Double Exponential Smoothing
 $xt = mt-1 + rt-1 α + εt mt = α xt + 1-α mt-1 rt = α mt - mt-1 + 1-α rt-1$
• Linear Holt Exponential Smoothing
 $xt = mt-1 + ϕrt-1 + εt mt = α xt + 1-α mt-1 + ϕ rt-1 rt = γ mt - mt-1 + 1-γ ϕ rt-1$
 $xt = mt-1 + ϕrt-1 + st-1-p + εt mt = α xt - s t-p + 1-α m t-1 +ϕ r t-1 rt = γ mt - m t-1 + 1-γ ϕ rt-1 st = β xt - mt + 1-β s t-p$
• Multiplicative Holt–Winters Smoothing
 $xt = mt-1 + ϕrt-1 × s t-1-p + εt mt = α xt / s t-p + 1-α m t-1 +ϕ r t-1 rt = γ mt - m t-1 + 1-γ ϕ r t-1 st = β xt / mt + 1-β s t-p$
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 errors, ${\epsilon }_{t}$ are either drawn from a normal distribution with mean zero and variance ${\sigma }^{2}$ or randomly sampled, with replacement, from a user-supplied vector.

## 4  References

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

## 5  Arguments

1:     modeNag_InitialValuesInput
On entry: indicates if nag_rand_exp_smooth (g05pmc) is continuing from a previous call or, if not, how the initial values are computed.
${\mathbf{mode}}=\mathrm{Nag_InitialValuesSupplied}$
Values for ${m}_{0}$, ${r}_{0}$ and ${s}_{-\mathit{j}}$, for $\mathit{j}=0,1,\dots ,p-1$, are supplied in init.
${\mathbf{mode}}=\mathrm{Nag_ContinueNoUpdate}$
nag_rand_exp_smooth (g05pmc) continues from a previous call using values that are supplied in r. r is not updated.
${\mathbf{mode}}=\mathrm{Nag_ContinueAndUpdate}$
nag_rand_exp_smooth (g05pmc) continues from a previous call using values that are supplied in r. r is updated.
Constraint: ${\mathbf{mode}}=\mathrm{Nag_InitialValuesSupplied}$, $\mathrm{Nag_ContinueNoUpdate}$ or $\mathrm{Nag_ContinueAndUpdate}$.
2:     nIntegerInput
On entry: the number of terms of the time series being generated.
Constraint: ${\mathbf{n}}\ge 0$.
3:     itypeNag_ExpSmoothTypeInput
On entry: the smoothing function.
${\mathbf{itype}}=\mathrm{Nag_SingleExponential}$
Single exponential.
${\mathbf{itype}}=\mathrm{Nag_BrownsExponential}$
Brown's double exponential.
${\mathbf{itype}}=\mathrm{Nag_LinearHolt}$
Linear Holt.
${\mathbf{itype}}=\mathrm{Nag_AdditiveHoltWinters}$
${\mathbf{itype}}=\mathrm{Nag_MultiplicativeHoltWinters}$
Multiplicative Holt–Winters.
Constraint: ${\mathbf{itype}}=\mathrm{Nag_SingleExponential}$, $\mathrm{Nag_BrownsExponential}$, $\mathrm{Nag_LinearHolt}$, $\mathrm{Nag_AdditiveHoltWinters}$ or $\mathrm{Nag_MultiplicativeHoltWinters}$.
4:     pIntegerInput
On entry: if ${\mathbf{itype}}=\mathrm{Nag_AdditiveHoltWinters}$ or $\mathrm{Nag_MultiplicativeHoltWinters}$, the seasonal order, $p$, otherwise p is not referenced.
Constraint: if ${\mathbf{itype}}=\mathrm{Nag_AdditiveHoltWinters}$ or $\mathrm{Nag_MultiplicativeHoltWinters}$, ${\mathbf{p}}>1$.
5:     param[$\mathit{dim}$]const doubleInput
Note: the dimension, dim, of the array param must be at least
• $1$ when ${\mathbf{itype}}=\mathrm{Nag_SingleExponential}$ or $\mathrm{Nag_BrownsExponential}$;
• $3$ when ${\mathbf{itype}}=\mathrm{Nag_LinearHolt}$;
• $4$ when ${\mathbf{itype}}=\mathrm{Nag_AdditiveHoltWinters}$ or $\mathrm{Nag_MultiplicativeHoltWinters}$.
On entry: the smoothing parameters.
If ${\mathbf{itype}}=\mathrm{Nag_SingleExponential}$ or $\mathrm{Nag_BrownsExponential}$, ${\mathbf{param}}\left[0\right]=\alpha$ and any remaining elements of param are not referenced.
If ${\mathbf{itype}}=\mathrm{Nag_LinearHolt}$, ${\mathbf{param}}\left[0\right]=\alpha$, ${\mathbf{param}}\left[1\right]=\gamma$, ${\mathbf{param}}\left[2\right]=\varphi$ and any remaining elements of param are not referenced.
If ${\mathbf{itype}}=\mathrm{Nag_AdditiveHoltWinters}$ or $\mathrm{Nag_MultiplicativeHoltWinters}$, ${\mathbf{param}}\left[0\right]=\alpha$, ${\mathbf{param}}\left[1\right]=\gamma$, ${\mathbf{param}}\left[2\right]=\beta$ and ${\mathbf{param}}\left[3\right]=\varphi$ and any remaining elements of param are not referenced.
Constraints:
• if ${\mathbf{itype}}=\mathrm{Nag_SingleExponential}$, $0.0\le \alpha \le 1.0$;
• if ${\mathbf{itype}}=\mathrm{Nag_BrownsExponential}$, $0.0<\alpha \le 1.0$;
• if ${\mathbf{itype}}=\mathrm{Nag_LinearHolt}$, $0.0\le \alpha \le 1.0$ and $0.0\le \gamma \le 1.0$ and $\varphi \ge 0.0$;
• if ${\mathbf{itype}}=\mathrm{Nag_AdditiveHoltWinters}$ or $\mathrm{Nag_MultiplicativeHoltWinters}$, $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$.
6:     init[$\mathit{dim}$]const doubleInput
Note: the dimension, dim, of the array init must be at least
• $1$ when ${\mathbf{itype}}=\mathrm{Nag_SingleExponential}$;
• $2$ when ${\mathbf{itype}}=\mathrm{Nag_BrownsExponential}$ or $\mathrm{Nag_LinearHolt}$;
• $2+{\mathbf{p}}$ when ${\mathbf{itype}}=\mathrm{Nag_AdditiveHoltWinters}$ or $\mathrm{Nag_MultiplicativeHoltWinters}$.
On entry: if ${\mathbf{mode}}=\mathrm{Nag_InitialValuesSupplied}$, 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}}=\mathrm{Nag_SingleExponential}$, ${\mathbf{init}}\left[0\right]={m}_{0}$ and any remaining elements of init are not referenced.
If ${\mathbf{itype}}=\mathrm{Nag_BrownsExponential}$ or $\mathrm{Nag_LinearHolt}$, ${\mathbf{init}}\left[0\right]={m}_{0}$ and ${\mathbf{init}}\left[1\right]={r}_{0}$ and any remaining elements of init are not referenced.
If ${\mathbf{itype}}=\mathrm{Nag_AdditiveHoltWinters}$ or $\mathrm{Nag_MultiplicativeHoltWinters}$, ${\mathbf{init}}\left[0\right]={m}_{0}$, ${\mathbf{init}}\left[1\right]={r}_{0}$ and ${\mathbf{init}}\left[2\right]$ to ${\mathbf{init}}\left[2+p-1\right]$ hold the values for ${s}_{-\mathit{j}}$, for $\mathit{j}=0,1,\dots ,p-1$. Any remaining elements of init are not referenced.
7:     vardoubleInput
On entry: the variance, ${\sigma }^{2}$ of the Normal distribution used to generate the errors ${\epsilon }_{i}$. If ${\mathbf{var}}\le 0.0$ then Normally distributed errors are not used.
8:     r[$\mathit{dim}$]doubleInput/Output
Note: the dimension, dim, of the array r must be at least
• $13$ when ${\mathbf{itype}}=\mathrm{Nag_SingleExponential}$, $\mathrm{Nag_BrownsExponential}$ or $\mathrm{Nag_LinearHolt}$;
• $13+{\mathbf{p}}$ when ${\mathbf{itype}}=\mathrm{Nag_AdditiveHoltWinters}$ or $\mathrm{Nag_MultiplicativeHoltWinters}$.
On entry: if ${\mathbf{mode}}=\mathrm{Nag_ContinueNoUpdate}$ or $\mathrm{Nag_ContinueAndUpdate}$, r must contain the values as returned by a previous call to nag_rand_exp_smooth (g05pmc), r need not be set otherwise.
On exit: if ${\mathbf{mode}}=\mathrm{Nag_ContinueNoUpdate}$, r is unchanged. Otherwise, r contains the information on the current state of smoothing.
Constraint: if ${\mathbf{mode}}=\mathrm{Nag_ContinueNoUpdate}$ or $\mathrm{Nag_ContinueAndUpdate}$, r must have been initialized by at least one call to nag_rand_exp_smooth (g05pmc) or nag_tsa_exp_smooth (g13amc) with ${\mathbf{mode}}\ne \mathrm{Nag_ContinueNoUpdate}$, and r must not have been changed since that call.
9:     state[$\mathit{dim}$]IntegerCommunication Array
Note: the dimension, $\mathit{dim}$, of this array is dictated by the requirements of associated functions that must have been previously called. This array MUST be the same array passed as argument state in the previous call to nag_rand_init_repeatable (g05kfc) or nag_rand_init_nonrepeatable (g05kgc).
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
10:   e[en]const doubleInput
On entry: if ${\mathbf{en}}>0$ and ${\mathbf{var}}\le 0.0$, a vector from which the errors, ${\epsilon }_{t}$ are randomly drawn, with replacement.
If ${\mathbf{en}}\le 0$, e is not referenced.
11:   enIntegerInput
On entry: if ${\mathbf{en}}>0$, then the length of the vector e.
If both ${\mathbf{var}}\le 0.0$ and ${\mathbf{en}}\le 0$ then ${\epsilon }_{\mathit{t}}=0.0$, for $\mathit{t}=1,2,\dots ,n$.
12:   x[n]doubleOutput
On exit: the generated time series, ${x}_{\mathit{t}}$, for $\mathit{t}=1,2,\dots ,n$.
13:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_ENUM_INT
On entry, ${\mathbf{itype}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{itype}}=\mathrm{Nag_AdditiveHoltWinters}$ or $\mathrm{Nag_MultiplicativeHoltWinters}$, ${\mathbf{p}}>1$.
On entry, ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{itype}}=\mathrm{Nag_AdditiveHoltWinters}$ or $\mathrm{Nag_MultiplicativeHoltWinters}$, ${\mathbf{p}}\ge 2$.
NE_INT
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 0$.
NE_INT_ARRAY
On entry, some of the elements of the array r have been corrupted or have not been initialized.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_INVALID_STATE
On entry, state vector has been corrupted or not initialized.
NE_REAL_ARRAY
Model unsuitable for multiplicative Holt–Winter, try a different set of parameters.
On entry, ${\mathbf{param}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: $0\le {\mathbf{param}}\left[i\right]\le 1$.
On entry, ${\mathbf{param}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{itype}}=\mathrm{Nag_BrownsExponential}$, $0<{\mathbf{param}}\left[i\right]\le 1$.
On entry, ${\mathbf{param}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{param}}\left[i\right]\ge 0$.

Not applicable.

Not applicable.

None.

## 10  Example

This example reads $11$ observations from a time series relating to the rate of the earth's rotation about its polar axis and fits an exponential smoothing model using nag_tsa_exp_smooth (g13amc).
nag_rand_exp_smooth (g05pmc) is then called multiple times to obtain simulated forecast confidence intervals.

### 10.1  Program Text

Program Text (g05pmce.c)

### 10.2  Program Data

Program Data (g05pmce.d)

### 10.3  Program Results

Program Results (g05pmce.r)