CH 21Phase 3 · Probability & Statistics for AI

Bayes Theorem

১৫–২৫ মিনিট বাংলা · Math · Python
📖 একটি ছোট গল্প

একটি rare disease ১০,০০০ জনে ১ জনের হয়। Test ৯৯% accurate। আপনার test positive এলো — আপনার আসলেই disease থাকার probability কত? অনেকে বলবেন ৯৯%। সত্যি উত্তর: প্রায় ১%। Bayes theorem-ই এই counter-intuitive উত্তর ব্যাখ্যা করে।

Intuitive Explanation

Bayes theorem = "প্রমাণ দেখে belief update করা"-র formula।

P(H|E) = P(E|H) · P(H) / P(E)
  • P(H) = prior — evidence-এর আগে belief।
  • P(E|H) = likelihood — H সত্য হলে E দেখার probability।
  • P(E) = evidence — total probability of E।
  • P(H|E) = posterior — evidence-এর পর updated belief।

Derivation

Conditional probability-র definition দিয়ে শুরু:

P(H|E) = P(H ∩ E) / P(E)
P(E|H) = P(H ∩ E) / P(H) ⟹ P(H ∩ E) = P(E|H) · P(H)

প্রথম সমীকরণে substitute করলে:

P(H|E) = P(E|H) · P(H) / P(E)

Law of total probability (denominator-এর জন্য)

P(E) = P(E|H) · P(H) + P(E|¬H) · P(¬H)

Disease Test Paradox — সংখ্যা সহ

  • Prior: P(D) = 0.0001
  • Sensitivity: P(+|D) = 0.99
  • False positive: P(+|¬D) = 0.01
P(+) = 0.99·0.0001 + 0.01·0.9999 ≈ 0.0101
P(D|+) = 0.99 · 0.0001 / 0.0101 ≈ 0.0098 ≈ 1%
💡 ইনসাইট
Rare disease + imperfect test = ৯৯% test accuracy সত্ত্বেও positive পেলে আপনার আসলেই disease থাকার chance মাত্র ~১%। Prior matters enormously।

Python Implementation

pythonPython · NumPy
# Bayes theorem: disease test
def bayes(prior, likelihood, false_positive):
    evidence = likelihood * prior + false_positive * (1 - prior)
    return likelihood * prior / evidence

# Rare disease
print(f"P(D|+) when prior=0.0001 → {bayes(0.0001, 0.99, 0.01):.4f}")
print(f"P(D|+) when prior=0.01   → {bayes(0.01,   0.99, 0.01):.4f}")
print(f"P(D|+) when prior=0.10   → {bayes(0.10,   0.99, 0.01):.4f}")

# Sequential update: two independent positive tests
prior = 0.0001
posterior_1 = bayes(prior, 0.99, 0.01)
posterior_2 = bayes(posterior_1, 0.99, 0.01)   # use posterior as new prior
print(f"\nAfter 1 positive: {posterior_1:.4f}")
print(f"After 2 positives: {posterior_2:.4f}")

AI/ML সংযোগ

  • Naive Bayes classifier: P(class | features) ∝ P(features | class) · P(class)
  • Bayesian Neural Networks: weight-এর posterior distribution শেখা।
  • MAP estimation: posterior maximize করে parameter বের করা।
  • Bayesian Optimization: hyperparameter tuning — Gaussian Process prior + observation।
  • RAG-এ retrieval scoring: query-এর শর্তে document-এর posterior probability।

Common Mistakes

  • Base-rate neglect: prior ignore করে শুধু likelihood দেখা।
  • P(H|E) এবং P(E|H)-কে একই ভেবে নেওয়া।
  • Sequential update-এ posterior-কে নতুন prior হিসেবে ব্যবহার না করা।

Practice Tasks

  1. উপরের code-এ prior পরিবর্তন করুন (0.001 → 0.5)। Posterior কীভাবে বদলায়?
  2. একটি email "lottery" word আছে। Spam হওয়ার posterior probability calculate করুন।
  3. ৩টি urn problem (Monty Hall) Bayes দিয়ে solve করুন।

Assignment

একটি spam dataset (Enron, SMS Spam Collection) দিয়ে scratch থেকে Naive Bayes classifier implement করুন। Vocabulary build, P(word | spam), P(word | ham), Laplace smoothing, log-probability — সব step নিজে করুন। Accuracy, precision, recall report করুন।

Interview Questions

  1. Bayes theorem-এর চারটি term ব্যাখ্যা করুন।
  2. Frequentist vs Bayesian — পার্থক্য?
  3. MLE এবং MAP-এর মধ্যে কী সম্পর্ক?
  4. একটি classifier-এর precision = 0.95 — এর মানে কি accuracy 95%? কেন না?

Mini Project

"Bayesian Coin Estimator" — একটি unknown biased coin। User কয়েকবার toss করে, প্রতিটি result-এর পর posterior distribution (Beta) update হয় এবং plot হয়। User দেখতে পায় কীভাবে belief gradually narrow হচ্ছে।

Summary · সারসংক্ষেপ

  • Bayes theorem = prior + likelihood → posterior।
  • Rare event-এ posterior dramatically prior-এর উপর dependent — base rate ignore করবেন না।
  • Sequential evidence = posterior-কে নতুন prior হিসেবে ব্যবহার।
  • Modern AI-এর Bayesian deep learning, MAP, Naive Bayes সব এই formula-র উপর দাঁড়ানো।
✨ পরবর্তী পদক্ষেপ
Chapter 22-এ Random Variables — সংখ্যা এবং probability-র মেলবন্ধন।