Riddler: Simulating and Optimizing Coin Flipping

Simulation

Published

May 2, 2020

Notable topics: Simulation

Recorded on: 2020-05-02

Timestamps by: Alex Cookson

View code

Screencast

Timestamps

crossing

Using crossing function to set up "tidy" simulation (gives you all possible combinations of values you provide it)

rbinom

Using rbinom function to simulate the number of prisoners who choose to flip, then using rbinom again to simulate number of tails

dbinom

Using dbinom function (probability mass function) to see probabilities of any given number of prisoners choosing to flip

map_dbl

Using map_dbl function to iterate a function, making sure to return a dbl-class object

seq_len

Using seq_len(n) instead of 1:n to be slightly more efficient

optimise

Using optimise function to conduct single-dimension optimisation (for analytical solution to this question)

Using backticks (like this) for inline R functions in RMarkdown

Starting the Extra Credit portion of the problem (N prisoners instead of 4)

map2_dbl

Using map2_dbl function to iterate a function that requires two inputs (and make sure it returns a dbl-class object)

Reviewing visualisation of probabilties with a varying numbers of prisoners

Tweaking graph to look nicer

Get the exact optimal probability value for each number of prisoners

optimise

Troubleshooting optimise function to work when iterated over different numbers of prisoners

unnest_wider

Using unnest_wider function to disaggregate a list, but put different elements on separate columns (not separate rows, which unnest does

Explanation of what happens to probabilities as number of prisoners increases

Summary of screencast