Computes the Kullback-Leibler divergence between two probability distributions.
kld(p, q, base = c("log", "log2", "log10"), margin = FALSE)
numeric. probability distributions.
numeric. probability distributions.
character. log bases. "log", "log2", "log10". default is "log"
logical. Choose whether to return individual values or totals. The default value is FALSE, which returns individual values.
numeric. Kullback-Leibler divergence of probability distributions p and q.
jsd
.
# Sample data for probability distributions p.
event <- c(115, 76, 61, 39, 55, 10, 1)
no_event <- c(3, 3, 7, 10, 28, 44, 117)
p <- event / sum(event)
q <- no_event / sum(no_event)
kld(p, q)
#> [1] 1.00670753 0.57712559 0.28087582 0.09174581 0.02372228 -0.05609955
#> [7] -0.01479923
kld(p, q, base = "log2")
#> [1] 1.45237196 0.83261623 0.40521815 0.13236123 0.03422402 -0.08093455
#> [7] -0.02135077
kld(p, q, margin = TRUE)
#> [1] 1.909278