dit.divergences

Divergences, measuring the distance between distributions. They are not necessarily true metrics, but some are.

Functions

alpha_divergence

The alpha divergence of dist1 and dist2, as used in Information Geometry.

bhattacharyya_coefficient

Compute the Bhattacharyya coefficient.

chernoff_information

Compute the Chernoff information.

copy_mutual_information

Computes the copy mutual information.

coupling_metric

Residual entropy of the minimum-entropy coupling with the given marginals.

coupling_min_residual_entropy

Minimum residual entropy over couplings with the given marginals.

cross_entropy

The cross entropy between dist1 and dist2.

earth_movers_distance

Compute the Earth Mover's Distance (EMD) between dist1 and dist2.

earth_movers_distance_coupling

Compute the optimal transport coupling underlying the Earth Mover's Distance between dist1 and dist2.

f_divergence

The Csiszar f-divergence of dist1 and dist2.

hellinger_distance

Compute the Hellinger distance.

hellinger_divergence

The Hellinger divergence of dist1 and dist2.

hellinger_sum

The Hellinger sum/integral of dist1 and dist2, used to define other divergences.

hypercontractivity_coefficient

Computes the hypercontractivity coefficient:

jensen_shannon_divergence

The Jensen-Shannon Divergence: \(H(sum(w_i*P_i)) - sum(w_i*H(P_i))\).

kullback_leibler_divergence

The Kullback-Liebler divergence between dist1 and dist2.

max_caekl_coupling

Coupling with maximal CAEKL mutual information.

max_dual_total_correlation_coupling

Coupling with maximal dual total correlation (binding information).

max_total_correlation_coupling

Coupling with maximal total correlation (multi-information).

maximal_cross_entropy

The maximal cross entropy [YT20a]: the largest cross entropy, relative to dist2, of any joint distribution whose marginals on the two groups of variables in rvs match those of dist1,

maximum_correlation

Compute the (conditional) maximum or Renyi correlation between two variables:

min_residual_entropy_coupling

Coupling with minimal residual entropy (variation of information).

relative_entropy

The Kullback-Liebler divergence between dist1 and dist2.

renyi_divergence

The Renyi divergence of dist1 and dist2.

tsallis_divergence

The Tsallis divergence of dist1 and dist2.

variational_distance

Compute the variational distance.

Reference

alpha_divergence(dist1, dist2, alpha=1.0, rvs=None)[source]

The alpha divergence of dist1 and dist2, as used in Information Geometry. Note there is more than one inequivalent definition of “alpha divergence” in the literature, this one comes from http://en.wikipedia.org/wiki/Information_geometry .

Parameters:
  • dist1 (Distribution) – The first distribution in the alpha divergence.

  • dist2 (Distribution) – The second distribution in the alpha divergence.

  • alpha (float, 1) – The divergence is a one parameter family in alpha.

  • rvs (list, None) – The indexes of the random variable used to calculate the alpha divergence between. If None, then the alpha divergence is calculated over all random variables.

Returns:

dkl – The alpha divergence between dist1 and dist2.

Return type:

float

Raises:

ditException – Raised if either dist1 or dist2 doesn’t have rvs or, if rvs is None, if dist2 has an outcome length different than dist1.

bhattacharyya_coefficient(dist1, dist2)[source]

Compute the Bhattacharyya coefficient.

Parameters:
Returns:

bc – The Bhattacharyya coefficient.

Return type:

float

chernoff_information(dist1, dist2)[source]

Compute the Chernoff information.

Parameters:
Returns:

ci – The Chernoff information.

Return type:

float

copy_mutual_information(dist, X, Y)[source]

Computes the copy mutual information. Roughly, it is the portion of the mutual information which results from \(X = Y\).

Parameters:
  • dist (Distribution) – The distribution of interest.

  • X (iterable) – The indicies to consider as X.

  • Y (iterable) – The indicies to consider as Y.

Returns:

Icopy – The copy mutual information of x.

Return type:

float

coupling_metric(dists, p=1.0)[source]

Residual entropy of the minimum-entropy coupling with the given marginals.

Note

This uses MinEntOptimizer (minimum joint entropy), not a direct minimization of residual entropy. For the latter, use coupling_min_residual_entropy() or min_residual_entropy_coupling().

Parameters:
  • dists (list of Distribution) – The distributions to consider as marginals

  • p (float) – The p-norm used when evaluating residual entropy on the coupling.

Returns:

cm – The residual entropy of the minimum joint-entropy coupling.

Return type:

float

coupling_min_residual_entropy(dists, *, niter=25)[source]

