Loading W Code...
š®š³ Indian Context
Spam detection for Indian emails, Zomato order prediction, and exam pass rates!
Foundation
import numpy as np
print("=" * 55)
print("PROBABILITY FUNDAMENTALS")
print("=" * 55)
# Coin toss experiment
np.random.seed(42)
n_flips = 10000
flips = np.random.choice(['H', 'T'], size=n_flips)
p_heads = np.mean(flips == 'H')
print(f"\nCoin toss ({n_flips} flips):")
print(f" P(Heads) ā {p_heads:.4f} (theory: 0.5)")
# Dice example
rolls = np.random.randint(1, 7, size=n_flips)
p_six = np.mean(rolls == 6)
p_even = np.mean(rolls % 2 == 0)
print(f"\nDice ({n_flips} rolls):")
print(f" P(6) ā {p_six:.4f} (theory: {1/6:.4f})")
print(f" P(even) ā {p_even:.4f} (theory: 0.5)")
# Conditional Probability
print("\n" + "=" * 55)
print("CONDITIONAL PROBABILITY")
print("=" * 55)
# P(pass | studied) vs P(pass | not studied)
# 100 students: 60 studied, 40 didn't
# Of those who studied: 50 passed
# Of those who didn't: 10 passed
total = 100
studied = 60; not_studied = 40
passed_studied = 50; passed_not_studied = 10
p_pass_given_study = passed_studied / studied
p_pass_given_no_study = passed_not_studied / not_studied
p_pass = (passed_studied + passed_not_studied) / total
print(f"\nš Indian Example: Exam Results")
print(f" P(Pass | Studied) = {passed_studied}/{studied} = {p_pass_given_study:.3f}")
print(f" P(Pass | Not Studied) = {passed_not_studied}/{not_studied} = {p_pass_given_no_study:.3f}")
print(f" P(Pass) = {passed_studied + passed_not_studied}/{total} = {p_pass:.3f}")
# Independence Check
print(f"\n Independent? P(Pass,Study) = P(Pass)ĆP(Study)?")
p_ps = passed_studied / total # Joint
p_p = p_pass # Marginal
p_s = studied / total
print(f" P(Pass ā© Study) = {p_ps:.3f}")
print(f" P(Pass) Ć P(Study) = {p_p:.3f} Ć {p_s:.3f} = {p_p*p_s:.3f}")
print(f" Independent? {abs(p_ps - p_p*p_s) < 0.01} ā {'Yes' if abs(p_ps - p_p*p_s) < 0.01 else 'No, studying matters!'}")