Saya ingin menghitung divergensi jensen-shannon untuknya mengikuti 3 distribusi. Apakah perhitungan di bawah ini benar? (Saya mengikuti rumus JSD dari wikipedia ):
P1 a:1/2 b:1/2 c:0
P2 a:0 b:1/10 c:9/10
P3 a:1/3 b:1/3 c:1/3
All distributions have equal weights, ie 1/3.
JSD(P1, P2, P3) = H[(1/6, 1/6, 0) + (0, 1/30, 9/30) + (1/9,1/9,1/9)] -
[1/3*H[(1/2,1/2,0)] + 1/3*H[(0,1/10,9/10)] + 1/3*H[(1/3,1/3,1/3)]]
JSD(P1, P2, P3) = H[(1/6, 1/5, 9/30)] - [0 + 1/3*0.693 + 0] = 1.098-0.693 = 0.867
Terima kasih sebelumnya...
Sunting Berikut adalah beberapa kode Python kotor sederhana yang menghitung ini juga:
def entropy(prob_dist, base=math.e):
return -sum([p * math.log(p,base) for p in prob_dist if p != 0])
def jsd(prob_dists, base=math.e):
weight = 1/len(prob_dists) #all same weight
js_left = [0,0,0]
js_right = 0
for pd in prob_dists:
js_left[0] += pd[0]*weight
js_left[1] += pd[1]*weight
js_left[2] += pd[2]*weight
js_right += weight*entropy(pd,base)
return entropy(js_left)-js_right
usage: jsd([[1/2,1/2,0],[0,1/10,9/10],[1/3,1/3,1/3]])