Skip to content

Instantly share code, notes, and snippets.

@ck37
Last active September 20, 2018 02:15
Show Gist options
  • Save ck37/d1b14aef02be89139e6cd042f4be1a12 to your computer and use it in GitHub Desktop.
Save ck37/d1b14aef02be89139e6cd042f4be1a12 to your computer and use it in GitHub Desktop.
options(scipen = 20)
worst_case = (2 / 5) * 2^32
(worst_cases = c(worst_case - 1, worst_case))
(not_worst_cases = c((1 / 5) * 2^32, 1.5 * 2^32))
(m_max = c(round(exp(10:21)), not_worst_cases, worst_cases))
set.seed(3932354)
result = data.frame(m_max, lg_m_max = log(m_max), stat = NA)
for (m_i in 1:length(m_max)) {
x = sample(m_max[m_i], 1000000, replace = TRUE)
result[m_i, "stat"] = sum(x %% 2) / sum(x %% 2 == 0)
}
summary(result)
# Review last two observations.
tail(result, n = 2)
library(ggplot2)
# Worst cases plotted.
(p1 = ggplot(data = result, aes(lg_m_max, stat)) +
geom_point() + theme_minimal() +
ggtitle("Worst 2 cases plotted: ratio of odd vs even"))
# Skip worst two cases to zoom in on smaller values of m.
(p2 = ggplot(data = result[-seq(nrow(result) - 1, nrow(result)), ],
aes(lg_m_max, stat)) +
geom_point() + theme_minimal() + scale_x_log10() +
geom_hline(yintercept = 1.0004, color = "red") +
geom_hline(yintercept = 0.9996, color = "blue") +
ggtitle("Skip worst cases, zoom in on smaller values of m"))
library(gridExtra)
grid.arrange(p1, p2, ncol = 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment