hide long namesshow long names
hide short namesshow short names
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_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}{xt:t=1,2,,n}, a realisation of a time series from an exponential smoothing model defined by one of five smoothing functions: where mtmt is the mean, rtrt is the trend and stst is the seasonal component at time tt with pp being the seasonal order. The errors, εtεt are either drawn from a normal distribution with mean zero and variance σ2σ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 = 0mode=0
Values for m0m0, r0r0 and sjs-j, for j = 0,1,,p1j=0,1,,p-1, are supplied in init.
mode = 1mode=1
nag_rand_times_smooth_exp (g05pm) continues from a previous call using values that are supplied in r. r is not updated.
mode = 2mode=2
nag_rand_times_smooth_exp (g05pm) continues from a previous call using values that are supplied in r. r is updated.
Constraint: mode = 0mode=0, 11 or 22.
2:     n – int64int32nag_int scalar
The number of terms of the time series being generated.
Constraint: n0n0.
3:     itype – int64int32nag_int scalar
The smoothing function.
itype = 1itype=1
Single exponential.
itype = 2itype=2
Brown's double exponential.
itype = 3itype=3
Linear Holt.
itype = 4itype=4
Additive Holt–Winters.
itype = 5itype=5
Multiplicative Holt–Winters.
Constraint: itype = 1itype=1, 22, 33, 44 or 55.
4:     p – int64int32nag_int scalar
If itype = 4itype=4 or 55, the seasonal order, pp, otherwise p is not referenced.
Constraint: if itype = 4itype=4 or 55, p > 1p>1.
5:     param( : :) – double array
Note: the dimension of the array param must be at least 11 if itype = 1itype=1 or 22, 33 if itype = 3itype=3 and at least 44 if itype = 4itype=4 or 55.
The smoothing parameters.
If itype = 1itype=1 or 22, param(1) = αparam1=α and any remaining elements of param are not referenced.
If itype = 3itype=3, param(1) = αparam1=α, param(2) = γparam2=γ, param(3) = φparam3=ϕ and any remaining elements of param are not referenced.
If itype = 4itype=4 or 55, param(1) = αparam1=α, param(2) = γparam2=γ, param(3) = βparam3=β and param(4) = φparam4=ϕ and any remaining elements of param are not referenced.
Constraints:
  • if itype = 1itype=1, 0.0α1.00.0α1.0;
  • if itype = 2itype=2, 0.0 < α1.00.0<α1.0;
  • if itype = 3itype=3, 0.0α1.00.0α1.0 and 0.0γ1.00.0γ1.0 and φ0.0ϕ0.0;
  • if itype = 4itype=4 or 55, 0.0α1.00.0α1.0 and 0.0γ1.00.0γ1.0 and 0.0β1.00.0β1.0 and φ0.0ϕ0.0.
6:     init( : :) – double array
Note: the dimension of the array init must be at least 11 if itype = 1itype=1, 22 if itype = 2itype=2 or 33 and at least 2 + p2+p if itype = 4itype=4 or 55.
If mode = 0mode=0, the initial values for m0m0, r0r0 and sjs-j, for j = 0,1,,p1j=0,1,,p-1, used to initialize the smoothing.
If itype = 1itype=1, init(1) = m0init1=m0 and any remaining elements of init are not referenced.
If itype = 2itype=2 or 33, init(1) = m0init1=m0 and init(2) = r0init2=r0 and any remaining elements of init are not referenced.
If itype = 4itype=4 or 55, init(1) = m0init1=m0, init(2) = r0init2=r0 and init(3)init3 to init(2 + p)init2+p hold the values for sjs-j, for j = 0,1,,p1j=0,1,,p-1. Any remaining elements of init are not referenced.
7:     var – double scalar
The variance, σ2σ2 of the Normal distribution used to generate the errors εiεi. If var0.0var0.0 then Normally distributed errors are not used.
8:     r( : :) – double array
Note: the dimension of the array r must be at least 1313 if itype = 1itype=1, 22 or 33 and at least 13 + p13+p if itype = 4itype=4 or 55.
If mode = 1mode=1 or 22, 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 = 1mode=1 or 22, 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 mode1mode1, 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 > 0en>0 and var0.0var0.0, a vector from which the errors, εtεt are randomly drawn, with replacement.
If en0en0, e is not referenced.

Optional Input Parameters

1:     en – int64int32nag_int scalar
Default: The dimension of the array e.
If en > 0en>0, then the length of the vector e.
If both var0.0var0.0 and en0en0 then εt = 0.0εt=0.0, for t = 1,2,,nt=1,2,,n.

Input Parameters Omitted from the MATLAB Interface

None.

Output Parameters

1:     r( : :) – double array
Note: the dimension of the array r must be at least 1313 if itype = 1itype=1, 22 or 33 and at least 13 + p13+p if itype = 4itype=4 or 55.
If mode = 1mode=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, xtxt, for t = 1,2,,nt=1,2,,n.
4:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
  ifail = 1ifail=1
On entry, mode0mode0, 11 or 22.
  ifail = 2ifail=2
On entry, itype1itype1, 22, 33, 44 or 55.
  ifail = 3ifail=3
On entry,itype = 4itype=4 or 55 and p < 2p<2.
  ifail = 4ifail=4
On entry, at least one of αα, ββ or γ > 0.0γ>0.0 or > 1.0>1.0.
On entry, itype = 2itype=2 and α = 0.0α=0.0.
On entry, φ < 0.0ϕ<0.0.
  ifail = 5ifail=5
On entry,n < 0n<0.
  ifail = 8ifail=8
On entry,mode = 1mode=1 or 22 and the array r has not been initialized correctly.
  ifail = 9ifail=9
On entry, the array state has not been initialized correctly.
  ifail = 12ifail=12
itype = 5itype=5 and model is unsuitable for multiplicative Holt–Winter.

Accuracy

Not applicable.

Further Comments

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('Absolute Deviation = %12.4e\n', ad);
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('Absolute Deviation = %12.4e\n', ad);
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


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