/* nag_tsa_multi_part_lag_corr (g13dnc) Example Program. * * Copyright 2002 Numerical Algorithms Group. * * Mark 7, 2002. */ #include #include #include #include #include #include #include static void zprint(Integer, Integer, Integer, Integer, double *, double *, double *, FILE *); int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* 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); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); exit_status = 0; fprintf(fpout, "nag_tsa_multi_part_lag_corr (g13dnc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%ld%ld%ld%*[^\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))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } kmax = k; for (i = 1; i <= k; ++i) { for (j = 1; j <= n; ++j) fscanf(fpin, "%lf", &W(i, j)); fscanf(fpin, "%*[^\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) { fprintf(fpout, "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) { fprintf(fpout, "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, fpout); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (parlag) NAG_FREE(parlag); if (pvalue) NAG_FREE(pvalue); if (r0) NAG_FREE(r0); if (r) NAG_FREE(r); if (w) NAG_FREE(w); if (wmean) NAG_FREE(wmean); if (x) 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, FILE *fpout) { /* 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); fprintf(fpout, "\n"); fprintf(fpout, " PARTIAL LAG CORRELATION MATRICES\n"); fprintf(fpout, " --------------------------------\n"); for (lf = 1; lf <= m; ++lf) { fprintf(fpout, "\n"); fprintf(fpout, " Lag = %2ld\n", lf); for (ii = 1; ii <= k; ii++) { for (jj = 1; jj <= k; jj++) fprintf(fpout, "%9.3f", PARLAG(ii, jj, lf)); fprintf(fpout, "\n"); } } fprintf(fpout, "\n"); fprintf(fpout, " Standard error = 1 / SQRT(N) = %5.3f\n", cnst); /* Print indicator symbols to indicate significant elements. */ fprintf(fpout, "\n"); fprintf(fpout, " TABLES OF INDICATOR SYMBOLS\n"); fprintf(fpout, " ---------------------------\n"); fprintf(fpout, "\n"); fprintf(fpout, " For Lags 1 to %2ld\n", m); fprintf(fpout, "\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) { fprintf(fpout, "\n"); fprintf(fpout, "\n"); if (i == j) { fprintf(fpout, "Auto-correlation function for series %2ld\n", i); fprintf(fpout, "\n"); } else { fprintf(fpout, "Cross-correlation function for series %2ld" " and series%2ld\n", i, j); fprintf(fpout, "\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 */ fprintf(fpout, "%s\n", &rec[i2][0]); } } } /* Print the chi-square statistics and p-values. */ fprintf(fpout, "\n"); fprintf(fpout, " Lag Chi-square statistic P-value\n"); fprintf(fpout, " --- -------------------- -------\n"); fprintf(fpout, "\n"); for (lf = 1; lf <= m; ++lf) fprintf(fpout, "%4ld %17.3f %18.4f\n", lf, x[lf-1], pvalue[lf-1]); return; }