如何在Python中使用Kullback-Leibler Divergence获得概率密度函数

时间:2018-07-26 06:32:27

标签: python statistics

我在python中保存了一组一维数据。我可以从scipy使用gaussian_kde函数获得概率密度函数。我想知道返回的分布是否与理论分布(例如正态分布)匹配。为此,我可以使用KL散度吗?如果可以,我该如何使用python做到这一点?

这是我获取概率密度函数的python代码。

   
def permute(a, l, r):
    if l == r:
        print(a)
    else:
        for i in range(l, r + 1):
            a[l], a[i] = a[i], a[l]
            permute(a, l + 1, r)
            a[l], a[i] = a[i], a[l]

data = [1,2,3,4,5]
n = len(data)
a = list(data)
permute(a, 0, n - 1)

1 个答案:

答案 0 :(得分:1)

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.