Skip to content

Instantly share code, notes, and snippets.

@Torvaney
Last active July 19, 2018 20:31
Show Gist options
  • Save Torvaney/bee891d0d04c29d7c3778bdc1936d0e7 to your computer and use it in GitHub Desktop.
Save Torvaney/bee891d0d04c29d7c3778bdc1936d0e7 to your computer and use it in GitHub Desktop.
library(dplyr)
library(ggplot2)
probs <- c(home = 0.35, draw = 0.4, away = 0.25)
points <- c(home = 0, draw = 1, away = 3)
n_sim <- 1e4
n_games <- 11
samples <- sample(points, size = n_sim * n_games, prob = probs, replace = TRUE)
sims <- data_frame(
sim = rep.int(1:n_sim, n_games),
game = rep_len(1:n_games, n_sim * n_games),
points = samples
)
observed_points <- 5
sims %>%
group_by(sim) %>%
summarise(points = sum(points)) %>%
ungroup() %>%
summarise(100 * mean(points <= observed_points))
sims %>%
group_by(sim) %>%
summarise(points = sum(points)) %>%
ggplot(aes(x = points)) +
geom_histogram(aes(y = 100 * ..count.. / sum(..count..)),
binwidth = 1) +
geom_vline(xintercept = observed_points, linetype = "dotted") +
scale_x_continuous(breaks = seq(0, 50, 2)) +
theme_minimal() +
ggtitle("Simulated points",
sprintf("Home/Draw/Away = %0.0f%% / %0.0f%% / %0.0f%%",
100*probs["home"], 100*probs["draw"], 100*probs["away"])) +
xlab("Points") +
ylab("%")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment