提问人:ridul 提问时间:10/1/2023 最后编辑:ridul 更新时间:10/1/2023 访问量:26
计算 Chao-Shen 修正了 Jensen-Shannon 散度
Calculating Chao-Shen corrected Jensen-Shannon Divergence
问:
我正在尝试使用超沉校正 (https://link.springer.com/article/10.1023/a:1026096204727) 计算两个分布 P 和 R 的 JS 散度。P 和 R 是简单的数组,其中每个元素代表某些微观状态的计数。它们的长度是相同的。P 可能包含很多 0 个 bin(因此使用了 Chao-Shen 校正)。这是我的代码:
def js_cs(P,R):
M = (P+R)/2
js = 0.5 * kl_cs(P, M) + 0.5 * kl_cs(R, M)
return js
def kl_cs(P, R):
# Convert to float
P = P.astype(float)
R = R.astype(float)
yx = P[P > 0] # remove bins with zero counts
n = np.sum(yx)
p = yx / n
f1 = np.sum(yx == 1) # number of singletons in the sample
if f1 == n: # avoid C == 0
f1 -= 1
C = 1 - (f1 / n) # estimated coverage of the sample
pa = C * p # coverage adjusted empirical frequencies
la = (1 - (1 - pa) ** n) # probability to see a bin (species) in the sample
H = -np.sum((pa * np.log(pa)) / la)
# Make sure no zero values in R for log computation
R /= np.sum(R)
# Only consider entries where P > 0
R = R[P > 0]
# Compute corrected KL-divergence
cross_entropy = -np.sum((pa * np.log(R)) / la)
return (cross_entropy - H)/np.log(2)
我看到 JS div 值为负数,这很奇怪。据我所知,这不应该发生。可能是什么原因?我是否正确计算了 JS 背离?
需要注意的一点是,负值不是很大,它们非常接近 0,但仍然是负值。
答: 暂无答案
评论