Loading content...
In statistics and information theory, measuring the similarity or divergence between two probability distributions is a fundamental operation with widespread applications across machine learning, pattern recognition, and data science.
The Bhattacharyya distance provides an elegant and interpretable measure of the overlap between two probability distributions. Unlike some other divergence measures, it is bounded, symmetric, and has a clear geometric interpretation—it measures the amount of overlap between distributions in probability space.
Mathematical Foundation:
For two discrete probability distributions P and Q defined over the same sample space with n outcomes, we first compute the Bhattacharyya coefficient (BC):
$$BC(P, Q) = \sum_{i=1}^{n} \sqrt{P_i \cdot Q_i}$$
The Bhattacharyya coefficient measures the amount of overlap between the two distributions and ranges from 0 (no overlap) to 1 (identical distributions).
The Bhattacharyya distance (DB) is then derived from this coefficient as:
$$D_B(P, Q) = -\ln(BC(P, Q))$$
This transformation converts the overlap measure into a proper distance metric. When distributions are identical (BC = 1), the distance is 0. As distributions become more dissimilar (BC approaches 0), the distance increases toward infinity.
Input Validation:
For the computation to be meaningful:
If either condition is violated, the function should return 0.0 to indicate an invalid comparison.
Your Task:
Write a Python function that calculates the Bhattacharyya distance between two probability distributions. The result should be rounded to 4 decimal places for consistent precision.
p = [0.1, 0.2, 0.3, 0.4]
q = [0.4, 0.3, 0.2, 0.1]0.1166Step 1: Compute element-wise products and their square roots: • √(0.1 × 0.4) = √0.04 = 0.2 • √(0.2 × 0.3) = √0.06 ≈ 0.2449 • √(0.3 × 0.2) = √0.06 ≈ 0.2449 • √(0.4 × 0.1) = √0.04 = 0.2
Step 2: Sum for Bhattacharyya coefficient: BC = 0.2 + 0.2449 + 0.2449 + 0.2 ≈ 0.8898
Step 3: Calculate distance: DB = -ln(0.8898) ≈ 0.1166
The distributions are somewhat similar, resulting in a relatively small distance.
p = [0.25, 0.25, 0.25, 0.25]
q = [0.25, 0.25, 0.25, 0.25]0.0When both distributions are identical (uniform distributions in this case):
Step 1: Each element-wise product: 0.25 × 0.25 = 0.0625 Step 2: Square root: √0.0625 = 0.25 Step 3: Sum of all 4 values: BC = 0.25 × 4 = 1.0 Step 4: Distance: DB = -ln(1.0) = 0.0
Identical distributions have zero divergence, confirming the distance metric property.
p = [0.5, 0.5]
q = [0.33, 0.33, 0.34]0.0The distribution p has 2 elements while q has 3 elements. Since the distributions are defined over different sample spaces (different lengths), comparison is mathematically undefined. The function returns 0.0 to indicate an invalid input condition.
Constraints