๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #43

Bayes' Theorem

Bayes' theorem gives you a precise way to flip a conditional probability around โ€” to go from \(P(\text{evidence}\mid\text{hypothesis})\), which is often easy to reason about, to \(P(\text{hypothesis}\mid\text{evidence})\), which is usually what you actually want to know. It resolves the exact confusion flagged at the end of the previous note.

Formula

\[ P(A \mid B) = \frac{P(B \mid A)\,P(A)}{P(B)} \]
TermNameMeaning
\(P(A)\)PriorBelief in \(A\) before seeing any evidence
\(P(B\mid A)\)LikelihoodHow probable the evidence \(B\) is, if \(A\) is true
\(P(A\mid B)\)PosteriorUpdated belief in \(A\), after observing evidence \(B\)
\(P(B)\)Evidence / normalizerTotal probability of observing \(B\), across all hypotheses

Solving the Disease Test Example

Continuing the practice question from Conditional Probability: \(P(\text{disease})=0.01\), \(P(\text{positive}\mid\text{disease})=0.95\), \(P(\text{positive}\mid\text{no disease})=0.10\) (a 90% accurate negative test means a 10% false-positive rate).

\[ P(\text{positive}) = P(\text{pos}\mid\text{disease})P(\text{disease}) + P(\text{pos}\mid\text{no disease})P(\text{no disease}) \] \[ = (0.95)(0.01) + (0.10)(0.99) = 0.0095 + 0.099 = 0.1085 \] \[ P(\text{disease}\mid\text{positive}) = \frac{(0.95)(0.01)}{0.1085} = \frac{0.0095}{0.1085} \approx 0.0876 \]

Despite a "95% accurate" test, a positive result only means an 8.76% chance of actually having the disease โ€” because the disease is rare (1% prior) and the 10% false-positive rate applies to the much larger healthy population. This is the classic illustration of why the prior matters enormously, not just the test's accuracy.

Code

p_disease = 0.01
p_pos_given_disease = 0.95
p_pos_given_no_disease = 0.10

p_no_disease = 1 - p_disease
p_positive = p_pos_given_disease * p_disease + p_pos_given_no_disease * p_no_disease
p_disease_given_positive = (p_pos_given_disease * p_disease) / p_positive
print(p_disease_given_positive)   # 0.0876...

Where This Shows Up in Deep Learning

  • Naive Bayes classifiers apply this formula directly, assuming feature independence given the class, to compute \(P(\text{class}\mid\text{features})\).
  • Bayesian deep learning treats a network's weights themselves as random variables with a prior distribution, updating to a posterior distribution over weights given the training data โ€” used for uncertainty estimation.
  • Class imbalance intuition: the disease example generalizes directly โ€” a rare class (low prior) needs strong evidence to be predicted confidently, which is exactly why naively-trained classifiers underperform on imbalanced datasets unless the class imbalance is explicitly addressed.

Common Mistakes

  • Ignoring the prior \(P(A)\) and reasoning only from the test's accuracy โ€” as the worked example shows, a rare condition combined with even a modest false-positive rate can make the posterior probability surprisingly low.
  • Forgetting the denominator \(P(B)\) must sum over all possible hypotheses, not just the one you're currently interested in.

Interview Relevance

Q: "A model is 99% accurate on a dataset where only 1% of examples are the positive class. Why might this be a red flag rather than good news?" A model that always predicts the negative class would already achieve 99% accuracy on such an imbalanced dataset, without learning anything useful. This is directly analogous to the disease-test example โ€” accuracy alone, without accounting for class priors (base rates), can be deeply misleading; precision, recall and F1 (covered in Evaluation Metrics) are needed instead.

Practice Question

Using Bayes' theorem, if a spam filter has \(P(\text{spam})=0.4\), \(P(\text{"free"}\mid\text{spam})=0.6\), and \(P(\text{"free"}\mid\text{not spam})=0.05\), compute \(P(\text{spam}\mid\text{"free"})\).

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Bayes' Theorem โ€“ FAQs

Quick answers about learning Bayes' Theorem in Deep Learning.

This free note from CodingNow 2.0 explains Bayes' Theorem in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Bayes' Theorem, is 100% free with no signup required.
With focused practice, most students grasp Bayes' Theorem in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now