Bayesian Inference for Coin Flipping
Arrays & HashingAuto-graded · 13 test casesLast updated
Evaluate posterior coin probabilities, posterior predictive likelihoods, and expected stopping times following observed heads.
Consider a experiment involving two coins:
- Coin $F$ is fair, with probability of landing Heads $P(H \mid F) = 0.5$.
- Coin $B$ is biased, with probability of landing Heads $P(H \mid B) = 0.7$.
One of the two coins is selected uniformly at random ($P(F) = P(B) = 0.5$). You flip the chosen coin repeatedly. The outcomes of the first two flips are both Heads ($HH$).
Write a function that computes and returns the following three quantities in order:
-
The posterior probability that the selected coin is the biased coin $B$, given the observed outcome $HH$: $$P(B \mid HH)$$
-
The posterior predictive probability that the third flip will also be Heads, given $HH$: $$P(H_3 \mid HH)$$
-
The conditional expected value of $T$, where $T$ is the total number of flips up to and including the first Tails ($T$), given $HH$: $$E[T \mid HH]$$
Output Format
Return a list of three floating-point numbers: [P(B | HH), P(H_3 | HH), E[T | HH]].
Constraints
- This problem requires no inputs and expects precise floating-point values for the specified scenario.
Examples
Example 1
Input: coin_probabilities()
Output: [0.6621621621621622, 0.6324324324324324, 4.882882882882883]
Hints
Hint 1
Calculate the likelihood of observing two consecutive Heads under each coin type independently.
Hint 2
Use Bayes' theorem to update the prior probabilities of picking each coin after observing HH.
Hint 3
To find the expected stopping time, use the law of total expectation conditioned on which coin was picked.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We break down the solution into three distinct Bayesian steps:
-
Posterior Probability $P(B \mid HH)$: Using Bayes' Theorem: $$P(B \mid HH) = \frac{P(HH \mid B) P(B)}{P(HH \mid B) P(B) + P(HH \mid F) P(F)}$$ Since $P(B) = P(F) = 0.5$, the priors cancel: $$P(HH \mid B) = 0.7^2 = 0.49$$ $$P(HH \mid F) = 0.5^2 = 0.25$$ $$P(B \mid HH) = \frac{0.49}{0.49 + 0.25} = \frac{49}{74} \approx 0.66216$$
-
Posterior Predictive Probability $P(H_3 \mid HH)$: We marginalize over the hidden state (the coin identity) using our updated posterior weights: $$P(H_3 \mid HH) = P(H \mid B) P(B \mid HH) + P(H \mid F) P(F \mid HH)$$ $$P(H_3 \mid HH) = 0.7 \cdot \frac{49}{74} + 0.5 \cdot \frac{25}{74} = \frac{117}{185} \approx 0.63243$$
-
Expected Stopping Time $E[T \mid HH]$: Since the first two flips are already known to be Heads, $T = 2 + R$, where $R$ is the remaining number of flips until the first Tail appears. Given the memoryless property of coin flips conditioned on the specific coin:
- If coin $B$ was picked, $R \sim \text{Geometric}(p_{\text{Tail}} = 0.3)$, so $E[R \mid B] = \frac{1}{0.3} = \frac{10}{3}$.
- If coin $F$ was picked, $R \sim \text{Geometric}(p_{\text{Tail}} = 0.5)$, so $E[R \mid F] = \frac{1}{0.5} = 2$.
By the Law of Total Expectation: $$E[R \mid HH] = E[R \mid B] P(B \mid HH) + E[R \mid F] P(F \mid HH) = \frac{10}{3} \cdot \frac{49}{74} + 2 \cdot \frac{25}{74} = \frac{320}{111}$$ $$E[T \mid HH] = 2 + E[R \mid HH] = 2 + \frac{320}{111} = \frac{542}{111} \approx 4.88288$$
Complexity Analysis
- Time Complexity: $\mathcal{O}(1)$ because the calculation performs a constant number of arithmetic operations.
- Space Complexity: $\mathcal{O}(1)$ auxiliary memory.
Python Reference Solution
from typing import List
def coin_probabilities() -> List[float]:
# Likelihoods of HH
p_hh_given_f = 0.5 ** 2 # 0.25
p_hh_given_b = 0.7 ** 2 # 0.49
# Posterior probability of coin B
posterior_b = p_hh_given_b / (p_hh_given_b + p_hh_given_f) # 49 / 74
posterior_f = 1.0 - posterior_b # 25 / 74
# Predictive probability of next flip being Heads
p_h3_given_hh = posterior_b * 0.7 + posterior_f * 0.5 # 117 / 185
# Expected total number of flips until first Tails
exp_r_given_b = 1.0 / 0.3
exp_r_given_f = 1.0 / 0.5
exp_r = posterior_b * exp_r_given_b + posterior_f * exp_r_given_f
exp_t_given_hh = 2.0 + exp_r # 542 / 111
return [posterior_b, p_h3_given_hh, exp_t_given_hh]