SyntaxPilot
Courses/Data Analytics with Python/Lesson 41
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

Data Analytics with Python · Lesson 41 of 90

Random variables and discrete distributions

random variablediscrete vs continuous random variableprobability mass function (PMF)expected valuevariance of a random variableBernoullibinomialPoissongeometricdiscrete uniformscipy.stats (pmf, cdf, rvs)

These are the notes for Data Analytics with Python, episode 41. Until now probability answered yes/no questions ("will this customer buy a snack?"). Today the answer is a number: how many of the next 10 customers will buy one? You will learn the random variable, discrete vs continuous random variables, the probability mass function (PMF), the expected value and variance of a random variable, and five classic discrete distributions: Bernoulli, binomial, Poisson, geometric and discrete uniform, using scipy.stats (pmf, cdf, rvs). Every term is defined the first time it appears and collected in the Glossary.

Every program below was run for real (Python 3.13, NumPy 2.5.3, pandas 3.0.5, SciPy 1.18.1, matplotlib 3.11.2, on a server with no screen). Outputs and images are from those runs. All data is made up for teaching. Every program starts with these lines:

import sys; sys.tracebacklimit = 0
import numpy as np, pandas as pd
from scipy import stats
rng = np.random.default_rng(41)

Random variables

Let X = the number of samosas bought by Ramesh's next 10 customers. X is different every morning, decided by chance. A random variable is a number whose value comes from a random experiment (episode 39). Capital letters (X) name the random variable; small letters (x, k) name particular values.

Kind Values Examples
Discrete random variable countable: 0, 1, 2, … orders, calls, defects, goals, samosas
Continuous random variable any value in a range waiting time, weight, height, temperature (episode 42)

The probability mass function (PMF)

Each customer buys with probability 0.5 (episode 39 measured 0.497). The obvious guess: "5 samosas a day". Simulate 100,000 days:

days = rng.random((100_000, 10)) < 0.5      # 100,000 days x 10 customers
X = days.sum(axis=1)                         # samosas sold each day
print(X[:12])
pmf = pd.Series(X).value_counts(normalize=True).sort_index()
print(pmf.round(3))
[3 4 5 4 4 6 3 4 3 6 4 6]
0     0.001
1     0.009
2     0.044
3     0.118
4     0.205
5     0.246
6     0.204
7     0.119
8     0.044
9     0.010
10    0.001
Name: proportion, dtype: float64

X ranges from 0 to 10; exactly 5 happens on only 24.6% of days. The table of each possible value with its probability is the probability mass function (PMF), written P(X = k). "Mass" because it puts a lump of probability on each value. Every PMF is between 0 and 1 for each value, and the values add up to 1.

Bernoulli and binomial

  • Bernoulli(p): a single yes/no trial, 1 with probability p, 0 otherwise (one customer, one coin toss, possibly biased). Mean p, variance p(1 − p).
  • Binomial(n, p): the number of yeses in n independent trials, each with the same p. Here n = 10 customers, p = 0.5.

A distribution is the full pattern of which values a random variable takes and how likely each is. SciPy knows the binomial formula, P(X = k) = C(n, k) pᵏ (1 − p)ⁿ⁻ᵏ, where C(n, k) counts the ways to choose which k trials are yeses:

k = np.arange(0, 11)
print(pd.Series(stats.binom.pmf(k, 10, 0.5), index=k).round(3))
print("sum of all probabilities:", stats.binom.pmf(k, 10, 0.5).sum())
0     0.001
1     0.010
2     0.044
3     0.117
4     0.205
5     0.246
6     0.205
7     0.117
8     0.044
9     0.010
10    0.001
dtype: float64
sum of all probabilities: 0.9999999999999998

The exact PMF matches the simulation (0.246 for 5). The sum is 1 up to floating-point rounding (0.9999999999999998).

Expected value and variance

The expected value E[X] is the long-run average: multiply each value by its probability and add, E[X] = Σ x · P(X = x). The variance of a random variable is the probability-weighted average squared distance from the mean, Var(X) = Σ (x − E[X])² · P(X = x); its square root is the standard deviation.

