Classic Probability Problem #2: The Coupon Problem | by Andrew Rothman | Towards Data Science

This is my second entry on classic toy problems in probability theory. “The Coupon Problem” is great problem to be aware of; it shows up in a number of applied situations. From a technical interviewing perspective, by adding a small computational component at the end it can serve as a solid test of general probability & coding knowledge. The question is as follows:
There is a brand of cereal that comes with a single coupon with the purchase of each box. There are a 5 different types of coupons. The probability of getting any one of the 5 coupons with each cereal box purchase is uniform. The objective is to collect all 5 unique coupon types.
Now the questions begs, what is the expected number of boxes of cereal you will need to purchase to collect all 5 unique coupons?
We will first solve this problem analytically, and then simulate the problem computationally to verify our findings.
Let’s start with a few observations:
Each cereal box purchased is independent of the last box (or boxes) purchased.The probability of getting any of the 5 coupon types in a given box is the same.
Each cereal box purchased is independent of the last box (or boxes) purchased.
The probability of getting any of the 5 coupon types in a given box is the same.
We need someway of mathematically bounding this problem. Perhaps there are some parametric tools we can use? Let’s take a quick detour and discuss geometric random variables.
A Geometric random variable is a discrete random variable with a one-parameter distribution as detailed below:
Note, that we can view our coupon collecting problem as the sum of the expected value of five geometric random variables, each with different success parameters p:
Note, that the expected value of a geometric random variable is 1/p:
For those interested in derivations of the expected value of a Geometric random variable, below I have included two derivations (one directly using the PMF and another using the MGF). If these proofs are not of interest to you, feel free to skip this section:
Now we simply sum the expected values of our five specified random variables:
Therefore, rounding up to the nearest integer, the expected number of boxes we will need to purchase to collect all 5 unique coupons is 12 boxes.
Let’s now simulate this scenario in python computationally over 10,000 iterations to confirm our analytical results above:
import numpy as npimport pandas as pdimport matplotlib.pyplot as plt## specify number of coupons, and the probability of getting a couponnum_coupons=5expected_number_of_trials = 0for i in range(num_coupons, 0, -1):    expected_number_of_trials = expected_number_of_trials + (1/(i/num_coupons))iterations = 10000df = pd.DataFrame()df['iteration'] = range(1,iterations+1)df['average_num_trials_required'] = Nonefor i in range(0, iterations):    full_set = set(range(0,num_coupons))    current_set = set([])    total_number_of_coupons_collected = 0    while(current_set != full_set):        total_number_of_coupons_collected = total_number_of_coupons_collected+1        random_coupon = np.asscalar(np.random.randint(low=0, high=num_coupons, size=1))        update_set = list(current_set)        update_set.append(random_coupon)        current_set = set(update_set)        del random_coupon, update_set        if(i+1 == 1):        df.loc[df['iteration']==i+1, 'average_num_trials_required'] = total_number_of_coupons_collected    else:        df.loc[df['iteration']==i+1, 'average_num_trials_required'] = float(((df.loc[df['iteration']==i, 'average_num_trials_required']*i) + total_number_of_coupons_collected)/(i+1))print(df['average_num_trials_required'][i])
As shown above, over 10,000 iterations of this scenario, the sample average of the number of boxes needed to be purchased to collect all 5 unique coupons was approximately 11.431. This result coincides with our analytical solution above.
As with most well-known toy problems in technical fields, this “Coupon Problem” and silly child-like scenario about cereal and coupon collecting serves an important purpose. This problem (and slight variants of it) shows up in applied practice constantly; problems concerning the expected value of discrete waiting times, or the number of events that likely need to occur before something happens. These are problems that can often be reduced to some variant of the Coupon Problem. Therefore having good fundamentals and grounded knowledge in theory about toy problems such as the one discussed here can very much assist you in applied practice.
Additionally, it’s worth mentioning we solved this problem for 5 unique coupons. But we could have chosen any arbitrary constant n unique coupons greater than zero. The methodology used here still applies.
Lastly, I would like to leave you with a lingering question. In our problem above, let’s say you’ve already collected 4 of the unique coupons, and now working on collecting that 5th and last unique coupon. Post your 4th unique coupon collected, you’ve already purchased 50 more cereal boxes, but all have contained more of the 4 unique coupons you already have. From that point forward, what is the expected number of boxes you will need to purchase to get that 5th coupon? I will give you a hint, the Geometric distribution is the discrete counterpart of the Exponential distribution, which has a particular unique property regarding waiting times. Feel free to add in the comments your thoughts, and possible answers to my riddle above.
I hope the above is insightful. As I’ve mentioned in some of my previous pieces, it’s my opinion not enough folks take the time to go through these types of exercises. For me, this type of theory-based insight leaves me more comfortable using methods in practice. A personal goal of mine is to encourage others in the field to take a similar approach. I’m planning on writing based pieces in the future, so feel free to connect with me on LinkedIn, and follow me here on Medium for updates!