Probability Fundamentals & Conditional Probability
Events, independence, and P(A|B) as the language of uncertainty in data science.
In plain words
Probability is how likely something is, from 0 to 1, and conditional probability is how likely it is once you already know something else.
Think of it like…
A bag of marbles sorted into labelled boxes. "What fraction of all marbles are red?" is a plain probability. "What fraction of marbles in the glass box are red?" is a conditional one: you've restricted attention to the glass box and ask within it.
Where you’ve seen it
Of 1,000 visitors, 400 used mobile and 600 used desktop. 120 of the mobile visitors bought, and 90 of the desktop visitors bought. So P(buy) = 210 / 1,000 = 0.21, P(buy | mobile) = 120 / 400 = 0.30, P(buy | desktop) = 90 / 600 = 0.15, and P(mobile | buy) = 120 / 210 ≈ 0.57. The last two numbers answer different questions: mobile visitors convert twice as often, yet "57% of buyers are mobile" mostly reflects traffic share. Since P(buy | mobile) ≠ P(buy), device and purchase are not independent.
Words you’ll hear
- Event
- A set of outcomes, such as "the visitor buys".
- Conditional probability P(A | B)
- Probability of A given that B occurred: P(A and B) / P(B).
- Independence
- P(A and B) = P(A) × P(B); equivalently P(A | B) = P(A).
- Law of total probability
- P(A) = Σ P(A | Bᵢ) × P(Bᵢ) over a partition of groups Bᵢ.
Visual diagram
Theory & deep dive
Rules of probability
A probability is the long-run relative frequency (or a degree of belief) of an event, between 0 and 1. Because exactly one outcome occurs, the probabilities of a complete set of mutually exclusive outcomes sum to 1, and P(not A) = 1 − P(A) is often easier to compute ("at least one" = 1 − "none"). For overlapping events, add and subtract the overlap: P(mobile or buyer) = 400/1000 + 210/1000 − 120/1000 = 0.49. Events are mutually exclusive when they can't both occur (P(A and B) = 0), which is different from independent.
The multiplication rule P(A and B) = P(A | B) × P(B) lets you build joint probabilities from conditionals, for example a sequence of funnel steps: P(view) × P(click | view) × P(buy | click). With independence, joint probabilities simply multiply (two fair coins land heads with 0.5 × 0.5 = 0.25); real data are often dependent, so test it by comparing P(A | B) with P(A).
10,000 impressions, 3% click, 8% of clickers sign up, 25% of sign-ups buy: P(buy per impression) = 0.03 × 0.08 × 0.25 = 0.0006, so 6 purchases per 10,000 impressions.
Conditional probability, total probability and pitfalls
P(A | B) restricts the world to cases where B holds and asks what fraction also have A. The law of total probability recovers a marginal from group-wise conditionals: P(buy) = P(buy | mobile) P(mobile) + P(buy | desktop) P(desktop) = 0.30 × 0.40 + 0.15 × 0.60 = 0.21. This is the logic behind weighted averages and Simpson's paradox, where trends within groups reverse after aggregation when group sizes differ.
Classic pitfalls: confusing the conditional's direction (P(positive test | disease) vs P(disease | positive test): see Bayes' theorem), the prosecutor's fallacy, assuming independence to multiply probabilities of correlated events (understating the chance of simultaneous failures), and the gambler's fallacy (independent coin flips have no memory). In code, estimate probabilities by counting: df[df.device == "mobile"].bought.mean() is P(buy | mobile), and pd.crosstab(df.device, df.bought, normalize="index") gives all conditionals at once.
pd.crosstab(df.device, df.bought, normalize="index") returns, for each device, the fraction who did and didn't buy: P(bought | device). Use normalize="columns" for P(device | bought).
How it works, step by step
Step 1 of 41. Define the events
State precisely what A and B are, and what population the probabilities refer to.
- Population
- matters
When to use it & trade-offs
What it is
Probability assigns each event a number between 0 and 1. Core rules: the probabilities of all outcomes sum to 1; the complement is P(not A) = 1 − P(A); and for two events P(A or B) = P(A) + P(B) − P(A and B). Conditional probability P(A | B) = P(A and B) / P(B) is the probability of A once you know B happened, and it's the language of every prediction: P(churn | features), P(click | ad shown). Two events are independent if knowing one doesn't change the other: P(A and B) = P(A) × P(B). The law of total probability combines conditional probabilities over groups.
Why it matters: Models, metrics and tests are all statements about probabilities. Confusing P(A | B) with P(B | A), assuming independence where it doesn't hold, or double-counting overlapping events are the root of many wrong conclusions, from misread medical tests to biased A/B test analyses. Counting from a contingency table is the safest way to learn these ideas and to sanity-check results.
When to reach for it
- Funnel and conversion analysis
- Reading contingency tables
- Foundations for Bayes, models and tests
When to avoid or be careful
- Assuming independence without checking
- Mixing up P(A | B) and P(B | A)
- Adding probabilities of overlapping events without subtracting the overlap
Relationships between events
Strengths: Joint probability is a simple product.
Watch out: Rarely exactly true in real data.
Strengths: P(A or B) = P(A) + P(B).
Watch out: Mutually exclusive events with positive probability are dependent.
Strengths: Captures information one event gives about another.
Watch out: Needs conditional estimates.
Interview signals & numbers to know
When this topic comes up
- “What's the probability given that…?”
- “Are these events independent?”
- “Why do the percentages not add up?”
- Contingency tables
- Simpson's paradox
Benchmarks & rules of thumb
- 0.21 / 0.30 / 0.15
- P(buy), P(buy | mobile), P(buy | desktop) in the example
- 0.49
- P(mobile or buyer) in the example
- 0.0006
- funnel probability in the example
Common mistakes & follow-ups
Common interview mistakes
- Reading "57% of buyers are mobile" as "mobile visitors are 57% likely to buy".
- Multiplying probabilities of correlated failures as if they were independent.
- Treating mutually exclusive events as independent.
Likely interviewer follow-ups
- How do you check whether two events are independent?
- What's the difference between P(A | B) and P(B | A)?
- How does the law of total probability work?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Probability Fundamentals & Conditional Probability with data science interview Q&A and hands-on practice.