KL distance in R

I would like to calculate the KL distance from two gamma distributions using R.

enter image description here

Here is my R code:

theta1 <- 0.2
theta2 <- 2

f <- function(u)
{
     dgamma(u, shape=1/theta1, scale=theta1) * 
      (dgamma(u, shape=1/theta1, scale=theta1, log=TRUE) -
       dgamma(u, shape=1/theta2, scale=theta2, log=TRUE)) 
}

f <- Vectorize(f)
integrate(f, lower=0, upper=Inf)

Do you have a comment on my R-code? Do you think this is a good way to calculate the distance KL?

Any suggestion would be appreciated,

thanks Marco

+3
source share
2 answers

I would define all the arguments that are used in this function. I mean:

my.theta1 <- 0.2
my.theta2 <- 2

f <- function(u, theta1, theta2)
{
     dgamma(u, shape=1/theta1, scale=theta1) * 
      (dgamma(u, shape=1/theta1, scale=theta1, log=TRUE) -
       dgamma(u, shape=1/theta2, scale=theta2, log=TRUE)) 
}


f <- Vectorize(f)
integrate(f, lower=0, upper=Inf, theta1 = my.theta1, theta2 = my.theta2)

Being more explicit, to prevent "accidents" because your function performs a search theta1, and theta2in the higher (global) media (which can become messy if you use this function deep within the program).

+2
source

All Articles