def discrete_shannon_entropy(x, n_bins, _range=None): from R import digamma #@UnresolvedImport hist = density(x, n_bins, _range, steps=0, hist=1) x = hist[:, 1] v = Numeric.sum(x) s = shape(x) z = zeros(s, Float) for i in range(s[0]): z[i] = digamma(x[i] + 1) return -Numeric.sum(x / v * (z - digamma(v + 1)))
def discrete_shannon_entropy(x, n_bins, _range = None): from R import digamma #@UnresolvedImport hist = density(x, n_bins, _range, steps = 0, hist = 1) x = hist[:,1] v = Numeric.sum(x) s = shape(x) z = zeros(s, Float) for i in range(s[0]): z[i] = digamma(x[i] + 1) return - Numeric.sum(x / v * (z - digamma(v + 1)))
def mutualInformation(counts): """ bayesian estimator for the mutual information between two series of discrete numbers. 'counts': matrix of counts """ from R import digamma #@UnresolvedImport counts = Numeric.array(counts).astype(Float32) v = Numeric.sum(Numeric.sum(counts)) r = digamma(v + 1) s = shape(counts) z = Numeric.zeros(shape(counts), Float) for i in range(s[0]): for j in range(s[1]): z[i, j] = digamma(counts[i, j] + 1) a = Numeric.sum(Numeric.sum(counts / v * (z - r))) m1 = Numeric.sum(counts, 1) m0 = Numeric.sum(counts) z0 = Numeric.zeros(shape(m0), Float) z1 = Numeric.zeros(shape(m1), Float) for i in range(len(m0)): z0[i] = digamma(m0[i] + 1) for i in range(len(z1)): z1[i] = digamma(m1[i] + 1) b = Numeric.sum(m0 / v * (z0 - r)) c = Numeric.sum(m1 / v * (z1 - r)) return a - b - c
def mutualInformation(counts): """ bayesian estimator for the mutual information between two series of discrete numbers. 'counts': matrix of counts """ from R import digamma #@UnresolvedImport counts = Numeric.array(counts).astype(Float32) v = Numeric.sum(Numeric.sum(counts)) r = digamma(v + 1) s = shape(counts) z = Numeric.zeros(shape(counts), Float) for i in range(s[0]): for j in range(s[1]): z[i,j] = digamma(counts[i,j] + 1) a = Numeric.sum(Numeric.sum(counts / v * (z - r))) m1 = Numeric.sum(counts, 1) m0 = Numeric.sum(counts) z0 = Numeric.zeros(shape(m0), Float) z1 = Numeric.zeros(shape(m1), Float) for i in range(len(m0)): z0[i] = digamma(m0[i] + 1) for i in range(len(z1)): z1[i] = digamma(m1[i] + 1) b = Numeric.sum(m0 / v * (z0 - r)) c = Numeric.sum(m1 / v * (z1 - r)) return a - b - c