/* nag_correg_robustm_corr_huber (g02hkc) Example Program.
*
* Copyright 2023 Numerical Algorithms Group.
*
* Mark 29.2, 2023.
*
*/
#include <nag.h>
#include <stdio.h>
#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);
printf("nag_correg_robustm_corr_huber (g02hkc) Example Program Results\n");
/* Skip heading in data file */
scanf("%*[^\n]\n");
/* Read in the dimensions of X */
scanf("%" NAG_IFMT " %" NAG_IFMT " %*[^\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))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
tdx = m;
} else {
printf("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)
scanf("%lf", &X(i, j));
scanf("%*[^\n]\n");
}
/* Read in value of eps */
scanf("%lf%*[^\n]\n", &eps);
/* Set up remaining parameters */
max_iter = 100;
tol = 5e-5;
/* Set print_iter to a positive value for iteration monitoring */
print_iter = 0;
/* nag_correg_robustm_corr_huber (g02hkc).
* Robust estimation of a correlation matrix, Huber's weight
* function
*/
fflush(stdout);
nag_correg_robustm_corr_huber(n, m, x, tdx, eps, cov, theta, max_iter,
print_iter, 0, tol, &iter, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_correg_robustm_corr_huber (g02hkc).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
printf("\nnag_correg_robustm_corr_huber (g02hkc) required %" NAG_IFMT
" iterations "
"to converge\n\n",
iter);
printf("Covariance matrix\n");
l2 = 0;
for (j = 1; j <= m; ++j) {
l1 = l2 + 1;
l2 += j;
for (k = l1; k <= l2; ++k)
printf("%10.3f", cov[k - 1]);
printf("\n");
}
printf("\ntheta\n");
for (j = 1; j <= m; ++j)
printf("%10.3f\n", theta[j - 1]);
END:
NAG_FREE(x);
NAG_FREE(theta);
NAG_FREE(cov);
return exit_status;
}