Variational Distance

The variational (total variation) distance [CT06] between two distributions on the same alphabet is

\[\delta(p, q) = \tfrac{1}{2} \sum_x \lvert p(x) - q(x) \rvert\]
In [1]: from dit.divergences import variational_distance

In [2]: p = dit.Distribution(['0', '1'], [3/4, 1/4])

In [3]: q = dit.Distribution(['0', '1'], [1/2, 1/2])

In [4]: variational_distance(p, q)
Out[4]: 0.25

The Bhattacharyya Coefficient

The rest of this module is built on the Bhattacharyya coefficient [Bha43], the overlap between the square roots of the two pmfs:

\[BC(p, q) = \sum_x \sqrt{p(x) q(x)}\]

It ranges from 1, when \(p = q\), down to 0, when \(p\) and \(q\) have disjoint support. Geometrically it is the inner product of \(\sqrt{p}\) and \(\sqrt{q}\), both of which are unit vectors on the positive orthant of the sphere — so \(BC\) is the cosine of the angle between them.

In [5]: from dit.divergences import bhattacharyya_coefficient

In [6]: bhattacharyya_coefficient(p, q)
Out[6]: 0.9659258262890682

Derived Quantities

Each of the following is a function of \(BC\) alone:

\[\begin{split}D_B(p, q) &= -\log_2 BC(p, q) &&\quad \text{Bhattacharyya distance} \\ \theta(p, q) &= \arccos BC(p, q) &&\quad \text{Bhattacharyya angle} \\ d_{FR}(p, q) &= 2 \arccos BC(p, q) &&\quad \text{Fisher-Rao distance} \\ H(p, q) &= \sqrt{1 - BC(p, q)} &&\quad \text{Hellinger distance} \\ d_M(p, q) &= \sqrt{2 (1 - BC(p, q))} &&\quad \text{Matusita distance}\end{split}\]

The Bhattacharyya distance is the one that is not a metric: it is symmetric and vanishes only when \(p = q\), but it fails the triangle inequality, and it diverges when the supports are disjoint. The other four are true metrics.

The Bhattacharyya angle is the angle between \(\sqrt{p}\) and \(\sqrt{q}\) on the sphere, and the Fisher-Rao distance [Rao45] is the corresponding geodesic distance — the distance induced by the Fisher information metric, which on the simplex is the round metric. The Matusita distance [Mat55] is instead the straight-line Euclidean distance between \(\sqrt{p}\) and \(\sqrt{q}\), making it the chord to the Fisher-Rao arc, and it is \(\sqrt{2}\) times the Hellinger distance as dit defines it.

In [7]: from dit.divergences import bhattacharyya_distance, bhattacharyya_angle

In [8]: from dit.divergences import fisher_rao_distance, hellinger_distance, matusita_distance

In [9]: bhattacharyya_distance(p, q)
Out[9]: 0.05001568652350428

In [10]: bhattacharyya_angle(p, q)
Out[10]: 0.26179938779914974

In [11]: fisher_rao_distance(p, q)
Out[11]: 0.5235987755982995

In [12]: hellinger_distance(p, q)
Out[12]: 0.18459191128251476

In [13]: matusita_distance(p, q)
Out[13]: 0.26105238444010354

The angle-based quantities are reported in radians, so they max out at \(\pi/2\) and \(\pi\) respectively on disjoint distributions, where the Bhattacharyya distance instead diverges.

Note

The Bhattacharyya coefficient and distance are also reachable through Generalized Divergences: \(BC\) is the Hellinger sum at \(\alpha = \tfrac{1}{2}\), and \(D_B\) is half the Rényi divergence of order \(\tfrac{1}{2}\).

API

variational_distance(dist1, dist2)[source]

Compute the variational distance.

Parameters:
Returns:

vd – The variational distance.

Return type:

float

bhattacharyya_coefficient(dist1, dist2)[source]

Compute the Bhattacharyya coefficient.

Parameters:
Returns:

bc – The Bhattacharyya coefficient.

Return type:

float

bhattacharyya_distance(dist1, dist2)[source]

Compute the Bhattacharyya distance.

The Bhattacharyya distance is defined as:

D_B(P, Q) = -log_2( sum_x sqrt(P(x) Q(x)) ) = -log_2( BC(P, Q) )

It is a non-negative quantity that equals zero if and only if P = Q. While it does not satisfy the triangle inequality, it is closely related to the Hellinger distance and Chernoff bound, and is widely used in feature selection and statistical signal detection.

Parameters:
Returns:

bd – The Bhattacharyya distance (in bits).

Return type:

float

bhattacharyya_angle(dist1, dist2)[source]

Compute the Bhattacharyya angle.

The Bhattacharyya angle is the angle between the two distributions once they are mapped to the unit sphere by the square root of their pmfs:

theta(P, Q) = arccos( sum_x sqrt(P(x) Q(x)) ) = arccos( BC(P, Q) )

Unlike the Bhattacharyya distance it is a true metric, satisfying the triangle inequality. It ranges from 0, when P = Q, to pi/2, when P and Q have disjoint support.

Parameters:
Returns:

ba – The Bhattacharyya angle (in radians).

Return type:

float

fisher_rao_distance(dist1, dist2)[source]

Compute the Fisher-Rao distance.

The square roots of the pmfs of all distributions over a fixed alphabet lie on the positive orthant of the unit sphere, and the Fisher information metric there is the round metric. The Fisher-Rao distance is the resulting geodesic distance, which is twice the Bhattacharyya angle:

d_FR(P, Q) = 2 arccos( sum_x sqrt(P(x) Q(x)) )

It is a true metric, ranging from 0, when P = Q, to pi, when P and Q have disjoint support.

Parameters:
Returns:

frd – The Fisher-Rao distance (in radians).

Return type:

float

hellinger_distance(dist1, dist2)[source]

Compute the Hellinger distance.

Parameters:
Returns:

hd – The Hellinger distance.

Return type:

float

matusita_distance(dist1, dist2)[source]

Compute the Matusita distance.

The Matusita distance is the Euclidean distance between the square roots of the two pmfs:

d_M(P, Q) = sqrt( sum_x ( sqrt(P(x)) - sqrt(Q(x)) )^2 )

= sqrt( 2 ( 1 - BC(P, Q) ) )

It is a true metric, and is the dit Hellinger distance scaled by sqrt(2); it ranges from 0, when P = Q, to sqrt(2), when P and Q have disjoint support.

Parameters:
Returns:

md – The Matusita distance.

Return type:

float

chernoff_information(dist1, dist2)[source]

Compute the Chernoff information.

Parameters:
Returns:

ci – The Chernoff information.

Return type:

float