/* nag_robust_corr_estim (g02hkc) Example Program. * * Copyright 1996 Numerical Algorithms Group. * * Mark 4, 1996. * Mark 8 revised, 2004. * */ #include #include #include #include #include #define X(I, J) x[(I-1)*tdx + J-1] int main(int argc, char *argv[]) { FILE *fpin, *fpout; char *outfile = 0; Integer exit_status = 0, i, iter, j, k, l1, l2, m, max_iter, n, print_iter; Integer tdx; NagError fail; double *cov = 0, eps, *theta = 0, tol, *x = 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); (void) nag_example_file_io(argc, argv, "-nag_write", &outfile); fprintf(fpout, "nag_robust_corr_estim (g02hkc) Example Program Results\n\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]\n"); /* Read in the dimensions of X */ fscanf(fpin, "%ld %ld %*[^\n]\n", &n, &m); if (n > 1 && (m >= 1 && m <= n)) { if (!(x = NAG_ALLOC((n)*(m), double)) || !(theta = NAG_ALLOC(m, double)) || !(cov = NAG_ALLOC(m*(m+1)/2, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdx = m; } else { fprintf(fpout, "Invalid n or m.\n"); exit_status = 1; return exit_status; } /* Read in the x matrix */ for (i = 1; i <= n; ++i) { for (j = 1; j <= m; ++j) fscanf(fpin, "%lf", &X(i, j)); fscanf(fpin, "%*[^\n]\n"); } /* Read in value of eps */ fscanf(fpin, "%lf%*[^\n]\n", &eps); /* Set up remaining parameters */ max_iter = 100; tol = 5e-5; /* Set print_iter to positive value for iteration moiteroring */ print_iter = 1; /* nag_robust_corr_estim (g02hkc). * Robust estimation of a correlation matrix, Huber's weight * function */ if (outfile) fclose(fpout); nag_robust_corr_estim(n, m, x, tdx, eps, cov, theta, max_iter, print_iter, outfile, tol, &iter, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_robust_corr_estim (g02hkc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n\nnag_robust_corr_estim (g02hkc) required %ld iterations " "to converge\n\n", iter); fprintf(fpout, "Covariance matrix\n"); l2 = 0; for (j = 1; j <= m; ++j) { l1 = l2 + 1; l2 += j; for (k = l1; k <= l2; ++k) fprintf(fpout, "%10.3f", cov[k - 1]); fprintf(fpout, "\n"); } fprintf(fpout, "\n\ntheta\n"); for (j = 1; j <= m; ++j) fprintf(fpout, "%10.3f\n", theta[j - 1]); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (x) NAG_FREE(x); if (theta) NAG_FREE(theta); if (cov) NAG_FREE(cov); return exit_status; }