k = np.arange(0, 11)
p = stats.binom.pmf(k, 10, 0.5)
mean = (k * p).sum()
var = ((k - mean) ** 2 * p).sum()
print("E[X] =", round(mean, 3), "  Var(X) =", round(var, 3))
print("SD =", round(var ** 0.5, 3))
print("scipy:", stats.binom.mean(10, 0.5), stats.binom.var(10, 0.5))
E[X] = 5.0   Var(X) = 2.5
SD = 1.581
scipy: 5.0 2.5

Binomial shortcuts: E[X] = np = 5, Var(X) = np(1 − p) = 2.5, SD = 1.58.

The expected value need not be a possible value. A fair-stall game: pay ₹10, roll a die, win ₹30 on a 6 (profit +20), otherwise lose the ₹10:

win = np.array([30 - 10, -10])              # roll a 6: +20, else -10
prob = np.array([1/6, 5/6])
print("E[profit] per game =", round((win * prob).sum(), 2))
rolls = rng.integers(1, 7, 100_000)
profit = np.where(rolls == 6, 20, -10)
print("simulated average  =", round(profit.mean(), 2))
E[profit] per game = -5.0
simulated average  = -5.01

E[profit] = 20 × 1/6 − 10 × 5/6 = −5. You can never lose exactly ₹5 in one game, but that is the long-run average loss per game: how game stalls, insurers and lotteries make money.

The CDF: how many to fry

The cumulative distribution function (CDF) is F(x) = P(X ≤ x): the PMF added up from the bottom.

for fry in [5, 6, 7, 8, 9]:
    print(f"fry {fry}: P(X <= {fry}) = {stats.binom.cdf(fry, 10, 0.5):.3f}")
fry 5: P(X <= 5) = 0.623
fry 6: P(X <= 6) = 0.828
fry 7: P(X <= 7) = 0.945
fry 8: P(X <= 8) = 0.989
fry 9: P(X <= 9) = 0.999

Frying 5 is enough on only 62% of days; frying 8 is enough on 98.9%. If running out on about 5% of days is acceptable, the smallest choice is 8 (7 gives 94.5%, just short of 95%). For "more than", use 1 − cdf: P(X > 7) = 1 − 0.945 = 0.055.

Poisson: counts per hour

How many customers arrive in an hour? Try the binomial, but there is no fixed number of trials:

customers_per_hour = stats.binom.pmf(15, n=None, p=0.1)
TypeError: '>=' not supported between instances of 'NoneType' and 'int'

TypeError: '>=' not supported between instances of 'NoneType' and 'int': the binomial needs n. What Ramesh knows is the average rate: 12 customers an hour. For counts of events in a fixed stretch of time (or space), happening independently at a steady average rate, use the Poisson distribution with mean μ (mu): P(X = k) = e^(−μ) μᵏ / k!.

mu = 12                            # 12 customers per hour, on average
for k in [8, 12, 16, 20]:
    print(f"P(exactly {k:2}) = {stats.poisson.pmf(k, mu):.3f}")
print("P(more than 15) =", round(1 - stats.poisson.cdf(15, mu), 3))
s = stats.poisson.rvs(mu, size=100_000, random_state=41)
print("mean", round(s.mean(), 2), " variance", round(s.var(), 2))
P(exactly  8) = 0.066
P(exactly 12) = 0.114
P(exactly 16) = 0.054
P(exactly 20) = 0.010
P(more than 15) = 0.156
mean 12.0  variance 11.96

rvs draws random values from a distribution ("random variates"); random_state makes them reproducible. For a Poisson, mean = variance = μ (12.0 and 11.96 here). If real count data has a variance much larger than its mean, it is "overdispersed" and a plain Poisson fits badly.

Typical Poisson counts: calls per hour at a help desk, website sign-ups per minute, typos per page, accidents per month at a junction.

Geometric: waiting for the first success

10% of customers order the ₹200 special. How many customers until the first special? The number of trials up to and including the first success follows the geometric distribution: P(X = k) = (1 − p)^(k−1) p.

p = 0.1                                      # 10% order the ₹200 special
for k in [1, 2, 5, 10]:
    print(f"P(first special is customer {k:2}) = {stats.geom.pmf(k, p):.3f}")
