With probability problems in a math class, the probabilities you need are either given to you or it is relatively easy to compute them in a straight-forward manner.
But in reality, this is not the case. You need to compute the probability yourself based on the situation. That is where probability distributions can help.
Today we are going to explore the hypergeometric probability distribution by:
- Explaining what situations it is useful for.
- The information that you need to apply this distribution.
- Coding some of the computations from scratch using Python.
- Applying our code to problems.
The hypergeometric distribution is a discrete probability distribution. It is used when you want to determine the probability of obtaining a certain number of successes without replacement from a specific sample size. This is similar to the binomial distribution, but this time you are not given the probability of a single success. Some example situations to apply this distribution are:
- The probability of getting 3 spades in a 5 card hand in poker.
- The probability of getting 4 to 5 non-land cards in an opening hand in Magic the Gathering for a standard 60 card deck.
- The probability of drawing 60% boys for the freshman class from a mixed-gender group randomly selected in a charter school admissions lottery.
To compute the probability mass function (aka a single instance) of a hypergeometric distribution, we need:
a) The total number of items we are drawing from (called N).
b) The total number of desired items in N (called A).
c) The number of draws from N we will make (called n).
d) The number of desired items in our draw of n items (called x).
There are different letters used for these variables depending on the tutorial. I am using the letters used from the video I posted below where I initially learned about the hypergeometric distribution.
Recall the Probability Mass Function (PMF) is what allows us to compute the probability of a single situation. In our case, that is the specific value for x above. The hypergeometric distribution PMF is below.
numpy for our computations later with our other functions.
Matplotlib will be to create our plot function later. The
comb function from
scipy is a built-in function to compute our 3 combinations in our PMF. We create a variable for each combination we need to compute and return the computation for the PMF.
The Cumulative Distribution Function (CDF) is a function that computes the total probabilities for a range of values for x. This will allow us to solve the second example with the Magic the Gathering game. Another example is determining the probability of obtaining at most 2 spades in a five-card hand (aka 2 or fewer spades).
To answer the spades question with at most 2 spades, we need the CDF below:
For the Magic the Gathering game scenario, we can use the above function, but it needs to start at 4 and end at 5.
Luckily with Python, we can create a function flexible enough to handle both of these problems.
Finally, below is the code needed to plot the distribution for your situation. Some of the code and styling was based on this example from this scipy docs page.
Notice that since we are showing all possibilities from 0 to n, we do not need to create a parameter for x in this function. To see the hypergeometric distribution of the card scenario with a hand of 5, set
N = 52,
A = 13 and
n = 5. You will then obtain the plot below:
Now to make use of our functions. To answer the first question we use the following parameters in the
hypergeom_pmf since we want for a single instance:
N = 52 because there are 52 cards in a deck of cards.
A = 13 since there are 13 spades total in a deck.
n = 5 since we are drawing a 5 card opening hand.
x = 3 since we want to draw 3 spades in our opening hand.
hypergeom_pmf(52, 13, 5, 3) gives us a probability of 0.08154261704681873, which is about 8.1%. Not a good chance of getting that hand.
For the second problem, I will provide some quick background information for you. Magic the Gathering (Magic for short) is a collectible trading card game where players use creatures and spells to beat their opponents. Players start each game with 7 cards from a 60 card deck.
A deck consists of land cards that allow you to cast your spells, and the spells themselves (non-land cards). Depending on the deck, you usually want at 4–5 non-land cards in your opening hand and about 23 land cards in your deck.
Assuming the above, let’s compute the probability of getting 4 or 5 non-land cards. We will use the
hypergeom_cdf with the following parameters:
N = 60 since a deck has 60 cards.
A = 37 since we are assuming there are 23 lands in our 60 card deck.
n = 7 since we will be starting with a 7 card hand.
t = 5 since that is the max number of non-lands we want.
min_value = 4 since that is the minimum number of non-land cards we want in our opening hand.
hypergeom_cdf(60, 37, 7, 5, 4) gives us a probability of 0.5884090217751665, which is about 59%. This means we have about a 59% chance of drawing 4 or 5 non-land cards in our opening hand of 7. Not bad. How would you change the number of lands and spells in your deck to increase this probability?
Suppose you run a charter school and you admit students through a lottery system. You have 734 applicants, of which 321 are boys and 413 girls. You are only admitting the first 150 students that are randomly selected. Considering your school has more girls than boys, you hope to get more boys this year. Suppose you want to know what your chances are of admitting 90 boys to your school (60% of the 150).
Take a sec to compute your answer then check past the picture below.
You should have used the
hypergeom_pmf since this is a single instance probability. Using
hypergeom_pmf(734, 321, 150, 90) you should have obtained 3.1730164380350626e-06…which is less than one percent… Let’s see a visual of the distribution to see where we have a better chance.
Seems we would have a better chance if we try to get about 65 boys, but the probabilities are still fairly low. Maybe you should change your admission process…
Now to get you to practice with a game you may not be familiar with. Mahjong is a famous card game played all over China but has the same general win condition. It is often played for money, but that is not how I play. I am also not encouraging you to gamble…
The version of Mahjong we will be discussing is from a northeast area of China known as Harbin. It is home to the famous Harbin Ice Festival and my in-laws.
The game has 3 suits, numbered one to nine, with 4 copies of each card. This so far totals 108 cards. In addition, there are 4 copies of the Zhong card (the red block standing up in the image above), bring our total to 112 cards. In Harbin mahjong, you win based on fulfilling particular conditions in addition to the winning hand. By getting at least 3 Zhong cards in your opening hand, you cover almost all of those conditions.
The question is what is the probability of obtaining at least 3 Zhong cards in your opening hand if you are not the dealer? This means your opening hand will have 13 cards. Try to compute this probability on your own using the correct function provided. Then post your answer in the comments below and I’ll let you know if you got it correct.
Thanks for reading! To see the code for this tutorial you can find it here at my Github.
If you would like a video tutorial of the information presented here, you can check out the video below. This is the one I used to learn about the hypergeometric distribution.
You can follow or connect with me on Linkedin and Twitter. Contact me directly at firstname.lastname@example.org.
Until next time,