1 of 100

Normal Distribution

Chris Gregg

CS109, Stanford University

Summer, 2026

2 of 100

Enough Servers?

  • You are running a massive website. On the busiest minute you receive:
  • Average of 106 requests
  • Variance of 104 requests.
  • You are going to buy n servers
  • Each server can handle 10,000 requests per min, otherwise you drop requests
  • What is the smallest value of n such that P(drop) < 0.0001

3 of 100

Four Prototypical Trajectories

Announcements

4 of 100

Announcements

  • Problem Set 3 is going out today!
  • Our first midterm is next week. We will have review
  • Please fill out the form for OAE accommodations ASAP if you have them!

5 of 100

Four Prototypical Trajectories

Review

6 of 100

Review: Probability Density Function

Recall the definition of a derivative:

7 of 100

A probability!

What do you get if you integrate over a

probability density function?

8 of 100

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!

9 of 100

Uniform and Exponential Distributions

https://chrispiech.github.io/probabilityForComputerScientists/en/part2/uniform/

10 of 100

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!

11 of 100

Using CDF Example. X is Exp(λ = 1)

Probability density function

Cumulative density function

f(x)

x

x

P(1 < X < 2)

or

12 of 100

Did you know? Exponential is Memoryless!

12

X = time until the next event

What if s time has passed?

13 of 100

Did you know? Exponential is Memoryless!

13

X = time until the next event

What if s time has passed?

14 of 100

Did you know? Exponential is Memoryless!

14

X = time until the next event

What if s time has passed?

15 of 100

I am going to use these two properties later in class today

15

16 of 100

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

17 of 100

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

18 of 100

Four Prototypical Trajectories

/Review

19 of 100

Four Prototypical Trajectories

Big Day

20 of 100

NormCore: A Few Normal Examples

20

21 of 100

Normal Random Variable

  •  

21

 

 

 

Variance

Expectation

PDF

 

 

 

mean

variance

 

 

22 of 100

Carl Friedrich Gauss

  • Carl Friedrich Gauss (1777-1855) was a remarkably influential�German mathematician.�������
  • �Did not invent Normal distribution but rather popularized it

22

23 of 100

Why the Normal?

  • Common for natural phenomena: height, weight, etc.�
  • Most noise in the world is Normal�
  • Often results from the sum of many random variables�
  • Sample means are distributed normally

23

These are log-normal

Only if they are equally weighted and independent

Most noise is assumed normal

That is actually true…

24 of 100

“The simplest explanation is usually the best one”

25 of 100

Complexity is Tempting

25

value

probability

Probably over fitting…

* That describes the training data, but will it generalize?

26 of 100

Fewest Assumptions

26

value

probability

μ

σ2

* A Gaussian maximizes entropy for a given mean and variance

Simple. Will generalize

27 of 100

Fewest Assumptions

27

value

probability

μ

σ2

* A Gaussian makes the fewest assumptions after matching mean and variance.

Simple. Will generalize

28 of 100

Four Prototypical Trajectories

Normal is Beautiful!

29 of 100

Normal Probability Density Function

29

x

f(x)

30 of 100

Anatomy of a Beautiful Equation

30

probability density at x

the distance to the mean

a constant

“exponential”

sigma shows up twice

31 of 100

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!

32 of 100

Four Prototypical Trajectories

Lets go!

33 of 100

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:

  • Average panel thickness: 𝜇=500 microns
  • Variance of the thickness: 𝜎2=36 microns

34 of 100

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:

  • Average panel thickness: 𝜇=500 microns
  • Variance of the thickness: 𝜎2=36 microns

35 of 100

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:

  • Average panel thickness: 𝜇=500 microns
  • Variance of the thickness: 𝜎2=36 microns

36 of 100

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:

  • Average panel thickness: 𝜇=500 microns
  • Variance of the thickness: 𝜎2=36 microns

37 of 100

Submarine Manufacturing

37

37

 

  • Loving, not scary
  • …except this time

38 of 100

Four Prototypical Trajectories

No closed form for the integral

39 of 100

39

Four Prototypical Trajectories

No closed form for F(x)

40 of 100

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

41 of 100

Linear Transform of a Normal is… Normal!

41

42 of 100

Linear Transform of a Normal is… Normal!

Derivation:

42

43 of 100

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

44 of 100

Linear Transform of a Normal is… Normal!

45 of 100

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!

46 of 100

The Standard Normal

*This is the probability density function for the standard normal

Mean (μ) = 0

Variance (σ2) = 1

47 of 100

Phi

*This is the probability density function for the standard normal

48 of 100

Φ(1.31) = 0.7054

Using Table of ɸ

49 of 100

Symmetry of Phi

a

-a

μ = 0

*This is the probability density function for the standard normal

50 of 100

Symmetry of Phi

a

-a

μ = 0

*This is the probability density function for the standard normal

51 of 100

Interval of Phi

d

c

ɸ(c)

