Probability Foundations
🎲 Probability Foundations: Mastering Uncertainty
Probability is the mathematical framework for quantifying uncertainty. In software engineering, it is used for everything from A/B testing and risk analysis to machine learning and system reliability.
🟢 Level 1: The Axioms of Probability
1. Kolmogorov’s Axioms
Probability is defined over a sample space () and an event space (). For any event :
- Non-negativity: .
- Normalization: .
- Additivity: For disjoint events : .
2. Basic Properties
- Complement: .
- General Addition Rule: .
🟡 Level 2: Conditional Probability and Independence
3. Conditional Probability
The probability of event occurring given that event has already occurred:
4. Independence
Two events and are independent if the occurrence of one does not affect the probability of the other:
import random
# A simple simulation to show independence
n_trials = 10000
coin_flips = [(random.choice(['H', 'T']), random.choice(['H', 'T'])) for _ in range(n_trials)]
# P(Second flip is H)
p_h2 = sum(1 for f in coin_flips if f[1] == 'H') / n_trials
# P(Second flip is H | First flip is H)
p_h2_given_h1 = sum(1 for f in coin_flips if f[0] == 'H' and f[1] == 'H') / sum(1 for f in coin_flips if f[0] == 'H')
print(f"P(H2): {p_h2:.4f}, P(H2 | H1): {p_h2_given_h1:.4f}")🔴 Level 3: Bayes’ Theorem
5. The Power of Bayesian Inference
Bayes’ theorem allows us to update the probability of a hypothesis () based on new evidence ():
- : Posterior (Updated belief).
- : Likelihood (Evidence given hypothesis).
- : Prior (Initial belief).
- : Evidence (Normalization factor).
6. Law of Total Probability
To find the denominator , we sum over all possible hypotheses:
# Bayesian Spam Filter Concept
# P(Spam | 'Winner') = P('Winner' | Spam) * P(Spam) / P('Winner')
p_spam = 0.2
p_winner_given_spam = 0.9
p_winner_given_not_spam = 0.1
p_winner = (p_winner_given_spam * p_spam) + (p_winner_given_not_spam * (1 - p_spam))
p_spam_given_winner = (p_winner_given_spam * p_spam) / p_winner
print(f"Probability of spam given the word 'Winner': {p_spam_given_winner:.4f}")