# Coupon collector's problem

In probability theory, the coupon collector's problem refers to mathematical analysis of "collect all coupons and win" contests. It asks the following question: if each box of a given product (e.g., breakfast cereals) contains a coupon, and there are n different types of coupons, what is the probability that more than t boxes need to be bought to collect all n coupons? An alternative statement is: given n coupons, how many coupons do you expect you need to draw with replacement before having drawn each coupon at least once? The mathematical analysis of the problem reveals that the expected number of trials needed grows as $\Theta (n\log(n))$. For example, when n = 50 it takes about 225 trials on average to collect all 50 coupons.

## Solution
### Via generating functions

By definition of Stirling numbers of the second kind, the probability that exactly T draws are needed is${\frac {S(T-1,n-1)n!}{n^{T}}}$By manipulating the generating function of the Stirling numbers, we can explicitly calculate all moments of T:$f_{k}(x):=\sum _{T}S(T,k)x^{T}=\prod _{r=1}^{k}{\frac {x}{1-rx}}$In general, the k-th moment is $(n-1)!((D_{x}x)^{k}f_{n-1}(x)){\Big |}_{x=1/n}$, where $D_{x}$ is the derivative operator $d/dx$. For example, the 0th moment is$\sum _{T}{\frac {S(T-1,n-1)n!}{n^{T}}}=(n-1)!f_{n-1}(1/n)=(n-1)!\times \prod _{r=1}^{n-1}{\frac {1/n}{1-r/n}}=1$and the 1st moment is $(n-1)!(D_{x}xf_{n-1}(x)){\Big |}_{x=1/n}$, which can be explicitly evaluated to $nH_{n}$, etc.

### Calculating the expectation

Let time T be the number of draws needed to collect all n coupons, and let t$_i$ be the time to collect the i-th coupon after i − 1 coupons have been collected. Then $T=t_{1}+\cdots +t_{n}$. Think of T and t$_i$ as random variables. Observe that the probability of collecting a new coupon is $p_{i}={\frac {n-(i-1)}{n}}={\frac {n-i+1}{n}}$. Therefore, $t_{i}$ has geometric distribution with expectation ${\frac {1}{p_{i}}}={\frac {n}{n-i+1}}$. By the linearity of expectations we have:
    ${\begin{aligned}\operatorname {E} (T)&=\operatorname {E} (t_{1}+t_{2}+\cdots +t_{n})\\&=\operatorname {E} (t_{1})+\operatorname {E} (t_{2})+\cdots +\operatorname {E} (t_{n})\\&={\frac {1}{p_{1}}}+{\frac {1}{p_{2}}}+\cdots +{\frac {1}{p_{n}}}\\&={\frac {n}{n}}+{\frac {n}{n-1}}+\cdots +{\frac {n}{1}}\\&=n\cdot \left({\frac {1}{1}}+{\frac {1}{2}}+\cdots +{\frac {1}{n}}\right)\\&=n\cdot H_{n}.\end{aligned}}$
Here H$_n$ is the n-th harmonic number. Using the asymptotics of the harmonic numbers, we obtain:
    $\operatorname {E} (T)=n\cdot H_{n}=n\log n+\gamma n+{\frac {1}{2}}+O(1/n),$
where $\gamma \approx 0.5772156649$ is the Euler–Mascheroni constant.
Using the Markov inequality to bound the desired probability:
    $\operatorname {P} (T\geq cnH_{n})\leq {\frac {1}{c}}.$
The above can be modified slightly to handle the case when we've already collected some of the coupons. Let k be the number of coupons already collected, then:
    ${\begin{aligned}\operatorname {E} (T_{k})&=\operatorname {E} (t_{k+1}+t_{k+2}+\cdots +t_{n})\\&=n\cdot \left({\frac {1}{1}}+{\frac {1}{2}}+\cdots +{\frac {1}{n-k}}\right)\\&=n\cdot H_{n-k}\end{aligned}}$
And when $k=0$ then we get the original result.

### Calculating the variance

