Poisson
Chris Gregg
CS109, Stanford University
Summer 2026
Pset #2: Out Yesterday
2
You can solve every problem after today’s lecture
Small nit: Avoid answers that are just equations
3
Probability for Extreme Weather?
Four Prototypical Trajectories
Review
Natural Exponent Definition
Natural Exponent def:
Jacob
Bernoulli
Binomial Random Variable
7
The number of successes, in n independent trials, where each trial is a success with probability p:
Declare a Random Variable to be Binomial
Number of successes
Is distributed as a
Binomial
With these parameters
Num trials
Probability of success on each trial
Automatically Know the PMF
Probability that there are k successes
Probability Mass Function for a Binomial
* This is also called the binomial term
The PMF as a Graph: X ~ Bin(n = 20, p = 0.6)
10
You Get So Much For Free!
Dating at Stanford
Each person you date has a 0.2 probability of being someone you spend your life with. What is the probability you need to date more than 5 people? Your meta goal: what steps would you take to answer this question?
Let X be the # of people date before marrying
I accidentally started with 0 last week!
Four Prototypical Trajectories
What if we could summarize the whole beautiful PMF into a single number?
Expected Value
Loop over all values x that X can take on
The value
The probability of that value
Properties of Expectation (more on this later)
Four Prototypical Trajectories
End Review
Four Prototypical Trajectories
Variance
Intuition: Peer Grading
Peer Grading on Coursera HCI.
31,067 peer grades for 3,607 students.
Intuition: Peer Grading
70
100
40
B
50
80
20
C
70
100
40
True grade
A
P(X=x)
Intuition: Measure of Spread
Peer grading in Coursera HCI
60
80
100
40
20
0
Let X be a random variable that represents a peer grade
μ = E[X] = 57.5
Peer grading in Coursera HCI
60
80
100
40
20
0
Let X be a random variable that represents a peer grade
μ = E[X] = 57.5
Var(X) = E [(X – μ)2]
Peer grading in Coursera HCI
60
80
100
40
20
0
Var(X) = E [(X – μ)2]
Let X be a random variable that represents a peer grade
μ = E[X] = 57.5
x
(x – μ)2
25 points
1056 points2
P(X = x)
0.02
Peer grading in Coursera HCI
60
80
100
40
20
0
Let X be a random variable that represents a peer grade
μ = E[X] = 57.5
x
(x – μ)2
25 points
1056 points2
80 points
506 points2
P(X = x)
0.02
0.09
Var(X) = E [(X – μ)2]
Peer grading in Coursera HCI
60
80
100
40
20
0
Let X be a random variable that represents a peer grade
μ = E[X] = 57.5
x
(x – μ)2
25 points
1056 points2
80 points
506 points2
50 points
56 points2
P(X = x)
0.02
0.09
0.12
Var(X) = E [(X – μ)2]
Peer grading in Coursera HCI
60
80
100
40
20
0
Let X be a random variable that represents a peer grade
μ = E[X] = 57.5
x
(x – μ)2
25 points
1056 points2
80 points
506 points2
50 points
56 points2
…
E [(X – μ)2] = 52 points2
P(X = x)
0.02
0.09
0.12
Var(X) = E [(X – μ)2]
Peer grading in Coursera HCI
60
80
100
40
20
0
Let X be a random variable that represents a peer grade
μ = E[X] = 57.5
x
(x – μ)2
25 points
1056 points2
80 points
506 points2
50 points
56 points2
…
E [(X – μ)2] = 52 points2
P(X = x)
Std(X) = 7.2 points
0.02
0.09
0.12
Var(X) = E [(X – μ)2]
60
80
100
40
20
0
Normalized histograms are approximations of probability mass functions
Peer grading in Coursera HCI
60
80
100
40
20
0
x
(x – μ)2
25 points
1056 points2
80 points
506 points2
50 points
56 points2
…
E [(X – μ)2] = 52 points2
μ = E[X] = 57.5
Let X be a random variable that represents a peer grade
P(X = x)
Std(X) = 7.2 points
0.02
0.09
0.12
How Should We Measure Spread?
60
80
100
40
20
0
Different Possibility:
On average..
The random variable X
The mean of X
distance
Spread stat.
μ = E[X] = 57.5
Let X be a random variable
Variance
Var(X) = E [(X – μ)2]
Computing Variance
Law of unconscious statistician
Notation
How do you get E[X2]?
33
Unconscious statistician:
E[X2]:
Standard Deviation?
Units are in points squared
Units are in points
Example: Variance of a Dice Roll
Piech & Cain, CS109, Stanford University
Example: Variance of a Dice Roll
Piech & Cain, CS109, Stanford University
Example: Variance of a Dice Roll
Piech & Cain, CS109, Stanford University
Example: Variance of a Dice Roll
Piech & Cain, CS109, Stanford University
Variance of a 6 Sided Dice
39
Fundamental Properties of Random Variables
Random Variable
Moments
P(X = x)
Semantic Meaning
E[X]
Measure of spread
Var(X)
Std(X)
Mode(X)
Support
You Get So Much For Free!
Curious? Proof of Variance for a Binomial (Hard Way)
Four Prototypical Trajectories
Now the easy way….
Variance of a Bernoulli
Variance of a Binomial?
Variance of a Binomial (Easy Way)
Is this true? Is the variance of the sum the sum of variance?
Only if Xis are independent!
Definitions
Proved
Want to Show
Proof
Is Peer Grading Accurate Enough?
Peer Grading on Coursera HCI.
31,067 peer grades for 3,607 students.
Tuned Models of Peer Assessment. C Piech, J Huang, A Ng, D Koller
Looking ahead
Is Peer Grading Accurate Enough?
1. Defined random variables for:
Tuned Models of Peer Assessment. C Piech, J Huang, A Ng, D Koller
2. Designed a probabilistic model that
defined the distributions for all random variables
Problem param
Looking ahead
Is Peer Grading Accurate Enough?
Tuned Models of Peer Assessment. C Piech, J Huang, A Ng, D Koller
2. Designed a probabilistic model that
defined the relationship between all the random variables
3. Found the variable assignments that maximized the probability of our observed data
1. Defined random variables for:
Looking ahead
Inference or Machine Learning
99% within 10pp
Before:
After:
Yes, With Probabilistic Modelling
Tuned Models of Peer Assessment. C Piech, J Huang, A Ng, D Koller
81% within 10pp
Std 7.2
Std 4.7
“sweet spot of grading”: ~ 20 minutes
Grading Sweet Spot
Four Prototypical Trajectories
Ready..
Algorithmic Ride Sharing
Probability of k requests from this area in the next 1 min
Probability of k requests from this area in the next 1 min
Probability of k requests from this area in the next 1 min
On average λ = 5 requests per minute
Probability of k requests from this area in the next 1 min
We can break the next minute down into seconds
…
1
2
3
4
5
6
60
On average λ = 5 requests per minute
Probability of k requests from this area in the next 1 min
On average λ = 5 requests per minute
…
At each second either get a request or you don’t.
Let X = Number of requests in the minute
1
2
3
4
5
6
60
We can break the next minute down into seconds
Probability of k requests from this area in the next 1 min
On average λ = 5 requests per minute
…
At each second either get a request or you don’t.
Let X = Number of requests in the minute
1
2
3
4
5
6
60
We can break the next minute down into seconds
Probability of k requests from this area in the next 1 min
On average λ = 5 requests per minute
…
At each second either get a request or you don’t.
Let X = Number of requests in the minute
But what if there are two requests in the same second?
1
2
3
4
5
6
60
We can break the next minute down into seconds
Probability of k requests from this area in the next 1 min
On average λ = 5 requests per minute
We can break that next minute down into milli-seconds
…
60,000
But what if there are two requests in the same millisecond?
1
At each milli-second either get a request or you don’t.
Let X = Number of requests in the minute
Probability of k requests from this area in the next 1 min
On average λ = 5 requests per minute
…
60,000
1
Can we do any better than milli-seconds?
At each milli-second either get a request or you don’t.
Let X = Number of requests in the minute
We can break that next minute down into milli-seconds
Probability of k requests from this area in the next 1 min
On average λ = 5 requests per minute
We can break that minute down into infinitely small buckets
1
Infinitesimal…
Let X = Number of requests in the minute
Who wants to see some cool math?
Probability of k requests from this area in the next 1 min
Probability of k requests from this area in the next 1 min
Simeon-Denis Poisson
Poisson Random Variable
Poisson Process
# events in original time interval ~ Poi(λ)
1
2
3
To the reader!
69
Poisson is great when you have a rate!
Poisson is great when you have a rate and you care about # of occurrences!
Make sure that the time unit for “rate” and match the probability question
Four Prototypical Trajectories
Two quick examples!
Earthquakes
Average of 2.79 major earthquakes per year.
What is the probability of 3 major earthquakes next year?
Earthquake Probability Mass Function
Let X = number of earthquakes next year
P(X = x)
Number of earthquakes (x)
Earthquakes
Limited sample containers
A primatologist is studying a group of chimpanzees in the wild.
On average, 15 usable biological samples (e.g., hair samples) are collected per day.
Each sample must be stored in its own sterile container.
The researcher only brought 10 containers into the field.
What is the probability that the researcher runs out of containers during the day?
77
Let X be the number of usable biological samples taken.
from scipy import stats
def main():
lam = int(input("Number of samples per day: "))
num_containers = int(input("Number of containers brought: "))
X = stats.poisson(lam)
prob_enough_containers = 0
for i in range(0, num_containers + 1):
pr_i_samples = X.pmf(i)
prob_enough_containers += pr_i_samples
print(f"Prob of running out: {1 - prob_enough_containers}")
PMF of Poisson
λ = 15
Limited sample containers, follow-up
A primatologist is studying a group of chimpanzees in the wild.
On average, 15 usable biological samples (e.g., hair samples) are collected per day.
Each sample must be stored in its own sterile container.
How many containers should the researcher bring so that the probability of running out is at most 5%?
78
Limited sample containers, follow-up
A primatologist is studying a group of chimpanzees in the wild.
On average, 15 usable biological samples (e.g., hair samples) are collected per day.
Each sample must be stored in its own sterile container.
How many containers should the researcher bring so that the probability of running out is at most 5%?
79
This is the same distribution as before: X ~ Poi(𝞴=15), but now we want:
We can use a program (next slide) to check each cumulative sum until we find the one that fits.
Limited sample containers, follow-up
80
from scipy import stats
def main():
lam = int(input("Number of samples per day: "))
prob_of_not_running_out = int(input("What maximum probability of running out do you want to determine? (e.g, 5 for probability of running out is at max 5%)? "))
X = stats.poisson(lam)
prob_enough_containers = 0
minimum_num = -1
cdf_of_min = 0
for i in range(lam * 2): # try all possibilities up to twice the number
cdf = X.cdf(i)
print(f"Number of containers: {i}, P(X<={i}) = {cdf}")
if cdf > 1 - prob_of_not_running_out / 100 and minimum_num == -1:
minimum_num = i
cdf_of_min = cdf
print(f"You should bring {minimum_num} containers to have a maximum of {prob_of_not_running_out}% chance of running out.")
Limited sample containers, follow-up
81
Number of samples per day: What maximum probability of running out do you want to determine? 15
(e.g, 5 for probability of running out is at max 5%)? 5
Number of containers: 0, P(X<=0) = 3.0590232050182594e-07
Number of containers: 1, P(X<=1) = 4.894437128029217e-06
Number of containers: 2, P(X<=2) = 3.930844818448459e-05
...
Number of containers: 21, P(X<=21) = 0.9468935935407288
Number of containers: 22, P(X<=22) = 0.9672557550672212
Number of containers: 23, P(X<=23) = 0.9805354256279772
Number of containers: 24, P(X<=24) = 0.9888352197284497
Number of containers: 25, P(X<=25) = 0.9938150961887332
Number of containers: 26, P(X<=26) = 0.9966881018388968
Number of containers: 27, P(X<=27) = 0.9982842160889877
Number of containers: 28, P(X<=28) = 0.9991392772943934
Number of containers: 29, P(X<=29) = 0.9995815503316723
You should bring 22 containers to have a maximum of 5% chance of running out.
Poisson in Python
Function | Description |
X.pmf(k) | P(X = k) |
X.cdf(k) | P(X ≤ k) |
X.mean() | E[X] |
X.var() | Var(X) |
X.std() | Std(X) |
from scipy import stats # great package
X = stats.poisson(2.5) # X ~ Poi(λ = 2.5)
print(X.pmf(2)) # P(X = 2)
Four Prototypical Trajectories
Poisson can approximate a Binomial!
Wait why would you want to do that?
1) Binomial can be expensive to compute.
2) Connections help build math intuition.
Storing Data in DNA
All the movies, images, emails and other digital data from more than 600 smartphones (10,000 gigabytes) can be stored in the faint pink smear of DNA at the end of this test tube.
Storing Data in DNA
Storing Data in DNA
The “rate” here is the number of corruptions per string
Poisson is a Binomial in the Limit
n ➔ ∞ and p ➔ 0, where np = λ
Bin(10,0.3) vs Bin(100,0.03) vs Poi(3)
Owner unknown…
A Real License Plate Seen at Stanford
Poisson can be used to approximate a Binomial where n is large and p is small.
Central Moments with Poisson
Poisson Paradigm
“Successes” in trials are not entirely independent.
Probability of “Success” p in each trial varies slightly.
1
2
Web Server Load
Web Server Load
We can do this calculation in Python a couple of different ways
Web Server Load
We can do this calculation in Python a couple of different ways
>>> import math
>>> poi_sum = 0
>>> for i in range(5):
... poi_sum += math.e**-2 * 2**i / math.factorial(i)
...
>>> poi_sum
0.9473469826562889
Web Server Load
We can do this calculation in Python a couple of different ways
>>> from scipy import stats
>>> X = stats.poisson(2)
>>> X.cdf(4)
np.float64(0.9473469826562889)
Probability for Extreme Weather?
Hurricanes per Year since 1851
Four Prototypical Trajectories
To the code!
Historically ~ Poisson(8.5)
This is the pmf of a Poisson. Your favorite programming language has a function for it
= 0.0135
Improbability Drive
Four Prototypical Trajectories
Twice since 1966 there have been two years with over 30 hurricanes
Improbability Drive
This is the pdf of a Poisson. Your favorite programming language has a function for it
* Challenge: Calculate the probability of two years with over 30 hurricanes
The Distribution has Changed
Four Prototypical Trajectories
What’s up?
What’s Up?
CO2 leads to Hotter Oceans
107
What’s Up?
Four Prototypical Trajectories
Next Time
Bernoulli:
Binomial:
Poisson:
Geometric:
Negative Binomial:
Zipf:
Discrete Distributions
Solution
“Backbone”
The Poisson Common Path