Chapter SIMULATION OF DISCRETE PROBABILITIES
Exercise 4 Page 13
In raquetball, a player continues to serve as long as she is winning; a point is scored only when a player is serving and wins the volley. The first player to win 21 points wins the game. Assume that you serve first and have a probability .6 of winning a volley when you serve and probability .5 when your opponent serves. Estimate, by simulation, the probability that you will win a game.
Solution
gamewinprob <- function(N){ # Creating a function To calculate the probability with sample size as input
game <- rep(0,N) # Declaring placeholder for all serves with 0's
for (i in 1:N){
myserve <- TRUE # Assuming First is my serve
mypoint <- 0
oppntpoint <- 0
while (mypoint < 21 & oppntpoint < 21){
if (myserve){
point <- sample(c(0,1), size=1, prob = c(.4, .6))
mypoint <- mypoint + point
if (point == 0){
myserve <- FALSE
}
}else{
point <- sample(c(0,1), size=1, prob=c(.5,.5))
oppntpoint <- oppntpoint + point
if (point == 0){
myserve <- TRUE
}
}
}
game[i] <- mypoint > oppntpoint # 1's will be the serves i win
}
#return (game)
return ((mean(game) * 100))
}
## [1] 83
## [1] 79.2
## [1] 82.2