Created
September 28, 2018 04:21
-
-
Save GarrettMooney/a72f89622cf5d46abf23be582ee431cd to your computer and use it in GitHub Desktop.
KL Divergence & Rcpp
This file contains hidden or 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()) | |
kld <- function(p, q) sum(p * log(p / q)) | |
n <- 1e6 | |
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") + | |
xlab("Probability") + | |
ylab("KL Divergence") + | |
geom_vline(xintercept = p[1], colour = "grey") | |
# C++ | |
apply_kld <- rcpp_apply_generator('return sum(p * log(p / x));', | |
additional = ' | |
// [[Rcpp::plugins("cpp11")]] | |
NumericVector p {0.6, 0.4};') | |
bm <- bench::mark( | |
base = apply(z, 1, kld, p = p), | |
rcpp = apply_kld(z, 1) | |
) | |
summary(bm) | |
summary(bm, relative = T) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment