Probability Basics
Probability Basics
Implement fundamental probability calculations including conditional probability and Bayes' theorem.
Functions to implement
1. joint_probability(p_a, p_b)
Compute P(A and B) assuming A and B are independent.
- Input: P(A) and P(B)
- Output: P(A) * P(B)
2. conditional_probability(p_a_and_b, p_b)
Compute P(A|B) = P(A and B) / P(B).
- Input: P(A and B), P(B)
- Output: Conditional probability
3. bayes_theorem(p_b_given_a, p_a, p_b)
Compute P(A|B) using Bayes' theorem.
- Input: P(B|A), P(A), P(B)
- Output: P(A|B)
4. total_probability(p_b_given_a, p_a, p_b_given_not_a)
Compute P(B) using the law of total probability.
- Input: P(B|A), P(A), P(B|not A)
- Output: P(B)
5. bayesian_update(prior, likelihood, evidence)
Perform a Bayesian update to compute posterior.
- Input: Prior P(H), Likelihood P(E|H), Evidence P(E)
- Output: Posterior P(H|E)
Examples
# Independent events
joint_probability(0.5, 0.3) # 0.15
# Conditional probability
conditional_probability(0.15, 0.3) # 0.5
# Bayes theorem
bayes_theorem(0.8, 0.1, 0.14) # ~0.571
# Classic disease example
# P(disease) = 0.01
# P(positive | disease) = 0.95
# P(positive | no disease) = 0.05
p_b = total_probability(0.95, 0.01, 0.05) # 0.0594
p_disease_given_positive = bayes_theorem(0.95, 0.01, p_b) # ~0.16
Notes
- All probabilities should be between 0 and 1
- Handle edge cases (e.g., division by zero)
Run tests to see results
No issues detected