HuntExams Academy logo
Data Science · Chapter 17 of 43

Probability Basics

Probability measures uncertainty on a 0–1 scale. Rules: P(A or B) = P(A)+P(B)−P(A and B); independent events multiply.

Understanding probability is the foundation of statistics, ML and A/B testing.

Example 1 (python)
# P(rolling a 6) with a fair die
print(1/6)
Output
0.16666666666666666

One in six outcomes.

Example 2 (python)
# P(two 6s in a row) = 1/6 * 1/6
print((1/6) ** 2)
Output
0.027777777777777776

Independent events multiply.

Key points

  • Probability lives in [0, 1].
  • Complement: P(not A) = 1 − P(A).
  • Independent events multiply.
  • Conditional probability underlies Bayes.
💡 Note: Real-world events are rarely truly independent — beware of the multiplication assumption.

Understanding Probability Basics

Probability starts with a clearly defined experiment: what is selected or observed, and under what conditions? The sample space contains its possible outcomes. An event is a collection of outcomes satisfying a condition. When selecting one recorded website session, “the session used a mobile device” is an event. Changing the unit from sessions to people changes the question because one person may contribute several sessions.

Probabilities range from zero to one. For a fair die, six equally likely outcomes justify calculating P(6) = 1/6. Counting favorable outcomes and dividing by all outcomes works directly only when those outcomes are equally likely. In data analysis, an observed proportion estimates a probability for a broader population, provided the records reasonably represent that population. It is also the exact probability when selecting uniformly from those particular records.

The complement of A means every outcome outside A, so P(not A) = 1 − P(A). The union, “A or B,” includes either event and their overlap. Therefore P(A or B) = P(A) + P(B) − P(A and B). Subtracting the overlap prevents double counting. Mutually exclusive events cannot happen together, making their overlap zero; mobile and desktop sessions might be mutually exclusive under a single-device classification.

Conditional probability changes the group under consideration. P(B | A), read “probability of B given A,” equals P(A and B)/P(A), provided P(A) > 0. It asks how common B is within A, not within the entire sample. Rearranging gives the general multiplication rule: P(A and B) = P(A)P(B | A). Independence is the special case where learning A does not change B’s probability, allowing P(A)P(B).

Independence differs from mutual exclusivity: two mutually exclusive events with positive probabilities cannot be independent. Observing one rules out the other. Bayes’ rule reverses conditioning: P(A | B) = P(B | A)P(A)/P(B), for P(B) > 0. Its denominators explain why “mobile among purchasers” and “purchasers among mobile sessions” usually differ. These relationships describe associations, not causes. Observed differences can reflect audience composition, measurement choices, or sampling variation rather than an effect of device choice. Always state the population and time window before interpreting a probability.

Practical walkthrough

Consider a deliberately constructed table of twenty website sessions. Let A mean mobile and B mean purchase. Six sessions are both mobile and purchasing, four are mobile without purchase, two are nonmobile and purchasing, and eight are neither. These four groups are mutually exclusive and cover every session, so their counts must sum to twenty.

First, establish the denominator. For an unconditional probability, use all twenty sessions. Mobile sessions total ten, purchases total eight, and the overlap contains six. Thus P(A) = 10/20, P(B) = 8/20, and P(A and B) = 6/20. For “mobile or purchase,” count six plus four plus two, or apply the union rule. Both routes give twelve qualifying sessions, so P(A or B) = 12/20 = 0.60.

Next, restrict the denominator for a conditional question. Among mobile sessions, six of ten purchased, giving P(B | A) = 0.60. Among purchasing sessions, six of eight were mobile, giving P(A | B) = 0.75. Write the denominator’s meaning alongside each result to avoid accidentally answering the reversed question.

Then check independence within this constructed distribution. Multiplying the marginal probabilities gives 0.50 × 0.40 = 0.20, whereas the actual overlap probability is 0.30. They are not independent here. This arithmetic alone does not establish population dependence from a real sample; that requires attention to sampling uncertainty.

Finally, validate real records before counting: check duplicates, missing device values, and the purchase definition. The pandas user guide organizes relevant operations under selection, missing data, and grouping. The example uses plain Python instead, keeping the probability arithmetic visible without requiring any external packages.

Worked example

# Constructed counts: (mobile, purchased) -> number of sessions
counts = {
    (True, True): 6,
    (True, False): 4,
    (False, True): 2,
    (False, False): 8,
}
n = sum(counts.values())
mobile = sum(c for (m, p), c in counts.items() if m)
purchase = sum(c for (m, p), c in counts.items() if p)
both = counts[(True, True)]

print(f"P(mobile) = {mobile / n:.2f}")
print(f"P(purchase) = {purchase / n:.2f}")
print(f"P(both) = {both / n:.2f}")
print(f"P(mobile or purchase) = {(mobile + purchase - both) / n:.2f}")
print(f"P(no purchase) = {1 - purchase / n:.2f}")
print(f"P(purchase | mobile) = {both / mobile:.2f}")
print(f"P(mobile | purchase) = {both / purchase:.2f}")
print(f"Product of marginals = {(mobile / n) * (purchase / n):.2f}")

Expected output

P(mobile) = 0.50
P(purchase) = 0.40
P(both) = 0.30
P(mobile or purchase) = 0.60
P(no purchase) = 0.60
P(purchase | mobile) = 0.60
P(mobile | purchase) = 0.75
Product of marginals = 0.20

The dictionary stores counts rather than individual sessions, which is sufficient for these two events but loses information about timing and repeat visitors. Each comprehension sums the cells matching an event. Dividing by n produces a marginal or joint probability; dividing by mobile or purchase produces a conditional probability. Formatting displays every result to two decimal places without changing intermediate calculations. The union result includes sessions satisfying both conditions, while the complement excludes every purchase. These are exact probabilities for uniform selection from the constructed twenty sessions, not measured website performance or a guarantee about the behavior of future visitors.

Common mistakes and how to avoid them

  • Adding event probabilities without subtracting their overlap.

    Identify records satisfying both events. Subtract their probability once, unless the events are explicitly mutually exclusive.

  • Multiplying marginal probabilities whenever a question says “and.”

    Use P(A)P(B | A) generally. Replace the conditional probability with P(B) only when independence is justified.

  • Treating missing purchase status as “no purchase.”

    Distinguish unknown outcomes from confirmed negatives. State how missing records affect the denominator; excluding them can also introduce bias.

  • Interpreting a higher conditional purchase rate as a device effect.

    Report an association within the specified sessions. Investigate alternative explanations and use an appropriate causal study design before claiming an effect.

Review questions

What probability describes a session that is neither mobile nor purchasing?

It is the complement of the union: 1 − 0.60 = 0.40. Eight of the twenty sessions are neither mobile nor purchasing, so directly counting 8/20 gives the same result.

Why does P(purchase | mobile) use ten rather than twenty as its denominator?

Conditioning limits the sample space to mobile sessions. Only ten sessions satisfy that condition, and six of those purchased. Using twenty would calculate the joint probability instead.

How does Bayes’ rule recover P(mobile | purchase)?

Multiply P(purchase | mobile) by P(mobile), then divide by P(purchase): 0.60 × 0.50 / 0.40 = 0.75. The purchase rate supplies the new denominator.

Does observing no purchases in a small group prove purchase probability is zero?

No. Zero is the observed proportion for that group. A positive population probability can still produce no observed purchases in a finite sample, so report the count and sample size.

Further reading

📝 Quick Quiz

1. Probability is always between:

2. P(A and B) for independent events is:

3. P(not A) equals: