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).
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_bcount_b).count_a_and_b).P(A|B) = count(A and B) / count(B).