← back

Compute Posterior Probability using Bayes' Theorem

#336 · Probability · Easy

⊣ Solve on deep-ml.com

Problem

Compute the posterior probability using Bayes' Theorem. Given a prior probability P(H), likelihood P(E|H), and evidence P(E), calculate the posterior P(H|E) = P(E|H) * P(H) / P(E).

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from typing import Dict, List

def bayes_theorem(
    prior: float,
    likelihood: float,
    evidence: float
) -> float:
    if evidence == 0:
        raise ValueError("Evidence probability cannot be zero")
    posterior = (likelihood * prior) / evidence
    return round(posterior, 4)

def bayes_with_multiple_hypotheses(
    priors: List[float],
    likelihoods: List[float]
) -> List[float]:
    # Compute evidence as sum of prior * likelihood for all hypotheses
    evidence = sum(p * l for p, l in zip(priors, likelihoods))
    if evidence == 0:
        raise ValueError("Total evidence is zero")

    posteriors = [(l * p) / evidence for p, l in zip(priors, likelihoods)]
    return [round(p, 4) for p in posteriors]

Explanation

  1. Bayes' Theorem: P(H|E) = P(E|H) * P(H) / P(E). It updates the prior belief P(H) given observed evidence E.
  2. The simple version takes prior, likelihood, and evidence directly.
  3. For multiple hypotheses, the evidence P(E) is computed using the law of total probability: P(E) = sum(P(E|H_i) * P(H_i)).
  4. Each hypothesis's posterior is computed and the posteriors sum to 1.

Complexity

  • Time: O(k) where k is the number of hypotheses
  • Space: O(k) for storing posteriors