1 of 112

Moments

Chris Gregg

CS109, Stanford University

Autumn 2026

2 of 112

  1. Use New Random Variables!
  2. Calculate Expectation of a Random Variable

​

Learning Goals

3 of 112

Four Prototypical Trajectories

Announcements

4 of 112

  • Pset1 is due today
  • Pset2 is out, due next week
  • Hopefully you enjoyed your first tutorial! All feedback is welcome, either through your tutorial TA, or on Ed, or via an email to me (cgregg@stanford.edu)
  • InLecture is going strong – we have a feedback form we would love for you to fill out! QR code:

5 of 112

Four Prototypical Trajectories

End Announcements

6 of 112

Four Prototypical Trajectories

Review

7 of 112

A random variable is a number which takes on values probabilistically.

A discrete random variable is fully described by a probability mass function.

8 of 112

For example Y is the number of heads in 5 coin flips

Let Y be a random variable

9 of 112

For example Y is the number of heads in 5 coin flips

It is an event when

Y takes on a value

*note: here equals means == in coding

Let Y be a random variable

10 of 112

For example Y is the number of heads in 5 coin flips

It is an event when

you ask any comparison question

Let Y be a random variable

11 of 112

Then this is a probability

(between 0 and 1)

For example Y is the number of heads in 5 coin flips

If this is a number

12 of 112

Then this is a function

For example Y is the number of heads in 5 coin flips

If this is a variable

13 of 112

For example Y is the number of heads in 5 coin flips

This is a function

14 of 112

Four Prototypical Trajectories

Random Variables are a big deal, because they allow other people to give you a PMF (and other helpful equations)

15 of 112

Four Prototypical Trajectories

Classics

16 of 112

17 of 112

Declare a Random Variable to be Binomial

Our random variable

Is distributed as a

Binomial

With these parameters

Num trials

Probability of success on each trial

18 of 112

Exactly k heads in n coin flips. Probability of exactly k heads:

18

19 of 112

Automatically Know the PMF

Probability that our variable takes on the value k

Probability Mass Function for a Binomial

* This is also called the binomial term

20 of 112

The PMF as a Graph: X ~ Bin(n = 20, p = 0.6)

20

21 of 112

Many Stories fit the Binomial

21

22 of 112

Four Prototypical Trajectories

End Review

23 of 112

Galton Board Time!

24 of 112

Galton Board Fun

Piech & Cain, CS109, Stanford University

25 of 112

Galton Board Fun

When a marble hits a pin, it has equal chance of going left or right.

Piech & Cain, CS109, Stanford University

26 of 112

Galton Board Fun

When a marble hits a pin, it has equal chance of going left or right.

Piech & Cain, CS109, Stanford University

27 of 112

Galton Board Fun

When a marble hits a pin, it has equal chance of going left or right.

Piech & Cain, CS109, Stanford University

28 of 112

Galton Board Fun

When a marble hits a pin, it has equal chance of going left or right.

Each pin represents an independent event.

Piech & Cain, CS109, Stanford University

29 of 112

Galton Board Fun

When a marble hits a pin, it has equal chance of going left or right.

Each pin represents an independent event.

Piech & Cain, CS109, Stanford University

30 of 112

Galton Board Fun

When a marble hits a pin, it has equal chance of going left or right.

Each pin represents an independent event.

Piech & Cain, CS109, Stanford University

31 of 112

Galton Board Fun

When a marble hits a pin, it has equal chance of going left or right.

Each pin represents an independent event.

Piech & Cain, CS109, Stanford University

32 of 112

Galton Board Fun

When a marble hits a pin, it has equal chance of going left or right.

Each pin represents an independent event.

Piech & Cain, CS109, Stanford University

33 of 112

Galton Board Fun

When a marble hits a pin, it has equal chance of going left or right.

Each pin represents an independent event.

Piech & Cain, CS109, Stanford University

34 of 112

Galton Board Fun

When a marble hits a pin, it has equal chance of going left or right.

Each pin represents an independent event.

Piech & Cain, CS109, Stanford University

35 of 112

Galton Board Fun

Which bucket a marble lands in corresponds to the number of times the marble went right.

0

1

2

3

4

5

When a marble hits a pin, it has equal chance of going left or right.

Each pin represents an independent event.

Piech & Cain, CS109, Stanford University

36 of 112

Galton Board Fun

We can define a random variable (B)

representing which bucket a marble lands in.

B ~ Bin(n = levels, p = 0.5)

0

1

2

3

4

5

Piech & Cain, CS109, Stanford University

37 of 112

Galton Board Fun

What is the probability of a marble landing in each bucket?

We can define a random variable (B)

representing which bucket a marble lands in.

B ~ Bin(n = levels, p = 0.5)

0

1

2

3

4

5

Piech & Cain, CS109, Stanford University

38 of 112

Galton Board Fun

What is the probability of a marble landing in each bucket?

We can define a random variable (B)

representing which bucket a marble lands in.

B ~ Bin(n = levels, p = 0.5)

0

1

2

3

4

5

Piech & Cain, CS109, Stanford University

39 of 112

Galton Board Fun

What is the probability of a marble landing in each bucket?

We can define a random variable (B)

representing which bucket a marble lands in.

B ~ Bin(n = levels, p = 0.5)

0

1

2

3

4

5

Piech & Cain, CS109, Stanford University

40 of 112

Galton Board Fun

This is the PMF of the binomial!

What is the probability of a marble landing in each bucket?

We can define a random variable (B)

representing which bucket a marble lands in.

0

1

2

3

4

5

Piech & Cain, CS109, Stanford University

41 of 112

42 of 112

Where are We in CS109?

42

You are here

43 of 112

Classic Random Variables (with PMFs)

X ~ Bern(p)

Y ~ Bin(n, p)

X ~ Geo(p)

Y ~ NegBin(r, p)

Successes in one trial

Successes in n trials

Trials until one success

Trials until r success

44 of 112

Goal: Be Able to Use a New Random Variable

You are learning about servers…

You read about the MD1 queue…

You find a paper that says the length of a server “busy period” is distributed as a Borel with parameter μ = 0.2 …

45 of 112

Geometric Random Variable

  • X is Geometric Random Variable: X ~ Geo(p)
    • X is number of independent trials until first success
    • p is probability of success on each trial
    • Assumes p does not change
    • X takes on values 1, 2, 3, …, with probability:

​

​

46 of 112

Geometric Random Variable

  • X is Geometric Random Variable: X ~ Geo(p)

​

​

Example: Let X be the event that a flipped coin lands on its side. Both heads and tails are failures, but if the coin lands on its side, it would be a success. p=0.00002

​

What is the probability that it takes 10,000 or more flips to get a coin to land on its side?

Uh-oh…

47 of 112

Geometric Random Variable

  • X is Geometric Random Variable: X ~ Geo(p)

​

​

We would have to sum to infinity for this calculation:

Uh-oh…

(not fun to simulate, but stand by…)

48 of 112

Geometric Random Variable

  • X is Geometric Random Variable: X ~ Geo(p)

​

​

Instead, we could calculate the complement:

49 of 112

Geometric Random Variable

  • X is Geometric Random Variable: X ~ Geo(p)

​

​

It turns out that this:

does converge to a closed-form:

(same result as we calculated before)

50 of 112

Negative Binomial Random Variable

  • X is Negative Binomial RV: X ~ NegBin(r, p)
    • X is number of independent trials until r successes
    • p is probability of success on each trial
    • Assumes p does not change.
    • X takes on value n with probability:

​

​

51 of 112

Classic Random Variables (with PMFs)

X ~ Bern(p)

Y ~ Bin(n, p)

X ~ Geo(p)

Y ~ NegBin(r, p)

Successes in n trials

Trials until one success

Trials until r success

52 of 112

​

  1. Recognize a classic random variable type

​

​

  1. Define a random variable to be that type, with parameters

​

​

  1. Profit off the PMF

Recipe For Solving Problems:

53 of 112

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?

54 of 112

Equity in the Courts

Berghuis v. Smith

If a group is underrepresented in a jury pool, how do you tell?

​

​

​

Justice Breyer [Stanford Alum] opened the questioning by invoking the binomial theorem.  He hypothesized a scenario involving “an urn with a thousand balls, and sixty are yellow, and nine hundred forty are navy-blue, and then you select them at random… twelve at a time.”  According to Justice Breyer and the binomial theorem, if the purple balls were under represented jurors then “you would expect… something like a third to a half of juries would have at least one yellow ball” on them. 

​

55 of 112

Equity in the Courts

​

  • Approximation using Binomial distribution
    • Assume P(blue ball) constant for every draw = 60/1000
    • X = # blue balls drawn. X ~ Bin(12, 60/1000 = 0.06)
    • P(X ≥ 1) = 1 – P(X = 0) ≈ 1 – 0.4759 = 0.5240

In Breyer’s description, should actually expect just over half of juries to have at least one non-white person on them

​

56 of 112

Bitcoin Mining

SHA-256 Hash( , )

Data

Fixed

Salt Choice

Number that looks like random bits

You “mine a bitcoin” if, for given data D, you find a salt number N such that Hash(D, N) produces a string that starts with g zeroes.

57 of 112

You “mine a bitcoin” if, for given data D, you find a number N such that Hash(D, N) produces a string that starts with g zeroes.

(a) What is the probability that Hash outputs a bit string which starts with g zeroes (in other words you mine a bitcoin)?

(b) What is the probability that you will need under 100 attempts to mine 2 bit coins?

Let Y be the number of tries until you mine 2 bitcoins.

Call this answer pa

Let X be the number of zeros in the first g bits.

58 of 112

Classic Random Variables (with PMFs)

X ~ Bern(p)

Y ~ Bin(n, p)

X ~ Geo(p)

Y ~ NegBin(r, p)

Successes in n trials

Trials until one success

Trials until r success

59 of 112

Can Jacob Bernoulli Have a Variable Named After Him?

Here yee. I want to have a random variable named after myself. Huzzah.

Piech & Cain, CS109, Stanford University

60 of 112

Can Jacob Bernoulli Have a Variable Named After Him?

Here yee. I want to have a random variable named after myself. Huzzah.

X ~ Bern(p)

Yes - the Bernoulli random variable:

​

Piech & Cain, CS109, Stanford University

61 of 112

Can Jacob Bernoulli Have a Variable Named After Him?

Here yee. I want to have a random variable named after myself. Huzzah.

X ~ Bern(p)

Yes - the Bernoulli random variable:

  • The Bernoulli is an indicator random variable (value is either 0 or 1).
  • P(X = 1) = p
  • P(X = 0) = 1 – p
  • Examples: a single coin flip, one ad click, any binary event

(this is the whole PMF)

Piech & Cain, CS109, Stanford University

62 of 112

Random Variable Sums

Tails

Heads

Heads

Heads

Tails

Heads

Tails

Tails

Heads

The Binomial

63 of 112

Random Variable Sums

Tails

Heads

Heads

Heads

Tails

Heads

Tails

Tails

Heads

The Binomial

…is a sum of Bernoulli random variables

64 of 112

Random Variable Sums

Tails

Heads

Heads

Heads

Tails

Heads

Tails

Tails

Heads

The Binomial

…is a sum of Bernoulli random variables

Let X1 ~ Bern(p = 1/2) and X2 ~ Bern(p = 1/2).

Y ~ Bin(n = 2, p = 1/2)

Y = X1 + X2

65 of 112

Random Variable Sums

Tails

Tails

Heads

Tails

Tails

Heads

Tails

Tails

Heads

The Negative Binomial

66 of 112

Random Variable Sums

Tails

Tails

Heads

Tails

Tails

Heads

Tails

Tails

Heads

The Negative Binomial

…is a sum of Geometric random variables

67 of 112

Random Variable Sums

Tails

Tails

Heads

Tails

Tails

Heads

Tails

Tails

Heads

The Negative Binomial

…is a sum of Geometric random variables

Let X1 ~ Geo(p = 1/3), X2 ~ Geo(p = 1/3), and X3 ~ Geo(p = 1/3).

Y ~ NegBin(r = 3, p = 1/3)

Y = X1 + X2 + X3

68 of 112

Classic Random Variables (with PMFs)

X ~ Bern(p)

Y ~ Bin(n, p)

X ~ Geo(p)

Y ~ NegBin(r, p)

Successes in one trial

Successes in n trials

Trials until one success

Trials until r success

69 of 112

Time for some moments*

*Moments: numbers that summarize different aspects of a random variable

70 of 112

Expectation

71 of 112

Expected Value

Loop over all values x that X can take on

The value

The probability of that value

72 of 112

Expected Value

​

​

  • Also called: Mean, Expectation, Weighted Average, Center of Mass, 1st Moment

​

72

  • Expected value answers the question:
  • What is the average value we could expect some random variable to be?

73 of 112

Example: Expected Value of Dice Roll

  • Let X be the result of rolling a 6-sided dice.
  • What is the expectation of X?

Piech & Cain, CS109, Stanford University

74 of 112

Example: Expected Value of Dice Roll

  • Let X be the result of rolling a 6-sided dice.
  • What is the expectation of X?

Piech & Cain, CS109, Stanford University

75 of 112

Example: Expected Value of Dice Roll

  • Let X be the result of rolling a 6-sided dice.
  • What is the expectation of X?

Piech & Cain, CS109, Stanford University

76 of 112

Example: Expected Value of Dice Roll

  • Let X be the result of rolling a 6-sided dice.
  • What is the expectation of X?
  • E[X] is not always an actual possible outcome for X

Piech & Cain, CS109, Stanford University

77 of 112

Lying With Statistics

Imagine a university has 3 classes, with 5, 10, and 150 students in each class.

We randomly choose a class with equal probability.

​

Let X be the chosen class’s size. What is E[X]?

​

​

Piech & Cain, CS109, Stanford University

78 of 112

Lying With Statistics

Imagine a university has 3 classes, with 5, 10, and 150 students in each class.

We randomly choose a class with equal probability.

​

Let X be the chosen class’s size. What is E[X]?

​

​

Piech & Cain, CS109, Stanford University

79 of 112

Lying With Statistics

Imagine a university has 3 classes, with 5, 10, and 150 students in each class.

We randomly choose a student with equal probability.

​

Let X be the chosen student’s class size. What is E[X]?

​

​

Piech & Cain, CS109, Stanford University

80 of 112

Lying With Statistics

Imagine a university has 3 classes, with 5, 10, and 150 students in each class.

We randomly choose a student with equal probability.

​

Let X be the chosen student’s class size. What is E[X]?

​

​

Piech & Cain, CS109, Stanford University

81 of 112

Expectation from Data

81

List called data

Length of data

82 of 112

Four Prototypical Trajectories

Expectation is a single number summary…

83 of 112

Four Prototypical Trajectories

Expectation leaves much to be desired…

84 of 112

Expectation vs PMF

  • Let X be the number of problems that a randomly selected student has completed, as of 11a today.
  • X takes on values, with uncertainty. X is a random variable.

84

PMF of X

85 of 112

Why People Care?

86 of 112

Properties of Expectation (proof later)

  • Linearity:

​

        • Consider X = 6-sided die roll, Winnings = 2X – 1.
        • E[X] = 3.5 E[2X-1] = 6

​

​

​

​

​

​

​

​

87 of 112

Properties of Expectation (proof later)

  • Linearity:

​

        • Consider X = 6-sided die roll, Winnings = 2X – 1.
        • E[X] = 3.5 E[2X-1] = 6

​

  • Expectation of a sum is the sum of expectations

​

​

​

​

​

​

88 of 112

Properties of Expectation (proof later)

  • Linearity:

​

        • Consider X = 6-sided die roll, Winnings = 2X – 1.
        • E[X] = 3.5 E[2X-1] = 6

​

  • Expectation of a sum is the sum of expectations

​

​

  • Unconscious statistician:

​

​

​

89 of 112

Law of the Unconscious Statistician (LOTUS)

Examples:

This lets you get the expectation of any function of a random variable.

Piech & Cain, CS109, Stanford University

90 of 112

Expectation of Classic Random Variables

91 of 112

Expected Value of Free Throws

  • In basketball, players sometimes get a chance to shoot a free throw. If they make it, the team gets 1 point; otherwise they get no points.
  • Some players are not very good at free throws, such as Shaq. While in the NBA, Shaq made only 53% of his free throws.

Let X be the points gained from Shaq attempting a free throw.

What is E[X]?

92 of 112

Expected Value of Free Throws

  • In basketball, players sometimes get a chance to shoot a free throw. If they make it, the team gets 1 point; otherwise they get no points.
  • Some players are not very good at free throws, such as Shaq. While in the NBA, Shaq made only 53% of his free throws.

Let X be the points gained from Shaq attempting a free throw.

What is E[X]?

  • For Bernoulli random variables, E[X] = p (always)

93 of 112

With Classic RVs, You Get Expectations For Free Too!

94 of 112

We Can Now Calculate Expectation of Binomial

X ~ Bin(n, p)

95 of 112

We Can Now Calculate Expectation of Binomial

X ~ Bin(n, p)

Let Yi be 1 if trial i was a success, otherwise 0, with i from 1 to n. Yi ~ Bern(p).

