Section 2 · Lesson 1
Probability basics and conditional probability
Probability rules, joint and conditional probability, and why a conversion rate is a probability estimate.
A probability is a number between 0 and 1 that says how often something happens in the long run. When you can’t repeat the world a million times, you estimate it from data:
P(event) ≈ number of times it happened / number of chances
That is exactly what a conversion rate is: an estimate of the probability that a visitor converts. If 423 of 4,000 users converted, our best guess for P(convert) is 423 / 4,000 ≈ 0.106. Keep the word “estimate” in mind; the next sections are about how uncertain it is.
The core rules
- Complement:
P(not A) = 1 - P(A). If 10.6% convert, 89.4% don’t. - Addition (OR):
P(A or B) = P(A) + P(B) - P(A and B). Subtract the overlap so users who are in both groups aren’t counted twice. If A and B can’t happen together (mutually exclusive, like “mobile” and “desktop”), the overlap is 0. - Multiplication (AND):
P(A and B) = P(A) × P(B | A). If A and B are independent (knowing one tells you nothing about the other) this simplifies toP(A) × P(B). - Conditional:
P(A | B) = P(A and B) / P(B), read “probability of A given B”. It shrinks the universe to only the cases where B happened, then asks how often A happens there.
P(A | B) is not P(B | A)
On the course data, about 56% of converters used mobile: P(mobile | converted) ≈ 0.56. But only about 10% of mobile users converted: P(converted | mobile) ≈ 0.10. Same two events, different denominators, wildly different numbers. Mixing them up is one of the most common mistakes in reporting.
Independence check
If device and conversion were independent, P(converted | mobile) would equal P(converted). Compare the conditional rate for each device to the overall rate; a clear gap means device and conversion are related (it doesn’t tell you why).
In Python
A comparison like ab["device"] == "mobile" gives a True/False Series. Taking .mean() of it
gives the share of True values, which is a probability estimate.
import pandas as pd
ab = pd.read_csv("ab_test.csv")
is_mobile = ab["device"] == "mobile"
is_converted = ab["converted"] == 1
is_mobile.mean() # P(mobile)
(is_mobile & is_converted).mean() # P(mobile and converted); & is "and"
ab.loc[is_mobile, "converted"].mean() # P(converted | mobile)
# Conditional probabilities for every device at once:
pd.crosstab(ab["device"], ab["converted"], normalize="index")
pd.crosstab counts combinations of two columns. With normalize="all" each cell is a joint
probability; normalize="index" divides each row by its total, giving P(column | row);
normalize="columns" gives P(row | column).
In the real world
Attribution reports often say “56% of conversions came from mobile, so mobile is our best device.” That is P(mobile | converted), and it is mostly driven by the fact that most traffic is mobile (the base rate). To judge how well a segment performs, you want P(converted | segment): the conversion rate within that segment. Segment conversion rates in dashboards are conditional probabilities, so always ask “out of what?”
Practice
Write your answer, press Run to see results and Check answer to grade it. Shortcuts: Ctrl/⌘ + Enter run, Shift + Enter check.
Conversion rate as a probability
DoneLoad ab_test.csv. Each row is one user, and converted is 1 if they converted, else 0.
p_convert: the probability that a user converts (the overall conversion rate),
rounded to 4 decimals
p_not_convert: the probability that a user does not convert, using the complement
rule, rounded to 4 decimals
Because converted is 0/1, its mean is the share of 1s.
Need a hint?
ab["converted"].mean()= conversions ÷ users.- Complement rule: P(not A) = 1 - P(A).
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import pandas as pd
ab = pd.read_csv("ab_test.csv")
p_convert = ab["converted"].sum() # this is a count, not a probability
p_not_convert = None
print(p_convert, p_not_convert)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)Joint probability and the addition rule
DoneUsing ab_test.csv, let D = "the user is on desktop" and C = "the user converted". Create, each rounded to 4 decimals:
p_desktop: P(D)p_desktop_and_convert: P(D and C), the share of all users who are on desktop
and converted
p_desktop_or_convert: P(D or C), computed with the addition rule
P(D or C) = P(D) + P(C) - P(D and C)
Need a hint?
(is_desktop & is_converted).mean()is the share of all users where both are True.- You can check your addition-rule answer directly with
(is_desktop | is_converted).mean().
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import pandas as pd
ab = pd.read_csv("ab_test.csv")
is_desktop = ab["device"] == "desktop"
is_converted = ab["converted"] == 1
p_desktop = None
p_desktop_and_convert = None
p_desktop_or_convert = None
print(p_desktop, p_desktop_and_convert, p_desktop_or_convert)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)P(A | B) is not P(B | A)
DoneUsing ab_test.csv, create, each rounded to 4 decimals:
p_convert_given_mobile: P(converted | mobile), the conversion rate among mobile usersp_mobile_given_convert: P(mobile | converted), the share of converters who were on mobile
Use P(A | B) = P(A and B) / P(B), or simply filter to B first and take a mean.
Need a hint?
ab.loc[is_mobile, "converted"].mean()is the conversion rate among mobile users.is_mobile[is_converted].mean()is the share of converters who are on mobile.pd.crosstab(ab["device"], ab["converted"], normalize="index")shows P(converted | device) for every device.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import pandas as pd
ab = pd.read_csv("ab_test.csv")
is_mobile = ab["device"] == "mobile"
is_converted = ab["converted"] == 1
p_convert_given_mobile = None
p_mobile_given_convert = None
print(p_convert_given_mobile, p_mobile_given_convert)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)