Open in the MATLAB editor: g05pm_example
function g05pm_example fprintf('g05pm example results\n\n'); % Initialize the generator to a repeatable sequence seed = [int64(1762543)]; genid = int64(1); subid = int64(1); [state, ifail] = g05kf( ... genid, subid, seed); mode = int64(2); n = 11; k = int64(11); nf = int64(20); nsim = 100; alpha = 0.05; y = [180; 135; 213; 181; 148; 204; 228; 225; 198; 200; 187]; itype = int64(3); inits = [0; 0]; p = int64(0); r = zeros(p+13,1); param = [0.01; 1; 1]; e = []; gsim = zeros(nsim, nf); bsim = zeros(nsim, nf); % 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, p, param, y, k, inits, nf, r); % Simulate forecast values from the model, and no update of 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, nf, itype, p, param, inits, var, r, state, e); % Bootstrapping errors [r, state, bsim(i, :), ifail] = ... g05pm( ... smode, nf, itype, 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\n', ad); fprintf(' 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%52s%20s\n','Simulated CI','Simulated CI'); fprintf('%14s%17s%22s%21s\n', 'Obs. Forecast', 'Estimated CI', ... '(Gaussian Errors)', '(Bootstrap Errors)'); [z, ifail] = g01fa(q(2)); for i = 1:5 tmp = z*fse(i); fprintf('%3d', n+i); fprintf('%10.3f', fv(i), fv(i)-tmp, fv(i)+tmp, glim(1:2, i), blim(1:2, i)); fprintf('\n'); end fprintf('\n %5.1f%% CIs were produced\n', 100*(1-alpha)); fig1 = figure; hold on; plot([1:n], y, 'x-r'); plot([1:n], yhat,'-g'); plot([n+1:n+20], fv,'-b'); plot([n+1:n+20], fv-z*fse,'-c'); plot([n+1:n+20], fv+z*fse,'-c'); plot([n+1:n+20], glim(1,:),'-m'); plot([n+1:n+20], glim(2,:),'-m'); plot([n+1:n+20], blim(1,:),'-k'); plot([n+1:n+20], blim(2,:),'-k'); hold off legend('observed', 'smoothed', 'forecast', 'estimated CI', ... 'simulated CI', 'bootsrtapped CI', 'Location', 'SouthEast'); xlabel('Time'); ylabel('Data'); title({'Exponential smoothing', ... '95%% confidence intervals (CIs) are shown'});
g05pm example results 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 157.822 261.312 173.073 248.363 13 217.685 167.748 267.622 171.006 258.662 176.994 252.738 14 221.516 171.556 271.475 183.687 262.402 179.870 256.473 15 225.346 175.347 275.345 176.347 275.832 183.923 261.128 16 229.177 179.115 279.238 179.421 280.585 187.008 266.360 95.0% CIs were produced