Example Problem Set
Problem Set Due Wednesday September 35th at 7pm on Canvas.
For this problem set you will hand in a .Rmd
file and a knitted html output.
Question 1
Ivan and Sara are a married couple who like to play cribbage6, but argue over who is the better player. They decide that they will play an Ultimate Cribbage Championship to determine the Ultimate Cribbage Champion of the house.7 They will play 5 games, and whoever wins a majority of the 5 games is crowned the Ultimate Cribbage Champion.
Pretend that we are all-knowing about Ivan and Sara (and cribbage), and know that Sara is actually the better cribbage player, and the odds that she wins any given cribbage game is 60%.
(Also pretend that Ivan and Sara will play all 5 games, even if someone has already won 3 games.)
(a) Write code that simulates Ivan and Sara playing their 5 game cribbage championship, given that Sara is expected to win 60% of the games. (Hint: you can think of this like tossing a coin 5 times where “heads” is Sara winning the game, and comes up heads 60% of the time.) Who won the majority of the 5 cribbage games in your simulation, and therefore, was crowned Ultimate Cribbage Champion?
set.seed(2105)
#Generating a "coin" with 6 1s and 4 0s
probs <- c(1,1,1,1,1,1,0,0,0,0)
#One sample of 5 games from this
sample(probs,5, replace=T)
#> [1] 1 0 1 1 1
In my first simulation of the UCC Sara won 4 out of the 5 games, and therefore was crowned the champion. Congrats Sara, I guess!
This is the right answer! 100% .
(b) Now, using a for()
loop, simulate this same 5-game cribbage championship 10,000 times. Each time through the loop save the number of games that Sara wins. Create a table that lists the proportion of the simulated championships where Sara wins 0,1,2,3,4, and 5 games. What proportion of the time does Sara (who is the statistically better cribbage player) win 3 or more games – in other words, is crowned the Ultimate Cribbage Champion?
No answer given. 0% .
(c) Sara is mad at this setup because she feels like the odds are too great that Ivan will win the Ultimate Cribbage Champion. It would be nice if Sara, the better cribbage player, had a 95% chance of winning the Ultimate Cribbage Championship. Given that Sara wins 60% of cribbage matches, how long would a series have to be in order for Sara to win 95% of the 10,000 simulated Cribbage Championships? To determine this, modify your code with a second loop so that the length of the Cribbage Championships increases by two from 5, 7, to 9, to 11, all the way to 151 games. (Note, it took my computer about 10 seconds to run this loop, so if it’s taking minutes you should probably re-evaluate your code.) Make a graph with championship length on the x-axis, the proportion of time Sara wins the series on the y-axis, and a horizontal line at .95. At what championship length does Sara win 95% of the 10,000 simulated Championships? (No need for a precise answer, you can just eyeball it from the graph.)
I think the series would have to be twice as long. Around 10 games
No real attempt to answer the question or explain process. 0% .
(c) (again)
for(j in 1:length(num.sara.wins)){}
for(i in 1:length(num.sara.wins)){
num.sara.wins[i] <- sum(sample(probs,series[i], replace=T))
}
sara.champ.prob[j] <- mean(num.sara.wins>(series[j]/2))
}
I know that I have to use a double loop here to try to figure this out but I can’t figure out what is going wrong.
Incomplete answer. 100% .
(c) (again)
length.series <- seq(5,151,2)
sara.champ.prob <- rep(NA, length(length.series))
for(j in 1:length(length.series) ){
num.sara.wins <- rep(NA, 10000)
for(i in 1:length(num.sara.wins)){
num.sara.wins[i] <- sum(sample(probs,length.series[i], replace=T))
}
sara.champ.prob[i] <- mean(num.sara.wins>(length.series[i]/2))
}
plot(length.series,sara.champ.prob, type="b", pch=16, xlab="Length of Championship", ylab="Probability Better Player Wins Championship",
main="Championship Length Sims")
abline(h=.95, lty=2, col="firebrick")
Incorrect answer. 100% .