Minimum residual entropy over couplings with the given marginals.

Parameters:
  • dists (list of Distribution) – Marginal distributions to couple.

  • niter (int) – Number of basin-hopping iterations.

Returns:

R – The residual entropy of min_residual_entropy_coupling().

Return type:

float

cross_entropy(dist1, dist2, rvs=None, crvs=None)[source]

The cross entropy between dist1 and dist2.

Parameters:
  • dist1 (Distribution) – The first distribution in the cross entropy.

  • dist2 (Distribution) – The second distribution in the cross entropy.

  • rvs (list, None) – The indexes of the random variable used to calculate the cross entropy between. If None, then the cross entropy is calculated over all random variables.

Returns:

xh – The cross entropy between dist1 and dist2.

Return type:

float

Raises:

ditException – Raised if either dist1 or dist2 doesn’t have rvs or, if rvs is None, if dist2 has an outcome length different than dist1.

earth_movers_distance(dist1, dist2, distances=None)[source]

Compute the Earth Mover’s Distance (EMD) between dist1 and dist2. The EMD is the least amount of “probability mass flow” that must occur to transform dist1 to dist2.

Parameters:
  • dist1 (Distribution) – The first distribution.

  • dist2 (Distribution) – The second distribution.

  • distances (np.ndarray, None) – A matrix of distances between outcomes of the distributions. If None, a distance matrix is constructed; if the distributions are categorical each non-equal event is considered at unit distance, and if numerical abs(x, y) is used as the distance.

Returns:

emd – The Earth Mover’s Distance.

Return type:

float

earth_movers_distance_coupling(dist1, dist2, distances=None)[source]

Compute the optimal transport coupling underlying the Earth Mover’s Distance between dist1 and dist2.

The Earth Mover’s Distance is the minimal expected ground-metric cost over all joint distributions (couplings) whose marginals are dist1 and dist2. This returns the minimizing coupling itself: a joint distribution with dist1 as its first marginal and dist2 as its second.

Note

The optimal coupling is not unique in general; the linear program returns a single optimal vertex.

Parameters:
  • dist1 (Distribution) – The first distribution (first marginal of the coupling).

  • dist2 (Distribution) – The second distribution (second marginal of the coupling).

  • distances (np.ndarray, None) – A matrix of distances between outcomes of the distributions. If None, a distance matrix is constructed; if the distributions are categorical each non-equal event is considered at unit distance, and if numerical abs(x, y) is used as the distance.

Returns:

coupling – The optimal transport plan as a joint distribution over pairs of outcomes (o1, o2), with dist1 and dist2 as its marginals.

Return type:

Distribution

f_divergence(dist1, dist2, f, rvs=None)[source]

The Csiszar f-divergence of dist1 and dist2. Note that it is typically more accurate to use a specialized divergence function when available due to roundoff errors and small probability effects.

Parameters:
  • dist1 (Distribution) – The first distribution in the f-divergence.

  • dist2 (Distribution) – The second distribution in the f-divergence.

  • f (function) – The auxillary function defining the f-divergence

  • rvs (list, None) – The indexes of the random variable used to calculate the f-divergence between. If None, then the f-divergence is calculated over all random variables.

Returns:

dkl – The f-divergence between dist1 and dist2.

Return type:

float

Raises:

ditException – Raised if either dist1 or dist2 doesn’t have rvs or, if rvs is None, if dist2 has an outcome length different than dist1.

Notes

Outcomes where dist2 is zero but dist1 is not are omitted: their contribution, p * lim_{t -> inf} f(t) / t, depends on f and is not computed.

hellinger_distance(dist1, dist2)[source]

Compute the Hellinger distance.

Parameters:
Returns:

hd – The Hellinger distance.

Return type:

float

hellinger_divergence(dist1, dist2, alpha=1.0, rvs=None)[source]

The Hellinger divergence of dist1 and dist2.

Parameters:
  • dist1 (Distribution) – The first distribution in the Hellinger divergence.

  • dist2 (Distribution) – The second distribution in the Hellinger divergence.

  • alpha (float, 1) – The divergence is a one parameter family in alpha.

  • rvs (list, None) – The indexes of the random variable used to calculate the Hellinger divergence between. If None, then the Hellinger divergence is calculated over all random variables.

Returns:

dkl – The Hellinger divergence between dist1 and dist2.

Return type:

float

Raises:

ditException – Raised if either dist1 or dist2 doesn’t have rvs or, if rvs is None, if dist2 has an outcome length different than dist1.

