doc/01-simulate-coin-tosses.md

Simulating runs in a coin toss sequence

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

Motivating question

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

Simulation setup

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)

Simulation

runs_big <- runs_big %>% 
  mutate(
    run_lengths = pmap(list(n = n, prob = prob, n_sims = n_sims), 
      sim_run_lengths)) 

Summarization

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)
  )

Save for future use

write_rds(runs_big, here("results", "sim-output.rds"))


ST541-Fall2018/cointoss documentation built on May 31, 2019, 1:53 p.m.