Binomial
CS109
Summer 2026
Review
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
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
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
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
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
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:
Sample space:
Divide:
Counting
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
There are n animals.
How many distinct pairs of animals are there?
There are n animals.
How many distinct pairs of animals are there?
This is just
End Review
Serendipity
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
It’s Time…
It’s Time…
…For Random Variables
Random Variables Are Variables…That Are Random
Random Variables Are Variables…That Are Random
Random Variables Are Variables…That Are Random
Random Variables Are Variables…That Are Random
Random Variables Are Variables…That Are Random
Random Variables Are Variables…That Are Random
“Let X be the result of flipping a coin.”
P(X = 0) = 0.5
P(X = 1) = 0.5
Random variables are an abstraction on top of events
Random variables are not events
Let X be a
random variable
Random Variables vs. Events
Piech & Cain, CS109, Stanford University
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
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
Examples of Random Variables
"Let X be the result of rolling a six sided die."
Examples of Random Variables
"Let X be the result of rolling a dice."
…or, P(X = x) = 1/6 for
Examples of Random Variables
"Let X be the result of rolling a dice."
…or, P(X = x) = 1/6 for
Examples of Random Variables
"Let Y be the number of heads seen in 2 coin flips."
"Let X be the result of rolling a dice."
…or, P(X = x) = 1/6 for
Examples of Random Variables
"Let Y be the number of heads seen in 2 coin flips."
"Let X be the result of rolling a dice."
…or, P(X = x) = 1/6 for
Examples of Random Variables
"Let Z be the sum of the result of rolling two dice."
Examples of Random Variables
"Let Z be the sum of the result of rolling two dice."
There’s a name for what we’re describing, when we list out all possible outcomes + their probabilities:
Probability Mass Function (PMF)
Probability Mass Functions
"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
"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
"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
The relationship between values a random variable can take on, and the corresponding probability, is a function!
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 (z – 1) / 36
else:
return (13 - z) / 36
Probability Mass Function: Representations
All of these are different ways we can represent probability mass functions!
Remember this super critical problem from last time?
40
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
Quick Understanding Check
What is the sum of the probabilities of all possible outcomes for Y?
Piech & Cain, CS109, Stanford University
Quick Understanding Check
What is the sum of the probabilities of all possible outcomes for Y?
1
Piech & Cain, CS109, Stanford University
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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 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
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:
Just convert your data into counts:
Piech & Cain, CS109, Stanford University
Can You Calculate A PMF From Data?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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:
Just convert your data into counts:
And then use those counts to calculate probabilities for each outcome:
Piech & Cain, CS109, Stanford University
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?
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!
What is the most important thing to know about a discrete random variable?
Probability Mass Function
Random Variables Are Awesome
Some Random Variables Are “Classics”
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
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”
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
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
We Have Discovered The Binomial
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?
We Have Discovered The Binomial
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
We Have Discovered The Binomial
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
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
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
New Recipe For Solving Problems!
Piech & Cain, CS109, Stanford University
New Recipe For Solving Problems!
Piech & Cain, CS109, Stanford University
New Recipe For Solving Problems!
Piech & Cain, CS109, Stanford University
You Get So Much�For Free!
The PMF as a Graph: X ~ Bin(n = 20, p = 0.6)
Piech & Cain, CS109, Stanford University
The PMF as a Graph: X ~ Bin(n = 20, p = 0.1)
67
0.1
Piech & Cain, CS109, Stanford University
The PMF as a Graph: X ~ Bin(n = 20, p = 0.01)
68
0.01
Piech & Cain, CS109, Stanford University
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
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
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
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
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
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
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
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
What is the most important thing to know about a discrete random variable?
Probability Mass Function
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.
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.
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)?
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)?
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)?
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
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}")
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)
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
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.
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.
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.
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.
Debugging Probability
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
Debugging Probability
# ways to choose slots for success
Probability that each is success
Chose slots for success, don’t care about rest
Debugging Probability
# ways to choose slots for success
Probability that each is success
Chose slots for success, don’t care about rest
Debugging Probability
# ways to choose slots for success
Probability that each is success
Chose slots for success, don’t care about rest
We are counting this case 3 times!
Debugging Probability
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
Galton Board Time!
Galton Board Fun
Piech & Cain, CS109, Stanford University
Galton Board Fun
When a marble hits a pin, it has equal chance of going left or right.
Piech & Cain, CS109, Stanford University
Galton Board Fun
When a marble hits a pin, it has equal chance of going left or right.
Piech & Cain, CS109, Stanford University
Galton Board Fun
When a marble hits a pin, it has equal chance of going left or right.
Piech & Cain, CS109, Stanford University
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
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
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
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
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
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
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
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
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
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
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
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
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
Probability is Everywhere
135
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
Challenge!
137