假设我有两个pdf,例如:
from scipy import stats
pdf_y = stats.beta(5, 9).pdf
pdf_x = stats.beta(9, 5).pdf我想计算他们的KL divergence。在我重新发明轮子之前,PyData生态系统中有没有内置的东西可以做到这一点?
发布于 2014-03-01 00:45:48
KL分歧在scipy.stats.entropy中可用。从文档字符串
stats.entropy(pk, qk=None, base=None)
Calculate the entropy of a distribution for given probability values.
If only probabilities `pk` are given, the entropy is calculated as
``S = -sum(pk * log(pk), axis=0)``.
If `qk` is not None, then compute a relative entropy (also known as
Kullback-Leibler divergence or Kullback-Leibler distance)
``S = sum(pk * log(pk / qk), axis=0)``. 发布于 2014-02-28 23:07:55
看起来nimfa包里有你要找的东西。http://nimfa.biolab.si
V = np.matrix([[1,2,3],[4,5,6],[6,7,8]])
fctr = nimfa.mf(V, method = "lsnmf", max_iter = 10, rank = 3)
fctr_res = nimfa.mf_run(fctr)
# Print the loss function according to Kullback-Leibler divergence. By default Euclidean metric is used.
print "Distance Kullback-Leibler: %5.3e" % fctr_res.distance(metric = "kl")这并不完全是您想要的,因为它似乎只需要一个输入,但它可能是一个起点。
此外,此链接可能会很有用。似乎有一些代码(而不是numpy)来计算相同的东西。https://code.google.com/p/tackbp2011/source/browse/TAC-KBP2011/src/python-utils/LDA/kullback-leibler-divergence.py?r=100
发布于 2017-05-31 04:30:36
由于KL-散度被定义为超散度( integral for the continuous case ),恐怕您必须在(超)空间上对这两个分布进行Monte Carlo integration。
在你的例子中,这意味着在区间0,1中均匀地绘制随机数,并计算两个PDF的值,以用于积分计算。
https://stackoverflow.com/questions/22097409
复制相似问题