KatInterview

Bayesian Inference for Coin Flipping

Arrays & HashingAuto-graded · 13 test cases
MediumAuto-gradedArrays & Hashingprobabilitybayes-theoremexpected-value

Last 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:

  1. The posterior probability that the selected coin is the biased coin $B$, given the observed outcome $HH$: $$P(B \mid HH)$$

  2. The posterior predictive probability that the third flip will also be Heads, given $HH$: $$P(H_3 \mid HH)$$

  3. 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.