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

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.

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

Conditional Probability โ€“ FAQs

Quick answers about learning Conditional Probability in Deep Learning.

This free note from CodingNow 2.0 explains Conditional Probability 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 Conditional Probability, is 100% free with no signup required.
With focused practice, most students grasp Conditional Probability 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