Bayesian Thinking
There are two camps in statistics: Bayesians and frequentists. They are fundamentally different ways of thinking about probability and uncertainty.[1]
The Bayesians treat probability as a measure of belief rather than just frequencies of events. It all sounds very reasonable, but in practice, it’s pretty tricky.
In this lesson, we’ll just scratch the surface.
This Lesson Will Cover
- 🔍 Bayesian vs. Frequentist Statistics and their fundamental differences
- 📏 Bayes’ Theorem and its role in updating probabilities
- 📩 Naive Bayes Classifier and its application in spam detection
- 🚀 Bayesian Optimization in machine learning
- 🌍 Real-world applications of Bayesian statistics
Bayesian vs Frequentist Statistics
Fundamental Differences
| Aspect | Bayesian | Frequentist |
|---|---|---|
| Definition of Probability | Probability represents a degree of belief that can be updated with new evidence | Probability represents the long-run frequency of events in repeated experiments |
| Parameters | Parameters are treated as random variables with probability distributions | Parameters are fixed, unknown constants |
| Prior Knowledge | Explicitly incorporates prior knowledge through prior distributions | Typically doesn’t use prior knowledge, focuses only on data |
| Results Interpretation | “There’s a 95% probability that the parameter lies in this interval” | “If we repeated this experiment many times, 95% of intervals would contain the true parameter” |
The Basics of Bayesian Thinking
Bayesian thinking revolves around Bayes’ Theorem, which describes how to update probabilities based on new evidence. It’s expressed as:
Here we have:
| Term | Symbol | Description |
|---|---|---|
| Prior Probability | Your belief about before seeing any data | |
| Likelihood | The probability of observing given that is true | |
| Marginal Probability | The total probability of across all possible scenarios | |
| Posterior Probability | Your updated belief about after observing |
Updating Beliefs
Bayesian thinking emphasizes iterative updates as new evidence arrives.
Consider spam detection:
- Prior belief: 10% chance any email is spam →
- Evidence: The email contains “Buy now!”, which occurs in 90% of spam emails but only 1% of non-spam emails.
Using Bayes’ Theorem, we calculate the posterior:
Where:
Therefore:
So after seeing “Buy now!” in the email, our belief that it’s spam increases to 91% (compared to the prior belief of 10%).
Example: Naive Bayes Classifier
The Naive Bayes Classifier applies this type of Bayesian thinking under the assumption of feature independence. It calculates:
Where:
- is the class label (e.g., Spam/Not Spam).
- is the observed data.
Since we haven’t been able to look at much code in this section, here’s a Python implementation:
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
# Data
emails = ["Buy now!", "Limited offer", "Hello friend", "Meeting schedule"]
labels = [1, 1, 0, 0] # 1 = Spam, 0 = Not Spam
# Transform text to feature vectors
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(emails)
# Train Naive Bayes model
model = MultinomialNB()
model.fit(X, labels)
# Predict on new data
test_email = vectorizer.transform(["Special offer"])
prediction = model.predict(test_email)
prediction_proba = model.predict_proba(test_email)
if prediction[0] == 0:
print(f"Not Spam ({prediction_proba[0][0]:.2f}% probability)")
elif prediction[0] == 1:
print(f"Spam ({prediction_proba[0][1]:.2f}% probability)")
# Output:
# Spam (0.67% probability)
To give a flavor of how this works, let’s walk through some of the algorithm:
-
Vectorization: The CountVectorizer creates a vocabulary and transforms our emails into word frequency vectors:
Vocabulary = {"buy", "now", "limited", "offer", "hello", "friend", "meeting", "schedule"}Then we can embed the emails as vectors in this space, i.e.
Email buy now limited offer hello friend meeting schedule “Buy now!” 1 1 0 0 0 0 0 0 “Limited offer” 0 0 1 1 0 0 0 0 “Hello friend” 0 0 0 0 1 1 0 0 “Meeting schedule” 0 0 0 0 0 0 1 1 -
Prior Probabilities: The classifier calculates for each class. Since we have 2 spam emails and 2 non-spam emails in our dataset, we assign them a 50/50 chance:
-
Likelihood Calculation: For each word in vocabulary, calculate using Laplace smoothing with :
For example:
-
Classification
For a new email “Special offer”, the classifier computes:The class with the higher probability is chosen as the prediction.
Applications in Machine Learning
Here are a few places we might run into Bayesian statistics with ML/AI and data science:
-
Probabilistic Models
- Naive Bayes Classifier (like we saw above), Bayesian Neural Networks, Hidden Markov Models, Latent Dirichlet Allocation.
-
Bayesian Optimization
- Hyperparameter tuning for machine learning models, A/B testing and experimental design.
-
Real-World Decision Making
- Guide decisions in applications like medical diagnosis, recommendation systems, fraud detection.
Conclusion
I tried to be in the Bayesian camp. I tried really hard. I read a book about it, I learned how to use PyMC (a Python library for Bayesian modeling) and even shipped a project with it.[1:1]
Yet somehow it seems that I always drift back to my frequentist ways. But who knows, maybe if I keep trying then I’ll become a true Bayesian some day!
Key Takeaways
🎲 Bayesians and frequentists approach probability differently.
🔢 Bayes’ Theorem is fundamental for updating probabilities based on evidence.
📊 Naive Bayes Classifier is a simple yet powerful tool for classification.
🤖 Bayesian statistics plays a key role in machine learning and decision-making.
🏆 Bayesian optimization is useful for hyperparameter tuning and A/B testing.
Next: Calculus and Optimization
Next up is calculus, where we’ll get to the very heart of ML.
Footnotes
Cassie Kozyrkov’s blog post & video Statistics: Are you Bayesian or Frequentist? ↩︎ ↩︎
Comments (0)
No comments yet.