Lesson 9 of 15
Total Variation and Hellinger Distance
Total Variation Distance
The total variation (TV) distance is the maximum possible difference in probability assigned to any event:
The factor of ensures TV .
Properties
- Metric: TV satisfies symmetry, non-negativity, and triangle inequality
- Bounded:
- TV = 0 iff ; TV = 1 iff and have disjoint supports
Hellinger Distance
The Hellinger distance is another probability metric based on the norm of square-root probabilities:
It is also bounded in and has useful relationships to both TV distance and KL divergence.
Comparison of Distances
| Distance | Formula | Range |
|---|---|---|
| Total Variation | $\frac{1}{2}\sum | p_i - q_i |
| Hellinger | ||
| JS Distance |
import math
def total_variation(p, q):
return 0.5 * sum(abs(p[i] - q[i]) for i in range(len(p)))
def hellinger_distance(p, q):
return math.sqrt(0.5 * sum((math.sqrt(p[i]) - math.sqrt(q[i]))**2
for i in range(len(p))))
p = [0.7, 0.3]
q = [0.4, 0.6]
print(round(total_variation(p, q), 4)) # 0.3
print(round(hellinger_distance(p, q), 4)) # 0.2158
Your Task
Implement:
total_variation(p, q)—hellinger_distance(p, q)—
Python runtime loading...
Loading...
Click "Run" to execute your code.