1 of 137

Binomial

CS109

Summer 2026

2 of 137

Review

3 of 137

How many different ways can you arrange three square magnatiles in a row when you have 1 red, 1 blue, and 1 green square?

Counting

4 of 137

How many different ways can you arrange three square magnatiles in a row when you have 1 red, 1 blue, and 1 green square?

Counting

5 of 137

How many different ways can you arrange nine square magnatiles in a row when you have 2 red, 3 blue, and 4 green squares?

Counting

6 of 137

How many different ways can you arrange nine square magnatiles in a row when you have 2 red, 3 blue, and 4 green squares?

Counting

7 of 137

What is the probability of getting a flush in a 5-card poker hand? A flush is all five cards of the same suit (clubs, diamonds, hearts, or spades).

Counting

8 of 137

What is the probability of getting a flush in a 5-card poker hand? A flush is all five cards of the same suit (clubs, diamonds, hearts, or spades).

For the number of events that make a flush:

  1. Choose the suit: 4 choices
  2. Choose 5 cards from the suit:
  3. Multiply:

Sample space:

Divide:

Counting

9 of 137

Recall from last time that P(straight)=0.00394.

It may be unintuitive, but getting a flush is almost twice as unlikely as getting a straight: flushes are restritive in suit (only 4), and straights are flexible in suit.

You might think, “any five hearts…that seems easy.” But, you only get to pick from 13 total cards. For a straight, the ranks are fixed, but each rank can be chosen in any of 4 suits.

Counting

10 of 137

There are n animals.

How many distinct pairs of animals are there?

11 of 137

There are n animals.

How many distinct pairs of animals are there?

This is just

12 of 137

End Review

13 of 137

  • Say the population of Stanford is 17,000 people
    • You are friends with 80 people.
    • Walk into a room, see 450 random people. Independently.
    • What is the probability that you see someone you know?
    • Assume you are equally likely to see each person at Stanford

Serendipity

14 of 137

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

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

A binomial is a particular random variable which represents number of heads in n coin flips.

Learning Goals for Today

15 of 137

It’s Time…

16 of 137

It’s Time…

…For Random Variables

17 of 137

Random Variables Are Variables…That Are Random

18 of 137

  • Check out the variable result in the code below.

Random Variables Are Variables…That Are Random

19 of 137

Random Variables Are Variables…That Are Random

  • Check out the variable result in the code below.

    • Do we know the value of result before we run the code?

20 of 137

Random Variables Are Variables…That Are Random

  • Check out the variable result in the code below.

    • Do we know the value of result before we run the code? Nope!
    • Is the value of result the same every time we run the code?

21 of 137

Random Variables Are Variables…That Are Random

  • Check out the variable result in the code below.

    • Do we know the value of result before we run the code? Nope!
    • Is the value of result the same every time we run the code? Nope!

  • Like result, a random variable is a variable whose value is uncertain.

22 of 137

Random Variables Are Variables…That Are Random

  • A random variable is a variable whose value is uncertain.

“Let X be the result of flipping a coin.”

P(X = 0) = 0.5

P(X = 1) = 0.5

23 of 137

Random variables are an abstraction on top of events

Random variables are not events

24 of 137

Let X be a

random variable

Random Variables vs. Events

Piech & Cain, CS109, Stanford University

25 of 137

Let X be a

random variable

It is an event when

X takes on a value

Random Variables vs. Events

Piech & Cain, CS109, Stanford University

26 of 137

Let X be a

random variable

It is an event when

X takes on a value

Random Variables vs. Events

So we can still work with probabilities of events

Piech & Cain, CS109, Stanford University

27 of 137

Examples of Random Variables

"Let X be the result of rolling a six sided die."

        • P(X = 1) = 1/6
        • P(X = 2) = 1/6
        • P(X = 3) = 1/6

        • P(X = 4) = 1/6
        • P(X = 5) = 1/6
        • P(X = 6) = 1/6

28 of 137

Examples of Random Variables

"Let X be the result of rolling a dice."

        • P(X = 1) = 1/6
        • P(X = 2) = 1/6
        • P(X = 3) = 1/6

…or, P(X = x) = 1/6 for

        • P(X = 4) = 1/6
        • P(X = 5) = 1/6
        • P(X = 6) = 1/6

29 of 137

Examples of Random Variables

