Skip to content

Instantly share code, notes, and snippets.

@sysilviakim
Last active November 1, 2021 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sysilviakim/23b1dde0237a05d084831bd05eb68beb to your computer and use it in GitHub Desktop.
Save sysilviakim/23b1dde0237a05d084831bd05eb68beb to your computer and use it in GitHub Desktop.
Simulation of Probability Estimate with Repeated Experiments (Coin Toss)
library(tidyverse)
set.seed(100)
df <- tibble(
n = seq(1e5),
x = rbinom(1e5, 1, 0.5),
y = cumsum(x) / n
)
## In base R, plot(df$n, df$y)
ggplot(df, aes(x = n, y = y)) +
geom_point() +
xlab("Number of Trials") +
ylab("Estimated Probability of Heads") +
scale_y_continuous(
limits = c(0, 0.6),
breaks = seq(0, 0.6, by = 0.1)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment