/* nag_tsa_multi_part_lag_corr (g13dnc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg13.h>

static void zprint(Integer, Integer, Integer, Integer,
                   double *, double *, double *);

int main(void)
{
  /* Scalars */
  Integer exit_status, i, j, k, m, maxlag, n, kmax;
  /* Arrays */
  double *parlag = 0, *pvalue = 0, *r0 = 0, *r = 0, *w = 0, *wmean = 0;
  double *x = 0;
  Nag_CovOrCorr matrix;
  NagError fail;

#define W(I, J) w[(J-1)*kmax + I - 1]

  INIT_FAIL(fail);

  exit_status = 0;

  printf("nag_tsa_multi_part_lag_corr (g13dnc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &k, &n, &m);

  if (k > 0 && n >= 1 && m >= 1) {
    /* Allocate arrays */
    if (!(parlag = NAG_ALLOC(k * k * m, double)) ||
        !(pvalue = NAG_ALLOC(m, double)) ||
        !(r0 = NAG_ALLOC(k * k, double)) ||
        !(r = NAG_ALLOC(k * k * m, double)) ||
        !(w = NAG_ALLOC(k * n, double)) ||
        !(wmean = NAG_ALLOC(k, double)) || !(x = NAG_ALLOC(m, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

    kmax = k;

    for (i = 1; i <= k; ++i) {
      for (j = 1; j <= n; ++j)
        scanf("%lf", &W(i, j));
      scanf("%*[^\n] ");
    }

    matrix = Nag_AutoCorr;

    /* nag_tsa_multi_cross_corr (g13dmc).
     * Multivariate time series, sample cross-correlation or
     * cross-covariance matrices
     */
    nag_tsa_multi_cross_corr(matrix, k, n, m, w, wmean, r0, r, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_tsa_multi_cross_corr (g13dmc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }

    /* nag_tsa_multi_part_lag_corr (g13dnc).
     * Multivariate time series, sample partial lag correlation
     * matrices, chi^2 statistics and significance levels
     */
    nag_tsa_multi_part_lag_corr(k, n, m, r0, r, &maxlag, parlag, x, pvalue,
                                &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_tsa_multi_part_lag_corr (g13dnc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
    zprint(k, n, m, k, parlag, x, pvalue);
  }

END:
  NAG_FREE(parlag);
  NAG_FREE(pvalue);
  NAG_FREE(r0);
  NAG_FREE(r);
  NAG_FREE(w);
  NAG_FREE(wmean);
  NAG_FREE(x);

  return exit_status;
}

/* Print the partial lag correlation matrices. */
static void zprint(Integer k, Integer n, Integer m, Integer ik,
                   double *parlag, double *x, double *pvalue)
{
  /* Scalars */
  double c1, c2, c3, c5, c6, c7, cnst, sum;
  Integer i2, i, j, lf, llf, ii, jj;

  /* Arrays */
  char rec[7][80];

#define PARLAG(I, J, K) parlag[((K-1)*ik + (J-1))*ik + I-1]

  cnst = 1.0 / sqrt((double) n);

  printf("\n");
  printf(" PARTIAL LAG CORRELATION MATRICES\n");
  printf(" --------------------------------\n");

  for (lf = 1; lf <= m; ++lf) {
    printf("\n");
    printf(" Lag = %2" NAG_IFMT "\n", lf);
    for (ii = 1; ii <= k; ii++) {
      for (jj = 1; jj <= k; jj++)
        printf("%9.3f", PARLAG(ii, jj, lf));
      printf("\n");
    }
  }

  printf("\n");
  printf(" Standard error = 1 / SQRT(N) = %5.3f\n", cnst);

  /* Print indicator symbols to indicate significant elements. */
  printf("\n");
  printf(" TABLES OF INDICATOR SYMBOLS\n");
  printf(" ---------------------------\n");
  printf("\n");
  printf(" For Lags 1 to %2" NAG_IFMT "\n", m);
  printf("\n");

  /* Set up the critical values */
  c1 = cnst * 3.29;
  c2 = cnst * 2.58;
  c3 = cnst * 1.96;
  c5 = -c3;
  c6 = -c2;
  c7 = -c1;

  for (i = 1; i <= k; ++i) {
    for (j = 1; j <= k; ++j) {
      printf("\n");
      printf("\n");
      if (i == j) {
        printf("Auto-correlation function for series %2" NAG_IFMT "\n", i);
        printf("\n");
      }
      else {
        printf("Cross-correlation function for series %2" NAG_IFMT ""
               " and series%2" NAG_IFMT "\n", i, j);
        printf("\n");
      }

      /* Clear the last plot with blanks */
      sprintf(&rec[0][0], "              0.005  :");
      sprintf(&rec[1][0], "        +     0.01   :");
      sprintf(&rec[2][0], "              0.05   :");
      sprintf(&rec[3][0], "   Sig. Level        : - - - - - - - - - -  Lags");
      sprintf(&rec[4][0], "              0.05   :");
      sprintf(&rec[5][0], "        -     0.01   :");
      sprintf(&rec[6][0], "              0.005  :");
      for (i2 = 0; i2 < 7; ++i2) {
        for (ii = strlen(&rec[i2][0]); ii < 80; ii++)
          rec[i2][ii] = ' ';
      }

      for (lf = 1; lf <= m; ++lf) {
        llf = lf * 2 + 21;
        sum = PARLAG(i, j, lf);
        /* Check for significance */
        if (sum > c1)
          rec[0][llf] = '*';
        if (sum > c2)
          rec[1][llf] = '*';
        if (sum > c3)
          rec[2][llf] = '*';
        if (sum < c5)
          rec[4][llf] = '*';
        if (sum < c6)
          rec[5][llf] = '*';
        if (sum < c7)
          rec[6][llf] = '*';
      }

      /* Print */
      for (i2 = 0; i2 < 7; ++i2) {
        /* Terminate the string */
        for (ii = 80; ii > 1 && rec[i2][ii - 1] == ' '; ii--);
        rec[i2][ii] = '\0';
        /* Print the string */
        printf("%s\n", &rec[i2][0]);
      }
    }
  }

  /* Print the chi-square statistics and p-values. */
  printf("\n");
  printf(" Lag     Chi-square statistic     P-value\n");
  printf(" ---     --------------------     -------\n");
  printf("\n");

  for (lf = 1; lf <= m; ++lf)
    printf("%4" NAG_IFMT " %17.3f %18.4f\n", lf, x[lf - 1], pvalue[lf - 1]);

  return;
}