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

NAG CL Interface Introduction
Example description
/* nag_tsa_multi_xcorr (g13bcc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 */

#include <nag.h>
#include <stdio.h>

int main(void) {
  /* Scalars */
  double r0xy, r0yx, statxy, statyx, sxy, syx;
  Integer exit_status, i, nl, nxy;
  NagError fail;

  /* Arrays */
  double *rxy = 0, *ryx = 0, *x = 0, *y = 0;

  INIT_FAIL(fail);

  exit_status = 0;

  printf("nag_tsa_multi_xcorr (g13bcc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Read series length and number of lags */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &nxy, &nl);

  /* Allocate memory */
  if (!(rxy = NAG_ALLOC(nl, double)) || !(ryx = NAG_ALLOC(nl, double)) ||
      !(x = NAG_ALLOC(nxy, double)) || !(y = NAG_ALLOC(nxy, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read series */
  for (i = 1; i <= nxy; ++i)
    scanf("%lf", &x[i - 1]);
  scanf("%*[^\n] ");

  for (i = 1; i <= nxy; ++i)
    scanf("%lf", &y[i - 1]);
  scanf("%*[^\n] ");

  /* Call routine to calculate cross correlations between X and Y */
  /* nag_tsa_multi_xcorr (g13bcc).
   * Multivariate time series, cross-correlations
   */
  nag_tsa_multi_xcorr(x, y, nxy, nl, &sxy, &r0xy, rxy, &statxy, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_tsa_multi_xcorr (g13bcc), 1st call.\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Call routine to calculate cross correlations between Y and X */
  /* nag_tsa_multi_xcorr (g13bcc), see above. */
  nag_tsa_multi_xcorr(y, x, nxy, nl, &syx, &r0yx, ryx, &statyx, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_tsa_multi_xcorr (g13bcc), 2nd call.\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  printf("                           Between        Between\n");
  printf("                           X and Y        Y and X\n");
  printf("\n");
  printf("Standard deviation ratio%10.4f%15.4f\n", sxy, syx);
  printf("\n");
  printf("Cross correlation at lag\n");
  printf("                       0");
  printf("%10.4f%15.4f\n", r0xy, r0yx);
  for (i = 1; i <= nl; ++i)
    printf("                    %4" NAG_IFMT "%10.4f%15.4f\n", i, rxy[i - 1],
           ryx[i - 1]);
  printf("\n");
  printf("Test statistic          %10.4f%15.4f\n", statxy, statyx);

END:
  NAG_FREE(rxy);
  NAG_FREE(ryx);
  NAG_FREE(x);
  NAG_FREE(y);

  return exit_status;
}