0
$\begingroup$

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]

Parameters:

p(N,) array_like left probability vector

q(N,) array_like right probability vector

Question

How can I calculate probability vectors from sample data?

Example:

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)
$\endgroup$

1 Answer 1

1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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