An interesting coin flipping game is described here: flip a fair coin, stopping whenever you like, with a maximum number of tosses. Try to maximize the ratio of heads to total flips. What is the expected result?
Some beautiful mathematics are given, which I tried to verify with some simple python code. Apparently I missed something, since my result deviates a bit from the given answer. Can anyone spot my mistake?
I see the EV of a 100 round game as ~0.784, while the actual solution is given as ~0.793.
My code:
def calculate_payouts(total_rounds):
result = {}
for round_count in range(total_rounds, 0, -1):
payouts_this_round = []
for head_count in range(round_count + 1):
payouts_this_round.append(_calculate_payout_of_N_heads_in_round(head_count, round_count, result, total_rounds))
result[round_count] = payouts_this_round
return result
def _calculate_payout_of_N_heads_in_round(head_count, round_count, result_so_far, total_rounds):
payout_now = head_count / float(round_count)
# base case: final round payouts can be given directly
if round_count == total_rounds:
return payout_now
# base case: if we've already calculated this payout, return it
if result_so_far.get(round_count, None) is not None:
return result_so_far[round_count][head_count]
# calculate the answer recursively
ev_heads = _calculate_payout_of_N_heads_in_round(head_count + 1, round_count + 1, result_so_far, total_rounds)
ev_tails = _calculate_payout_of_N_heads_in_round(head_count, round_count + 1, result_so_far, total_rounds)
payout_if_flip = .5 * ev_heads + .5 * ev_tails
return max(payout_now, payout_if_flip)
if __name__ == '__main__':
result = calculate_payouts(100)
print 'game ev', (.5 * result[1][0] + .5 * result[1][1])
result_so_far.get(round_count, None), and why isn't itresult_so_far.get(round_count, head_count)? – David K Jul 17 '15 at 18:07