Tails

Heads

Heads

Heads

Tails

Heads

Tails

Tails

Heads

The Binomial

…is a sum of Bernoulli random variables

96 of 112

We Can Now Calculate Expectation of Binomial

X ~ Bin(n, p)

Let Yi be 1 if trial i was a success, otherwise 0, with i from 1 to n. Yi ~ Bern(p).

97 of 112

We Can Now Calculate Expectation of Binomial

Expectation of a sum is the sum of expectations:

X ~ Bin(n, p)

Let Yi be 1 if trial i was a success, otherwise 0, with i from 1 to n. Yi ~ Bern(p).

98 of 112

We Can Now Calculate Expectation of Binomial

  • True for every binomial ever

X ~ Bin(n, p)

Let Yi be 1 if trial i was a success, otherwise 0, with i from 1 to n. Yi ~ Bern(p).

99 of 112

You Get So Much For Free!

100 of 112

Expected Value of Free Throws

  • In basketball, players sometimes get a chance to shoot a free throw. If they make it, the team gets 1 point; otherwise they get no points.
  • Some players are not very good at free throws, such as Shaq. While in the NBA, Shaq made only 53% of his free throws.

Let Y be the points gained from Shaq attempting 500 free throws.

What is E[Y]?

101 of 112

Expected Value of Free Throws

  • In basketball, players sometimes get a chance to shoot a free throw. If they make it, the team gets 1 point; otherwise they get no points.
  • Some players are not very good at free throws, such as Shaq. While in the NBA, Shaq made only 53% of his free throws.

Let Y be the points gained from Shaq attempting 500 free throws.

What is E[Y]?

102 of 112

Expected Value of Free Throws

  • In basketball, players sometimes get a chance to shoot a free throw. If they make it, the team gets 1 point; otherwise they get no points.
  • Some players are not very good at free throws, such as Shaq. While in the NBA, Shaq made only 53% of his free throws.

Let Y be the points gained from Shaq attempting 500 free throws.

What is E[Y]?

103 of 112

Expected Value of Free Throws

  • In basketball, players sometimes get a chance to shoot a free throw. If they make it, the team gets 1 point; otherwise they get no points.
  • Some players are not very good at free throws, such as Shaq. While in the NBA, Shaq made only 53% of his free throws.

Let Y be the points gained from Shaq attempting 500 free throws.

What is E[Y]?

  • Challenge: If Shaq was 10% better at shooting free throws, how many more free throws would you expect him to make, out of 500?

104 of 112

Expected Value of The Geometric

If X ~ Geo(p), then

This definition has intuition built in:

  • If Shaq makes about half his free throws, then on average, it will take him two shots to make one free throw. E[X] = (1/2)-1 = 2.

105 of 112

Expected Value of The Geometric

If X ~ Geo(p), then

This definition has intuition built in:

  • If Shaq makes about half his free throws, then on average, it will take him two shots to make one free throw. E[X] = (1/2)-1 = 2.
  • Note: Expectation is often not the mode (the most likely outcome)

106 of 112

Expected Value of The Negative Binomial

We can derive using the sum of expectations property, similar to binomials.

Tails

Tails

Heads

Tails

Tails

Heads

Tails

Tails

Heads

The Negative Binomial

…is a sum of Geometric random variables

107 of 112

Expected Value of The Negative Binomial

We can derive using the sum of expectations property, similar to binomials.

Let Xi ~ Geo(p), for each i from 1 to r.

Let Y ~ NegBin(r, p).

108 of 112

Expected Value of The Negative Binomial

Let Xi ~ Geo(p), for each i from 1 to r.

Let Y ~ NegBin(r, p).

We can derive using the sum of expectations property, similar to binomials.

109 of 112

Expected Value of The Negative Binomial

Let Xi ~ Geo(p), for each i from 1 to r.

Let Y ~ NegBin(r, p).

We can derive using the sum of expectations property, similar to binomials.

110 of 112

Expected Value of The Negative Binomial

Let Xi ~ Geo(p), for each i from 1 to r.

Let Y ~ NegBin(r, p).

We can derive using the sum of expectations property, similar to binomials.

111 of 112

Expectations of Classic Random Variables

X ~ Geo(p)

Y ~ NegBin(r, p)

X ~ Bern(p)

Y ~ Bin(n, p)

112 of 112

Four Prototypical Trajectories

Expectation is easy to work with, but still leaves much to be desired