The similarity between two probability distributions can be measured using the Bhattacharyya coefficient, defined as:

where we've defined the vectors

Note that, since probabilities add to one, the vectors and are normalized (in the usual Euclidean norm), i.e., .

The Bhattacharyya coefficient is related to the Euclidean distance via

In that sense, if you know how to compute the Euclidean distance for and you can easily calculate the Bhattacharyya coefficient as well.

Fill in the code to calculate the Bhattacharyya coefficient for two probability distribution vectors and . If you already know how to calculate the Euclidean distance between two vectors, that can be helpful as well!

Hint 1

You can use numpy functions such as the inner product np.inner or np.linalg.norm.

Hint 2

Don't forget to take the element-wise square root of the vectors! Use np.sqrt.


def bhattacharyya_coefficient(p, q):
"""Compute the Bhattacharyya coefficient for two discrete
probability distributions.
Args:
p (np.array[float]): A probability distribution vector.
q (np.array[float]): A probability distribution vector.
Returns:
(float): A number that represents the Bhattacharyya coefficient
between two probability distribution vectors.
"""

##################
# YOUR CODE HERE #
##################


# RETURN THE BHATTACHARYYA COEFFICIENT
pass

or to submit your code

To interact with codercises, please switch to a larger screen size.

Learning Objectives:

  • Define and calculate the fidelity between two probability distributions.
  • Write down the relation between the total variation distance and fidelity.