Skip to content

Beginner: data foundations · Probability and inference · topic 13 of 62 · 30 min

Random variables and distributions

Explanation

A random variable is a number that comes from a chance process, like the number of customers in the next hour. Its distribution says how likely each value is. A few shapes appear again and again: uniform, binomial, Poisson and normal. The expected value is the long-run average.

Why distributions?

A restaurant does not know exactly how many orders will come at 8 p.m., but it knows the pattern: usually 40 to 60, rarely more than 80. A distribution is that pattern written down. Once you know the distribution, you can plan staff, stock and servers, and you can tell when something unusual is happening.

Random variables

A random variable is a number whose value depends on chance. It can be discrete (it takes separate values you can count, like 0, 1, 2 customers) or continuous (any value in a range, like a delivery time of 23.7 minutes). For discrete variables, each value has a probability, and all the probabilities add up to 1.

Expected value: the long-run average

The expected value E[X] is the average you would get if you repeated the process many times. Multiply each value by its probability and add them up. For a die: (1 + 2 + 3 + 4 + 5 + 6) × 1/6 = 3.5. You never roll 3.5, but over many rolls the average gets close to it.

python
values = [-10, 990]       # lose ₹10 or win ₹990
probs = [0.999, 0.001]
ev = sum(v * p for v, p in zip(values, probs))
print(round(ev, 2))
A ₹10 ticket with a 1 in 1,000 chance to win ₹1,000 (₹990 profit). Expected value: -9.0. On average you lose ₹9 per ticket.

Four shapes you will meet

DistributionWhat it counts or measuresExample
UniformEvery value in a range is equally likelyA fair die; a random number between 0 and 1
Binomial (n, p)Number of successes in n yes/no tries, each with chance pHow many of 10 calls lead to a sale when p = 0.2
Poisson (λ)Number of events in a fixed time when they happen independently at an average rate λCalls to a helpline per minute; accidents on a road per day
Normal (μ, σ)A continuous bell curve around the mean μ with spread σHeights, measurement errors, averages of many values

The binomial formula

For k successes out of n tries: P(k) = C(n, k) × p^k × (1 − p)^(n − k). C(n, k), read "n choose k", counts the ways to pick which k tries succeed. Python has it as math.comb(n, k). The expected number of successes is n × p.

python
import math

n, p = 10, 0.5
for k in [0, 5, 10]:
    print(k, round(math.comb(n, k) * p**k * (1 - p)**(n - k), 4))
Shows 0 0.001, then 5 0.2461, then 10 0.001. Five heads in ten tosses is the most likely result, but it happens only about a quarter of the time.

The Poisson formula

If events arrive at an average rate λ (lambda) per period, P(k events) = λ^k × e^(−λ) ÷ k!. A helpline that gets 3 calls a minute on average has P(0 calls) = e^(−3) ≈ 0.05 and P(2 calls) ≈ 0.224.

Tools you will use later

python
from scipy import stats

print(stats.binom.pmf(3, 10, 0.5))   # 0.1171875
print(stats.poisson.pmf(2, 3))        # about 0.224
print(stats.norm.cdf(1.96))           # about 0.975
scipy.stats has every common distribution. pmf gives P(X = k); cdf gives P(X ≤ x). Shown only; the tasks use the standard library.

Common mistakes

  • Using the binomial when the tries are not independent or p changes. Fix: check that each try is like the last.
  • Using the normal for counts that are small or cannot be negative, like 0 to 3 complaints a day. Fix: use Poisson or binomial.
  • Thinking the expected value is the most likely value. A lottery's expected value is a loss even though the most likely result is "lose ₹10".

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Random variables and distributions · 1/8

Distributions

This lesson is about the shapes that chance takes: distributions.

Prefer a video by a teacher? Find free videos on “binomial poisson normal distribution explained expected value data science” on YouTube ↗

In real life: Staffing a call centre

A bank's call centre gets an average of 3 calls per minute at night. Using the Poisson distribution, the manager computes the chance of 6 or more calls in a minute (about 8%) and decides how many agents to keep online so that callers rarely wait. The same idea is used for server capacity at online stores during sale days.

python
import math

lam = 3
p_upto_5 = sum(lam**k * math.exp(-lam) / math.factorial(k) for k in range(6))
print(round(1 - p_upto_5, 3))
Shows 0.084: about 8% of minutes see 6 or more calls.

Practice (0/3 done)

Task 1 · Code · Python

Write expected_value(values, probs) that returns the sum of value × probability, rounded to 2 decimal places. Example: expected_value([1, 2, 3, 4, 5, 6], [1/6] * 6) returns 3.5.

Task 2 · Code · Python

Write binomial_pmf(n, k, p): the probability of exactly k successes in n independent tries, each with success chance p. Use math.comb. Round to 4 decimal places. If k is below 0 or above n, return 0.0. Example: binomial_pmf(10, 3, 0.5) returns 0.1172.

Task 3 · Code · Python

Write poisson_pmf(k, lam): the probability of exactly k events when they happen on average lam times per period. Use math.exp and math.factorial. Round to 4 decimal places. Example: poisson_pmf(2, 3) returns 0.224.

Quick quiz

Question 1

1.Which variable is continuous?

Question 2

2.A game pays ₹50 with probability 0.2 and ₹0 otherwise. What is the expected value?

Question 3

3.Which distribution fits "number of WhatsApp messages a support team receives per minute"?

Question 4

4.A salesperson makes 20 calls, each with a 0.1 chance of a sale. What is the expected number of sales?

Question 5

5.Is the expected value always the most likely single result?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Which variable is continuous?

Answer: Time taken to deliver a pizza

Time can take any value in a range (23.7 minutes); the others are counts.

A game pays ₹50 with probability 0.2 and ₹0 otherwise. What is the expected value?

Answer: ₹10

50 × 0.2 + 0 × 0.8 = 10.

Which distribution fits "number of WhatsApp messages a support team receives per minute"?

Answer: Poisson

Events arriving independently at an average rate per time period follow a Poisson distribution.

A salesperson makes 20 calls, each with a 0.1 chance of a sale. What is the expected number of sales?

Answer: 2

For a binomial, expected successes = n × p = 20 × 0.1 = 2.

Is the expected value always the most likely single result?

Answer: No; for a die it is 3.5, which can never be rolled

The expected value is a long-run average, not necessarily a possible or most likely value.

A fair coin has landed heads 6 times in a row. What is the chance of heads on the next toss?Related

Answer: Exactly 0.5

Tosses are independent; the coin has no memory. Believing otherwise is the gambler's fallacy.

From Probability basics

Which data is least likely to be close to normal?Related

Answer: Monthly incomes of people in a city

Incomes are strongly right-skewed: most are moderate and a few are very large.

From The normal distribution and z-scores

Of 200 customers who opened a promo email, 30 bought something. What is P(bought | opened)?Related

Answer: 0.15

30 ÷ 200 = 0.15. The bottom is the count of the "given" group.

From Conditional probability and Bayes' theorem

Why would you call random.seed(10) or use random.Random(10)?Related

Answer: To get the same sequence of random numbers every time

The same seed gives the same sequence, which makes results repeatable for tests and fair draws.

From Modules and imports

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 2 more topics in “Probability and inference” before its exam: Samples, the central limit theorem and confidence intervals, Hypothesis tests, p-values and A/B testing.
  2. Module 3 exam: Probability and inference · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Cleaning, exploring and visualising data. 62 topics left in the course before the final exam.

← Previous: Conditional probability and Bayes' theorem