ɸ(d

μ = 0

52 of 100

Use Z to compute F(x)

Compute F(x) via Transform

53 of 100

For normal distribution, F(x) is computed using the phi transform.

54 of 100

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

55 of 100

Using the Phi Table

Φ(0.54) = 0.7054

56 of 100

Do We Have To Use The Table??

57 of 100

Four Prototypical Trajectories

Table is kinda old school

58 of 100

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

59 of 100

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

60 of 100

I Made One For You

61 of 100

Practice: Submarine Manufacturing

 

 

What fraction of the panels you manufacture will meet standards?

 

62 of 100

 

 

 

= ?

 

Practice: Submarine Manufacturing

What fraction of the panels you manufacture will meet standards?

 

Now using the CDF!

63 of 100

 

 

 

subtract mean, divide by std. dev.

 

Practice: Submarine Manufacturing

What fraction of the panels you manufacture will meet standards?

 

Now using the CDF!

64 of 100

Practice: Submarine Manufacturing

 

 

 

Now using the CDF!

 

What fraction of the panels you manufacture will meet standards?

 

65 of 100

Get your Gaussian On

  •  

 

Piech & Cain, CS109, Stanford University

66 of 100

Four Prototypical Trajectories

Are you ready for something different?

67 of 100

  • A new website design is tested out on 1M users.
    • Let X be the number of users whose time on the site increases with the new design.
    • The CEO will endorse the new design if X ≥ 501k.

What is P(CEO endorses change| it has no effect)?

Midterm: Website Testing

68 of 100

  • A new website design is tested out on 1M users.
    • Let X be the number of users whose time on the site increases with the new design.
    • The CEO will endorse the new design if X ≥ 501k.

What is P(CEO endorses change| it has no effect)?

Midterm: Website Testing

69 of 100

  • A new website design is tested out on 1M users.
    • Let X be the number of users whose time on the site increases with the new design.
    • The CEO will endorse the new design if X ≥ 501k.

What is P(CEO endorses change| it has no effect)?

Midterm: Website Testing

>>> math.comb(1000000,501000)

70 of 100

  • A new website design is tested out on 1M users.
    • Let X be the number of users whose time on the site increases with the new design.
    • The CEO will endorse the new design if X ≥ 501k.

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 of 100

71

72 of 100

Don’t worry, Normal approximates Binomial

  • Galton Board

  • (We’ll explain why�in 2 weeks’ time)

72

73 of 100

Poisson Approximates Binomial, With Extreme n and p

P(X = k)

k

Piech & Cain, CS109, Stanford University

74 of 100

  • A new website design is tested out on 1M users.
    • Let X be the number of users whose time on the site increases with the new design.
    • The CEO will endorse the new design if X ≥ 501k.

What is P(CEO endorses change| it has no effect)?

Midterm: Website Testing

🤨

Correct answer is 0.02270

75 of 100

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.

76 of 100

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 …

🤔

77 of 100

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 …

78 of 100

  • A new website design is tested out on 1M users.
    • Let X be the number of users whose time on the site increases with the new design.
    • The CEO will endorse the new design if X ≥ 501k.

What is P(CEO endorses change| it has no effect)?

Midterm: Website Testing

YOU ARE AMAZING!

79 of 100

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

80 of 100

Four Prototypical Trajectories

Just Invented the Normal Approximation

81 of 100

Stanford Admissions (a while back)

  •  

81

Strategy:

🤔

(by yourself)

  1. Just Binomial
  2. Poisson
  3. Normal
  4. None/other

82 of 100

Stanford Admissions

  •  

82

Strategy:

  1. Just Binomial
  2. Poisson
  3. Normal
  4. None/other

 

 

Define an approximation

Solve

 

 

Continuity�correction

⚠️

 

 

 

✅️

SciPy can do this

83 of 100

How many students should Stanford admit?

83

Admit rate: 4.3%

Yield rate: 81.9%

84 of 100

Why Be Normal? 68% rule

  • You may have heard the statement:
  • “68% of the class will fall within 1 standard deviation of the exam average.”
  • In general, this is only true of normal distributions:

84

 

 

 

 

 

 

 

 

 

85 of 100

Why Be Normal? 68% rule

  • You may have heard the statement:
  • “68% of the class will fall within 1 standard deviation of the exam average.”
  • In general, this is only true of normal distributions:

85

 

 

 

 

 

 

 

 

 

 

 

 

 

86 of 100

Four Prototypical Trajectories

Challenge

87 of 100

88 of 100

Enough Servers?

  • You receive R ~ N(10^6, 10^4) requests in the busiest min
  • You are going to buy n servers
  • Each server can handle 10,000 requests per min, otherwise you drop requests
  • What is the smallest value of n such that P(drop) < 0.0001

88

89 of 100

Four Prototypical Trajectories

Extra Content

90 of 100

Four Prototypical Trajectories

How does python sample from a Gaussian?

91 of 100

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?

92 of 100

How Does a Computer Sample a Normal?

5

0

-5

1

CDF of the Standard Normal

93 of 100

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

94 of 100

Four Prototypical Trajectories

Relative values of a PDF

95 of 100

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)

96 of 100

Four Prototypical Trajectories

Gaussian and ELO

97 of 100

Gaussian Sampling and ELO ratings

97

What is the probability that the Warriors win?

How do you model zero-sum games?

98 of 100

Gaussian Sampling and ELO ratings

  •  

98

Arpad Elo

 

 

99 of 100

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)

 

 

100 of 100

Is there a better way?

  •  

100