Exercise 2. The Cavs and the Warriors - Monte Carlo
Confirm the results of the previous question with a Monte Carlo simulation to estimate the probability of the Cavs winning the series after losing the first game.
This exercise is part of the course
HarvardX Data Science - Probability (PH125.3x)
Exercise instructions
- Use the
replicate
function to replicate the sample code forB <- 10000
simulations. - Use the
sample
function to simulate a series of 6 games with random, independent outcomes of either a loss for the Cavs (0) or a win for the Cavs (1) in that order. Use the default probabilities to sample. - Use the
sum
function to determine whether a simulated series contained at least 4 wins for the Cavs. - Use the
mean
function to find the proportion of simulations in which the Cavs win at least 4 of the remaining games. Print your answer to the console.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# The variable `B` specifies the number of times we want the simulation to run. Let's run the Monte Carlo simulation 10,000 times.
B <- 10000
# Use the `set.seed` function to make sure your answer matches the expected result after random sampling.
set.seed(1)
# Create an object called `results` that replicates for `B` iterations a simulated series and determines whether that series contains at least four wins for the Cavs.
# Calculate the frequency out of `B` iterations that the Cavs won at least four games in the remainder of the series. Print your answer to the console.