Python Central HubSign in

Data Analytics with Python / Phase 8: Statistics for Data Analytics

Probability Basics (events, conditional probability)

Learn probability foundations used in analytics - conditional probability, independence, Bayes rule.

Events and probability

  • An event is a set of outcomes.
  • Probability is a number between 0 and 1.

Example: probability of drawing a red card from a standard deck is 26/52 = 0.5.

Conditional probability

Probability of A given B:

P(A|B) = P(A and B) / P(B)

In analytics language:

  • A = “user churns”
  • B = “user is on basic plan”

Then (P(A|B)) is the churn rate for basic-plan users.

Independence

A and B are independent if:

[ P(A|B) = P(A) ]

If churn rate differs by plan, churn and plan are not independent.

Bayes’ rule

P(A|B) = P(B|A) * P(A) / P(B)

Common use cases:

  • Medical tests (false positives)
  • Fraud detection (base rate is tiny)

Mini example: base rate fallacy

If fraud is rare (say 0.1%), even a “95% accurate” detector can generate many false alarms.

Key lesson:

  • Always check base rates and precision/recall, not just accuracy.

How Bayes' rule flips a probability

Bayes' rule lets you go from "probability of evidence given a cause" to "probability of a cause given evidence" — which is usually the direction you actually care about.

diagram Bayes' rule in a fraud check mermaid
Bayes' rule combines a rare base rate with test accuracy to get the real probability of fraud given a flag.

Check your understanding

quizCheck yourself
  1. What is the probability of drawing a red card from a standard deck, as in the lesson?

    Show answer

    A — 26/52 = 0.5 — Half the 52 cards are red, so the probability is 0.5. Probabilities always lie between 0 and 1.

  2. If A is user churns and B is user is on the basic plan, what is P(A|B)?

    Show answer

    A — The churn rate among basic-plan users — P(A|B) is the probability of churn given the basic plan, which is the churn rate for basic-plan users.

  3. Churn rate differs by plan. What does that say about churn and plan?

    Show answer

    A — They are not independent — Independence needs P(A|B) = P(A). If the rate changes by plan, that equality fails, though it does not prove causation.

  4. Fraud is 0.1% of transactions and the detector is 95% accurate. What does Bayes' rule warn about?

    Show answer

    A — Most flagged transactions can still be false alarms — With a tiny base rate, the huge pool of legit transactions produces more false positives than the few frauds produce hits. Check base rates, precision and recall.

🧪 Try It Yourself

Run each snippet and check your output against the comment.

Exercise 1 – Conditional probability from a table

Python
# Task: compute P(churn | plan == "basic") from raw counts
# counts from the churn table: 200 basic-plan users, 40 of whom churned
basic_total = 200
basic_churned = 40
 
p_churn_given_basic = basic_churned / basic_total  # churned among basic, over all basic
print("P(churn | basic):", p_churn_given_basic)
 
# Expected output:
# P(churn | basic): 0.2

Exercise 2 – Checking independence

Python
# Task: two events are independent if P(A|B) == P(A)
p_a = 0.30          # overall churn rate
p_a_given_b = 0.30  # churn rate for "pro" plan users
 
independent = p_a_given_b == p_a  # knowing B leaves the probability of A unchanged
print("Independent?", independent)
 
# Expected output:
# Independent? True

Exercise 3 – Bayes' rule for a fraud detector

Python
# Task: apply Bayes' rule to find P(fraud | flagged)
p_fraud = 0.001            # base rate: 0.1% of transactions are fraud
p_flag_given_fraud = 0.95  # detector catches 95% of real fraud
p_flag_given_ok = 0.05     # detector also flags 5% of legit transactions
 
p_flag = p_flag_given_fraud * p_fraud + p_flag_given_ok * (1 - p_fraud)
p_fraud_given_flag = (p_flag_given_fraud * p_fraud) / p_flag  # Bayes' rule
 
print("P(fraud | flagged):", round(p_fraud_given_flag, 3))
 
# Expected output:
# P(fraud | flagged): 0.019

Next

Continue to Distributions (normal, binomial, Poisson) to see how probability rules build up into full probability models.

Was this page helpful?

Let us know how we did