hellinger_sum(dist1, dist2, alpha=1.0, rvs=None)[source]

The Hellinger sum/integral of dist1 and dist2, used to define other divergences.

Parameters:
  • dist1 (Distribution) – The first distribution in the sum.

  • dist2 (Distribution) – The second distribution in the sum.

  • alpha (float, 1) – Exponent used in the sum.

  • rvs (list, None) – The indexes of the random variable used to calculate the sum. If None, then the sum is calculated over all random variables.

Returns:

dkl – The Hellinger sum between dist1 and dist2.

Return type:

float

Raises:

ditException – Raised if either dist1 or dist2 doesn’t have rvs or, if rvs is None, if dist2 has an outcome length different than dist1.

hypercontractivity_coefficient(dist, rvs, bound=None, niter=None)[source]

Computes the hypercontractivity coefficient:

\[s*(X||Y) = max_{U - X - Y} I[U:Y]/I[U:X]\]
Parameters:
  • dist (Distribution) – The distribution of interest.

  • rvs (iterable of iterables, len(rvs) == 2) – The variables to compute the hypercontractivity coefficient of. Order is important.

  • bound (int, None) – An external bound on the size of U. If None, \(|U| <= |X|+1\).

  • niter (int, None) – The number of basin-hopping steps to perform. If None, use the default.

Returns:

hc – The hypercontractivity coefficient.

Return type:

float

jensen_shannon_divergence(dists, weights=None)[source]

The Jensen-Shannon Divergence: \(H(sum(w_i*P_i)) - sum(w_i*H(P_i))\).

The square root of the Jensen-Shannon divergence is a distance metric.

Parameters:
  • dists ([Distribution]) – The distributions, P_i, to take the Jensen-Shannon Divergence of.

  • weights ([float], None) – The weights, w_i, to give the distributions. If None, the weights are assumed to be uniform.

Returns:

jsd – The Jensen-Shannon Divergence

Return type:

float

Raises:
  • ditException – Raised if there dists and weights have unequal lengths.

  • InvalidNormalization – Raised if the weights do not sum to unity.

  • InvalidProbability – Raised if the weights are not valid probabilities.

kullback_leibler_divergence(dist1, dist2, rvs=None, crvs=None)[source]

The Kullback-Liebler divergence between dist1 and dist2.

Parameters:
  • dist1 (Distribution) – The first distribution in the Kullback-Leibler divergence.

  • dist2 (Distribution) – The second distribution in the Kullback-Leibler divergence.

  • rvs (list, None) – The indexes of the random variable used to calculate the Kullback-Leibler divergence between. If None, then the Kullback-Leibler divergence is calculated over all random variables.

Returns:

dkl – The Kullback-Leibler divergence between dist1 and dist2.

Return type:

float

Raises:

ditException – Raised if either dist1 or dist2 doesn’t have rvs or, if rvs is None, if dist2 has an outcome length different than dist1.

max_caekl_coupling(dists, *, niter=50)[source]

Coupling with maximal CAEKL mutual information.

Parameters:
  • dists (list of Distribution) – Marginal distributions to couple.

  • niter (int) – Number of basin-hopping iterations.

Returns:

dist – A joint distribution with the prescribed marginals and approximately maximal CAEKL mutual information.

Return type:

Distribution

max_dual_total_correlation_coupling(dists, *, niter=50)[source]

Coupling with maximal dual total correlation (binding information).

Parameters:
  • dists (list of Distribution) – Marginal distributions to couple.

  • niter (int) – Number of basin-hopping iterations.

Returns:

dist – A joint distribution with the prescribed marginals and approximately maximal dual total correlation.

Return type:

Distribution

max_total_correlation_coupling(dists, *, niter=50)[source]

Coupling with maximal total correlation (multi-information).

With fixed marginals, this is equivalent to minimum joint entropy.

Parameters:
  • dists (list of Distribution) – Marginal distributions to couple.

  • niter (int) – Number of basin-hopping iterations.

Returns:

dist – A joint distribution with the prescribed marginals and approximately maximal total correlation.

Return type:

Distribution

maximal_cross_entropy(dist1, dist2=None, rvs=None)[source]

The maximal cross entropy [YT20a]: the largest cross entropy, relative to dist2, of any joint distribution whose marginals on the two groups of variables in rvs match those of dist1,

\[\mathbb{H}(p_X, p_Y \| q) = \max_{r \in \mathcal{C}(p_X, p_Y)} -\sum_{x, y} r(x, y) \log_2 q(x, y).\]

