/* nag_tsa_cross_corr (g13bcc) Example Program. * * Copyright 2002 Numerical Algorithms Group. * * Mark 7, 2002. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* 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); /* 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_cross_corr (g13bcc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); /* Read series length and number of lags */ fscanf(fpin, "%ld%ld%*[^\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))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read series */ for (i = 1; i <= nxy; ++i) fscanf(fpin, "%lf", &x[i-1]); fscanf(fpin, "%*[^\n] "); for (i = 1; i <= nxy; ++i) fscanf(fpin, "%lf", &y[i-1]); fscanf(fpin, "%*[^\n] "); /* Call routine to calculate cross correlations between X and Y */ /* nag_tsa_cross_corr (g13bcc). * Multivariate time series, cross-correlations */ nag_tsa_cross_corr(x, y, nxy, nl, &sxy, &r0xy, rxy, &statxy, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_tsa_cross_corr (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_cross_corr (g13bcc), see above. */ nag_tsa_cross_corr(y, x, nxy, nl, &syx, &r0yx, ryx, &statyx, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_tsa_cross_corr (g13bcc), 2nd call.\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); fprintf(fpout, " Between Between\n"); fprintf(fpout, " X and Y Y and X\n"); fprintf(fpout, "\n"); fprintf(fpout, "Standard deviation ratio%10.4f%15.4f\n", sxy, syx); fprintf(fpout, "\n"); fprintf(fpout, "Cross correlation at lag\n"); fprintf(fpout, " 0"); fprintf(fpout, "%10.4f%15.4f\n", r0xy, r0yx); for (i = 1; i <= nl; ++i) fprintf(fpout, " %4ld%10.4f%15.4f\n", i, rxy[i-1], ryx[i-1]); fprintf(fpout, "\n"); fprintf(fpout, "Test statistic %10.4f%15.4f\n", statxy, statyx); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (rxy) NAG_FREE(rxy); if (ryx) NAG_FREE(ryx); if (x) NAG_FREE(x); if (y) NAG_FREE(y); return exit_status; }