Bayesian Coin Identification from Flip Observations
Arrays & HashingAuto-graded · 21 test casesLast updated
Compute exact posterior probabilities over three weighted coins from a sequence of flips, plus the predictive probability the next flip is heads.
A bag holds three coins that look identical but have different head probabilities:
- Coin
"A":P(H) = 1/2 - Coin
"B":P(H) = 4/5 - Coin
"C":P(H) = 1
One coin is picked uniformly at random (each with prior probability 1/3), and then it is flipped repeatedly. You are given the sequence of results.
Implement coin_beliefs(observations) that returns each coin's posterior probability after observing the sequence, plus the probability that one more flip of the same coin lands heads.
Inputs and outputs:
observationsis a list of strings, each either"H"or"T". Any other entry must raiseValueError.- Flips are conditionally independent given the coin.
Return a dictionary with exactly these keys:
"coin_c_fraction": the posterior probability of coin"C"as a string"numerator/denominator"in lowest terms."coin_c_decimal": that same probability as a float rounded to 4 decimal places."posterior_fractions": a dict mapping"A","B","C"to the posterior probability of each coin, as reduced fraction strings."posterior_decimals": a dict mapping"A","B","C"to the same posteriors rounded to 4 decimal places as floats."next_heads_fraction": the probability the next flip is heads, given the observations, as a reduced fraction string. This is computed by averaging each coin's head probability over the posterior."next_heads_decimal": that value rounded to 4 decimal places.
Rules and edge cases:
- Each posterior is the coin's likelihood (the probability of the observed sequence under that coin) times its prior, divided by the total likelihood. Since all priors are equal, the common factor cancels, but the returned probabilities must still be normalized to sum to 1.
- A posterior of zero is returned as
"0/1"and0.0. - If the sequence has zero probability under all three coins (for example a sequence containing flips impossible under every coin — impossible here since only
"T"under coin"C"has zero probability but every sequence with a"T"is still possible under"A"), raiseValueError. In practice the only way to raise is an invalid symbol. - Use exact rational arithmetic (for example Python's
fractions.Fraction) so fractions are exact and only the final decimals are rounded.
Examples
Example 1
Input: coin_beliefs(['H', 'H', 'H'])
Output: {'coin_c_fraction': '1000/1637', 'coin_c_decimal': 0.6109, 'posterior_fractions': {'A': '125/1637', 'B': '512/1637', 'C': '1000/1637'}, 'posterior_decimals': {'A': 0.0764, 'B': 0.3128, 'C': 0.6109}, 'next_heads_fraction': '14721/16370', 'next_heads_decimal': 0.8993}
Hints
Hint 1
The posterior for each coin is proportional to its prior times the likelihood of the whole sequence under that coin; with equal priors it is just the likelihood divided by the sum of likelihoods.
Hint 2
The likelihood of a sequence under a coin is the product over flips of p for heads and (1-p) for tails; track it exactly with fractions.
Hint 3
The next-flip probability is not any single coin's head rate: average each coin's head probability weighted by its posterior.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a Bayesian update over a tiny discrete hypothesis space (three coins).
- Likelihood per coin. For each coin with head probability
p, compute the probability of the whole observed sequence by multiplyingpfor each head and1-pfor each tail. Do this withFractionso values stay exact. Reject any symbol that is not"H"or"T". - Evidence. Sum the three likelihoods. Because the priors are all
1/3, that common factor cancels, so the posterior for a coin is simply its likelihood divided by the sum. Dividing by the total also normalizes the posteriors to sum to exactly 1. - Next flip. The next toss is linked to the unknown coin, so it is not independent of past tosses. Average each coin's head probability under the posterior:
sum over coins of P(H | coin) * P(coin | observations). - Formatting. Emit fraction strings from the numerator and denominator, and decimals as
round(float(x), 4). A zero posterior naturally becomes"0/1"and0.0.
Why it is correct
The posterior is, by Bayes' theorem, prior × likelihood / evidence. Equal priors let us fold the prior into the normalizing constant. The predictive probability is the law of total probability conditioned on the data, averaging the head rate over the posterior, so the arithmetic yields the exact conditional probability of the next head.
Complexity
For n flips and a fixed three coins, each likelihood is built in O(n), so time is O(n) and extra space is O(1) beyond the output (fraction sizes aside).