print("expected wait:", stats.geom.mean(p), "customers")
P(first special is customer  1) = 0.100
P(first special is customer  2) = 0.090
P(first special is customer  5) = 0.066
P(first special is customer 10) = 0.039
expected wait: 10.0 customers

Expected wait = 1/p = 10 customers. Each later value is less likely, because every earlier customer must say no. (SciPy's geom counts the trial of the first success, starting at 1.)

The three shapes

import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 3, figsize=(14, 4))
k = np.arange(0, 11); axs[0].bar(k, stats.binom.pmf(k, 10, 0.5))
axs[0].set_title("binomial(n=10, p=0.5): samosas")
k = np.arange(0, 26); axs[1].bar(k, stats.poisson.pmf(k, 12), color="#2a9d8f")
axs[1].set_title("Poisson(mu=12): customers per hour")
k = np.arange(1, 31); axs[2].bar(k, stats.geom.pmf(k, 0.1), color="#e4572e")
axs[2].set_title("geometric(p=0.1): wait for a special")
fig.tight_layout(); fig.savefig("pmfs.png")

pmfs.png

Situation Distribution SciPy Mean Variance
one yes/no trial Bernoulli(p) stats.bernoulli(p) p p(1 − p)
yeses in n independent trials Binomial(n, p) stats.binom(n, p) np np(1 − p)
events per interval at rate μ Poisson(μ) stats.poisson(mu) μ μ
trials until first success Geometric(p) stats.geom(p) 1/p (1 − p)/p²
equally likely integers a … b Discrete uniform stats.randint(a, b + 1) (a + b)/2 ((b − a + 1)² − 1)/12

Discrete uniform

Every value equally likely, like a fair die: the discrete uniform distribution. SciPy calls it randint(low, high), and like NumPy's integers (episode 39) high is excluded:

die = stats.randint(1, 6)
print("faces:", sorted(set(die.rvs(size=1000, random_state=1).tolist())))
die = stats.randint(1, 7)
print("faces:", sorted(set(die.rvs(size=1000, random_state=1).tolist())))
print("P(each face) =", round(die.pmf(4), 4), " mean =", die.mean())
faces: [1, 2, 3, 4, 5]
faces: [1, 2, 3, 4, 5, 6]
P(each face) = 0.1667  mean = 3.5

Silent traps in SciPy

print(stats.binom.pmf(3, 10, 1.5))
print(stats.poisson.pmf(3))
nan
TypeError: _parse_args() missing 1 required positional argument: 'mu'

An impossible parameter (p = 1.5) returns nan ("not a number") with no error. A missing parameter raises TypeError: _parse_args() missing 1 required positional argument: 'mu'. Check parameters and look for nan in outputs.

Common mistakes

Code / habit Real error / symptom Why Fix
stats.binom.pmf(15, n=None, p=0.1) TypeError: '>=' not supported between instances of 'NoneType' and 'int' no fixed number of trials counts per time → stats.poisson
stats.poisson.pmf(3) TypeError: _parse_args() missing 1 required positional argument: 'mu' Poisson needs its mean stats.poisson.pmf(3, mu)
stats.binom.pmf(3, 10, 1.5) no error; nan p must be in [0, 1] validate parameters
stats.randint(1, 6) for a die no error; never a 6 high is excluded stats.randint(1, 7)
"E[X] = 5, so we sell 5 every day" no error; stock-outs the mean is not a guarantee use the CDF for planning
binomial for dependent trials no error; wrong spread trials must be independent check independence

Mini project: staffing the help desk

A help desk gets 30 calls per hour on average; one agent handles up to 10 calls an hour. How many agents are needed so that the hour is fully covered at least 95% of the time?

from scipy import stats
mu = 30
for agents in range(3, 7):
    capacity = 10 * agents
    print(f"{agents} agents: P(calls <= {capacity}) = {stats.poisson.cdf(capacity, mu):.3f}")
3 agents: P(calls <= 30) = 0.548
4 agents: P(calls <= 40) = 0.968
5 agents: P(calls <= 50) = 1.000
6 agents: P(calls <= 60) = 1.000

3 agents cover only about 55% of hours; 4 agents cover about 97%, so 4 is the smallest number that reaches 95%.

