/* nag_rand_egarch (g05pgc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */
/* Pre-processor includes */
#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg05.h>

int main(void)
{
  /* Integer scalar and array declarations */
  Integer exit_status = 0;
  Integer lr, i, lstate;
  Integer *state = 0;

  /* NAG structures */
  NagError fail;
  Nag_Boolean fcall;

  /* Double scalar and array declarations */
  double *et = 0, *ht = 0, *r = 0;

  /* Number of terms to generate */
  Integer num = 10;

  /* Normally distributed errors */
  Nag_ErrorDistn dist = Nag_NormalDistn;
  Integer df = 0;

  /* Set up the parameters for the series being generated */
  Integer ip = 1;
  Integer iq = 1;
  double theta[] = { 0.1e0, -0.3e0, 0.1e0, 0.9e0 };

  /* Choose the base generator */
  Nag_BaseRNG genid = Nag_Basic;
  Integer subid = 0;

  /* Set the seed */
  Integer seed[] = { 1762543 };
  Integer lseed = 1;

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

  printf("nag_rand_egarch (g05pgc) Example Program Results\n\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) {
    printf("Error from nag_rand_init_repeatable (g05kfc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Calculate the size of the reference vector */
  lr = 2 * (2 * iq + ip + 2);

  /* Allocate arrays */
  if (!(et = NAG_ALLOC(num, double)) ||
      !(ht = NAG_ALLOC(num, double)) ||
      !(r = NAG_ALLOC(lr, double)) || !(state = NAG_ALLOC(lstate, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Initialize the generator to a repeatable sequence */
  nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_rand_init_repeatable (g05kfc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Generate the first realization */
  fcall = Nag_TRUE;
  nag_rand_egarch(dist, num, ip, iq, theta, df, ht, et, fcall, r, lr,
                  state, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_rand_egarch (g05pgc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Display the results */
  printf("  Realization Number 1\n");
  printf("    I            HT(I)            ET(I)\n");
  printf("   --------------------------------------\n");
  for (i = 0; i < num; i++)
    printf(" %5" NAG_IFMT " %16.4f %16.4f\n", i + 1, ht[i], et[i]);
  printf("\n");

  /*  Generate a second realization */
  fcall = Nag_FALSE;
  nag_rand_egarch(dist, num, ip, iq, theta, df, ht, et, fcall, r, lr,
                  state, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_rand_egarch (g05pgc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Display the results */
  printf("  Realization Number 2\n");
  printf("    I            HT(I)            ET(I)\n");
  printf("   --------------------------------------\n");
  for (i = 0; i < num; i++)
    printf(" %5" NAG_IFMT " %16.4f %16.4f\n", i + 1, ht[i], et[i]);

END:
  NAG_FREE(et);
  NAG_FREE(ht);
  NAG_FREE(r);
  NAG_FREE(state);

  return exit_status;
}