It is at least the cross entropy of any particular coupling, such as \(p_X p_Y\), and it is infinite when some coupling places mass where \(q\) vanishes, that is, when the product of the supports of \(p_X\) and \(p_Y\) is not contained in the support of \(q\). With dist2 equal to dist1 it is at least the joint entropy, with equality exactly when the two groups are independent.

Parameters:
  • dist1 (Distribution) – The distribution whose marginals \(p_X\) and \(p_Y\) are coupled.

  • dist2 (Distribution, None) – The target distribution \(q\). If None, dist1 is used.

  • rvs (list, None) – Two lists of indexes, the variables of \(X\) and of \(Y\). If None, the distributions must have exactly two variables.

Returns:

xh – The maximal cross entropy.

Return type:

float

Raises:

ditException – Raised if rvs does not specify exactly two groups of variables, or if a distribution is symbolic.

maximum_correlation(dist, rvs=None, crvs=None)[source]

Compute the (conditional) maximum or Renyi correlation between two variables:

\[\rho^{*} = \max_{f, g} \rho(f(X,Z), g(Y,Z) | Z)\]
Parameters:
  • dist (Distribution) – The distribution for which the maximum correlation is to computed.

  • rvs (list, None; len(rvs) == 2) – A list of lists. Each inner list specifies the indexes of the random variables for which the maximum correlation is to be computed. If None, then all random variables are used, which is equivalent to passing rvs=dist.rvs.

  • crvs (list, None) – A single list of indexes specifying the random variables to condition on. If None, then no variables are conditioned on.

Returns:

rho_max – The conditional maximum correlation between rvs given crvs.

Return type:

float; -1 <= rho_max <= 1

min_residual_entropy_coupling(dists, *, niter=50)[source]

Coupling with minimal residual entropy (variation of information).

Parameters:
  • dists (list of Distribution) – Marginal distributions to couple.

  • niter (int) – Number of basin-hopping iterations.

Returns:

dist – A joint distribution with the prescribed marginals and approximately minimal residual entropy.

Return type:

Distribution

relative_entropy(dist1, dist2, rvs=None, crvs=None)

The Kullback-Liebler divergence between dist1 and dist2.

Parameters:
  • dist1 (Distribution) – The first distribution in the Kullback-Leibler divergence.

  • dist2 (Distribution) – The second distribution in the Kullback-Leibler divergence.

  • rvs (list, None) – The indexes of the random variable used to calculate the Kullback-Leibler divergence between. If None, then the Kullback-Leibler divergence is calculated over all random variables.

Returns:

dkl – The Kullback-Leibler divergence between dist1 and dist2.

Return type:

float

Raises:

ditException – Raised if either dist1 or dist2 doesn’t have rvs or, if rvs is None, if dist2 has an outcome length different than dist1.

renyi_divergence(dist1, dist2, alpha=1, rvs=None)[source]

The Renyi divergence of dist1 and dist2.

Parameters:
  • dist1 (Distribution) – The first distribution in the Renyi divergence.

  • dist2 (Distribution) – The second distribution in the Renyi divergence.

  • alpha (float, 1) – The divergence is a one parameter family in alpha. alpha=1 is the Kullback-Leibler divergence, and alpha=np.inf is the max-divergence, log2 max_x p(x) / q(x) over the support of dist1.

  • rvs (list, None) – The indexes of the random variable used to calculate the Renyi divergence between. If None, then the Renyi divergence is calculated over all random variables.

Returns:

dkl – The Renyi divergence between dist1 and dist2.

Return type:

float

Raises:

ditException – Raised if either dist1 or dist2 doesn’t have rvs or, if rvs is None, if dist2 has an outcome length different than dist1.

tsallis_divergence(dist1, dist2, alpha=1.0, rvs=None)[source]

The Tsallis divergence of dist1 and dist2.

Parameters:
  • dist1 (Distribution) – The first distribution in the Tsallis divergence.

  • dist2 (Distribution) – The second distribution in the Tsallis divergence.

  • alpha (float, 1) – The divergence is a one parameter family in alpha.

  • rvs (list, None) – The indexes of the random variable used to calculate the Tsallis divergence between. If None, then the Tsallis divergence is calculated over all random variables.

Returns:

dkl – The Tsallis divergence between dist1 and dist2.

Return type:

float

Raises:

ditException – Raised if either dist1 or dist2 doesn’t have rvs or, if rvs is None, if dist2 has an outcome length different than dist1.

variational_distance(dist1, dist2)[source]

Compute the variational distance.

Parameters:
Returns:

vd – The variational distance.

Return type:

float