"Let X be the result of rolling a dice."

        • P(X = 1) = 1/6
        • P(X = 2) = 1/6
        • P(X = 3) = 1/6

…or, P(X = x) = 1/6 for

        • P(X = 4) = 1/6
        • P(X = 5) = 1/6
        • P(X = 6) = 1/6

30 of 137

Examples of Random Variables

"Let Y be the number of heads seen in 2 coin flips."

        • P(Y = 0) = 1/4 (T, T)
        • P(Y = 1) = 1/2 (H, T), (T, H)
        • P(Y = 2) = 1/4 (H, H)

"Let X be the result of rolling a dice."

        • P(X = 1) = 1/6
        • P(X = 2) = 1/6
        • P(X = 3) = 1/6

…or, P(X = x) = 1/6 for

        • P(X = 4) = 1/6
        • P(X = 5) = 1/6
        • P(X = 6) = 1/6

31 of 137

Examples of Random Variables

"Let Y be the number of heads seen in 2 coin flips."

        • P(Y = 0) = 1/4 (T, T)
        • P(Y = 1) = 1/2 (H, T), (T, H)
        • P(Y = 2) = 1/4 (H, H)

"Let X be the result of rolling a dice."

        • P(X = 1) = 1/6
        • P(X = 2) = 1/6
        • P(X = 3) = 1/6

…or, P(X = x) = 1/6 for

        • P(X = 4) = 1/6
        • P(X = 5) = 1/6
        • P(X = 6) = 1/6

32 of 137

Examples of Random Variables

"Let Z be the sum of the result of rolling two dice."

        • P(Z = 2) = 1/36
        • P(Z = 3) = 2/36
        • P(Z = 4) = 3/36
        • P(Z = 5) = 4/36

        • P(Z = 6) = 5/36
        • P(Z = 7) = 6/36
        • P(Z = 8) = 5/36
        • P(Z = 9) = 4/36

        • P(Z = 10) = 3/36
        • P(Z = 11) = 2/36
        • P(Z = 12) = 1/36

33 of 137

Examples of Random Variables

"Let Z be the sum of the result of rolling two dice."

        • P(Z = 2) = 1/36
        • P(Z = 3) = 2/36
        • P(Z = 4) = 3/36
        • P(Z = 5) = 4/36

        • P(Z = 6) = 5/36
        • P(Z = 7) = 6/36
        • P(Z = 8) = 5/36
        • P(Z = 9) = 4/36

        • P(Z = 10) = 3/36
        • P(Z = 11) = 2/36
        • P(Z = 12) = 1/36

There’s a name for what we’re describing, when we list out all possible outcomes + their probabilities:

Probability Mass Function (PMF)

34 of 137

Probability Mass Functions

35 of 137

"Let Y be the number of heads seen in 2 coin flips."

Then this is a number

(between 0 and 1)

If this is a number

Random Variables & Functions

Piech & Cain, CS109, Stanford University

36 of 137

"Let Y be the number of heads seen in 2 coin flips."

Then this is a function

If this is a variable

Random Variables & Functions

Piech & Cain, CS109, Stanford University

37 of 137

"Let Y be the number of heads seen in 2 coin flips."

Random Variables & Functions

We can put in different inputs

…and get out their probabilities!

Piech & Cain, CS109, Stanford University

38 of 137

The relationship between values a random variable can take on, and the corresponding probability, is a function!

39 of 137

def event_probability(z):

# probability mass function of Z

if not z.is_integer() or z > 12 or z < 1:

return 0

if z < 7:

return (z1) / 36

else:

return (13 - z) / 36

Probability Mass Function: Representations

All of these are different ways we can represent probability mass functions!

40 of 137

Remember this super critical problem from last time?

40

41 of 137

Can We Formalize Many Coin Flips?

Probability that our variable takes on the value k

Number of flips

X = number of heads on n coin flips. Each flip is independent and has prob p of being heads.

Probability of heads

42 of 137

Quick Understanding Check

What is the sum of the probabilities of all possible outcomes for Y?

Piech & Cain, CS109, Stanford University

43 of 137

Quick Understanding Check

What is the sum of the probabilities of all possible outcomes for Y?

1

Piech & Cain, CS109, Stanford University

44 of 137

Can You Calculate A PMF From Data? Yes

[2, 4, 5, 12, 9, 7, 9, 4, 6, 4, 3, 7, 5, 8, 9, 6, 5, 10, 10, 10, 7, 11, 4, 6, 7, 9, 10, 6, 11, 6, 5, 12, 7, 3, 11, 6, 4, 7, 8, 2, 7, 8, 6, 6, 8, 3, 2, 8, 6, 9, 5, 11, 8, 6, 9, 7, 10, 10, 10, 6, 5, 9, 4, 5, 8, 8, 6, 6, 6, 10, 4, 7, 7, 5, 7, 9, 12, 6, 7, 5, 5, 10, 7, 5, 4, 7, 6, 6, 5, 5, 8, 9, 7, 7, 7, 9, 9, 8, 9, 11, 11, 10, 5, 3, 8, 10, 9, 7, 11, 6, 12, 6, 3, 8, 6, 3, 11, 11, 9, 6, 5, 7, 9, 7, 9, 6, 8, 9, 3, 7, 9, 10, 8, 9, 9, 7, 6, 9, 7, 5, 5, 5, 3, 8, 10, 6, 10, 8, 10, 8, 4, 11, 4, 12, 6, 7, 3, 9, 5, 11, 5, 7, 4, 7, 8, 12, 9, 8, 10, 4, 4, 5, 6, 4, 5, 6, 7, 3, 3, 11, 8, 9, 2, 8, 4, 8, 7, 8, 9, 10, 5, 10, 7, 9, 8, 8, 6, 7, 5, 6, 11, 2, 5, 3, 8, 4, 7, 7, 4, 7, 2, 7, 10, 10, 7, 9, 3, 5, 8, 6, 4, 8, 7, 7, 6, 8, 6, 11, 7, 3, 6, 6, 6, 9, 11, 6, 5, 7, 3, 12, 7, 10, 4, 6, 7, 4, 11, 3, 3, 6, 6, 12, 11, 12, 10, 11, 7, 9, 7, 5, 12, 6, 3, 6, 4, 5, 10, 6, 11, 11, 7, 6, 8, 11, 5, 12, 4, 7, 9, 9, 9, 10, 7, 9, 7, 4, 4, 6, 8, 6, 3, 4, 9, 7, 11, 8, 6, 11, 5, 7, 11, 7, 7, 6, 4, 9, 12, 9, 8, 8, 8, 9, 6, 8, 5, 11, 6, 8, 6, 5, 8, 5, 8, 6, 11, 5, 8, 3, 7, 8, 8, 10, 9, 8, 9, 8, 4, 7, 9, 5, 8, 8, 9, 7, 3, 9, 3, 4, 6, 9, 9, 5, 6, 4, 8, 9, 7, 5, 10, 5, 8, 5, 5, 5, 8, 9, 3, 9, 10, 10, 6, 4, 6, 2, 6, 2, 8, 7, 4, 6, 6, 7, 9, 4, 6, 8, 5, 7, 7, 7, 9, 2, 6, 7, 3, 10, 10, 7, 3, 5, 3, 6, 6, 7, 12, 9, 9, 11, 9, 4, 4, 10, 8, 8, 9, 8, 4, 4, 6, 2, 7, 5, 7, 7, 10, 4, 11, 5, 7, 8, 8, 2, 8, 6, 9, 8, 7, 8, 8, 10, 4, 7, 10, 10, 10, 4, 6, 12, 11, 4, 9, 12, 2, 3, 5, 3, 3, 11, 7, 8, 8, 5, 10, 8, 9, 4, 7, 7, 2, 5, 10, 7, 10, 9, 9, 4, 7, 8, 9, 8, 7, 7, 6, 12, 2, 7, 11, 10, 8, 7, 9, 11, 7, 9, 6, 8, 9, 10, 7, 3, 8, 10, 6, 6, 4, 2, 7, 11, 5, 6, 5, 4, 3, 2, 8]

Say this is your dataset, a list of observations of random variable Y:

Piech & Cain, CS109, Stanford University

45 of 137

Can You Calculate A PMF From Data? Yes

[2, 4, 5, 12, 9, 7, 9, 4, 6, 4, 3, 7, 5, 8, 9, 6, 5, 10, 10, 10, 7, 11, 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, 8, 9, 3, 9, 10, 10, 6, 4, 6, 2, 6, 2, 8, 7, 4, 6, 6, 7, 9, 4, 6, 8, 5, 7, 7, 7, 9, 2, 6, 7, 3, 10, 10, 7, 3, 5, 3, 6, 6, 7, 12, 9, 9, 11, 9, 4, 4, 10, 8, 8, 9, 8, 4, 4, 6, 2, 7, 5, 7, 7, 10, 4, 11, 5, 7, 8, 8, 2, 8, 6, 9, 8, 7, 8, 8, 10, 4, 7, 10, 10, 10, 4, 6, 12, 11, 4, 9, 12, 2, 3, 5, 3, 3, 11, 7, 8, 8, 5, 10, 8, 9, 4, 7, 7, 2, 5, 10, 7, 10, 9, 9, 4, 7, 8, 9, 8, 7, 7, 6, 12, 2, 7, 11, 10, 8, 7, 9, 11, 7, 9, 6, 8, 9, 10, 7, 3, 8, 10, 6, 6, 4, 2, 7, 11, 5, 6, 5, 4, 3, 2, 8]

Say this is your dataset, a list of observations of random variable Y:

Just convert your data into counts:

Piech & Cain, CS109, Stanford University

46 of 137

Can You Calculate A PMF From Data? Yes

[2, 4, 5, 12, 9, 7, 9, 4, 6, 4, 3, 7, 5, 8, 9, 6, 5, 10, 10, 10, 7, 11, 4, 6, 7, 9, 10, 6, 11, 6, 5, 12, 7, 3, 11, 6, 4, 7, 8, 2, 7, 8, 6, 6, 8, 3, 2, 8, 6, 9, 5, 11, 8, 6, 9, 7, 10, 10, 10, 6, 5, 9, 4, 5, 8, 8, 6, 6, 6, 10, 4, 7, 7, 5, 7, 9, 12, 6, 7, 5, 5, 10, 7, 5, 4, 7, 6, 6, 5, 5, 8, 9, 7, 7, 7, 9, 9, 8, 9, 11, 11, 10, 5, 3, 8, 10, 9, 7, 11, 6, 12, 6, 3, 8, 6, 3, 11, 11, 9, 6, 5, 7, 9, 7, 9, 6, 8, 9, 3, 7, 9, 10, 8, 9, 9, 7, 6, 9, 7, 5, 5, 5, 3, 8, 10, 6, 10, 8, 10, 8, 4, 11, 4, 12, 6, 7, 3, 9, 5, 11, 5, 7, 4, 7, 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, 7, 11, 5, 6, 5, 4, 3, 2, 8]

Say this is your dataset, a list of observations of random variable Y:

Just convert your data into counts:

And then use those counts to calculate probabilities for each outcome:

Piech & Cain, CS109, Stanford University

47 of 137

You Can Use PMFs Other People Give You

Let X be the number of earthquakes that happen in California next year.

Here’s the PMF for X:

What is the probability that there are 60 earthquakes in California next year?

48 of 137

You Can Use PMFs Other People Give You

Let X be the number of earthquakes that happen in California next year.

Here’s the PMF for X:

What is the probability that there are 60 earthquakes in California next year?

Just plug numbers in!

49 of 137

What is the most important thing to know about a discrete random variable?

50 of 137

Probability Mass Function

51 of 137

Random Variables Are Awesome

52 of 137

Some Random Variables Are “Classics”

53 of 137

Practice: Ad Clicks

Every day, Youtube shows a particular ad 1000 times.

Each ad served is clicked with p = 0.01 (otherwise it’s ignored).

What is the probability of this ad getting 10 clicks?

Piech & Cain, CS109, Stanford University

54 of 137

Practice: Ad Clicks

Every day, Youtube shows a particular ad 1000 times.

Each ad served is clicked with p = 0.01 (otherwise it’s ignored).

What is the probability of this ad getting 10 clicks?

Piech & Cain, CS109, Stanford University

Same as asking: “What is the probability of getting 10 heads out of 1000 coin flips, when the probability of each head is 0.01”

55 of 137

Practice: Ad Clicks

Every day, Youtube shows a particular ad 1000 times.

Each ad served is clicked with p = 0.01 (otherwise it’s ignored).

What is the probability of this ad getting 10 clicks?

Piech & Cain, CS109, Stanford University

Same as asking: “What is the probability of getting 10 heads out of 1000 coin flips, when the probability of each head is 0.01”

Number of trials: n=1000

Probability of success (click): p=0.01

Desired number of successes: k=10

56 of 137

Practice: Ad Clicks

Every day, Youtube shows a particular ad 1000 times.

Each ad served is clicked with p = 0.01 (otherwise it’s ignored).

What is the probability of this ad getting 10 clicks?

Piech & Cain, CS109, Stanford University

Same as asking: “What is the probability of getting 10 heads out of 1000 coin flips, when the probability of each head is 0.01”

Number of trials: n=1000

Probability of success (click): p=0.01

Desired number of successes: k=10

57 of 137

We Have Discovered The Binomial

  • Many, many scenarios fit this description:
      • # of 1’s in randomly generated in length n bit string
      • # of servers working in a large computer cluster
      • # of votes for one of two candidates in an election
      • # of jury members selected from a particular demographic
      • # of CS109 students who decided to come to lecture today

The binomial describes scenarios with:

1. n independent trials (coin flips)

2. A consistent probability p of success on each trial (heads)

3. What we want: what is the probability of exactly k successes?

58 of 137

We Have Discovered The Binomial

  • Jacob “James” Bernoulli (1654-1705): Swiss mathematician

One of many mathematicians in the Bernoulli family

Here yee. This type of random variable is so common it needs a name so that I can talk about it generally.

I shall call it: the Binomial Random Variable. Huzzah.

Piech & Cain, CS109, Stanford University

59 of 137

We Have Discovered The Binomial

  • Jacob “James” Bernoulli (1654-1705): Swiss mathematician

One of many mathematicians in the Bernoulli family

Here yee. This type of random variable is so common it needs a name so that I can talk about it generally.

I shall call it: the Binomial Random Variable. Huzzah.

Piech & Cain, CS109, Stanford University

60 of 137

Declaring 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

Piech & Cain, CS109, Stanford University

61 of 137

Then We Automatically Know the PMF!

Probability that our variable takes on the value k

Probability Mass Function for a Binomial

Piech & Cain, CS109, Stanford University

62 of 137

  1. Recognize a classic random variable type

New Recipe For Solving Problems!

Piech & Cain, CS109, Stanford University

63 of 137

  1. Recognize a classic random variable type

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

New Recipe For Solving Problems!

Piech & Cain, CS109, Stanford University

64 of 137

  1. Recognize a classic random variable type

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

  1. Profit off the PMF

New Recipe For Solving Problems!

Piech & Cain, CS109, Stanford University

65 of 137

You Get So Much�For Free!

66 of 137

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

Piech & Cain, CS109, Stanford University

67 of 137

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

67

0.1

Piech & Cain, CS109, Stanford University

68 of 137

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

68

0.01

Piech & Cain, CS109, Stanford University

69 of 137

More Ad Clicks

Every day, Youtube shows a particular ad 1000 times.

Each ad served is clicked with p = 0.01 (otherwise it’s ignored).

What is the probability of this ad getting 20 clicks?

Let X be the number of ad clicks.

X ~ Bin(n = 1000, p = 0.01).

P(X = k) =

P(X = 20) =

Piech & Cain, CS109, Stanford University

70 of 137

Practice: Ad Clicks

Every day, Youtube shows a particular ad 1000 times.

Each ad served is clicked with p = 0.01 (otherwise it’s ignored).

What is the probability of this ad getting 20 clicks?

Let X be the number of ad clicks.

X ~ Bin(n = 1000, p = 0.01).

n

p

k

Piech & Cain, CS109, Stanford University

>>> from scipy import stats

>>> X = stats.binom(1000, 0.01)

>>> X.pmf(20)

0.0017918782400182223

71 of 137

Practice: Ad Clicks

Every day, Youtube shows a particular ad 1000 times.

Each ad served is clicked with p = 0.01 (otherwise it’s ignored).

What is the probability of this ad getting 20 clicks?

Let X be the number of ad clicks.

X ~ Bin(n = 1000, p = 0.01).

Piech & Cain, CS109, Stanford University

72 of 137

Server Redundancy

A network can remain functional as long as at least 2 out of 7 servers are alive.

The probability of any server working is 0.8.

What is the probability that less than 2 servers are alive?

Piech & Cain, CS109, Stanford University

73 of 137

Server Redundancy

A network can remain functional as long as at least 2 out of 7 servers are alive.

The probability of any server working is 0.8.

What is the probability that less than 2 servers are alive?

Let X be the number of servers alive.

