/* nag_rand_exp_smooth (g05pmc) Example Program. * * Copyright 2008, Numerical Algorithms Group. * * Mark 9, 2009. */ /* Pre-processor includes */ #include #include #include #include #include #include #include #include #define BLIM(I, J) blim[J*2 + I] #define BSIM(I, J) bsim[J*nsim + I] #define GLIM(I, J) glim[J*2 + I] #define GSIM(I, J) gsim[J*nsim + I] int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* Integer scalar and array declarations */ Integer exit_status = 0; Integer en, i, ival, j, k, lstate, n, nf, nsim, p, nq; Integer *state = 0; /* NAG structures */ NagError fail; Nag_TailProbability tail; Nag_InitialValues mode; Nag_ExpSmoothType itype; /* Double scalar and array declarations */ double ad, alpha, dv, tmp, var, z, bvar; double *blim = 0, *bsim = 0, *e = 0, *fse = 0, *fv = 0; double *glim = 0, *gsim = 0, *init = 0, *param = 0, *r = 0; double *res = 0, *tsim1 = 0, *tsim2 = 0, *y = 0, *yhat = 0; double q[2]; /* Character scalar and array declarations */ char smode[26], sitype[30]; /* Choose the base generator */ Nag_BaseRNG genid = Nag_Basic; Integer subid = 0; /* Set the seed */ Integer seed[] = { 1762543 }; Integer lseed = 1; /* Initialise the error structure */ INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_rand_exp_smooth (g05pmc) Example Program Results\n\n"); /* Get the length of the state array */ lstate = -1; nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_init_repeatable (g05kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Skip headings in data file */ fscanf(fpin, "%*[^\n] "); /* Read in the initial arguments and check array sizes */ fscanf(fpin, "%s%s%ld%ld%ld%lf%*[^\n] ", smode, sitype, &n, &nf, &nsim, &alpha); /* * nag_enum_name_to_value (x04nac). * Converts NAG enum member name to value */ mode = (Nag_InitialValues) nag_enum_name_to_value(smode); itype = (Nag_ExpSmoothType) nag_enum_name_to_value(sitype); /* Allocate arrays */ if (!(blim = NAG_ALLOC(2*nf, double)) || !(bsim = NAG_ALLOC(nsim*nf, double)) || !(e = NAG_ALLOC(1, double)) || !(fse = NAG_ALLOC(nf, double)) || !(fv = NAG_ALLOC(nf, double)) || !(glim = NAG_ALLOC(2*nf, double)) || !(gsim = NAG_ALLOC(nsim*nf, double)) || !(res = NAG_ALLOC(n, double)) || !(tsim1 = NAG_ALLOC(nf, double)) || !(tsim2 = NAG_ALLOC(nf, double)) || !(y = NAG_ALLOC(n, double)) || !(yhat = NAG_ALLOC(n, double)) || !(state = NAG_ALLOC(lstate, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Initialise the generator to a repeatable sequence */ nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_init_repeatable (g05kfc).\n%s\n", fail.message); exit_status = 1; goto END; } for (i = 0; i < n; i++) fscanf(fpin, "%lf ", &y[i]); fscanf(fpin, "%*[^\n] "); /* Read in the itype dependent arguments (skipping headings) */ fscanf(fpin, "%*[^\n] "); if (itype == Nag_SingleExponential) { /* Single exponential smoothing required */ if (!(param = NAG_ALLOC(1, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } fscanf(fpin, "%lf%*[^\n] ", ¶m[0]); p = 0; ival = 1; } else if (itype == Nag_BrownsExponential) { /* Browns exponential smoothing required */ if (!(param = NAG_ALLOC(2, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } fscanf(fpin, "%lf %lf%*[^\n] ", ¶m[0], ¶m[1]); p = 0; ival = 2; } else if (itype == Nag_LinearHolt) { /* Linear Holt smoothing required */ if (!(param = NAG_ALLOC(3, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } fscanf(fpin, "%lf %lf %lf%*[^\n] ", ¶m[0], ¶m[1], ¶m[2]); p = 0; ival = 2; } else if (itype == Nag_AdditiveHoltWinters) { /* Additive Holt Winters smoothing required */ if (!(param = NAG_ALLOC(4, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } fscanf(fpin, "%lf %lf %lf %lf %ld%*[^\n] ", ¶m[0], ¶m[1], ¶m[2], ¶m[3], &p); ival = p+2; } else if (itype == Nag_MultiplicativeHoltWinters) { /* Multiplicative Holt Winters smoothing required */ if (!(param = NAG_ALLOC(4, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } fscanf(fpin, "%lf %lf %lf %lf %ld%*[^\n] ", ¶m[0], ¶m[1], ¶m[2], ¶m[3], &p); ival = p+2; } else { fprintf(fpout, "%s is an unknown type\n", sitype); exit_status = -1; goto END; } /* Allocate arrays */ if (!(init = NAG_ALLOC(p+2, double)) || !(r = NAG_ALLOC(p+13, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read in the mode dependent arguments (skipping headings) */ fscanf(fpin, "%*[^\n] "); if (mode == Nag_InitialValuesSupplied) { /* User supplied initial values*/ for (i = 0; i < ival; i++) fscanf(fpin, "%lf ", &init[i]); fscanf(fpin, "%*[^\n] "); } else if (mode == Nag_ContinueAndUpdate) { /* Continuing from a previously saved R */ for (i = 0; i < p+13; i++) fscanf(fpin, "%lf ", &r[i]); fscanf(fpin, "%*[^\n] "); } else if (mode == Nag_EstimateInitialValues) { /* Initial values calculated from first k observations */ fscanf(fpin, "%ld%*[^\n] ", &k); } else { fprintf(fpout, "%s is an unknown mode\n", smode); exit_status = -1; goto END; } /* Fit a smoothing model (parameter r in * nag_rand_exp_smooth (g05pmc) and state in g13amc are in the same format) */ nag_tsa_exp_smooth(mode, itype, p, param, n, y, k, init, nf, fv, fse, yhat, res, &dv, &ad, r, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_tsa_exp_smooth (g13amc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Simulate forecast values from the model, and don't update r */ var = dv*dv; en = n; /* Change the mode used to continue from fit model */ mode = Nag_ContinueAndUpdate; /* Simulate nsim forecasts */ for (i = 0; i < nsim; i++) { /* Simulations assuming gaussian errors */ nag_rand_exp_smooth(mode, nf, itype, p, param, init, var, r, state, e, 0, tsim1, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_exp_smooth (g05pmc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Bootstrapping errors */ bvar = 0.0e0; nag_rand_exp_smooth(mode, nf, itype, p, param, init, bvar, r, state, res, en, tsim2, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_exp_smooth (g05pmc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Copy and transpose the simulated values */ for (j = 0; j < nf; j++) { GSIM(i, j) = tsim1[j]; BSIM(i, j) = tsim2[j]; } } /* Calculate CI based on the quantiles for each simulated forecast */ q[0] = alpha/2.0e0; q[1] = 1.0e0-q[0]; nq = 2; for (i = 0; i < nf; i++) { nag_double_quantiles(nsim, &GSIM(0, i), nq, q, &GLIM(0, i), &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_double_quantiles (g01amc).\n%s\n", fail.message); exit_status = 1; goto END; } nag_double_quantiles(nsim, &BSIM(0, i), nq, q, &BLIM(0, i), &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_double_quantiles (g01amc).\n%s\n", fail.message); exit_status = 1; goto END; } } /* Display the forecast values and associated prediction intervals */ fprintf(fpout, "Initial values used:\n"); for (i = 0; i < ival; i++) fprintf(fpout, "%4ld %12.3f \n", i+1, init[i]); fprintf(fpout, "\n"); fprintf(fpout, "Mean Deviation = %13.4e\n", dv); fprintf(fpout, "Absolute Deviation = %13.4e\n\n", ad); fprintf(fpout, " Observed 1-Step\n"); fprintf(fpout, " Period Values Forecast Residual\n\n"); for (i = 0; i < n; i++) fprintf(fpout, "%4ld %11.3f %11.3f %11.3f\n", i+1, y[i], yhat[i], res[i]); fprintf(fpout, "\n"); tail = Nag_LowerTail; z = nag_deviates_normal(tail, q[1], &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_deviates_normal (g01fac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, " Simulated CI" " Simulated CI\n"); fprintf(fpout, " Obs. Forecast Estimated CI (Gaussian Errors)" " (Bootstrap Errors)\n"); for (i = 0; i < nf; i++) { tmp = z*fse[i]; fprintf(fpout, "%3ld %10.3f %10.3f %10.3f" " %10.3f %10.3f %10.3f %10.3f\n", n+i+1, fv[i], fv[i]-tmp, fv[i]+tmp, GLIM(0, i), GLIM(1, i), BLIM(0, i), BLIM(1, i)); } fprintf(fpout, " %5.1f%% CIs were produced\n", 100.0e0*(1.0e0 - alpha)); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (blim) NAG_FREE(blim); if (bsim) NAG_FREE(bsim); if (e) NAG_FREE(e); if (fse) NAG_FREE(fse); if (fv) NAG_FREE(fv); if (glim) NAG_FREE(glim); if (gsim) NAG_FREE(gsim); if (init) NAG_FREE(init); if (param) NAG_FREE(param); if (r) NAG_FREE(r); if (res) NAG_FREE(res); if (tsim1) NAG_FREE(tsim1); if (tsim2) NAG_FREE(tsim2); if (y) NAG_FREE(y); if (yhat) NAG_FREE(yhat); if (state) NAG_FREE(state); return exit_status; }