Using the independence of random variables t$_i$, we obtain:
    ${\begin{aligned}\operatorname {Var} (T)&=\operatorname {Var} (t_{1}+\cdots +t_{n})\\&=\operatorname {Var} (t_{1})+\operatorname {Var} (t_{2})+\cdots +\operatorname {Var} (t_{n})\\&={\frac {1-p_{1}}{p_{1}^{2}}}+{\frac {1-p_{2}}{p_{2}^{2}}}+\cdots +{\frac {1-p_{n}}{p_{n}^{2}}}\\&=\left({\frac {n^{2}}{n^{2}}}+{\frac {n^{2}}{(n-1)^{2}}}+\cdots +{\frac {n^{2}}{1^{2}}}\right)-\left({\frac {n}{n}}+{\frac {n}{n-1}}+\cdots +{\frac {n}{1}}\right)\\&=n^{2}\cdot \left({\frac {1}{1^{2}}}+{\frac {1}{2^{2}}}+\cdots +{\frac {1}{n^{2}}}\right)-n\cdot \left({\frac {1}{1}}+{\frac {1}{2}}+\cdots +{\frac {1}{n}}\right)\\&<{\frac {\pi ^{2}}{6}}n^{2}\end{aligned}}$
since ${\frac {\pi ^{2}}{6}}={\frac {1}{1^{2}}}+{\frac {1}{2^{2}}}+\cdots +{\frac {1}{n^{2}}}+\cdots$ (see Basel problem).
Bound the desired probability using the Chebyshev inequality:
    $\operatorname {P} \left(|T-nH_{n}|\geq cn\right)\leq {\frac {\pi ^{2}}{6c^{2}}}.$

### Tail estimates

A stronger tail estimate for the upper tail be obtained as follows. Let ${Z}_{i}^{r}$ denote the event that the $i$-th coupon was not picked in the first $r$ trials. Then
    ${\begin{aligned}P\left[{Z}_{i}^{r}\right]=\left(1-{\frac {1}{n}}\right)^{r}\leq e^{-r/n}.\end{aligned}}$
Thus, for $r=\beta n\log n$, we have $P\left[{Z}_{i}^{r}\right]\leq e^{(-\beta n\log n)/n}=n^{-\beta }$. Via a union bound over the $n$ coupons, we obtain
    ${\begin{aligned}P\left[T>\beta n\log n\right]=P\left[\bigcup _{i}{Z}_{i}^{\beta n\log n}\right]\leq n\cdot P[{Z}_{1}^{\beta n\log n}]\leq n^{-\beta +1}.\end{aligned}}$

## Extensions and generalizations

* Pierre-Simon Laplace, but also Paul Erdős and Alfréd Rényi, proved the limit theorem for the distribution of T. This result is a further extension of previous bounds. A proof is found in.
       $\operatorname {P} (T<n\log n+cn)\to e^{-e^{-c}},{\text{ as }}n\to \infty .$ which is a Gumbel distribution. A simple proof by martingales is in the next section.
* Donald J. Newman and Lawrence Shepp gave a generalization of the coupon collector's problem when m copies of each coupon need to be collected. Let T$_m$ be the first time m copies of each coupon are collected. They showed that the expectation in this case satisfies:
       $\operatorname {E} (T_{m})=n\log n+(m-1)n\log \log n+O(n),{\text{ as }}n\to \infty .$
    Here m is fixed. When m = 1 we get the earlier formula for the expectation.
* Common generalization, also due to Erdős and Rényi:
       $\operatorname {P} \left(T_{m}<n\log n+(m-1)n\log \log n+cn\right)\to e^{-e^{-c}/(m-1)!},{\text{ as }}n\to \infty .$
* In the general case of a nonuniform probability distribution, according to Philippe Flajolet et al.
       $\operatorname {E} (T)=\int _{0}^{\infty }\left(1-\prod _{i=1}^{m}\left(1-e^{-p_{i}t}\right)\right)dt.$
    This is equal to
       $\operatorname {E} (T)=\sum _{q=0}^{m-1}(-1)^{m-1-q}\sum _{|J|=q}{\frac {1}{1-P_{J}}},$
    where m denotes the number of coupons to be collected and P$_J$ denotes the probability of getting any coupon in the set of coupons J.