Continuous Variables
Chris Gregg
CS109, Stanford University
Autumn, 2026
Piech & Cain, CS109, Stanford University
1906 Earthquake
Magnitude 7.8
Piech & Cain, CS109, Stanford University
1906 Earthquake
Magnitude 7.8
How long until the next “big one”?
Piech & Cain, CS109, Stanford University
Four Prototypical Trajectories
Review
Binomial Random Variable
6
The number of successes, in n independent trials, where each trial is a success with probability p:
Poisson Random Variable
Probability of k requests from this area in the next 1 min
Poisson Random Variable
8
Discrete Random Variables
X ~ Bern(p)
X ~ Bin(n, p)
X ~ Geo(p)
X ~ NegBin(r, p)
Successes in one trial
Successes in n trials
Trials until one success
Trials until r success
X ~ Poi(λ)
Events in one time interval
Learning Goals
Four Prototypical Trajectories
Big hole in our knowledge
Not all values are discrete
Can’t Talk About Continuous Values
13
Four Prototypical Trajectories
random()?
The random() Function
X ~ Uniform(0,1): A Continuous Random Variable
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
0
1
x
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
So far, the pattern looks like:
P(start ≤ X ≤ end) = end - start
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
The probability of any continuous random variable being exactly equal to any value is 0.
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
The probability of any continuous random variable being exactly equal to any value is 0.
No PMFs!
X ~ Uniform(0,1): A Continuous Random Variable
0
1
x
The only way to talk about probabilities of outcomes for continuous random variables is using ranges of possible values.
X ~ Uniform(0,1): A Continuous Random Variable
Curse of Dimensionality
30
Xi is close to an edge if Xi is less than 0.01 or Xi is greater than 0.99. What is the probability that Xi is close to an edge?
A random point of dimension d is a list of d random values: [X1…Xd]
Xi ~ Uni(0, 1) for all i
Each value Xi is independent of other values
Curse of Dimensionality
31
A random point [X1…X100] of dimension 100 is close to an edge if any of its values are close to an edge. What is the probability that a 100 dimensional point is close to an edge?
A random point of dimension d is a list of d random values: [X1…Xd]
Xi ~ Uni(0, 1) for all i
Each value Xi is independent of other values
X ~ Uniform(⍺, β): More General Case
⍺=5
β=7
x
⍺=5
β=7
x
?
X ~ Uniform(⍺, β): More General Case
⍺=5
β=7
x
X ~ Uniform(⍺, β): More General Case
⍺=5
β=7
x
?
X ~ Uniform(⍺, β): More General Case
⍺=5
β=7
x
X ~ Uniform(⍺, β): More General Case
⍺=5
β=7
x
For Uniform(0,1):
P(start ≤ X ≤ end) = end - start
start
end
X ~ Uniform(⍺, β): More General Case
⍺=5
β=7
x
For Uniform(0,1):
P(start ≤ X ≤ end) = end - start
start
end
X ~ Uniform(⍺, β): More General Case
⍺=5
β=7
x
For Uniform(⍺, β):
P(start ≤ X ≤ end) =
end - start
β - ⍺
start
end
X ~ Uniform(⍺, β): More General Case
⍺=5
β=7
x
For Uniform(⍺, β):
P(start ≤ X ≤ end) =
end - start
β - ⍺
start
end
X ~ Uniform(⍺, β): More General Case
Uniform Random Variable
41
Support: [⍺, β]
Piech, CS109, Stanford University
Four Prototypical Trajectories
Can we generalize to other continuous random variables?
Piech, CS109, Stanford University
Riding the Marguerite
Riding the Margueritte
You’re running to the bus stop. You don’t know exactly when the bus arrives.
You have a probability distribution for bus arrival times -- some times are more likely than others.
You show up at 2:15pm. What is P(wait < 5 min)?
Riding the Margueritte
You’re running to the bus stop. You don’t know exactly when the bus arrives.
You have a probability distribution for bus arrival times -- some times are more likely than others.
You show up at 2:15pm. What is P(wait < 5 min)?
2:00pm
20
30
wait <5 min
Bus arrives T mins after 2:00pm
P(T=t)
10
If time was discrete: a PMF could look like this.
Riding the Margueritte
You’re running to the bus stop. You don’t know exactly when the bus arrives.
You have a probability distribution for bus arrival times -- some times are more likely than others.
You show up at 2:15pm. What is P(wait < 5 min)?
2:00pm
20
30
wait <5 min
Bus arrives T mins after 2:00pm
P(T=t)
10
If time was discrete: a PMF could look like this.
Riding the Margueritte
You’re running to the bus stop. You don’t know exactly when the bus arrives.
You have a probability distribution for bus arrival times -- some times are more likely than others.
You show up at 2:15pm. What is P(wait < 5 min)?
2:00pm
20
30
wait <5 min
Bus arrives T mins after 2:00pm
f(T=t)
10
P(15 < T ≤ 20)
When interval sizes tend towards 0:
Why is it a derivative
48
Time For Integrals!!!!
Piech & Cain, CS109, Stanford University
Probability Density Function
The probability density function (PDF) of a continuous random variable represents the relative likelihood of various values.
Units of probability divided by units of X. Integrate it to get probabilities!
Probability Density Function
This is another way to write the PDF
The probability density function (PDF) of a continuous random variable represents the relative likelihood of various values.
Units of probability divided by units of X. Integrate it to get probabilities!
Probability Density Function
This is another way to write the PDF
The probability density function (PDF) of a continuous random variable represents the relative likelihood of various values.
Units of probability divided by units of X. Integrate it to get probabilities!
Probability Density Function
The probability density function (PDF) of a continuous random variable represents the relative likelihood of various values.
Units of probability divided by units of X. Integrate it to get probabilities!
PDFs like f(X = x) vs. PMFs like P(X = x)
“The probability that a discrete random variable X takes on the value x.”
“The derivative of the probability that a continuous random variable X takes at the value x.”
They are both measures of how likely X is to take on the value x.
Sometimes called the distribution function.
A probability!
What do you get if you integrate over a
probability density function?
The Relative Values of PDFs Are Meaningful
Probability density functions are derivatives that articulate relative belief.
Let X be the # of minutes after 2pm that the bus arrives at a stop.
x
x
x
Which of these represent that the bus’s arrival
is more likely to be close to 3:00pm?
0
60
0
60
0
60
A
B
C
The Relative Values of PDFs Are Meaningful
x
x
x
Which of these represent that the bus’s arrival
is more likely to be close to 3:00pm?
0
60
0
60
0
60
A
B
C
Piech & Cain, CS109, Stanford University
Probability density functions are derivatives that articulate relative belief.
Let X be the # of minutes after 2pm that the bus arrives at a stop.
The relative value of probability densities is meaningful
Truths of Probability For Continuous Random Variables
Truth 1:
Area under the curve!
Truth 2:
Truths of Probability For Continuous Random Variables
Truth 1:
Since the integral is a probability (Axiom 1)
⍺ = 0
β = 0.5
x
Can a PDF ever have a value > 1?
Yes!
Area under the curve!
Truth 2:
Truth 3:
Truths of Probability For Continuous Random Variables
Truth 1:
That’s all possible values (Axiom 2)
Since the integral is a probability (Axiom 1)
Area under the curve!
Truth 2:
Truth 3:
Truths of Probability For Continuous Random Variables
Truth 1:
Truth 4:
Know why!
That’s all possible values (Axiom 2)
Since the integral is a probability (Axiom 1)
Area under the curve!
63
Solve for K
Truth 2:
Truth 3:
Truth 1:
Truth 4:
Know why!
That’s all possible values (Axiom 2)
Since the integral is a probability (Axiom 1)
Area under the curve!
PDFs Need an Integral
2:00pm
20
30
wait <5 min
Bus arrives T mins after 2:00pm
f(T=t)
10
P(15 < T ≤ 20)
What is the probability that the bus arrives at: 12.12332343234 mins after 2pm?
PDFs Need an Integral
2:00pm
20
30
wait <5 min
Bus arrives T mins after 2:00pm
f(T=t)
10
P(15 < T ≤ 20)
What is the probability that the bus arrives at: 12.12332343234 mins after 2pm?
0
PDFs Need an Integral
2:00pm
20
30
wait <5 min
Bus arrives T mins after 2:00pm
f(T=t)
10
P(15 < T ≤ 20)
What is the probability that the bus arrives at: 12 mins after 2pm?
PDFs Need an Integral
2:00pm
20
30
wait <5 min
Bus arrives T mins after 2:00pm
f(T=t)
10
P(15 < T ≤ 20)
What is the probability that the bus arrives at: 12 mins after 2pm?
0
A probability!
What do you get if you integrate over a
probability density function?
Four Prototypical Trajectories
You are ready for the classic continuous random variables
It’s Time
To Talk About Time, Again
1906 Earthquake
Magnitude 7.8
How long until the next “big one”?
Exponential Random Variable
For any Poisson Process, the Exponential RV models time until an event:
PDF:
Piech & Cain, CS109, Stanford University
Piech, CS109, Stanford University
Exponential Random Variable
For any Poisson Process, the Exponential RV models time until an event:
PDF:
Examples:
Piech & Cain, CS109, Stanford University
Piech, CS109, Stanford University
Exponential Random Variable
For any Poisson Process, the Exponential RV models time until an event:
PDF:
Examples:
like “exponential decay”
Piech & Cain, CS109, Stanford University
Piech, CS109, Stanford University
1906 Earthquake
Magnitude 7.8
Piech, CS109, Stanford University
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
Integral Review
Piech, CS109, Stanford University
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
Based on historical data, major earthquakes (magnitude 8.0+) happen at a rate of 0.002 per year*. What is the expected number of years until the next earthquake?
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
Based on historical data, major earthquakes (magnitude 8.0+) happen at a rate of 0.002 per year*. What is the standard deviation of years until the next earthquake?
Y = Years until the next earthquake of magnitude 8.0+
*In California, according to the USGS, 2015
Piech, CS109, Stanford University
How Many Earthquakes
Based on historical data, major earthquakes (magnitude 8.0+) happen at a rate of 0.002 per year*. What is the probability of zero major earthquakes magnitude next year?
X = Number of major earthquakes next year
*In California, according to the USGS, 2015
Piech, CS109, Stanford University
Four Prototypical Trajectories
Is there a way to avoid integrals?
Piech, CS109, Stanford University
Cumulative Density Function
A cumulative density function (CDF) is a “closed form” equation for the probability that a random variable is less than a given value
This is also shorthand notation for the PMF
If you learn how to use a cumulative density function, you can avoid integrals!
Piech, CS109, Stanford University
Cumulative Density Function
Piech, CS109, Stanford University
CDF of an Exponential
Piech, CS109, Stanford University
Using CDF Example. X is Exp(λ = 1)
Probability density function
Cumulative density function
x
x
Piech, CS109, Stanford University
Using CDF Example. X is Exp(λ = 1)
Probability density function
Cumulative density function
x
x
P(X < 2)
Piech, CS109, Stanford University
Using CDF Example. X is Exp(λ = 1)
Probability density function
Cumulative density function
f(x)
x
x
P(X < 2)
Piech, CS109, Stanford University
Using CDF Example. X is Exp(λ = 1)
Probability density function
Cumulative density function
f(x)
x
x
P(X < 2)
or
Piech, CS109, Stanford University
Using CDF Example. X is Exp(λ = 1)
Probability density function
Cumulative density function
x
x
P(X > 1)
Piech, CS109, Stanford University
Using CDF Example. X is Exp(λ = 1)
Probability density function
Cumulative density function
x
x
P(X > 1)
Piech, CS109, Stanford University
Using CDF Example. X is Exp(λ = 1)
Probability density function
Cumulative density function
x
x
P(X > 1)
or
Piech, CS109, Stanford University
Using CDF Example. X is Exp(λ = 1)
Probability density function
Cumulative density function
x
x
P(1 < X < 2)
Piech, CS109, Stanford University
Using CDF Example. X is Exp(λ = 1)
Probability density function
Cumulative density function
x
x
P(1 < X < 2)
Piech, CS109, Stanford University
Using CDF Example. X is Exp(λ = 1)
Probability density function
Cumulative density function
f(x)
x
x
P(1 < X < 2)
or
Piech, CS109, Stanford University
Probability of Earthquake in Next 4 Years?
Based on historical data, earthquakes of magnitude 8.0+ happen at a rate of 0.002 per year*. What is the probability of
a major earthquake in the next 4 years?
Y = Years until the next earthquake of magnitude 8.0+
*According to USGS, 2015
Piech, CS109, Stanford University
Two Classic Random Variables
Piech, CS109, Stanford University
Funnest Fact: Exponential is Memoryless!
What if s time has passed?
Piech, CS109, Stanford University
Funnest Fact: Exponential is Memoryless!
Piech & Cain, CS109, Stanford University
s=5 years have passed
t=3 years after this
i.e., P(X>8 | X > 5)
Show that this is the same as P(X>3)
Piech, CS109, Stanford University
Funnest Fact: Exponential is Memoryless!
What if s time has passed?
Piech & Cain, CS109, Stanford University
Piech, CS109, Stanford University