Skip to content

Instantly share code, notes, and snippets.

@ericnovik
Last active September 9, 2019 10:44
Show Gist options
  • Save ericnovik/5d8757ffcf45c136c3255f8aae2188ea to your computer and use it in GitHub Desktop.
Save ericnovik/5d8757ffcf45c136c3255f8aae2188ea to your computer and use it in GitHub Desktop.
library(ggplot2)
theme_set(theme_minimal())
# Visualizing entropy
entropy <- function(p) -sum(p * log(p))
n <- 1e3
p <- runif(n)
q <- 1 - p
z <- matrix(c(p, q), ncol = 2)
ent <- apply(z, 1, entropy)
qplot(q, ent, geom = "line") +
ylab("Entropy") + xlab("Probability") +
ggtitle("Entropy For Two Events")
# Visualizing KL divergence
kld <- function(p, q) sum(p * log(p / q))
n <- 1e3
p <- c(0.6, 0.4)
q1 <- runif(n)
q2 <- 1 - q1
z <- matrix(c(q1, q2), ncol = 2)
kl <- apply(z, 1, kld, p = p)
qplot(q1, kl, geom = "line") +
ylab("KL Divergence") +
xlab("Probability") +
geom_vline(xintercept = p[1], colour = "grey")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment