1 of 101

Continuous Variables

Chris Gregg

CS109, Stanford University

Autumn, 2026

2 of 101

Piech & Cain, CS109, Stanford University

3 of 101

1906 Earthquake

Magnitude 7.8

Piech & Cain, CS109, Stanford University

4 of 101

1906 Earthquake

Magnitude 7.8

How long until the next “big one”?

Piech & Cain, CS109, Stanford University

5 of 101

Four Prototypical Trajectories

Review

6 of 101

Binomial Random Variable

6

The number of successes, in n independent trials, where each trial is a success with probability p:

7 of 101

Poisson Random Variable

Probability of k requests from this area in the next 1 min

8 of 101

Poisson Random Variable

8

9 of 101

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

10 of 101

  1. Integrate a density function (PDF) to get a probability
  2. Use a cumulative function (CDF) to get a probability

Learning Goals

11 of 101

Four Prototypical Trajectories

Big hole in our knowledge

12 of 101

Not all values are discrete

13 of 101

Can’t Talk About Continuous Values

  • Say the average rate of earthquakes is 1 every 100 years.

​

  • We can talk about the probability distribution of different numbers of earthquakes next year.

​

  • We can’t talk about the probability distribution of the amount of time until the next earthquake.

13

14 of 101

Four Prototypical Trajectories

random()?

15 of 101

The random() Function

  • - Outputs values between 0 and 1
  • - All possible values are equally likely
  • - This is a continuous random variable!

16 of 101

X ~ Uniform(0,1): A Continuous Random Variable

17 of 101

X ~ Uniform(0,1): A Continuous Random Variable

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely

18 of 101

X ~ Uniform(0,1): A Continuous Random Variable

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • ?

19 of 101

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • 1
  • Probability of the whole sample space must equal 1
  • (Axiom 2)

X ~ Uniform(0,1): A Continuous Random Variable

20 of 101

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • 1
  • ?

X ~ Uniform(0,1): A Continuous Random Variable

21 of 101

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • 1
  • 0.5
  • Half of all possible outcomes are between 0.5 and 1

X ~ Uniform(0,1): A Continuous Random Variable

22 of 101

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • 1
  • 0.5
  • ?

X ~ Uniform(0,1): A Continuous Random Variable

23 of 101

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • 1
  • 0.5
  • 0.1
  • 1/10 of all possible outcomes are between 0.5 and 0.6

​

So far, the pattern looks like:

P(start ≤ X ≤ end) = end - start

X ~ Uniform(0,1): A Continuous Random Variable

24 of 101

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • 1
  • 0.5
  • 0.1
  • As we get more precise, probabilities keep shrinking…
  • 0.0001

X ~ Uniform(0,1): A Continuous Random Variable

25 of 101

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • 1
  • 0.5
  • 0.1
  • 0.0001
  • ?

X ~ Uniform(0,1): A Continuous Random Variable

26 of 101

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • 1
  • 0.5
  • 0.1
  • 0.0001
  • 0
  • The probability of any exact outcome, with infinite precision…is zero

X ~ Uniform(0,1): A Continuous Random Variable

27 of 101

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • 1
  • 0.5
  • 0.1
  • 0.0001
  • 0
  • The probability of any exact outcome, with infinite precision…is zero

The probability of any continuous random variable being exactly equal to any value is 0.

​

​

X ~ Uniform(0,1): A Continuous Random Variable

28 of 101

  • How likely?

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • 1
  • 0.5
  • 0.1
  • 0.0001
  • 0
  • The probability of any exact outcome, with infinite precision…is zero

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

29 of 101

  • How likely?
  • 0
  • The probability of any exact outcome, with infinite precision…is zero

0

1

x

  • Possible values are between 0 and 1
  • All values are equally likely
  • 1
  • 0.5
  • 0.1
  • 0.0001

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

30 of 101

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

31 of 101

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

32 of 101

X ~ Uniform(⍺, β): More General Case

  • How likely?

⍺=5

β=7

x

  • Possible values are now between ⍺ and β
  • All values are equally likely

33 of 101

  • How likely?

⍺=5

β=7

x

  • Possible values are now between ⍺ and β
  • All values are equally likely

?

X ~ Uniform(⍺, β): More General Case

34 of 101

  • How likely?

⍺=5

β=7

x

  • Possible values are now between ⍺ and β
  • All values are equally likely

X ~ Uniform(⍺, β): More General Case

35 of 101

  • How likely?

⍺=5

β=7

x

  • Possible values are now between ⍺ and β
  • All values are equally likely

?

X ~ Uniform(⍺, β): More General Case

36 of 101

  • How likely?

⍺=5

β=7

x

  • Possible values are now between ⍺ and β
  • All values are equally likely

X ~ Uniform(⍺, β): More General Case

37 of 101

  • How likely?

⍺=5

β=7

x

  • Possible values are now between ⍺ and β
  • All values are equally likely
  • Does that still work?

For Uniform(0,1):

P(start ≤ X ≤ end) = end - start

start

end

X ~ Uniform(⍺, β): More General Case

38 of 101

  • How likely?

⍺=5

β=7

x

  • Possible values are now between ⍺ and β
  • All values are equally likely

For Uniform(0,1):

P(start ≤ X ≤ end) = end - start

  • Does that still work? No!
  • Need to divide by 2?

start

end

X ~ Uniform(⍺, β): More General Case

39 of 101

  • How likely?

⍺=5

β=7

x

  • Possible values are now between ⍺ and β
  • All values are equally likely

For Uniform(⍺, β):

  • 1/2

P(start ≤ X ≤ end) =

end - start

β - ⍺

start

end

X ~ Uniform(⍺, β): More General Case

40 of 101

  • How likely?

⍺=5

β=7

x

  • Possible values are now between ⍺ and β
  • All values are equally likely

For Uniform(⍺, β):

  • If we set y = 1/2 between ⍺ and β, then probabilities are “slices of the whole box”
  • 1/2

P(start ≤ X ≤ end) =

end - start

β - ⍺

  • Area = 0.05

start

end

X ~ Uniform(⍺, β): More General Case

41 of 101

Uniform Random Variable

  •  

41

Support: [⍺, β]

Piech, CS109, Stanford University

42 of 101

Four Prototypical Trajectories

Can we generalize to other continuous random variables?

Piech, CS109, Stanford University

43 of 101

Riding the Marguerite

44 of 101

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)?

45 of 101

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.

46 of 101

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.

47 of 101

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:

​

  • Time is now a continuous variable

​

  • The probability mass function (PMF) becomes a derivative called a probability density function (PDF)

​

  • Probability are now calculated as area under the curve

48 of 101

Why is it a derivative

48

49 of 101

Time For Integrals!!!!

Piech & Cain, CS109, Stanford University

50 of 101

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!

51 of 101

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!

52 of 101

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!

53 of 101

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!

54 of 101

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.

55 of 101

A probability!

What do you get if you integrate over a

probability density function?

56 of 101

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

57 of 101

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.

58 of 101

The relative value of probability densities is meaningful

59 of 101

Truths of Probability For Continuous Random Variables

Truth 1:

Area under the curve!

60 of 101

Truth 2:

Truths of Probability For Continuous Random Variables

Truth 1:

Since the integral is a probability (Axiom 1)

⍺ = 0

β = 0.5

x

  • PDF
  • 2

Can a PDF ever have a value > 1?

Yes!

Area under the curve!

61 of 101

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!

62 of 101

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

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!

64 of 101

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?

65 of 101

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

66 of 101

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?

67 of 101

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

68 of 101

A probability!

What do you get if you integrate over a

probability density function?

69 of 101

Four Prototypical Trajectories

You are ready for the classic continuous random variables

70 of 101

It’s Time

To Talk About Time, Again

71 of 101

1906 Earthquake

Magnitude 7.8

How long until the next “big one”?

72 of 101

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

73 of 101

Exponential Random Variable

For any Poisson Process, the Exponential RV models time until an event:

 

 

PDF:

Examples:

  • Time until next earthquake
  • Time until a ping reaches a web server
  • Time until next Uber request

Piech & Cain, CS109, Stanford University

Piech, CS109, Stanford University

74 of 101

Exponential Random Variable

For any Poisson Process, the Exponential RV models time until an event:

 

 

PDF:

 

 

Examples:

  • Time until next earthquake
  • Time until a ping reaches a web server
  • Time until a Uranium atom decays

like “exponential decay”

Piech & Cain, CS109, Stanford University

Piech, CS109, Stanford University

75 of 101

 

76 of 101

1906 Earthquake

Magnitude 7.8

Piech, CS109, Stanford University

77 of 101

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

78 of 101

Integral Review

Piech, CS109, Stanford University

79 of 101

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

80 of 101

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

81 of 101

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

82 of 101

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

83 of 101

Four Prototypical Trajectories

Is there a way to avoid integrals?

Piech, CS109, Stanford University

84 of 101

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

85 of 101

Cumulative Density Function

Piech, CS109, Stanford University

86 of 101

CDF of an Exponential

Piech, CS109, Stanford University

87 of 101

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

Probability density function

Cumulative density function

x

x

Piech, CS109, Stanford University

88 of 101

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

Probability density function

Cumulative density function

x

x

P(X < 2)

Piech, CS109, Stanford University

89 of 101

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

Probability density function

Cumulative density function

f(x)

x

x

P(X < 2)

Piech, CS109, Stanford University

90 of 101

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

91 of 101

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

Probability density function

Cumulative density function

x

x

P(X > 1)

Piech, CS109, Stanford University

92 of 101

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

Probability density function

Cumulative density function

x

x

P(X > 1)

Piech, CS109, Stanford University

93 of 101

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

Probability density function

Cumulative density function

x

x

P(X > 1)

or

Piech, CS109, Stanford University

94 of 101

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

Probability density function

Cumulative density function

x

x

P(1 < X < 2)

Piech, CS109, Stanford University

95 of 101

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

Probability density function

Cumulative density function

x

x

P(1 < X < 2)

Piech, CS109, Stanford University

96 of 101

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

97 of 101

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

98 of 101

Two Classic Random Variables

Piech, CS109, Stanford University

99 of 101

Funnest Fact: Exponential is Memoryless!

What if s time has passed?

Piech, CS109, Stanford University

100 of 101

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

101 of 101

Funnest Fact: Exponential is Memoryless!

What if s time has passed?

Piech & Cain, CS109, Stanford University

Piech, CS109, Stanford University