/* nag_tsa_exp_smooth (g13amc) Example Program. * * Copyright 2008, Numerical Algorithms Group. * * Mark 9, 2009. */ /* Pre-processor includes */ #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* Integer scalar and array declarations */ Integer exit_status = 0; Integer i, ival, k, n, nf, p; /* Double scalar and array declarations */ double ad, dv; double *fse = 0, *fv = 0, *init = 0, *param = 0, *r = 0, *res = 0; double *y = 0, *yhat = 0; /* Character scalar and array declarations */ char smode[26], sitype[30]; /* NAG structures */ Nag_InitialValues mode; Nag_ExpSmoothType itype; NagError fail; /* 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_tsa_exp_smooth (g13amc) Example Program Results\n"); /* Skip headings in data file*/ fscanf(fpin, "%*[^\n] "); /* Read in the initial arguments */ fscanf(fpin, "%s%s%ld%ld%*[^\n] ", smode, sitype, &n, &nf); /* * 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); if (!(fse = NAG_ALLOC(nf, double)) || !(fv = NAG_ALLOC(nf, double)) || !(res = NAG_ALLOC(n, double)) || !(y = NAG_ALLOC(n, double)) || !(yhat = NAG_ALLOC(n, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read in the observed data */ 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 some more memory */ 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; } /* Call the library routine to smooth the series */ 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; } /* Display the output */ 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", ad); fprintf(fpout, "\n"); fprintf(fpout, " Observed 1-Step\n"); fprintf(fpout, " Period Values Forecast Residual\n"); for (i = 0; i < n; i++) fprintf(fpout, "%4ld %12.3f %12.3f %12.3f\n", i+1, y[i], yhat[i], res[i]); fprintf(fpout, "\n"); fprintf(fpout, " Forecast Standard\n"); fprintf(fpout, " Period Values Errors\n"); for (i = 0; i < nf; i++) fprintf(fpout, "%4ld %12.3f %12.3f \n", n+i+1, fv[i], fse[i]); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (fse) NAG_FREE(fse); if (fv) NAG_FREE(fv); if (init) NAG_FREE(init); if (param) NAG_FREE(param); if (r) NAG_FREE(r); if (res) NAG_FREE(res); if (y) NAG_FREE(y); if (yhat) NAG_FREE(yhat); return exit_status; }