/* nag_tsa_multi_part_regsn (g13dpc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2002.
 * Mark 7b revised, 2004.
 */

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

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

int main(void)
{
  /* Scalars */
  Integer  exit_status, i, j, k, m, maxlag, n, kmax;
  NagError fail;

  /* Arrays */
  double   *loglhd = 0, *parlag = 0, *pvalue = 0, *qq = 0, *se = 0, *z = 0;
  double   *x = 0;

#define Z(I, J) z[(J-1)*kmax + I - 1]

  INIT_FAIL(fail);

  exit_status = 0;

  printf("nag_tsa_multi_part_regsn (g13dpc) Example Program Results\n");

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

  if (k > 0 && n >= 1 && m >= 1)
    {
      /* Allocate arrays */
      if (!(loglhd = NAG_ALLOC(m, double)) ||
          !(parlag = NAG_ALLOC(k * k * m, double)) ||
          !(pvalue = NAG_ALLOC(m, double)) ||
          !(qq = NAG_ALLOC(k * k * m, double)) ||
          !(se = NAG_ALLOC(k * k * m, double)) ||
          !(z = NAG_ALLOC(k * n, 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", &Z(i, j));
          scanf("%*[^\n] ");
        }

      /* nag_tsa_multi_part_regsn (g13dpc).
       * Multivariate time series, partial autoregression matrices
       */
      nag_tsa_multi_part_regsn(k, n, z, m, &maxlag, parlag, se, qq, x, pvalue,
                               loglhd, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_tsa_multi_part_regsn (g13dpc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
      zprint(k, maxlag, parlag, se, qq, x, pvalue);
    }

 END:
  NAG_FREE(loglhd);
  NAG_FREE(parlag);
  NAG_FREE(pvalue);
  NAG_FREE(qq);
  NAG_FREE(se);
  NAG_FREE(z);
  NAG_FREE(x);

  return exit_status;
}

static void zprint(Integer k, Integer maxlag, double *parlag, double *se,
                   double *qq, double *x, double *pvalue)
{
  /* Scalars */
  double  sum;
  Integer i2, i, j, lf;

  /* Arrays */
  char    st[7];

#define SE(I, J, K)     se[((K-1)*k + (J-1))*k + I - 1]
#define QQ(I, J, K)     qq[((K-1)*k + (J-1))*k + I - 1]
#define PARLAG(I, J, K) parlag[((K-1)*k + (J-1))*k + I - 1]

  if (k > 1)
    {
      printf("\n");
      printf(" Partial Autoregression Matrices    Indicator"
              "  Residual   Chi-Square  Pvalue\n");
      printf("                                     Symbols"
              "   Variances   Statistic\n");
      printf(" -------------------------------    ---------"
              "  ---------  ----------- ------\n");
    }

  if (k == 1)
    {
      printf("\n");
      printf(" Partial Autoregression Function    Indicator"
              "  Residual   Chi-Square  Pvalue\n");
      printf("                                     Symbols");
      printf("   Variances");
      printf("   Statistic\n");
      printf(" -------------------------------    ---------"
              "  ---------  ----------- ------\n");
    }

  for (lf = 1; lf <= maxlag; ++lf)
    {
      for (j = 1; j <= k; ++j)
        {
          sum = PARLAG(1, j, lf);
          st[j] = '.';
          if (sum > SE(1, j, lf) * 1.96)
            st[j] = '+';
          if (sum < SE(1, j, lf) * -1.96)
            st[j] = '-';
        }

      if (k == 1)
        {
          printf("\n");
          printf(" Lag %2ld :", lf);
          for (j = 1; j <= k; ++j)
            {
              printf("%6.3f", PARLAG(1, j, lf));
              printf("%14s", "");
            }
          for (i2 = 1; i2 <= k; ++i2)
            printf("%c", st[i2]);
          printf("%14.3f%13.3f%9.3f\n", QQ(1, 1, lf), x[lf-1],
                  pvalue[lf-1]);
          printf("          ");
          for (j = 1; j <= k; ++j)
            printf("(%6.3f ) ", SE(1, j, lf));
          printf("\n");
        }
      else if (k == 2)
        {
          printf("\n");
          printf(" Lag %2ld :", lf);
          for (j = 1; j <= k; ++j)
            printf("%8.3f", PARLAG(1, j, lf));
          printf("%14s", "");
          for (i2 = 1; i2 <= k; ++i2)
            printf("%c", st[i2]);
          printf("%13.3f %12.3f %8.3f\n", QQ(1, 1, lf), x[lf-1],
                  pvalue[lf-1]);
          printf("           ");
          for (j = 1; j <= k; ++j)
            printf("(%5.3f) ", SE(1, j, lf));
          printf("\n");
        }
      else if (k == 3)
        {
          printf("\n");
          printf(" Lag %2ld :", lf);
          for (j = 1; j <= k; ++j)
            printf("%8.3f      ", PARLAG(1, j, lf));
          for (i2 = 1; i2 <= k; ++i2)
            printf("%c", st[i2]);
          printf("%12.3f%13.3f%9.3f\n", QQ(1, 1, lf), x[lf-1],
                  pvalue[lf-1]);
          printf("           ");
          for (j = 1; j <= k; ++j)
            printf("(%5.3f) ", SE(1, j, lf));
          printf("\n");
        }
      else if (k == 4)
        {
          printf("\n");
          printf(" Lag %2ld\n", lf);
          for (j = 1; j <= k; ++j)
            printf("%8.3f     ", PARLAG(1, j, lf));
          for (i2 = 1; i2 <= k; ++i2)
            printf("%c", st[i2]);
          printf("%12.3f%13.3f%9.3f\n", QQ(1, 1, lf), x[lf-1],
                  pvalue[lf-1]);
          printf("   ");
          for (j = 1; j <= k; ++j)
            printf("(%5.3f) ", SE(1, j, lf));
          printf("\n");
        }

      for (i = 2; i <= k; ++i)
        {
          for (j = 1; j <= k; ++j)
            {
              sum = PARLAG(i, j, lf);
              st[j] = '.';
              if (sum > SE(i, j, lf) * 1.96)
                st[j] = '+';
              if (sum < SE(i, j, lf) * -1.96)
                st[j] = '-';
            }
          if (k == 2)
            {
              printf("         ");
              for (j = 1; j <= k; ++j)
                printf("%8.3f", PARLAG(i, j, lf));
              printf("              ");
              for (i2 = 1; i2 <= k; ++i2)
                printf("%c", st[i2]);
              printf("%13.3f\n", QQ(i, i, lf));
              printf("           ");
              for (j = 1; j <= k; ++j)
                printf("(%5.3f) ", SE(i, j, lf));
              printf("\n");
            }
          else if (k == 3)
            {
              printf("         ");
              for (j = 1; j <= k; ++j)
                printf("%8.3f      ", PARLAG(i, j, lf));
              for (i2 = 1; i2 <= k; ++i2)
                printf("%c", st[i2]);
              printf("%12.3f\n", QQ(i, i, lf));
              printf("           ");
              for (j = 1; j <= k; ++j)
                printf("(%5.3f) ", SE(i, j, lf));
              printf("\n");
            }
          else if (k == 4)
            {
              for (j = 1; j <= k; ++j)
                printf("%8.3f     ", PARLAG(i, j, lf));
              for (i2 = 1; i2 <= k; ++i2)
                printf("%c", st[i2]);
              printf("%12.3f\n", QQ(i, i, lf));
              printf("   ");
              for (j = 1; j <= k; ++j)
                printf("(%5.3f) ", SE(i, j, lf));
              printf("\n");
            }
        }
    }

  return;
}