I'd like to compare two distributions using Jensen-Shannon Divergence metric. To do this, I need two probability vectors to plug into distance.jensenshannon(p, q). From the scipy.spatial documentation.

scipy.spatial.distance.jensenshannon(p, q, base=None)[source]


p(N,) array_like left probability vector

q(N,) array_like right probability vector


How can I calculate probability vectors from sample data?


from scipy.spatial import distance
import numpy as np

x1 = np.random.normal(size=100)
x2 = np.random.normal(size=100)

p = 

q = 

jsd_metric = distance.jensenshannon(p, q)

Can I accomplish this using scipy.stats.norm.pdf()?

p = scipy.stats.norm.pdf(x1)
q = scipy.stats.norm.pdf(x2)

Probability vectors are meant for discrete random variables. Your data is drawn from a continuous distribution. In order to get a probability vector from data, you need to bin it into a normalized histogram. This gets you a probability vector. You probably want to make sure though, that bin center and width for samples from both distributions are the same.

I guess you could use the pdf to calculate the probability vectors too, if you knew the pdfs of the distributions. But if you only have access to data, i.e. samples from the distributions, this is not the case.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.