# Dependent Coinflip Calculation

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:
iterations += 1
if random.random() > .1:
iterations += 1
else:
simulations += 1
else:
iterations += 1


• 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%. – CBowman Dec 14 '16 at 20:05
• 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. – CBowman Dec 14 '16 at 20:11
• yep, noticed that after reading Henry's response. Thanks for pointing that out! – Rob Dec 15 '16 at 1:09

• $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$