Machine LearningUnit 68 min read
Bayesian Learning: Probability, Naive Bayes, MAP, EM, and Applications
Unit 6 of Machine Learning explores Bayesian inference, conditional probability, Naive Bayes classifiers, Maximum A Posteriori (MAP) estimation, and the Expectation-Maximization (EM) algorithm, with real-world applications in spam filtering, medical diagnosis, and recommendation systems.
Key Concepts in Bayesian Learning
1. Bayesian Probability and Conditional Probability
Bayesian learning is rooted in conditional probability, which quantifies how the probability of an event changes given new evidence. The core formula is Bayes’ Theorem:
- : Posterior probability (what we want to find).
- : Likelihood (how likely the evidence is given the hypothesis).
- : Prior probability (our initial belief about the hypothesis).
- : Marginal probability (total probability of the evidence).
Example: Spam Detection in eSewa
Suppose eSewa wants to classify messages as spam (S) or not spam (¬S). We use Bayes’ Theorem to compute the probability that a message is spam given it contains the word "discount" ():
Given:
- (30% of messages are spam).
- (50% of spam messages contain "discount").
- (5% of non-spam messages contain "discount").
Compute (marginal probability):
Final posterior probability: P(S|D) = \frac{0.5 \times 0.3}{0.185} \approx 0.81 \quad (\text{81% chance of spam})
2. Naive Bayes Classifier
Naive Bayes assumes feature independence (e.g., words in a document are independent given the class). It is widely used for text classification, spam filtering, and sentiment analysis.
Types of Naive Bayes:
| Type | Use Case | Assumption |
|---|---|---|
| Gaussian NB | Continuous data (e.g., height) | Features follow a normal distribution |
| Multinomial NB | Discrete counts (e.g., word freq) | Features are multinomial distributed |
| Bernoulli NB | Binary features (e.g., 0/1) | Features are binary |
Example: Medical Diagnosis (Nepal Health App)
Suppose a health app predicts malaria (M) based on symptoms: fever (F), headache (H), and chills (C).
Given:
- (10% of patients have malaria).
- , , .
- , , .
Compute : Assuming independence: P(M|F, H, C) = \frac{0.504 \times 0.1}{0.0558} \approx 0.903 \quad (\text{90.3% chance of malaria})
3. Maximum A Posteriori (MAP) Estimation
MAP estimates the most probable parameter given data, balancing likelihood and prior:
Example: Loan Approval (Nepal Bank)
A bank uses MAP to decide whether to approve a loan () based on income (I) and credit score (S).
Given:
- Prior: , .
- Likelihoods (from historical data):
- , .
- , .
Compute : Assuming independence: Since , MAP approves the loan.
4. Expectation-Maximization (EM) Algorithm
EM is used for missing data problems (e.g., clustering with incomplete data). It alternates between:
- E-step: Compute expected values of missing data.
- M-step: Maximize the likelihood given current estimates.
Example: Customer Segmentation (Daraz)
Daraz wants to cluster customers into high-value (H) and low-value (L) groups, but some purchase data is missing.
Step 1 (E-step): Assume initial probabilities , . Compute expected responsibility of each customer for each cluster.
Step 2 (M-step): Update cluster means (e.g., average purchase amount) based on E-step results.
Repeat until convergence.
In the Real World
eSewa (Spam Filtering)
- Uses Naive Bayes to classify transactions as fraudulent or legitimate by analyzing keywords like "urgent," "discount," and sender history.
- Example: A message with "immediate payment" triggers a high spam score due to .
Nepal Rastra Bank (Loan Risk Assessment)
- Applies MAP estimation to approve loans by balancing applicant credit history (prior) with current financials (likelihood).
- Example: A farmer with a high credit score but low income may still get a loan if is maximized.
Pathao (Ride Demand Prediction)
- Uses Bayesian networks to predict rider demand in Kathmandu by combining historical data (prior) with real-time traffic (likelihood).
- Example: If is high, Pathao increases driver incentives.
Visualizing Bayesian Learning
1. Bayesian Network for Medical Diagnosis
graph TD
A["Malaria (M)"] --> B["Fever (F)"]
A --> C["Headache (H)"]
A --> D["Chills (C)"]
B --> E["Diagnosis"]
C --> E
D --> E2. Naive Bayes Feature Independence
graph TD
A["Class: Spam"] --> B["Word: Discount"]
A --> C["Word: Urgent"]
A --> D["Word: Free"]3. EM Algorithm Convergence (Missing Data)
graph TD
A["Initial Guess"] --> B["E-step: Compute Expectations"]
B --> C["M-step: Update Parameters"]
C --> D["Check Convergence?"]
D -->|"No"| B
D -->|"Yes"| E["Final Clusters"]Advantages and Limitations
| Advantage | Limitation |
|---|---|
| Works well with small data | Assumes feature independence (Naive Bayes) |
| Incorporates prior knowledge | Computationally expensive for complex models |
| Handles uncertainty well | Sensitive to prior choice (MAP) |
Exam Tip
- Bayes’ Theorem: Always show all steps (prior, likelihood, marginal). Partial credit is given for correct setup.
- Naive Bayes: Assume independence unless stated otherwise. Compare with logistic regression in exams.
- MAP vs MLE: MAP includes a prior; MLE does not. State which is used when.
- EM Algorithm: Explain E-step and M-step clearly. Use a small example (e.g., 2 clusters, 3 data points).
- Real-world mapping: Relate problems to eSewa (spam), banks (loans), or health apps (diagnosis).
Final Worked Example: Traffic Light Optimization (NTC)
NTC wants to predict traffic jam (J) at a junction based on:
- Time (T): Peak (P) or Off-peak (O).
- Weather (W): Rain (R) or Sunny (S).
Given:
- , .
- , .
- , .
Question: What is ?
Solution: Assuming independence: P(J|P, R) = \frac{0.42 \times 0.4}{0.186} \approx 0.909 \quad (\text{90.9% chance of jam}) Action: NTC may extend the green light duration at this junction.
Based on the PU BE Computer (PU) syllabus for Machine Learning (CMP364), unit 6.
Discussion
Loading…