Charlotte Wickham
This file just sets up and runs the simulation, saving the results in results/sim-output.rds
.
The functions used to simulate the coin toss sequences live in R/simulation.R
.
doc/02-report.Rmd
presents and summarizes the results.
knitr::opts_chunk$set(echo = TRUE, results = "hide")
library(tidyverse)
library(here)
set.seed(181871)
Setting a seed here is important because we want the simulations to be the same if we re-run this file.
This loads the functions in R/
, so we can use them in this document:
devtools::load_all()
## Loading cointoss
A random sequence of H's and T's is generated by tossing a fair coin n = 20 times. What's the expected length of the longest run of consecutive heads or tails?
Taken from Tijms, Henk. Probability: A Lively Introduction. Cambridge University Press, 2017
How does the expected length of the longest run vary with the number of coinflips, n?
How does the answer change if the coin isn't fair?
How does the variance of our estimate vary with the number of simulations we do?
simulation_params_big <- list(
n = c(10, 20, 50, 100),
prob = seq(0.1, 0.9, by = 0.2),
n_sims = c(100, 1000, 10000)
)
simulation_params_big
runs_big <- cross_df(simulation_params_big)
runs_big <- runs_big %>%
mutate(
run_lengths = pmap(list(n = n, prob = prob, n_sims = n_sims),
sim_run_lengths))
Get mean and standard deviation of each vector of simulated longest runs:
runs_big <- runs_big %>%
mutate(
mean_max_length = map_dbl(run_lengths, mean),
sd_max_length = map_dbl(run_lengths, sd)
)
write_rds(runs_big, here("results", "sim-output.rds"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.