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_rand_times_smooth_exp (g05pm)

## Purpose

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

## Syntax

[r, state, x, ifail] = g05pm(mode, n, itype, p, param, init, var, r, state, e, 'en', en)
[r, state, x, ifail] = nag_rand_times_smooth_exp(mode, n, itype, p, param, init, var, r, state, e, 'en', en)

## Description

nag_rand_times_smooth_exp (g05pm) returns {xt : t = 1,2,,n}$\left\{{x}_{t}:t=1,2,\dots ,n\right\}$, a realisation 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
$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
$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 + ε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 − st − p) + (1 − α) (mt − 1 + φrt − 1) rt = γ (mt − mt − 1) + (1 − γ) φ rt − 1 st = β (xt − mt) + (1 − β) st − p
$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) × st − 1 − p + εt mt = α xt / st − p + (1 − α) (m t − 1 + φrt − 1) rt = γ (mt − mt − 1) + (1 − γ) φ rt − 1 st = β xt / mt + (1 − β) st − p
$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 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 errors, εt${\epsilon }_{t}$ are either drawn from a normal distribution with mean zero and variance σ2${\sigma }^{2}$ or randomly sampled, with replacement, from a user-supplied vector.

## References

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

## Parameters

### Compulsory Input Parameters

1:     mode – int64int32nag_int scalar
Indicates if nag_rand_times_smooth_exp (g05pm) is continuing from a previous call or, if not, how the initial values are computed.
mode = 0${\mathbf{mode}}=0$
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_rand_times_smooth_exp (g05pm) continues from a previous call using values that are supplied in r. r is not updated.
mode = 2${\mathbf{mode}}=2$
nag_rand_times_smooth_exp (g05pm) continues from a previous call using values that are supplied in r. r is updated.
Constraint: mode = 0${\mathbf{mode}}=0$, 1$1$ or 2$2$.
2:     n – int64int32nag_int scalar
The number of terms of the time series being generated.
Constraint: n0${\mathbf{n}}\ge 0$.
3:     itype – int64int32nag_int scalar
The smoothing function.
itype = 1${\mathbf{itype}}=1$
Single exponential.
itype = 2${\mathbf{itype}}=2$
Brown's 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$.
4:     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$.
5:     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$ and any remaining elements of param are not referenced.
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$.
6:     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 any 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 any 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(2 + p)${\mathbf{init}}\left(2+p\right)$ hold the values for sj${s}_{-\mathit{j}}$, for j = 0,1,,p1$\mathit{j}=0,1,\dots ,p-1$. Any remaining elements of init are not referenced.
7:     var – double scalar
The variance, σ2${\sigma }^{2}$ of the Normal distribution used to generate the errors εi${\epsilon }_{i}$. If var0.0${\mathbf{var}}\le 0.0$ then Normally distributed errors are not used.
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$.
If mode = 1${\mathbf{mode}}=1$ or 2$2$, r must contain the values as returned by a previous call to nag_rand_times_smooth_exp (g05pm), r need not be set otherwise.
Constraint: if mode = 1${\mathbf{mode}}=1$ or 2$2$, r must have been initialized by at least one call to nag_rand_times_smooth_exp (g05pm) or nag_tsa_uni_smooth_exp (g13am) with mode1${\mathbf{mode}}\ne 1$, and r must not have been changed since that call.
9:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains information on the selected base generator and its current state.
10:   e(en) – double array
If en > 0${\mathbf{en}}>0$ and var0.0${\mathbf{var}}\le 0.0$, a vector from which the errors, εt${\epsilon }_{t}$ are randomly drawn, with replacement.
If en0${\mathbf{en}}\le 0$, e is not referenced.

### Optional Input Parameters

1:     en – int64int32nag_int scalar
Default: The dimension of the array e.
If en > 0${\mathbf{en}}>0$, then the length of the vector e.
If both var0.0${\mathbf{var}}\le 0.0$ and en0${\mathbf{en}}\le 0$ then εt = 0.0${\epsilon }_{\mathit{t}}=0.0$, for t = 1,2,,n$\mathit{t}=1,2,\dots ,n$.

None.

### Output Parameters

1:     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 is unchanged. Otherwise, r contains the information on the current state of smoothing.
2:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains updated information on the state of the generator.
3:     x(n) – double array
The generated time series, xt${x}_{\mathit{t}}$, for t = 1,2,,n$\mathit{t}=1,2,\dots ,n$.
4:     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, mode0${\mathbf{mode}}\ne 0$, 1$1$ or 2$2$.
ifail = 2${\mathbf{ifail}}=2$
On entry, itype1${\mathbf{itype}}\ne 1$, 2$2$, 3$3$, 4$4$ or 5$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 = 8${\mathbf{ifail}}=8$
 On entry, mode = 1${\mathbf{mode}}=1$ or 2$2$ and the array r has not been initialized correctly.
ifail = 9${\mathbf{ifail}}=9$
On entry, the array state has not been initialized correctly.
ifail = 12${\mathbf{ifail}}=12$
itype = 5${\mathbf{itype}}=5$ and model is unsuitable for multiplicative Holt–Winter.

Not applicable.

None.

## Example

```function nag_rand_times_smooth_exp_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

mode = int64(2);
n = 11;
k = 11;
nf = 5;
nsim = 100;
alpha = 0.05;
y = [180; 135; 213; 181; 148; 204; 228; 225; 198; 200; 187];
itype = int64(3);
inits = [0; 0];
p = 0;
r = zeros(p+13,1);
param = [0.01; 1; 1];
e = [];
gsim = zeros(nsim, nf);
bsim = zeros(nsim, nf);
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);

% Fit a smoothing model (parameter r in nag_rand_times_smooth_exp and state in nag_tsa_uni_smooth_exp
% are in the same format)
[inits, fv, fse, yhat, res, dv, ad, r, ifail] =  ...
nag_tsa_uni_smooth_exp(mode, itype, int64(p), param, y, int64(k), inits, int64(nf), r);

% Simulate forecast values from the model, and don't update r
smode = int64(2);
var = dv*dv;
% Simulate nsim forecasts
for i = 1:nsim
% Simulations assuming gaussian errors
[r, state, gsim(i, :), ifail] = ...
nag_rand_times_smooth_exp(smode, int64(nf), itype, int64(p), param, inits, var, r, state, e);
% Bootstrapping errors
[r, state, bsim(i, :), ifail] = ...
nag_rand_times_smooth_exp(smode, int64(nf), itype, int64(p), param, inits, 0, r, state, res);
end

% Calculate CI based on the quantiles for each simulated forecast
q = [alpha/2; 1-alpha/2];
glim = zeros(2, nf);
blim = zeros(2, nf);
for i = 1:nf
[glim(:, i), ifail] = nag_stat_quantiles(gsim(:,i), q);
[blim(:, i), ifail] = nag_stat_quantiles(bsim(:,i), q);
end

% Display the forecast values and associated prediction intervals
fprintf('\n Initial values used:\n');
for i = 1:numel(inits);
fprintf(' %d %12.3f\n', i, inits(i));
end
fprintf('\nMean Deviation     = %12.4e\n', dv);
fprintf('\n         Observed      1-Step\n');
fprintf(' Period   Values      Forecast      Residual\n');
for i=1:n
fprintf('%4d %12.3f %12.3f %12.3f\n', i, y(i,1), yhat(i,1), res(i,1));
end
fprintf('\n                                           Simulated CI         Simulated CI\n');
fprintf(' Obs.  Forecast      Estimated CI        (Gaussian Errors)    (Bootstrap Errors)\n');
[z, ifail] = nag_stat_inv_cdf_normal(q(2));
for i = 1:nf
tmp = z*fse(i);
fprintf('%3d %10.3f %10.3f %10.3f %10.3f %10.3f %10.3f %10.3f\n', n+i, ...
fv(i), fv(i) - tmp, fv(i) + tmp, glim(1, i), glim(2, i), ...
blim(1, i), blim(2, i) );
end
fprintf(' %5.1f%% CIs were produced\n', 100*(1-alpha));
```
```

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

Simulated CI         Simulated CI
Obs.  Forecast      Estimated CI        (Gaussian Errors)    (Bootstrap Errors)
12    213.854    163.928    263.781    161.431    258.001    173.073    248.363
13    217.685    167.748    267.622    172.660    262.100    177.311    252.638
14    221.516    171.556    271.475    169.259    263.107    179.344    256.921
15    225.346    175.347    275.345    180.721    272.776    183.672    260.804
16    229.177    179.115    279.238    184.790    263.591    186.398    264.173
95.0% CIs were produced

```
```function g05pm_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

mode = int64(2);
n = 11;
k = 11;
nf = 5;
nsim = 100;
alpha = 0.05;
y = [180; 135; 213; 181; 148; 204; 228; 225; 198; 200; 187];
itype = int64(3);
inits = [0; 0];
p = 0;
r = zeros(p+13,1);
param = [0.01; 1; 1];
e = [];
gsim = zeros(nsim, nf);
bsim = zeros(nsim, nf);
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);

% Fit a smoothing model (parameter r in g05pm and state in g13am
% are in the same format)
[inits, fv, fse, yhat, res, dv, ad, r, ifail] =  ...
g13am(mode, itype, int64(p), param, y, int64(k), inits, int64(nf), r);

% Simulate forecast values from the model, and don't update r
smode = int64(2);
var = dv*dv;
% Simulate nsim forecasts
for i = 1:nsim
% Simulations assuming gaussian errors
[r, state, gsim(i, :), ifail] = ...
g05pm(smode, int64(nf), itype, int64(p), param, inits, var, r, state, e);
% Bootstrapping errors
[r, state, bsim(i, :), ifail] = ...
g05pm(smode, int64(nf), itype, int64(p), param, inits, 0, r, state, res);
end

% Calculate CI based on the quantiles for each simulated forecast
q = [alpha/2; 1-alpha/2];
glim = zeros(2, nf);
blim = zeros(2, nf);
for i = 1:nf
[glim(:, i), ifail] = g01am(gsim(:,i), q);
[blim(:, i), ifail] = g01am(bsim(:,i), q);
end

% Display the forecast values and associated prediction intervals
fprintf('\n Initial values used:\n');
for i = 1:numel(inits);
fprintf(' %d %12.3f\n', i, inits(i));
end
fprintf('\nMean Deviation     = %12.4e\n', dv);
fprintf('\n         Observed      1-Step\n');
fprintf(' Period   Values      Forecast      Residual\n');
for i=1:n
fprintf('%4d %12.3f %12.3f %12.3f\n', i, y(i,1), yhat(i,1), res(i,1));
end
fprintf('\n                                           Simulated CI         Simulated CI\n');
fprintf(' Obs.  Forecast      Estimated CI        (Gaussian Errors)    (Bootstrap Errors)\n');
[z, ifail] = g01fa(q(2));
for i = 1:nf
tmp = z*fse(i);
fprintf('%3d %10.3f %10.3f %10.3f %10.3f %10.3f %10.3f %10.3f\n', n+i, ...
fv(i), fv(i) - tmp, fv(i) + tmp, glim(1, i), glim(2, i), ...
blim(1, i), blim(2, i) );
end
fprintf(' %5.1f%% CIs were produced\n', 100*(1-alpha));
```
```

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

Simulated CI         Simulated CI
Obs.  Forecast      Estimated CI        (Gaussian Errors)    (Bootstrap Errors)
12    213.854    163.928    263.781    161.431    258.001    173.073    248.363
13    217.685    167.748    267.622    172.660    262.100    177.311    252.638
14    221.516    171.556    271.475    169.259    263.107    179.344    256.921
15    225.346    175.347    275.345    180.721    272.776    183.672    260.804
16    229.177    179.115    279.238    184.790    263.591    186.398    264.173
95.0% CIs were produced

```