Skip to Content

KL divergence in R using base 2 logarithm

I want to find JS divergence of two distributions in R. wikipedia says that Jensen–Shannon divergence is bounded by 1, given that one uses the base 2 logarithm. I want that my resulting JS divergence lies between 0 and 1. I am using KLdiv function in R to find JS:

JSD(P || Q)= 1/2*D(P || M) + 1/2*D(Q || M)  

where Kullback–Leibler divergence KLdiv(P,M) = D(P || M)

But I want to specify that I need base 2 logarithm. Looks like KLdiv does not allow me to specify which log I want to use. Any clue as to how to do that?

Ok this is the R code for finding JSdivergence between 2 distributions ..

m <- 0.5 *(dist1 + dist2) #JSD(P||Q)=0.5*D(P||M) + 0.5*D(Q||M), where M=0.5*(P+Q)
Dpm <- KLdiv(cbind(dist1,m))
Dqm <- KLdiv(cbind(m,dist2))
js <- 0.5*Dpm + 0.5*Dqm

I want a JS value between 0 and 1 which as per wiki is possible only if I take base 2 logarithm. How can I do this with my exisiting R code