Created
September 28, 2018 04:22
-
-
Save GarrettMooney/b54a5daecaef05a9035e7744afd89d16 to your computer and use it in GitHub Desktop.
Entropy & Rcpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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