/* initialize a three-dimensional contingency table and the marginals. */ void fill_3d_table(int *xx, int *yy, int *zz, int ****n, int ***ni, int ***nj, int **nk, int llx, int lly, int llz, int num) { int i = 0, j = 0, k = 0; *n = alloc3dcont(llz, llx, lly); *ni = alloc2dcont(llz, llx); *nj = alloc2dcont(llz, lly); *nk = alloc1dcont(llz); /* compute the joint frequency of x, y, and z. */ for (k = 0; k < num; k++) (*n)[zz[k] - 1][xx[k] - 1][yy[k] - 1]++; /* compute the marginals. */ for (i = 0; i < llx; i++) for (j = 0; j < lly; j++) for (k = 0; k < llz; k++) { (*ni)[k][i] += (*n)[k][i][j]; (*nj)[k][j] += (*n)[k][i][j]; (*nk)[k] += (*n)[k][i][j]; }/*FOR*/ }/*FILL_3D_TABLE*/
/* conditional mutual information, to be used in C code. */ double c_cmi(int *xx, int *llx, int *yy, int *lly, int *zz, int *llz, int *num) { int i = 0, j = 0, k = 0; int ***n = NULL, **ni = NULL, **nj = NULL, *nk = NULL; double res = 0; /* initialize the contingency table and the marginal frequencies. */ n = alloc3dcont(*llx, *lly, *llz); ni = alloc2dcont(*llx, *llz); nj = alloc2dcont(*lly, *llz); nk = alloc1dcont(*llz); /* compute the joint frequency of x, y, and z. */ for (k = 0; k < *num; k++) { n[xx[k] - 1][yy[k] - 1][zz[k] - 1]++; }/*FOR*/ /* compute the marginals. */ for (i = 0; i < *llx; i++) for (j = 0; j < *lly; j++) for (k = 0; k < *llz; k++) { ni[i][k] += n[i][j][k]; nj[j][k] += n[i][j][k]; nk[k] += n[i][j][k]; }/*FOR*/ /* compute the conditional mutual information from the joint and marginal frequencies. */ for (i = 0; i < *llx; i++) for (j = 0; j < *lly; j++) for (k = 0; k < *llz; k++) res += MI_PART(n[i][j][k], ni[i][k], nj[j][k], nk[k]); res = res/(*num); return res; }/*C_CMI*/