Practice

1. You roll a fair die 10 times. What is P(at least 3 sixes)?

Solution
from scipy import stats
print(round(1 - stats.binom.cdf(2, 10, 1/6), 4))
0.2248

"At least 3" = 1 − P(X ≤ 2).

2. A shop gets 4 complaints per day on average. What is P(no complaints tomorrow)? P(more than 8)?

Solution
from scipy import stats
print(round(stats.poisson.pmf(0, 4), 4), round(1 - stats.poisson.cdf(8, 4), 4))
0.0183 0.0214

3. Simulate the geometric wait for p = 0.1 and check that the average is about 1/p.

Solution
import numpy as np
rng = np.random.default_rng(3)
waits = rng.geometric(0.1, size=100_000)
print(round(waits.mean(), 2), waits.min())
10.02 1

4. Compute E[X] and Var(X) for a fair die from its PMF.

Solution
import numpy as np
k = np.arange(1, 7); p = np.full(6, 1/6)
m = (k * p).sum(); v = ((k - m) ** 2 * p).sum()
print(m, round(v, 4))
3.5 2.9167

Quick quiz

# A help desk gets 30 calls per hour on average.
# X = number of calls in the next hour.
# Which scipy.stats distribution models X?

A. stats.binom(n=30, p=0.5) B. stats.poisson(mu=30) C. stats.geom(p=1/30) D. stats.randint(0, 31)

Answer

B. A count of events over time with a known average rate and no fixed number of trials is Poisson; its mean and variance are both 30.

Interview questions

  1. What is a random variable? A number determined by the outcome of a random experiment.
  2. PMF vs CDF? PMF: P(X = x) for each value; CDF: P(X ≤ x), cumulative.
  3. How do you compute an expected value? E[X] = Σ x P(X = x); the long-run average.
  4. Binomial vs Poisson? Binomial: number of successes in a fixed number n of independent trials; Poisson: number of events in an interval at a constant average rate, no fixed n. (A binomial with large n and small p is close to Poisson with μ = np.)
  5. What is special about the Poisson's mean and variance? They are equal.
  6. What does the geometric distribution model? The number of trials until the first success; mean 1/p.
  7. Give a business use of the CDF. Inventory or staffing: choose the smallest quantity with P(demand ≤ quantity) ≥ the service level.

Glossary

Term Meaning Example
Random variable A number decided by a random experiment X = samosas sold
Discrete random variable Takes countable values 0 … 10 samosas
Continuous random variable Takes any value in a range waiting time
Distribution Which values a random variable takes and how likely each is binomial(10, 0.5)
Probability mass function (PMF) P(X = x) for each value of a discrete random variable 0.246 for 5
Cumulative distribution function (CDF) P(X ≤ x) 0.945 for 7
Expected value E[X] Long-run average: Σ x P(x) 5 samosas, −₹5 per game
Variance of a random variable Σ (x − E[X])² P(x) 2.5
Trial One run of a yes/no experiment one customer
Bernoulli distribution One trial: 1 with probability p, else 0 buys or not
Binomial distribution Number of yeses in n independent trials samosas from 10 customers
Poisson distribution Number of events in an interval at average rate μ customers per hour
Rate (μ) Average number of events per interval 12 per hour
Geometric distribution Number of trials until the first success wait for a special order
Discrete uniform distribution Equally likely integers a fair die
scipy.stats SciPy's statistics module of distributions and tests stats.binom
pmf, cdf, rvs Probability of a value, cumulative probability, random draws stats.poisson.rvs(12)
nan "Not a number": an undefined result binom.pmf(3, 10, 1.5)

Summary

  • A random variable is a number decided by chance; discrete ones take countable values.
  • Its PMF gives each value's probability (sums to 1); the CDF adds them up and drives planning decisions.
  • Expected value = long-run average; variance = long-run spread.
  • Bernoulli (one trial), binomial (yeses in n trials), Poisson (counts per interval, mean = variance), geometric (wait for the first success), discrete uniform (all equal).
  • scipy.stats: pmf, cdf, rvs, mean, var. Watch for nan and excluded upper ends.
  • Next episode: continuous distributions and the normal curve.