## Problem 10 of Monte Carlo solutions to Fifty Challenging Problems...

(This is another part of the Fifty Problems series, a set of example applications of Monte Carlo methods. In each post, I present source code which answers a probabilistic question using simulated models of the underlying system.)

Problem 10: An urn drawing game: an urn contains 10 black and 10 white balls. You call "black" or "white," and a ball is drawn. If it matches your call, you win $10. a) What's the most you will pay to play? b) Change the game such that the black/white mix is unknown to you. What would you pay if you were only allowed to play the game one time?

#!/usr/bin/env ruby
TRIALS=10000
# For the fair game, we can just try it a bunch of times.
# The game is symmetric about color choice, so we can
# ignore the details and just use the 50/50 odds.
wins = 0
TRIALS.times do
wins +=1 if (rand() < 0.5)
end
payout = 10.0 * wins
puts "Out of #{TRIALS} fair plays, we won #{wins}, $#{payout}."
puts "Therefore, the maximum amount to pay would be "
puts " $#{payout/TRIALS.to_f}"
# Now, for the weird game. Your friend can vary the
# ratio of balls in the urn, so we need to try a lot
#of games at different probabilities. We can do this
# stratified or randomly. Let's do randomly first.
wins = 0
TRIALS.times do
wins +=1 if (rand() < rand())
end
payout = 10.0 * wins
puts "Out of #{TRIALS} randomized friend-chosen plays"
puts " we won #{wins}, $#{payout}. Therefore, the "
puts " maximum amount to pay would be "
puts "$#{payout/TRIALS.to_f}"
# And now stratified, to see if it makes any difference
wins = 0
TRIALS.times do |i|
wins +=1 if (rand() < i.to_f/TRIALS.to_f)
end
payout = 10.0 * wins
puts "Out of #{TRIALS} stratified friend-chosen plays,"
puts " we won #{wins}, $#{payout}. Therefore, the "
puts " maximum amount to pay would be "
puts " $#{payout/TRIALS.to_f}"

I've been coding my way through *Fifty Challenging Problems in Statistics with Solutions*. This post is a part of the *Fifty Challenging Problems* series.

*This was brought to you by Josh Myer.* He has other
fun things at his homepage.