Exercise 4. A and B play a series - part 2
Repeat the previous exercise, but now keep the probability that team \(A\) wins fixed at p <- 0.75
and compute the probability for different series lengths. For example, wins in best of 1 game, 3 games, 5 games, and so on through a series that lasts 25 games.
This exercise is part of the course
HarvardX Data Science - Probability (PH125.3x)
Exercise instructions
- Use the
seq
function to generate a list of odd numbers ranging from 1 to 25. - Use the function
sapply
to compute the probability, call itPr
, of winning during series of different lengths. - Then plot the result
plot(N, Pr)
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Given a value 'p', the probability of winning the series for the underdog team B can be computed with the following function based on a Monte Carlo simulation:
prob_win <- function(N, p=0.75){
B <- 10000
result <- replicate(B, {
b_win <- sample(c(1,0), N, replace = TRUE, prob = c(1-p, p))
sum(b_win)>=(N+1)/2
})
mean(result)
}
# Assign the variable 'N' as the vector of series lengths. Use only odd numbers ranging from 1 to 25 games.
# Apply the 'prob_win' function across the vector of series lengths to determine the probability that team B will win. Call this object `Pr`.
# Plot the number of games in the series 'N' on the x-axis and 'Pr' on the y-axis.