def mutual_information(jointfreq, rowfreq=None, colfreq=None, unit='bit'):
"""
Calculates the Mutual Information (I) of a joint frequency. The marginal
frequencies can be given or are calculated from the joint frequency.
Arguments:
- jointfreq (``numpy.ndarray``) A normalized ``JointFreq`` instance or
``numpy.ndarray`` of rank-2, which is a joint probability distribution
function of two random variables.
- rowfreq (``numpy.ndarray``) [default: ``None``] A normalized marginal
probability distribution function for the variable along the axis =0.
- colfreq (``numpy.ndarray``) [default: ``None``] A normalized marginal
probability distribution function for the variable along the axis =1.
- unit (``str``) [defualt: ``"bit"``] Unit of the returned information.
"""
log = get_base(unit)
rowfreq = rowfreq or np.sum(jointfreq, axis=1)
colfreq = colfreq or np.sum(jointfreq, axis=0)
indfreq = np.dot(rowfreq[None].transpose(), colfreq[None])
non_zero = jointfreq != 0.
jntf = jointfreq[non_zero]
indf = indfreq[non_zero]
return np.sum(jntf * log(jntf/indf))
评论列表
文章目录