Skip to content

Instantly share code, notes, and snippets.

@GarrettMooney
Created September 28, 2018 04:22
Show Gist options
  • Save GarrettMooney/b54a5daecaef05a9035e7744afd89d16 to your computer and use it in GitHub Desktop.
Save GarrettMooney/b54a5daecaef05a9035e7744afd89d16 to your computer and use it in GitHub Desktop.
Entropy & Rcpp
li(ggplot2)
theme_set(theme_classic())
entropy <- function(p) -sum(p * log(p))
n <- 1e6
p <- runif(n)
q <- 1 - p
z <- matrix(c(p, q), ncol = 2)
ent <- apply(z, 1, entropy)
qplot(q, ent, geom = "line") +
xlab("Probability") +
ylab("Entropy") +
ggtitle("Entropy For Two Events")
# C++
apply_entropy <- rcpp_apply_generator("return -sum(x * log(x));")
bm <- bench::mark(
base = apply(z, 1, entropy),
rcpp = apply_entropy(z, 1)
)
summary(bm)
summary(bm, relative = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment