/* nag_robust_corr_estim (g02hkc) Example Program. * * Copyright 1996 Numerical Algorithms Group. * * Mark 4, 1996. * Mark 8 revised, 2004. * */ #include #include #include #include #define X(I,J) x[(I-1)*tdx + J-1] int main(void) { 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); Vprintf("nag_robust_corr_estim (g02hkc) Example Program Results\n\n"); /* Skip heading in data file */ Vscanf("%*[^\n]\n"); /* Read in the dimensions of X */ Vscanf("%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)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } tdx = m; } else { Vprintf("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) Vscanf("%lf", &X(i,j)); Vscanf("%*[^\n]\n"); } /* Read in value of eps */ Vscanf("%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 */ nag_robust_corr_estim(n, m, x, tdx, eps, cov, theta, max_iter, print_iter, "", tol, &iter, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_robust_corr_estim (g02hkc).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n\nnag_robust_corr_estim (g02hkc) required %ld iterations " "to converge\n\n", iter); Vprintf("Covariance matrix\n"); l2 = 0; for (j = 1; j <= m; ++j) { l1 = l2 + 1; l2 += j; for (k = l1; k <= l2; ++k) Vprintf("%10.3f", cov[k - 1]); Vprintf("\n"); } Vprintf("\n\ntheta\n"); for (j = 1; j <= m; ++j) Vprintf("%10.3f\n", theta[j - 1]); END: if (x) NAG_FREE(x); if (theta) NAG_FREE(theta); if (cov) NAG_FREE(cov); return exit_status; }