## Problem 2 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 2: To encourage Elmer's promising tennis carrer, his father offers him a prize if he wins two or more sets in a row in a three-set series to be played with this father and the club champion alternately: father-champion-father or champ-dad-champ. The champion is a better player than Elmer's father. Which order should Elmer choose?

#!/usr/bin/env ruby
# This is another search problem. We're not entirely
# sure if the relative gap between the pro and the
# father matters, and we're not even sure that the son
# can beat the father, much less the pro. Alternately,
# he might beat them both handily.
#
# So, we'll take sample points: we'll give the son 11
# different chances against the pro: 0% chance of
# winning, 10% chance of winning, ... , 90% chance,
# 100%. Then, for each of these, we'll give him all
# the same chances up to this chance against his father.
# At each of these, we run a number of trials with both
# orders.
#
# Yes, this gets expensive: it's the upper triangle of a
# 11x11 matrix, so we're going to do
# 11x11/2*2*TRIALS = 121*TRIALS trials.
# But, hey, cycles are cheap.
TRIALS=10000
RANGE=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
n_dpd = 0
n_pdp = 0
def win(series)
did_win = 0 # 1 is "won last", 2 is "won bet"
series.each do |p_now|
if p_now > rand()
did_win += 1
return true if did_win == 2
else
did_win = 0
end
end
return false
end
RANGE.each do |p_pro|
RANGE.each do |p_dad|
break if p_dad > p_pro # we'll let dad == pro through for now
TRIALS.times do
n_dpd += 1 if (win([p_dad, p_pro, p_dad]))
n_pdp += 1 if (win([p_pro, p_dad, p_pro]))
end
end
end
puts "Total Wins: dad/pro/dad=#{n_dpd}, pro/dad/pro=#{n_pdp}"

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.