0
$\begingroup$

If I have a coin that's fair on the first flip, but after getting a heads it has a 90% chance of getting heads on the following flip, what are the odds of the coin being heads on any random trial? Assume after getting tails the probability resets to fair.

I wrote some python code that tells me it's around 91%, but I'm not sure how to calculate this:

heads_counter = 0
iterations = 0

while iterations < 1000000:
    if random.random() > .5:
        heads_counter += 1
        iterations += 1
        heads = True
        while heads:
            if random.random() > .1:
                heads_counter += 1
                iterations += 1
            else:
                heads = False
                simulations += 1
    else:
        iterations += 1

heads_counter / (1. * iterations)
$\endgroup$
  • 2
    $\begingroup$ The answer below looks correct, but just wanted to point out that the 91% prediction should raise a red flag about the code - If the coin was always weighted to favor heads at 90%, then the heads fraction would be 90%. Because some of the time it is weighted 50/50, the heads fraction must therefore be < 90%. $\endgroup$ – CBowman Dec 14 '16 at 20:05
  • 1
    $\begingroup$ Remember every time you call random.random() you are "flipping" the coin, so each time you do this it must result in a iterations += 1. However, this is not the case when you get a tails following a previous heads (i.e. following the heads = False line). As a result you're under-counting the iterations and this erroneously increase the heads fractions you get at the end. $\endgroup$ – CBowman Dec 14 '16 at 20:11
  • $\begingroup$ yep, noticed that after reading Henry's response. Thanks for pointing that out! $\endgroup$ – Rob Dec 15 '16 at 1:09
2
$\begingroup$

If you are looking for the stable long-term distribution, you have:

  • $P(H)=\dfrac9{10}P(H)+\dfrac12P(T)$
  • $P(T)=\dfrac1{10}P(H)+\dfrac12P(T)$
  • $P(H)+P(T)=1$

The first two each give $P(H)=5P(T)$, which combined with the third gives

  • $P(H)=\dfrac56$
  • $P(T)=\dfrac16$
$\endgroup$
  • $\begingroup$ this conflicts with my simulation's results because my simulation had a bug! Great answer $\endgroup$ – Rob Dec 14 '16 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.