void AssocEntropy::Calc(int ** nn, int ni, int nj) { int i, j; double p; Vector sumi(ni), sumj(nj); sumi.Zero(); sumj.Zero(); sum = 0.0; // Get the row totals for (i = 0; i < ni; i++) for ( j = 0; j < nj; j++) { sumi[i] += nn[i][j]; sum += nn[i][j]; } // Get the column totals for (j = 0; j < nj; j++) for ( i = 0; i < ni; i++ ) sumj[j] += nn[i][j]; // Entropy of the x distribution hx = 0.0; for (i = 0; i < ni; i++) if (sumi[i] > FPMIN) { p = sumi[i] / sum; hx -= p * log(p); } // Entropy of the y distribution hy = 0.0; for (j = 0; j < nj; j++) if (sumj[j] > FPMIN) { p = sumj[j] / sum; hx -= p * log(p); } // Total entropy for the table h = 0.0; for (i = 0; i < ni; i++) for (j = 0; j < nj; j++) if (nn[i][j] > 0) { p = nn[i][j] / sum; h -= p * log ( p ); } hygx = h - hx; hxgy = h - hy; uygx = (hy - hygx) / ( hy + TINY ); uxgy = (hx - hxgy) / ( hx + TINY ); uxy = 2.0 * ( hx + hy - h ) / ( hx + hy + TINY ); isValid = 1; }
void AssocChi::Calc(int ** nn, int ni, int nj) { int nnj, nni, j, i, minij; double expected, temp; Vector sumi(ni), sumj(nj); sumi.Zero(); sumj.Zero(); sum = 0.0; nni = ni; nnj = nj; // Get the row totals for (i = 0; i < ni; i++) { for ( j = 0; j < nj; j++) { sumi[i] += nn[i][j]; sum += nn[i][j]; } if ( sumi[i] < FPMIN) --nni; // eliminate zero rows by reducing number } // Get the column totals for (j = 0; j < nj; j++) { for ( i = 0; i < ni; i++ ) sumj[j] += nn[i][j]; if ( sumj[j] < FPMIN) --nnj; // eliminated any zero columns } df = nni * nnj - nni - nnj + 1; // corrected degrees of freedom chisq = 0.0; for (i = 0; i < ni; i++) { for (j = 0; j < nj; j++) { expected = sumj[j] * sumi[i] / sum; temp = nn[i][j] - expected; chisq += temp * temp / (expected + TINY); } } prob = df ? gammq ( 0.5 * df, 0.5 * chisq ) : 1.0; lop = prob > 1e-100 ? -log10(prob) : 99.999; minij = nni < nnj ? nni - 1 : nnj - 1; cramrv = minij ? sqrt ( chisq / ( sum * minij ) ) : 0.0; ccc = sum ? sqrt ( chisq / ( chisq + sum) ) : 0.0; isValid = 1; }
int main(){ std::cout << sumi(std::cin); }