Bayes-এর উপপাদ্য — উদাহরণসহ
এই পাঠে যা শিখবেন
- Bayes-এর সূত্র — কোন থেকে আসে, কী বলে
- Prior, Likelihood, Posterior — তিন প্রধান শব্দ
- মেডিকেল পরীক্ষা — কেন "৯৫% সঠিক টেস্ট" এও আপনার রোগ থাকার সম্ভাবনা ১০% হতে পারে
- Spam filter — Naive Bayes-এর মূলনীতি
১ · কেন এই উপপাদ্য এত শক্তিশালী?
Bayes-এর উপপাদ্য আমাদের একটি বিশেষ ক্ষমতা দেয় — নতুন তথ্য পেলে বিশ্বাস কীভাবে আপডেট করবেন। বিজ্ঞানের পদ্ধতি, চিকিৎসা নির্ণয়, AI-এর প্রায় সব কিছুই এই উপপাদ্যের ছায়ায়।
আগে আপনি ১৯-শতকের যাজক Thomas BayesThomas Bayes (১৭০২-১৭৬১)ইংরেজ Presbyterian যাজক ও amateur গণিতবিদ। জীবিত অবস্থায় কখনো এই theorem publish করেননি। মৃত্যুর পর Richard Price ১৭৬৩-তে "An Essay towards solving a Problem in the Doctrine of Chances" শিরোনামে publish. Pierre-Simon Laplace ১৮১২-তে modern form-এ generalize. Bayes জানতেন না — তাঁর কাজ ভবিষ্যতে AI-এর মূল হবে।-কে ধন্যবাদ দিন — যিনি ১৭৬৩ সালে এই সূত্র লিখেছিলেন।
২ · সূত্র
$$P(A \mid B) = \dfrac{P(B \mid A) \cdot P(A)}{P(B)}$$
প্রতিটি অংশের একটি নাম আছে —
- $P(A \mid B)$ — PosteriorPosteriorনতুন প্রমাণ $B$ দেখার পর $A$-এর update-করা সম্ভাবনা। Bayesian inference-এর "answer"।: $B$ দেখার পর $A$-এর সম্ভাবনা।
- $P(B \mid A)$ — LikelihoodLikelihoodএকটি hypothesis সত্য হলে — observed data দেখার কতটা সম্ভাবনা। model কতটা data ব্যাখ্যা করে।: $A$ সত্য হলে $B$ দেখার সম্ভাবনা।
- $P(A)$ — PriorPriordata দেখার আগে hypothesis-এ আগের বিশ্বাস। background knowledge encode করে।: $B$ জানার আগে $A$-এর সম্ভাবনা।
- $P(B)$ — EvidenceEvidence (Marginal Likelihood)সব hypothesis মিলিয়ে data $B$ দেখার মোট সম্ভাবনা। posterior-কে normalize করে।: $B$-এর সামগ্রিক সম্ভাবনা।
সূত্র কোথা থেকে এসেছে?
Conditional probability-র সংজ্ঞা থেকে — দু'বার:
$P(A \cap B) = P(A \mid B) P(B) = P(B \mid A) P(A)$
সমান করলেই Bayes-এর সূত্র। সরল গণিত — কিন্তু গভীর প্রভাব।
৩ · উদাহরণ ১ — মেডিকেল পরীক্ষা
একটি বিরল রোগ — জনসংখ্যার ১% মানুষের আছে।
একটি পরীক্ষা — রোগ থাকলে ৯৯% সময় পজিটিভ; রোগ না থাকলেও ৫% সময় পজিটিভ (false positive)।
আপনার পরীক্ষা পজিটিভ এসেছে। আপনার আসলেই রোগ থাকার সম্ভাবনা কত?
অনেকে আন্দাজ করেন — "৯৯% সঠিক পরীক্ষা, তাই হয়তো ৯৫%+।" — চলুন হিসাব করি।
ধাপ ১ — প্রতিটি অংশ চিনে নেওয়া
- $A$ = রোগ আছে। $P(A) = 0.01$ (Prior)
- $B$ = পরীক্ষা পজিটিভ।
- $P(B \mid A) = 0.99$ (Likelihood — রোগ থাকলে পজিটিভ)
- $P(B \mid \neg A) = 0.05$ (false positiveFalse Positiveরোগ না থাকলেও test "positive" বলা — Type I error. rare event detection-এ এই রকম ভুল বেশি ঘটে।)
ধাপ ২ — $P(B)$ হিসাব (Total ProbabilityLaw of Total Probability$P(B) = \sum_i P(B \mid A_i) P(A_i)$ — একটি event-এর মোট সম্ভাবনা সব path-এর যোগফল। Bayes-এ denominator হিসাব করতে দরকার।)
পরীক্ষা পজিটিভ আসতে পারে দু'ভাবে — রোগ থাকলে, বা না থাকলেও false positive হলে:
$P(B) = P(B \mid A) P(A) + P(B \mid \neg A) P(\neg A)$
$\quad\;\, = 0.99 \cdot 0.01 + 0.05 \cdot 0.99$
$\quad\;\, = 0.0099 + 0.0495 = 0.0594$
ধাপ ৩ — Bayes প্রয়োগ
$P(A \mid B) = \dfrac{P(B \mid A) P(A)}{P(B)} = \dfrac{0.99 \cdot 0.01}{0.0594} = \dfrac{0.0099}{0.0594} \approx 0.167$
এই গণনা ছাড়া অনেক অপ্রয়োজনীয় ভয় ও ভুল চিকিৎসা হতে পারে। তাই ডাক্তাররা প্রায়ই দ্বিতীয় পরীক্ষা করতে বলেন।
৪ · Python-এ Bayes
# মেডিকেল পরীক্ষা উদাহরণ
P_disease = 0.01 # রোগের সম্ভাবনা (prior)
P_pos_given_disease = 0.99 # রোগ থাকলে পজিটিভ
P_pos_given_no = 0.05 # রোগ না থাকলেও পজিটিভ
# Total probability of positive
P_pos = (P_pos_given_disease * P_disease
+ P_pos_given_no * (1 - P_disease))
# Bayes
P_disease_given_pos = P_pos_given_disease * P_disease / P_pos
print(f"P(রোগ | পজিটিভ) = {P_disease_given_pos:.4f}")
print(f"অর্থাৎ {P_disease_given_pos*100:.1f}% সম্ভাবনা।")
৫ · উদাহরণ ২ — Spam Filter (Naive BayesNaive Bayes ClassifierBayes-এর সূত্র + features conditionally independent assumption — একটি সরল কিন্তু শক্তিশালী classifier. spam, sentiment, text classification-এ ক্লাসিক।)
ই-মেইলে "জিতুন" শব্দটি আছে। এই ই-মেইল spam হওয়ার সম্ভাবনা?
- $P(\text{spam}) = 0.4$ (আপনার ইনবক্সের ৪০% spam — prior)
- $P(\text{"জিতুন"} \mid \text{spam}) = 0.6$ (spam-এ এই শব্দ আসার সম্ভাবনা)
- $P(\text{"জিতুন"} \mid \text{ham}) = 0.05$ (সাধারণ ই-মেইলে এই শব্দ)
$P(\text{"জিতুন"}) = 0.6 \cdot 0.4 + 0.05 \cdot 0.6 = 0.24 + 0.03 = 0.27$
$P(\text{spam} \mid \text{"জিতুন"}) = \dfrac{0.6 \cdot 0.4}{0.27} = \dfrac{0.24}{0.27} \approx 0.889$
"জিতুন" শব্দ থাকলে spam হওয়ার সম্ভাবনা ৮৯%! একটি একক শব্দ কী শক্তিশালী indicator.
একাধিক শব্দ — Naive Bayes-এর "naive" অংশ
বাস্তবে spam filter একটি শব্দ নয়, অনেক শব্দ দেখে। সব শব্দ স্বাধীন ধরে — সম্ভাবনা গুণ করা হয়। "NaiveNaive Bayes Assumptionসব features class-এর সাপেক্ষে conditionally independent — এই simplifying assumption. Reality-তে শব্দগুলো independent না ("New York" tightly coupled)। তবু — practice-এ এই assumption সত্ত্বেও Naive Bayes অসাধারণ কাজ করে। ১৯৬০ থেকে spam filter-এ standard. দ্রুত, low-data-তে কাজ করে।" নাম এই অনুমান থেকে — শব্দগুলো আসলে স্বাধীন না, কিন্তু তবু পদ্ধতি বেশ ভালো কাজ করে।
৬ · Bayes-এর দর্শন — প্রিয়র + প্রমাণ = পোস্টিরিয়র
Bayes-এর সবচেয়ে সুন্দর দিক — কীভাবে নতুন প্রমাণ পেলে আমরা আগের বিশ্বাস আপডেট করি।
আগের বিশ্বাস (prior) × নতুন প্রমাণ (likelihood) ÷ স্বাভাবিকীকরণ = নতুন বিশ্বাস (posterior)
৭ · AI-তে Bayes কোথায়?
- Naive Bayes Classifier: ক্লাসিক spam filter, sentiment analysis.
- Bayesian NetworksBayesian Networkএকটি graph যেখানে nodes = random variables, edges = causal/conditional dependencies. জটিল domain-এ joint distribution efficient ভাবে represent করে।: জটিল সম্ভাবনার সম্পর্কের মডেল।
- Bayesian OptimizationBayesian Optimizationexpensive black-box function (যেমন hyperparameter → accuracy) optimize করার পদ্ধতি — Gaussian Process surrogate model দিয়ে।: AI মডেলের hyperparameter খোঁজা।
- Bayesian Deep Learning: মডেলের নিজের অনিশ্চয়তা পরিমাপ করা।
- Bayesian A/B Testing: পণ্য পরীক্ষার আধুনিক পদ্ধতি।
- Reinforcement Learning: Thompson sampling — explore vs exploit.
৮ · একটি সাধারণ ভুল — Base rate fallacyBase Rate Fallacyprior probability (base rate) উপেক্ষা করে শুধু likelihood দেখার ভুল। rare disease/event-এ এই ভুল বিপজ্জনক — মেডিকেল test, fraud detection, surveillance-এ।
যখন আমরা prior ($P(A)$) উপেক্ষা করি — শুধু likelihood-কে দেখি — তখন বড় ভুল হয়। মেডিকেল পরীক্ষা উদাহরণে এটাই ঘটে।
এ কারণেই AI সিস্টেমে — শুধু accuracy দেখা যথেষ্ট নয়। False positive ও prior দু'টোই গুরুত্বপূর্ণ।
ভাবনার প্রশ্ন
প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।
প্র ০১ Frequentist বনাম Bayesian — কোনটি "সঠিক"? কীভাবে দু'টি পদ্ধতি একই data-তে ভিন্ন উত্তর দিতে পারে? উদাহরণসহ।
এটি ৩০০-বছর পুরানো philosophical battle — এখনো জীবিত।
Frequentist worldview:
- Probability = long-run frequency.
- Parameters fixed, data random.
- "This coin's true bias is some specific number; we estimate it from samples."
- Tools: MLE, p-values, confidence intervals.
Bayesian worldview:
- Probability = degree of belief.
- Parameters random (with prior), data fixed.
- "My belief about this coin's bias is a distribution; data updates it."
- Tools: Bayes' theorem, posterior distributions, credible intervals.
Concrete example:
A coin flipped 10 times — 7 heads observed. What's the bias?
- Frequentist (MLE): $\hat{p} = 7/10 = 0.7$। 95% CI: [0.42, 0.92]। "If we repeat this experiment many times, 95% of such intervals will cover true p."
- Bayesian (uniform prior): Posterior = Beta(8, 4)। Mean ≈ 0.67. 95% credible interval: [0.40, 0.89]। "True p is in [0.40, 0.89] with 95% probability."
- Bayesian (informative prior): If you know coins typically fair, prior Beta(50, 50) — posterior Beta(57, 53)। Mean ≈ 0.52. Different answer!
কখন কোনটি ব্যবহার:
-
Frequentist:
- প্রচুর data — Bayesian এর সাথে practically same.
- Standard scientific reporting.
- Don't want to specify prior.
-
Bayesian:
- Sparse data — prior knowledge incorporate.
- Uncertainty quantification critical.
- Sequential decisions (active learning)।
- Hierarchical structures.
Modern AI-তে:
- DL training: mostly frequentist (MLE)।
- Hyperparameter tuning: Bayesian optimization.
- Uncertainty quantification: Bayesian (MC dropout, deep ensembles)।
- RL: Bayesian flavor (Thompson sampling)।
Real-world disagreement:
- 2020 COVID drug efficacy debates — Bayesian reanalyses sometimes contradicted frequentist trial conclusions.
- A/B testing — Bayesian increasingly preferred (Optimizely, Google)।
মূল উপলব্ধি: "Right answer" depends on philosophy + data + use case. Modern practice — pragmatic mix. Mature data scientist দু'টি pole-ই understand.
প্র ০২ "Naive" Bayes — feature independence assumption ভুল, তবু practice-এ অসাধারণ কাজ করে। কেন? কী paradox এখানে?
এই paradox — ML-এর সবচেয়ে চমকপ্রদ phenomena-গুলোর একটি।
Independence assumption — কেন স্পষ্টতই ভুল:
- "New" + "York" — highly correlated. কখনো একসাথে।
- "Free" + "money" + "click" — spam-এ একসাথে আসে।
- Image pixels — neighbor pixels strongly correlated.
- True conditional independence rare.
তবু কেন কাজ করে:
- Classification accuracy ≠ probability calibration: Naive Bayes "Yes/No" decision সঠিক হলেই হয়। সঠিক probability estimate দরকার নেই।
- Argmax robustness: Even if probabilities miscalibrated, argmax (highest prob class) often still correct.
- Domain-redundancy: Multiple features pointing same direction → wrong probabilities cancel out.
- Domingos & Pazzani (১৯৯৭): "On the optimality of the simple Bayesian classifier under zero-one loss" — এই paradox formally explore.
Practical strengths:
- Speed: Training O(n × d) — extremely fast. Inference real-time.
- Scalable: Word counts maintain — incremental update easy.
- Small data friendly: Each class needs only feature distributions, not joint.
- Interpretable: Per-feature contribution visible.
সফলতার জায়গা:
- Spam filtering — ১৯৯০ থেকে standard.
- Document classification (sentiment, topic)।
- Medical diagnosis (older systems)।
- Search query understanding.
Limitations:
- Probability calibration: Outputs don't represent true probabilities. Use Platt scaling for calibration.
- Zero-frequency problem: A word never seen with a class → P = 0, kills product. Solution: Laplace smoothing.
- Continuous features: Need distribution assumption (Gaussian Naive Bayes)।
- Modern DL outperforms: CNN/Transformer for images/text — better contextual understanding.
Variants:
- Multinomial NB: Word counts.
- Bernoulli NB: Binary word presence.
- Gaussian NB: Continuous features.
- Complement NB: Imbalanced data.
Lesson:
- "Wrong assumption" + "right structure" = often surprisingly good.
- ML wisdom: simple methods first, complex if needed.
- Naive Bayes — interpretable baseline before DL.
মূল উপলব্ধি: Pragmatic সাফল্য > theoretical purity. Naive Bayes — এই philosophy-র poster child. AI-তে "good enough fast" বেশিরভাগ সময় "perfect slow"-কে হারায়।
প্র ০৩ একটি AI ফেসিয়াল রিকগনিশন সিস্টেম "৯৯.৯% সঠিক"। এটি বাংলাদেশের ১৭ কোটি মানুষের মধ্যে ১,০০০ সন্দেহভাজন খুঁজতে ব্যবহার হলে — কত false positives? এই সিস্টেম কেন বিপজ্জনক?
এই scenario — surveillance AI-র সবচেয়ে gripping case study. Bayes মাথায় না রাখলে — civil liberty disaster.
Numbers:
- Population: ১৭ কোটি (1.7 × 10⁸)।
- Suspects: ১,০০০।
- Innocent: 169,999,000.
- Accuracy 99.9% → error rate 0.1%।
- If "accuracy" = sensitivity = specificity (অনুমান):
- True positives: ১,০০০ × ০.৯৯৯ = ৯৯৯।
- False positives: 169,999,000 × ০.০০১ = ১৬৯,৯৯৯।
Bayesian interpretation:
- Total alarms: 169,999 + 999 ≈ 170,998.
- P(actual suspect | alarm) = 999 / 170,998 ≈ 0.58%।
- আপনি AI-চিহ্নিত হলে — actual suspect মাত্র ০.৫৮%। মানে — ১৭০ জন alarm-এর মধ্যে মাত্র ১ জন আসল।
কেন বিপজ্জনক:
- Massive false-positive load: ১,৭০,০০০ innocent মানুষের harassment. Most don't know why.
- Demographic bias amplification: ফেসিয়াল রিকগনিশন brown skin-এ less accurate. Innocent ethnic minorities disproportionately affected.
- Burden of proof reversal: "Prove you're not the suspect" — alarming.
- Chilling effects: Surveillance-এর সচেতনতা political/religious expression কমায়।
- Mission creep: ১,০০০ "suspects" থেকে — ১০,০০০ জন নজরদারিতে। ১০ লক্ষ। Police state.
Real-world examples:
- 2017 South Wales Police: Facial recognition deployed. Identified 2,470 suspects, 92% (~2,300) false positives. Many wrongful detentions.
- Robert Williams (২০২০, Detroit): Black man wrongly arrested due to facial recognition error. 30 hours in jail. Innocent.
- UK Met Police trials: 81% false positive rate in some deployments.
- China's Social Credit System: Massive surveillance, accuracy claims often unverified.
Mathematical solutions — কী করা যায়:
- Higher specificity: 99.9999% accuracy needed. Currently impossible practically.
- Multi-stage verification: AI flags → human review → second biometric (fingerprint, ID) → action. Compound accuracy higher.
- Restricted use: Only investigations where prior probability is high (e.g., crime scene location, time)।
- Error budgets: "How many false positives are we willing to accept?" Public debate.
Policy implications:
- EU AI Act (২০২৪) — public-space facial recognition heavily restricted.
- San Francisco, Boston (US) — banned.
- Bangladesh-এ — National Telecommunication Monitoring Centre debate.
- Civil society pushback worldwide.
মূল উপলব্ধি: "৯৯.৯% accurate" — sounds great, statistically dangerous in rare-event search. Bayes' theorem — mathematical truth এবং civil rights protection. AI engineers যাঁরা societal impact বুঝেন — তাঁরা Bayes-এর implications প্রথমে চিন্তা করেন।
প্র ০৪ Bayesian Optimization — modern ML-এ hyperparameter tuning-এ standard. কীভাবে কাজ করে? Grid search-এর তুলনায় কেন এত efficient?
Hyperparameter tuning — DL-এর "secret sauce"। Bayesian Optimization এই tuning-কে science বানিয়েছে।
সমস্যা:
- একটি model train করতে হয়তো ৩-৬ ঘণ্টা।
- Hyperparameters (learning rate, depth, batch size, dropout, ...) — প্রতিটি combination test.
- Grid search 5 hyperparams × 5 values = 3,125 trials = মাস।
- Random search better, but still wasteful.
Bayesian Optimization-এর approach:
- Surrogate model: "Hyperparameter → performance" function-এর Gaussian Process model.
- Acquisition function: "পরবর্তী কোন hyperparameter try করব?" — exploration vs exploitation balance.
- Expected Improvement (EI): "এই point try করলে best-এর চেয়ে কত improvement প্রত্যাশা?"
- Iteration: Try → observe performance → update surrogate → choose next.
Step by step example:
- Iter 1: Random hyperparam, train, observe accuracy 80%।
- Iter 2: GP-এ — "near similar config" predicted 78%। Try different config — accuracy 75%।
- Iter 3: GP updated — surface model. Best EI point pick → accuracy 85%।
- Iter 10: 92%, GP-এ confidence high.
- Iter 20: 94% — converged.
কেন Bayes-এর সাথে সম্পর্ক:
- Prior: "Performance smooth function of hyperparameters" — Gaussian Process prior.
- Likelihood: Observed accuracy values.
- Posterior: Updated belief about performance landscape.
- Decision: Posterior থেকে next best point.
Grid vs Random vs Bayesian:
- Grid: Exponential cost, no learning. 5 dims × 10 values = 100,000 trials.
- Random: Better empirically (Bergstra & Bengio, ২০১২) — most dims unimportant.
- Bayesian: Learns from past trials. 50-100 trials reach grid-quality.
Practical tools:
- Optuna: Most popular Python library.
- Hyperopt: Older, classic.
- Google Vizier: Internal tool.
- Microsoft NNI: Distributed BayesOpt.
- Weights & Biases Sweeps: Cloud-based.
Beyond hyperparameters:
- Drug design — molecular configurations.
- Chemistry — reaction conditions.
- Hardware design — chip layout.
- A/B testing — variant selection.
- RL — sample-efficient policy search.
Limitations:
- GP scales poorly with high dimensions (>20)।
- Categorical hyperparameters tricky.
- Noisy observations (training stochasticity)।
- Modern alternatives: BOHB, ASHA — early stopping + Bayesian.
Lesson:
- "Sequential decisions under uncertainty" — Bayesian framework natural.
- $5 GPU hour saved × 100 trials = significant cost reduction.
- Modern AI-র cost optimization-এ Bayesian thinking essential.
মূল উপলব্ধি: Bayes-এর শক্তি — শুধু theory নয়, expensive optimization problem-এ practical efficiency. Production AI-তে Bayesian Optimization use না হলে — হাজার-হাজার ডলার GPU waste.
অনুশীলন
-
হিসাব করুন: $P(\text{spam}) = 0.3$, $P(\text{"discount"} \mid \text{spam}) = 0.4$, $P(\text{"discount"} \mid \text{ham}) = 0.02$।
একটি ই-মেইলে "discount" আছে — spam হওয়ার সম্ভাবনা?
$P(\text{discount}) = 0.4 \cdot 0.3 + 0.02 \cdot 0.7 = 0.12 + 0.014 = 0.134$।
$P(\text{spam} | \text{discount}) = (0.4 \cdot 0.3) / 0.134 = 0.12 / 0.134 \approx 0.896 = 89.6\%$।
"discount" শব্দ থাকলে spam হওয়ার সম্ভাবনা ~৯০%।
-
সেনারিও: COVID দ্রুত-পরীক্ষা — sensitivity ৯০%, specificity ৯৮%, রোগের prevalence ২%। পরীক্ষা পজিটিভ এসেছে — আপনার আসলেই COVID-এর সম্ভাবনা?
- $P(\text{COVID}) = 0.02$।
- $P(+ | \text{COVID}) = 0.90$।
- $P(+ | \neg\text{COVID}) = 1 - 0.98 = 0.02$।
- $P(+) = 0.90 \cdot 0.02 + 0.02 \cdot 0.98 = 0.018 + 0.0196 = 0.0376$।
- $P(\text{COVID} | +) = 0.018 / 0.0376 \approx 0.479 = 47.9\%$।
উপলব্ধি: "৯০% sensitive test" — পজিটিভে COVID প্রায় ৫০-৫০। Confirmatory PCR দরকার।
-
চিন্তা করুন: AI মুখ-চেনা সিস্টেম যদি ৯৯% সঠিক হয়, কিন্তু নির্দিষ্ট অপরাধী বিরল (১০ লক্ষে ১০জন) — সিস্টেম কেন বিপজ্জনক হতে পারে?
প্র ০৩-এ বিস্তারিত আছে। সংক্ষেপে:
- ১০ লক্ষে ১০ অপরাধী, ৯,৯৯,৯৯০ innocent.
- ৯৯% accuracy → 1% error → 9,999.9 innocent মানুষ false positive.
- আপনি AI-চিহ্নিত হলে actual অপরাধী হওয়ার সম্ভাবনা মাত্র ~০.১%।
- মাস্ surveillance — wrongful detentions, demographic bias amplification, civil liberty violations.
- "৯৯% সঠিক" sounds great, mathematically dangerous in rare-event search.
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ২০ · মডিউল ২ পর্যালোচনা পরবর্তী পাঠ Module 2 (Math) সমাপ্তি — চূড়ান্ত কুইজ ও মডিউল ৩-এর প্রস্তুতি।
- পাঠ ১৮ · প্রত্যাশা ও ভেরিয়েন্স আগের পাঠ Probability summary statistics — Bayes-এর সাথে integrate.
- পাঠ ১৬ · সম্ভাবনার মূল এই পাঠের সাথে সম্পর্কিত Conditional probability — Bayes-এর ভিত্তি।
- সব AI Courses দেখুন ABCL TECH Python, ML, DL, NLP, CV, GenAI, RL, MLOps — সব AI কোর্স একসাথে।