Normal Distribution
Chris Gregg
CS109, Stanford University
Summer, 2026
Enough Servers?
Four Prototypical Trajectories
Announcements
Announcements
Four Prototypical Trajectories
Review
Review: Probability Density Function
Recall the definition of a derivative:
A probability!
What do you get if you integrate over a
probability density function?
Review: Probability Density Function
The probability density function (PDF) of a continuous random variable represents the derivative of probability at a given point.
Units of probability divided by units of X. Integrate it to get probabilities!
Uniform and Exponential Distributions
https://chrispiech.github.io/probabilityForComputerScientists/en/part2/uniform/
Cumulative Distribution Function
A cumulative distribution function (CDF) is a “closed form” equation for the probability that a random variable is less than a given value
If you learn how to use a cumulative distribution function, you can avoid integrals!
Using CDF Example. X is Exp(λ = 1)
Probability density function
Cumulative density function
f(x)
x
x
P(1 < X < 2)
or
Did you know? Exponential is Memoryless!
12
X = time until the next event
What if s time has passed?
Did you know? Exponential is Memoryless!
13
X = time until the next event
What if s time has passed?
Did you know? Exponential is Memoryless!
14
X = time until the next event
What if s time has passed?
I am going to use these two properties later in class today
15
How Long Until the Next Earthquake
Based on historical data, major earthquakes (magnitude 8.0+) happen at a rate of 0.002 per year*. What is the probability of
a major earthquake in the next 30 years?
Y = Years until the next earthquake of magnitude 8.0+
*In California, according to the USGS, 2015
Piech, CS109, Stanford University
How Long Until the Next Earthquake: another way!
Based on historical data, major earthquakes (magnitude 8.0+) happen at a rate of 0.002 per year*. What is the probability of
a major earthquake in the next 30 years?
Y = # of Earthquakes in 30 years
*In California, according to the USGS, 2015
Piech, CS109, Stanford University
Four Prototypical Trajectories
/Review
Four Prototypical Trajectories
Big Day
NormCore: A Few Normal Examples
20
Normal Random Variable
21
Variance
Expectation
mean
variance
Carl Friedrich Gauss
22
Why the Normal?
23
These are log-normal
Only if they are equally weighted and independent
Most noise is assumed normal
That is actually true…
“The simplest explanation is usually the best one”
Complexity is Tempting
25
value
probability
Probably over fitting…
* That describes the training data, but will it generalize?
Fewest Assumptions
26
value
probability
μ
σ2
* A Gaussian maximizes entropy for a given mean and variance
Simple. Will generalize
Fewest Assumptions
27
value
probability
μ
σ2
* A Gaussian makes the fewest assumptions after matching mean and variance.
Simple. Will generalize
Four Prototypical Trajectories
Normal is Beautiful!
Normal Probability Density Function
29
x
f(x)
Anatomy of a Beautiful Equation
30
probability density at x
the distance to the mean
a constant
“exponential”
sigma shows up twice
Does it look less scary like this?
31
What if you had to take the log of this function?
This means "e to the power of" and is common function in code math libraries
This means "proportional to". There is a constant but there are many cases where we don’t care what it is!
Four Prototypical Trajectories
Lets go!
Let’s Try It Out: Submarine Manufacturing
33
What fraction of the panels you manufacture will meet standards?
Your team is tasked with producing the side panels for Deep Sea Submarines. Physics requires all panels to be built to within 10 microns of 500. You check how precise your manufacturing is, and find these stats:
34
What fraction of the panels you manufacture will meet standards?
Let’s Try It Out: Submarine Manufacturing
Your team is tasked with producing the side panels for Deep Sea Submarines. Physics requires all panels to be built to within 10 microns of 500. You check how precise your manufacturing is, and find these stats:
Let’s Try It Out: Submarine Manufacturing
35
What fraction of the panels you manufacture will meet standards?
Your team is tasked with producing the side panels for Deep Sea Submarines. Physics requires all panels to be built to within 10 microns of 500. You check how precise your manufacturing is, and find these stats:
36
What fraction of the panels you manufacture will meet standards?
Let’s Try It Out: Submarine Manufacturing
Your team is tasked with producing the side panels for Deep Sea Submarines. Physics requires all panels to be built to within 10 microns of 500. You check how precise your manufacturing is, and find these stats:
Submarine Manufacturing
37
37
Four Prototypical Trajectories
No closed form for the integral
39
Four Prototypical Trajectories
No closed form for F(x)
Spoiler: Numerically Solved CDF
40
A function that has been solved for numerically
* We are going to spend the next few slides getting here
The cumulative density function of any normal
Linear Transform of a Normal is… Normal!
41
Linear Transform of a Normal is… Normal!
Derivation:
42
Aside: Celsius to Fahrenheit
Average temp in Palo Alto (for some date)
in Celsius:
What is the distribution in Fahrenheit?
Let
be the temperature in Fahrenheit.
Because this is a linear transform…
10
60
20
30
40
50
Probability Density
Distribution in Celsius
Distribution in Fahrenheit
Linear Transform of a Normal is… Normal!
The cutest linear transform
This is called the “standard normal”
There is a special case of linear transform for any X:
is also Normal
Standard deviation, not variance!
The Standard Normal
*This is the probability density function for the standard normal
Mean (μ) = 0
Variance (σ2) = 1
Phi
*This is the probability density function for the standard normal
Φ(1.31) = 0.7054
Using Table of ɸ
Symmetry of Phi
a
-a
μ = 0
*This is the probability density function for the standard normal
Symmetry of Phi
a
-a
μ = 0
*This is the probability density function for the standard normal
Interval of Phi
d
c
ɸ(c)
ɸ(d
μ = 0
Use Z to compute F(x)
Compute F(x) via Transform
For normal distribution, F(x) is computed using the phi transform.
And here we are
Table of Φ(z) values in textbook, p. 201 and handout
CDF of Standard Normal: A function that has been solved for numerically
The cumulative density function (CDF) of any normal
Using the Phi Table
Φ(0.54) = 0.7054
Do We Have To Use The Table??
Four Prototypical Trajectories
Table is kinda old school
We Are Computer Scientists!
from scipy import stats
stats.norm.cdf(x, mean, std)
Every modern programming language has phi stored in a library:
Piech & Cain, CS109, Stanford University
We Are Computer Scientists!
from scipy import stats
stats.norm.cdf(x, mean, std)
Every modern programming language has phi stored in a library:
not variance!!!
Piech & Cain, CS109, Stanford University
I Made One For You
Practice: Submarine Manufacturing
What fraction of the panels you manufacture will meet standards?
= ?
Practice: Submarine Manufacturing
What fraction of the panels you manufacture will meet standards?
Now using the CDF!
subtract mean, divide by std. dev.
Practice: Submarine Manufacturing
What fraction of the panels you manufacture will meet standards?
Now using the CDF!
Practice: Submarine Manufacturing
Now using the CDF!
What fraction of the panels you manufacture will meet standards?
Get your Gaussian On
Piech & Cain, CS109, Stanford University
Four Prototypical Trajectories
Are you ready for something different?
What is P(CEO endorses change| it has no effect)?
Midterm: Website Testing
What is P(CEO endorses change| it has no effect)?
Midterm: Website Testing
What is P(CEO endorses change| it has no effect)?
Midterm: Website Testing
>>> math.comb(1000000,501000)
What is P(CEO endorses change| it has no effect)?
Midterm: Website Testing
>>> math.comb(1000000,501000)
ValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit
>>>
71
Don’t worry, Normal approximates Binomial
72
Poisson Approximates Binomial, With Extreme n and p
P(X = k)
k
Piech & Cain, CS109, Stanford University
What is P(CEO endorses change| it has no effect)?
Midterm: Website Testing
🤨
Correct answer is 0.02270
Normal Approximation (with continuity correction)
75
64 65 66
65
✅
Binomial
Normal
You must perform a continuity correction when approximating a Binomial RV with a Normal RV.
Continuity correction
76
Discrete (e.g., Binomial) probability question | Continuous (Normal) probability question |
P(X=6) | |
P(X≥6) | |
P(X>6) | |
P(X<6) | |
P(X≤6) | |
… 5 6 7 …
🤔
Continuity correction
77
Discrete (e.g., Binomial) probability question | Continuous (Normal) probability question |
P(X=6) | |
P(X≥6) | |
P(X>6) | |
P(X<6) | |
P(X≤6) | |
… 5 6 7 …
What is P(CEO endorses change| it has no effect)?
Midterm: Website Testing
YOU ARE AMAZING!
Two Ways To Approximate The Binomial
Poisson approximation for big n, small p.
Normal approximation for big n, medium p.
Piech & Cain, CS109, Stanford University
n > 20
p < 0.05
n > 20
Var(X) > 10
Four Prototypical Trajectories
Just Invented the Normal Approximation
Stanford Admissions (a while back)
81
Strategy:
🤔
(by yourself)
Stanford Admissions
82
Strategy:
Define an approximation
Solve
Continuity�correction
⚠️
✅️
SciPy can do this
How many students should Stanford admit?
83
Admit rate: 4.3%
Yield rate: 81.9%
Why Be Normal? 68% rule
84
Why Be Normal? 68% rule
85
Four Prototypical Trajectories
Challenge
Enough Servers?
88
Four Prototypical Trajectories
Extra Content
Four Prototypical Trajectories
How does python sample from a Gaussian?
91
from random import *
for i in range(10):
mean = 5
std = 1
sample = gauss(mean, std)
print sample
3.79317794179
5.19104589315
4.209360629
5.39633891584
7.10044176511
6.72655475942
5.51485158841
4.94570606131
6.14724644482
4.73774184354
How does this work?
How Does a Computer Sample a Normal?
5
0
-5
1
CDF of the Standard Normal
How Does a Computer Sample a Normal?
Further reading: Box–Muller transform
Inverse Transform Sampling
5
0
-5
1
CDF of the Standard Normal
Step 1: pick a uniform number y
between 0,1
Step 2: Find the x such that
Sample 1:
1.201234
Sample 2:
-0.45123
Four Prototypical Trajectories
Relative values of a PDF
Relative Probability of Continuous Variables
x
f(x)
Time to finish pset 3
How much more likely are you to complete in 10 hours than in 5?
X = time to finish pset 3
X ~ N(μ = 10, σ2 = 2)
Four Prototypical Trajectories
Gaussian and ELO
Gaussian Sampling and ELO ratings
97
What is the probability that the Warriors win?
How do you model zero-sum games?
Gaussian Sampling and ELO ratings
98
Arpad Elo
Gaussian Sampling and ELO ratings
99
from scipy import stats
WARRIORS_ELO = 1657
OPPONENT_ELO = 1470
STDEV = 200
NTRIALS = 10000
nSuccess = 0
for i in range(NTRIALS):
w = stats.norm.rvs(WARRIORS_ELO, STDEV)
o = stats.norm.rvs(OPPONENT_ELO, STDEV)
if w > o:
nSuccess += 1
print("Warriors sampled win fraction: ", float(nSuccess) / NTRIALS)
Is there a better way?
100