Pasindu Tennage
Pasindu Tennage

Reputation: 1568

How to get probability density function using Kullback-Leibler Divergence in Python

I have a set of 1D data saved in a python. I can get the probability density function using gaussian_kde function from scipy. I want to know whether the returned distribution matches with a theoretical distribution such as Normal distribution. For that can I use KL divergence? If so how can I use python to do that?

This is my python code to get the probability density function.

array = np.array(values)
KDEpdf = gaussian_kde(array)
x = np.linspace(0, 50, 1500)
kdepdf = KDEpdf.evaluate(x)
plt.plot(x, kdepdf, label="", color="blue")
plt.legend()
plt.show()

Upvotes: 0

Views: 394

Answers (1)

Kuang Liang
Kuang Liang

Reputation: 230

There are couple of ways to do it:

  1. Plot it against a normal fitted probability distribution. Like: plt.hist(x, norm.pdf(x,mu, std))

  2. Compare kdepdf distribution with a uniform random dataset using something like Q-Q plot for both dataset.

  3. Use chi square test, be cautious with the bin size you choose. Basically, this tests whether the number of draws that fall into various intervals is consistent with a uniform random distribution.chi square test. Basically, this tests whether the number of draws that fall into various intervals is consistent with a uniform random distribution.

Upvotes: 1

Related Questions