← back

Calculate Conditional Probability from Data

#168 · Probability · Easy

⊣ Solve on deep-ml.com

Problem

Calculate the Conditional Probability P(A|B) from data. Given a dataset of observations, compute the probability of event A occurring given that event B has occurred, using the formula P(A|B) = P(A and B) / P(B).

Solution

1
2
3
4
5
6
7
8
9
from typing import List, Callable

def conditional_probability(data: List[dict], condition_b: Callable,
                            condition_a: Callable) -> float:
    count_b = sum(1 for d in data if condition_b(d))
    if count_b == 0:
        return 0.0
    count_a_and_b = sum(1 for d in data if condition_b(d) and condition_a(d))
    return count_a_and_b / count_b

Explanation

  1. Count the number of observations satisfying condition B (count_b).
  2. If no observations satisfy B, return 0 (undefined, but return 0 to avoid division by zero).
  3. Count observations satisfying both A and B (count_a_and_b).
  4. The conditional probability is the ratio: P(A|B) = count(A and B) / count(B).

Complexity

  • Time: O(n) where n is the number of data points
  • Space: O(1)