Problem 16 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 16: A tennis tournament of 8 players has the initial ladder chosen at random. Assuming the best player beats everyone, and the second-best player beats everyone else, what's the chance that the second-best player wins runner-up?
#!/usr/bin/env ruby
TRIALS=100000
# Here we get into some slightly gnarly modelling.
#
# We'll model the current stage of competition as an array
# of ranks:
PLAYERS=[1,2, 3,3, 3,3, 3,3]
# Then, we run a round of the game as follows:
def round(seedings)
next_round = []
i = 0
while i < seedings.length
a = seedings[i]
b = seedings[i+1]
winner = a < b ? a : b # ranks, so lower wins
next_round.push(winner)
i += 2
end
return next_round
end
def second_runner_up(first_round)
quarters = round(first_round)
semis = round(quarters)
return semis.include?(2)
end
n_second_runner_up = 0
TRIALS.times do
srand # There's a fun story here... guess what it is.
seeding = PLAYERS.shuffle
n_second_runner_up += 1 if second_runner_up(seeding)
end
puts "Out of #{TRIALS} brackets, #2 was runner up "
puts " #{n_second_runner_up} times. "
puts " P = #{n_second_runner_up/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.