Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Deep Learning Notes
Topic #231

Conditional Probability

Conditional probability asks: given that we already know something happened, how does that change the probability of something else? It's the mathematical foundation of Bayes' theorem, and of how a classifier's prediction should be read in context.

Formula

\[ P(A \mid B) = \frac{P(A \cap B)}{P(B)}, \qquad P(B) > 0 \]

Read as "the probability of \(A\) given \(B\)." It's the probability of both \(A\) and \(B\) happening, divided by the probability of \(B\) happening at all — restricting the sample space to only the cases where \(B\) is true.

Numerical Example

Among 100 emails: 30 are spam. Of those 30 spam emails, 27 contain the word "free." Of the 70 non-spam emails, 7 contain "free." What is \(P(\text{spam} \mid \text{contains "free"})\)?

\[ P(\text{spam} \cap \text{"free"}) = \frac{27}{100} = 0.27, \qquad P(\text{"free"}) = \frac{27+7}{100} = 0.34 \] \[ P(\text{spam} \mid \text{"free"}) = \frac{0.27}{0.34} \approx 0.794 \]

Knowing the email contains "free" raises the probability of spam from a base rate of 30% to about 79.4% — this exact kind of reasoning, generalized and automated, is what a Naive Bayes spam classifier does.

Code

# Verifying the numerical example directly by counting
spam_and_free = 27
total_free = 27 + 7
p_spam_given_free = spam_and_free / total_free
print(p_spam_given_free)   # 0.7941...

Independence — A Special Case

\[ A \text{ and } B \text{ are independent} \iff P(A \mid B) = P(A) \iff P(A \cap B) = P(A)P(B) \]

Independence means knowing \(B\) happened gives you no new information about \(A\). Many deep learning assumptions (e.g. that training examples are independently and identically distributed, "i.i.d.") rely on this idea directly.

Where This Shows Up in Deep Learning

A classifier's softmax output, \(P(\text{class} \mid \text{input})\), is itself a conditional probability — the probability of each class, given the specific input observed. This framing (modeling \(P(y \mid x)\) rather than \(P(y)\) alone) is exactly what distinguishes a useful predictive model from just reporting the base rate of each class in the training data.

Common Mistakes

  • Confusing \(P(A\mid B)\) with \(P(B\mid A)\) — these are generally very different numbers (this exact confusion is often called the "prosecutor's fallacy"), and correctly relating them requires Bayes' theorem, covered next.
  • Assuming independence without justification — the i.i.d. assumption behind most ML training is a simplification, and violations of it (e.g. time-correlated data) can silently break standard training and evaluation procedures.

Interview Relevance

Q: "What does a classifier's softmax output actually represent, probabilistically?" It represents \(P(\text{class} \mid \text{input})\) — a conditional probability distribution over classes, given the specific input. This is different from \(P(\text{class})\) alone (the base rate in the training data), and different from \(P(\text{input}\mid\text{class})\), which a generative model would estimate instead.

Practice Question

In a population, 1% have a disease. A test is 95% accurate for people who have it (true positive rate) and 90% accurate for people who don't (true negative rate). Without computing the exact answer yet, explain qualitatively why \(P(\text{disease} \mid \text{positive test})\) might be surprisingly low.

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →