Computes the Jensen-Shannon divergence between two probability distributions.

jsd(p, q, base = c("log", "log2", "log10"), margin = FALSE)

Arguments

p

numeric. probability distributions.

q

numeric. probability distributions.

base

character. log bases. "log", "log2", "log10". default is "log"

margin

logical. Choose whether to return individual values or totals. The default value is FALSE, which returns individual values.

Value

numeric. Jensen-Shannon divergence of probability distributions p and q.

See also

Examples

# 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)

jsd(p, q)
#> [1] 0.0872049861 0.0521977242 0.0255127366 0.0063314207 0.0004227586
#> [6] 0.0386776759 0.1834364603
jsd(p, q, base = "log2")
#> [1] 0.1258102010 0.0753053979 0.0368070986 0.0091343092 0.0006099118
#> [6] 0.0558000912 0.2646428716
jsd(p, q, margin = TRUE)
#> [1] 0.3937838