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])
  • $\begingroup$ Interesting. A point of clarification: are we allowed to stop at one toss? If so, then with probability $\frac 12$ you get H so you'd have an infinite ratio. $\endgroup$ – lulu Jul 17 '15 at 16:32
  • 2
    $\begingroup$ Or maybe you meant "proportion of H". So if I get my H first then I do indeed quit at once, with my answer of 1. $\endgroup$ – lulu Jul 17 '15 at 16:38
  • $\begingroup$ @lulu, the originating (linked-to) question asked about maximizing the ratio of heads to total flips (not tails). I've edited the question here to ask for the same ratio. $\endgroup$ – Barry Cipra Jul 17 '15 at 17:07
  • $\begingroup$ @Barry Cipra Thanks. That's the question that makes sense, surely. $\endgroup$ – lulu Jul 17 '15 at 17:18
  • 1
    $\begingroup$ Looking at the first answer that was posted, it's pretty clear: you make your strategy non-optimal by always giving up after 100 flips. You are accepting some nodes of the probability tree as having less than 0.5 expected payout (simply because the occur in round 100), whereas the expected payout from any node of the tree is at least 0.5. $\endgroup$ – David K Jul 17 '15 at 18:13

The reason your answer is too low is that the winning strategy requires patience, which your code lacks :-) The strategy relies on the property of the symmetric simple one-dimensional random walk of returning to the origin with probability $1$. So no matter how bad your ratio gets, you just have to wait long enough to get it back to $50\%$. Your code doesn't do that; it counts lots of cases where you stop at a very bad ratio.

You'd get a better approximation of the correct result if you'd count any ratio less than $50\%$ as $50\%$.

| cite | improve this answer | |

Your Answer

