NAG Library Manual, Mark 30
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_tsa_uni_smooth_exp (g13amc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 */
/* Pre-processor includes */
#include <math.h>
#include <nag.h>
#include <stdio.h>

int main(void) {
  /* 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[40], sitype[40];
  /* NAG structures */
  Nag_InitialValues mode;
  Nag_ExpSmoothType itype;
  NagError fail;

  /* Initialize the error structure */
  INIT_FAIL(fail);

  printf("nag_tsa_uni_smooth_exp (g13amc) Example Program Results\n");

  /* Skip headings in data file */
  scanf("%*[^\n] ");
  /* Read in the initial arguments */
  scanf("%39s%39s%" NAG_IFMT "%" NAG_IFMT "%*[^\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))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read in the observed data */
  for (i = 0; i < n; i++)
    scanf("%lf ", &y[i]);
  scanf("%*[^\n] ");
  /* Read in the itype dependent arguments (skipping headings) */
  scanf("%*[^\n] ");
  if (itype == Nag_SingleExponential) {
    /* Single exponential smoothing required */
    if (!(param = NAG_ALLOC(1, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    scanf("%lf%*[^\n] ", &param[0]);
    p = 0;
    ival = 1;
  } else if (itype == Nag_BrownsExponential) {
    /* Browns exponential smoothing required */
    if (!(param = NAG_ALLOC(2, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    scanf("%lf %lf%*[^\n] ", &param[0], &param[1]);
    p = 0;
    ival = 2;
  } else if (itype == Nag_LinearHolt) {
    /* Linear Holt smoothing required */
    if (!(param = NAG_ALLOC(3, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    scanf("%lf %lf %lf%*[^\n] ", &param[0], &param[1], &param[2]);
    p = 0;
    ival = 2;
  } else if (itype == Nag_AdditiveHoltWinters) {
    /* Additive Holt Winters smoothing required */
    if (!(param = NAG_ALLOC(4, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    scanf("%lf %lf %lf %lf %" NAG_IFMT "%*[^\n] ", &param[0], &param[1],
          &param[2], &param[3], &p);
    ival = p + 2;
  } else if (itype == Nag_MultiplicativeHoltWinters) {
    /* Multiplicative Holt Winters smoothing required */
    if (!(param = NAG_ALLOC(4, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    scanf("%lf %lf %lf %lf %" NAG_IFMT "%*[^\n] ", &param[0], &param[1],
          &param[2], &param[3], &p);
    ival = p + 2;
  } else {
    printf("%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))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read in the mode dependent arguments (skipping headings) */
  scanf("%*[^\n] ");
  if (mode == Nag_InitialValuesSupplied) {
    /* User supplied initial values */
    for (i = 0; i < ival; i++)
      scanf("%lf ", &init[i]);
    scanf("%*[^\n] ");
  } else if (mode == Nag_ContinueAndUpdate) {
    /* Continuing from a previously saved R */
    for (i = 0; i < p + 13; i++)
      scanf("%lf ", &r[i]);
    scanf("%*[^\n] ");
  } else if (mode == Nag_EstimateInitialValues) {
    /* Initial values calculated from first k observations */
    scanf("%" NAG_IFMT "%*[^\n] ", &k);
  } else {
    printf("%s is an unknown mode\n", smode);
    exit_status = -1;
    goto END;
  }

  /* Call the library routine to smooth the series */
  nag_tsa_uni_smooth_exp(mode, itype, p, param, n, y, k, init, nf, fv, fse,
                         yhat, res, &dv, &ad, r, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_tsa_uni_smooth_exp (g13amc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Display the output */
  printf("Initial values used:\n");
  for (i = 0; i < ival; i++)
    printf("%4" NAG_IFMT "   %12.3f  \n", i + 1, init[i]);
  printf("\n");
  printf("Mean Deviation     =  %13.4e\n", dv);
  printf("Absolute Deviation =  %13.4e\n", ad);
  printf("\n");
  printf("         Observed      1-Step\n");
  printf(" Period   Values      Forecast      Residual\n");
  for (i = 0; i < n; i++)
    printf("%4" NAG_IFMT "   %12.3f   %12.3f   %12.3f\n", i + 1, y[i], yhat[i],
           res[i]);
  printf("\n");
  printf("         Forecast     Standard\n");
  printf(" Period   Values       Errors\n");
  for (i = 0; i < nf; i++)
    printf("%4" NAG_IFMT "   %12.3f   %12.3f  \n", n + i + 1, fv[i], fse[i]);

END:
  NAG_FREE(fse);
  NAG_FREE(fv);
  NAG_FREE(init);
  NAG_FREE(param);
  NAG_FREE(r);
  NAG_FREE(res);
  NAG_FREE(y);
  NAG_FREE(yhat);

  return exit_status;
}