13.9 Exercises

1. One ball will be drawn at random from a box containing: 3 cyan balls, 5 magenta balls, and 7 yellow balls. What is the probability that the ball will be cyan?

cyan<-3
magenta<-5
yellow<-7
allballs<-cyan+magenta+yellow
pr<-cyan/(cyan+magenta+yellow)

2. What is the probability that the ball will not be cyan?

prnot<-1-pr

3. Instead of taking just one draw, consider taking two draws. You take the second draw without returning the first draw to the box. We call this sampling without replacement. What is the probability that the first draw is cyan and that the second draw is not cyan?

firstdraw<-pr
seconddraw<-1-(cyan-1)/(allballs-1)
prnorep<-firstdraw*seconddraw

4. Now repeat the experiment, but this time, after taking the first draw and recording the color, return it to the box and shake the box. We call this sampling with replacement. What is the probability that the first draw is cyan and that the second draw is not cyan?

prrep<-pr*prnot

5. Two events A and B are independent if Pr(A and B)= Pr(A)P(B). Under which situation are the draws independent?

praandb=.2*.8

You replace the draw.

6. Say you’ve drawn 5 balls from the box, with replacement, and all have been yellow. What is the probability that the next one is yellow?

pr<-yellow/(allballs)

7. If you roll a 6-sided die six times, what is the probability of not seeing a 6?

notsix<-5/6
pr<-(5/6)^6

8. Two teams, say the Celtics and the Cavs, are playing a seven game series. The Cavs are a better team and have a 60% chance of winning each game. What is the probability that the Celtics win at least one game?

cavs<-.6
celts<-1-cavs
prob<-1-(cavs)^4

9. Create a Monte Carlo simulation to confirm your answer to the previous problem. Use B <- 10000 simulations. Hint: use the following code to generate the results of the first four games:

b<-10000
celtic_wins<-sample(c("loss", "win"), 4, replace=TRUE, prob=c(0.6, 0.4))
set.seed(1)
celtic_wins<-replicate(b, {simulated_games<-sample(c("lose","win"), 4, replace=TRUE, prob=c(0.6, 0.4)) 
any(simulated_games=="win")})
mean(celtic_wins)
## [1] 0.8757

The Celtics must win one of these 4 games.

10. Two teams, say the Cavs and the Warriors, are playing a seven game championship series. The first to win four games, therefore, wins the series. The teams are equally good so they each have a 50-50 chance of winning each game. If the Cavs lose the first game, what is the probability that they win the series?

numremain<-6
outcomes<-c(0, 1)
l<-rep(list(outcomes), numremain)
possibilities<-expand.grid(l)
results<-rowSums(possibilities)>=4
results
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
## [25] FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE
## [37] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE
## [49] FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
## [61]  TRUE  TRUE  TRUE  TRUE
mean(results)
## [1] 0.34375

11. Confirm the results of the previous question with a Monte Carlo simulation.

B<-10000 
set.seed(50)
results<-replicate(B, {cavs_wins<-sample(c(0,1), 6, replace=TRUE)
sum(cavs_wins)>=4 })
mean(results)
## [1] 0.345

12. Two teams,\(A\) and \(B\), are playing a seven game series. Team \(A\) is better than team \(B\) and has a \(p>0.5\) chance of winning each game. 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:

Use the function \(sapply\) to compute the probability, call it Pr, of winning for \(p <- seq(0.5, 0.95, 0.025)\). Then plot the result.

prob_win<-function(p){
  B<-10000
  result<-replicate(B, {
  b_win<-sample(c(1,0), 7, replace=TRUE, prob=c(1-p, p))
  sum(b_win)>=4
    })
  mean(result)
}
p<-seq(.5, .95, .025)
Pr<-sapply(p, prob_win)
plot(p, Pr)

13. Repeat the exercise above, but now keep the probability fixed at p <- 0.75 and compute the probability for different series lengths: best of 1 game, 3 games, 5 games,… Specifically, N <- seq(1, 25, 2). Hint: use this function:

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)
}
N<-seq(1, 25, 2)
Pr<-sapply(N, prob_win)
plot(N, Pr)