How to perform the G-test using mutual information? I performed clustering and have clusters as result. I also have reference labels. Now, I want to perform the G-test, using mutual information
scipy.stats.distributions.chi2.sf(2 * cm.sum() * metrics.mutual_info_score(clusters, reference), 6)

but I get a p-value which is 0 - so apparently clusters and references are totally unlike each other.
However, they are actually identical!
My contingency matrix is:
from sklearn.metrics.cluster import contingency_matrix
cm = contingency_matrix(clusters, reference)
print(cm)
>>> array([[  11,    0,    0,    0],
           [   0, 1269,    0,    0],
           [   0,    0,  916,    0],
           [   0,    0,    0, 2424]])

Doing the calculation of the G-test by hand, gives me the same results:
f_exp = np.round(cm.sum(axis=1)[:, np.newaxis] @ cm.sum(axis=0)[np.newaxis, :] / cm.sum()).astype(int)
scipy.stats.stats.power_divergence(cm, f_exp=f_exp, ddof=6, axis=None, lambda_=1)

What am I doing wrong?

Remember that the G-test using mutual information is a test for independence. This low, effectively zero p-value means that there is a dependence between your reference labels and your clusters: of course there is, they're the same! If your p-value were large, that would mean you have no evidence of a relation between your labels and your clusters, which would probably be a disappointing result.
Hope it was helpful!