X ~ Bin(n = 7, p = 0.8).

Piech & Cain, CS109, Stanford University

74 of 137

Server Redundancy

A network can remain functional as long as at least 2 out of 7 servers are alive.

The probability of any server working is 0.8.

What is the probability that less than 2 servers are alive?

Let X be the number of servers alive.

X ~ Bin(n = 7, p = 0.8).

Piech & Cain, CS109, Stanford University

75 of 137

Server Redundancy

A network can remain functional as long as at least 2 out of 7 servers are alive.

The probability of any server working is 0.8.

What is the probability that less than 2 servers are alive?

Let X be the number of servers alive.

X ~ Bin(n = 7, p = 0.8).

Piech & Cain, CS109, Stanford University

76 of 137

Server Redundancy

A network can remain functional as long as at least 2 out of 7 servers are alive.

The probability of any server working is 0.8.

What is the probability that less than 2 servers are alive?

Let X be the number of servers alive.

X ~ Bin(n = 7, p = 0.8).

Piech & Cain, CS109, Stanford University

77 of 137

What is the most important thing to know about a discrete random variable?

78 of 137

Probability Mass Function

79 of 137

80 of 137

What is the probability of winning a 7 game series?

Warriors are going to play the Celtics in a best of 7 series during the 2050 NBA finals. What is the probability that the warriors win the series? Each game is independent. Each game, the warriors have a 0.55 probability of winning. Win series if you win at least 4 games.

81 of 137

What is the probability of winning a 7 game series?

81

Warriors are going to play the Celtics in a best of 7 series during the 2050 NBA finals. What is the probability that the warriors win the series? Each game is independent. Each game, the warriors have a 0.55 probability of winning. Win series if you win at least 4 games. Assume: All 7 games are played, even if a team has already won 4.

82 of 137

What is the probability of winning a 7 game series?

82

Warriors are going to play the Celtics in a best of 7 series during the 2050 NBA finals. What is the probability that the warriors win the series? Each game is independent. Each game, the warriors have a 0.55 probability of winning. Win series if you win at least 4 games. Assume: All 7 games are played, even if a team has already won 4.

Let X be the number of games won. X ~ Bin(n = 7, p = 0.55). P(X > 3)?

83 of 137

What is the probability of winning a 7 game series?

83

Warriors are going to play the Celtics in a best of 7 series during the 2050 NBA finals. What is the probability that the warriors win the series? Each game is independent. Each game, the warriors have a 0.55 probability of winning. Win series if you win at least 4 games. Assume: All 7 games are played, even if a team has already won 4.

Let X be the number of games won. X ~ Bin(n = 7, p = 0.55). P(X > 3)?

84 of 137

What is the probability of winning a 7 game series?

Warriors are going to play the Celtics in a best of 7 series during the 2050 NBA finals. What is the probability that the warriors win the series? Each game is independent. Each game, the warriors have a 0.55 probability of winning. Win series if you win at least 4 games. Assume: All 7 games are played, even if a team has already won 4.

Let X be the number of games won. X ~ Bin(n = 7, p = 0.55). P(X > 3)?

85 of 137

What is the probability of winning a 7 game series?

Warriors are going to play the Celtics in a best of 7 series during the 2050 NBA finals. What is the probability that the warriors win the series? Each game is independent. Each game, the warriors have a 0.55 probability of winning. Win series if you win at least 4 games. Assume: All 7 games are played, even if a team has already won 4.

Let X be the number of games won. X ~ Bin(n = 7, p = 0.55). P(X > 3)?

Claim: Even if we drop the 7 game assumption, we’ll still get the same answer

86 of 137

Simulation code (part 1):

import random

TRIALS = 1000000

WIN_CHANCE = 0.55

def run_simulation(num_trials, win_prob):

early_stop_wins = 0

full_seven_wins = 0

for _ in range(num_trials):

if simulate_series(win_prob, stop_early=True):

early_stop_wins += 1

if simulate_series(win_prob, stop_early=False):

full_seven_wins += 1

# Calculate and display probabilities

prob_early = early_stop_wins / num_trials

prob_full = full_seven_wins / num_trials

print(f"Results after {num_trials:,} simulated series:")

print(f"Probability with Early Stopping: {prob_early:.4f}")

print(f"Probability playing all 7 games: {prob_full:.4f}")

print(f"Difference: {abs(prob_early - prob_full):.4f}")

87 of 137

Simulation code (part 2):

def simulate_series(win_prob, stop_early=True):

wins = 0

losses = 0

games_to_play = 7

for _ in range(games_to_play):

if random.random() < win_prob:

wins += 1

else:

losses += 1

# Stop early if one team reaches 4 wins

if stop_early:

if wins == 4:

return True

if losses == 4:

return False

# If all 7 games are played, check total wins

return wins >= 4

if __name__ == "__main__":

run_simulation(TRIALS, WIN_CHANCE)

88 of 137

Simulation code output:

% python3 simulate_early_stopping.py

Results after 1,000,000 simulated series:

Probability with Early Stopping: 0.6090

Probability playing all 7 games: 0.6079

Difference: 0.0011

89 of 137

What is the probability of winning a 3 game series?

Same setting as before, but let’s make it a bit easier, so we can address some hard cases and common bugs. Warriors are playing a series, and they win if they win at least 2 out of 3 games.

90 of 137

What is the probability of winning a 3 game series?

Same setting as before, but let’s make it a bit easier, so we can address some hard cases and common bugs. Warriors are playing a series, and they win if they win at least 2 out of 3 games. Assume: All 3 games are played, even if a team has already won 2.

91 of 137

What is the probability of winning a 3 game series?

Same setting as before, but let’s make it a bit easier, so we can address some hard cases and common bugs. Warriors are playing a series, and they win if they win at least 2 out of 3 games. Assume: All 3 games are played, even if a team has already won 2.

92 of 137

What is the probability of winning a 3 game series?

Same setting as before, but let’s make it a bit easier, so we can address some hard cases and common bugs. Warriors are playing a series, and they win if they win at least 2 out of 3 games. Assume: All 3 games are played, even if a team has already won 2.

93 of 137

Debugging Probability

  • How to calculate the probability of at least k successes in n trials?
    • X is number of successes in n trials each with probability p

Correct:

First clue that something is wrong. Think about p = 1

# ways to choose slots for success

Probability that each is success

Not mutually exclusive…

Don’t care about the rest

94 of 137

Debugging Probability

  • How to calculate the probability of at least k successes in n independent trials?
    • X is number of successes in n trials each with probability p

# ways to choose slots for success

Probability that each is success

Chose slots for success, don’t care about rest

95 of 137

Debugging Probability

  • How to calculate the probability of at least k successes in n independent trials?
    • X is number of successes in n trials each with probability p

# ways to choose slots for success

Probability that each is success

Chose slots for success, don’t care about rest

96 of 137

Debugging Probability

  • How to calculate the probability of at least k successes in n independent trials?
    • X is number of successes in n trials each with probability p

# ways to choose slots for success

Probability that each is success

Chose slots for success, don’t care about rest

97 of 137

98 of 137

99 of 137

100 of 137

101 of 137

102 of 137

103 of 137

We are counting this case 3 times!

104 of 137

105 of 137

106 of 137

107 of 137

108 of 137

109 of 137

110 of 137

111 of 137

Debugging Probability

  • How to calculate the probability of at least k successes in n independent trials?
    • X is number of successes in n trials each with probability p

Correct:

First clue that something is wrong. Think about p = 1

# ways to choose slots for success

Probability that each is success

Not mutually exclusive…

Chose slots for success, don’t care about rest

112 of 137

113 of 137

114 of 137

115 of 137

116 of 137

Galton Board Time!

117 of 137

Galton Board Fun

Piech & Cain, CS109, Stanford University

118 of 137

Galton Board Fun

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

Piech & Cain, CS109, Stanford University

119 of 137

Galton Board Fun

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

Piech & Cain, CS109, Stanford University

120 of 137

Galton Board Fun

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

Piech & Cain, CS109, Stanford University

121 of 137

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

122 of 137

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

123 of 137

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

124 of 137

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

125 of 137

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

126 of 137

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

127 of 137

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

128 of 137

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

129 of 137

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

130 of 137

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

131 of 137

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

132 of 137

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

133 of 137

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.

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

0

1

2

3

4

5

Piech & Cain, CS109, Stanford University

134 of 137

135 of 137

Probability is Everywhere

135

136 of 137

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

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

A binomial is a particular random variable which represents number of heads in n coin flips.

Learning Goals for Today

137 of 137

Challenge!

137