In the context of comparing elements in a vector space, there is a huge variety of distance measures or metric at our disposal. Let us focus on a particular metric induced by the -norm. For two vectors , we define the -distance as:

which is the -norm of the vector .

For , we have the Euclidean distance, a very common metric that is related to the inner product of a vector with itself:

where .

Fill in the code to calculate the Euclidean distance between two vectors .

Hint

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


def euclidean_distance(r, s):
"""Compute the Euclidean distance for two vectors of same size.
Args:
r (np.array[float]): A real n-dimensional vector
s (np.array[float]): Another real n-dimensional vector
Returns:
(float): A number that represents the Euclidean distance.
"""

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

# RETURN THE DISTANCE
pass


or to submit your code

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

Learning Objectives:

  • Define and give examples of metrics.
  • Define and calculate the trace distance between two probability distributions.