贈券收集問題

贈券收集問題(Coupon collector's problem) 是機率論中的著名題目，其目的在解答以下問題： 假設有n種贈券，每種贈券獲取機率相同，而且贈券亦無限供應。若取贈券t張，能集齊n種贈券的機率多少？ 計算得出，平均需要 Θ ( n ln ⁡ ( n ) ) {\displaystyle \Theta (n\ln(n))} 次才能集齊n種贈券——这就是赠券收集问题的时间复杂度。例如n = 50時大約要取 50 ln ⁡ ( 50 ) + 50 γ + 1 / 2 ≈ 195.6011 + 28.8608 + 0.5 ≈ 224.9619 {\displaystyle 50\ln(50)+50\gamma +1/2\approx 195.6011+28.8608+0.5\approx 224.9619} 次才能集齊50種贈券。 贈券收集問題的特徵是開始收集時，可以在短時間內收集多種不同的贈券，但最後數種則要花很長時間才能集齊。例如有50種贈券，在集齊49種以後要約多50次收集才能找到最後一張，所以贈券收集問題的答案t的期望值要比50要大得多。 假設T是收集所有n種贈券的次数， t i {\displaystyle t_{i}} 是在收集了第i-1種贈券以後，到收集到第i種贈券所花的次数，那麼T和 t i {\displaystyle t_{i}} 都是隨機變量。在收集到i-1種贈券後能再找到「新」一種贈券的概率是 p i = n − i + 1 n {\displaystyle p_{i}={\frac {n-i+1}{n}}} ，所以 t i {\displaystyle t_{i}} 是一種幾何分佈，並有期望值 1 p i {\displaystyle {\frac {1}{p_{i}}}} 。根據期望值的線性性質， E ⁡ ( T ) = E ⁡ ( t 1 ) + E ⁡ ( t 2 ) + ⋯ + E ⁡ ( t n ) = 1 p 1 + 1 p 2 + ⋯ + 1 p n = n n + n n − 1 + ⋯ + n 1 = n ⋅ ( 1 1 + 1 2 + ⋯ + 1 n ) = n ⋅ H n . {\displaystyle {\begin{aligned}\operatorname {E} (T)&=\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}}} 其中 H n {\displaystyle H_{n}} 是調和數，根據其近似值，可化約為： E ⁡ ( T ) = n ⋅ H n = n ln ⁡ n + γ n + 1 2 + o ( 1 ) ,     as   n → ∞ , {\displaystyle \operatorname {E} (T)=n\cdot H_{n}=n\ln n+\gamma n+{\frac {1}{2}}+o(1),\ \ {\text{as}}\ n\to \infty ,} 其中 γ ≈ 0.5772156649 {\displaystyle \gamma \approx 0.5772156649} 是歐拉-馬歇羅尼常數. 那麼，可用馬爾可夫不等式求取機率的上限： P ⁡ ( T ≥ c n H n ) ≤ 1 c . {\displaystyle \operatorname {P} (T\geq c\,nH_{n})\leq {\frac {1}{c}}.} 基於 t i {\displaystyle t_{i}} 相互獨立的特性，則有： Var ⁡ ( T ) = Var ⁡ ( t 1 ) + Var ⁡ ( t 2 ) + ⋯ + Var ⁡ ( t n ) = 1 − p 1 p 1 2 + 1 − p 2 p 2 2 + ⋯ + 1 − p n p n 2 ≤ n 2 n 2 + n 2 ( n − 1 ) 2 + ⋯ + n 2 1 2 ≤ n 2 ⋅ ( 1 1 2 + 1 2 2 + ⋯ ) = π 2 6 n 2 ≤ 2 n 2 , {\displaystyle {\begin{aligned}\operatorname {Var} (T)&=\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}}}\\&\leq {\frac {n^{2}}{n^{2}}}+{\frac {n^{2}}{(n-1)^{2}}}+\cdots +{\frac {n^{2}}{1^{2}}}\\&\leq n^{2}\cdot \left({\frac {1}{1^{2}}}+{\frac {1}{2^{2}}}+\cdots \right)={\frac {\pi ^{2}}{6}}n^{2}\leq 2n^{2},\end{aligned}}} 最末一行的等式來自黎曼ζ函數的巴塞爾問題。此式繼而可用切比雪夫不等式求取機率上限： P ⁡ ( | T − n H n | ≥ c n ) ≤ 2 c 2 . {\displaystyle \operatorname {P} \left(|T-nH_{n}|\geq c\,n\right)\leq {\frac {2}{c^{2}}}.} 我們亦可用以下方法求另一個的上限：假設 Z i r {\displaystyle {Z}_{i}^{r}} 表示在首r次收集中未有見到第i種贈券，則 P [ Z i r ] = ( 1 − 1 n ) r ≤ e − r / n {\displaystyle {\begin{aligned}P\left[{Z}_{i}^{r}\right]=\left(1-{\frac {1}{n}}\right)^{r}\leq e^{-r/n}\end{aligned}}} 所以，若 r = β n log ⁡ n {\displaystyle r=\beta n\log n} ，則有 P [ Z i r ] ≤ e ( − β n log ⁡ n ) / n = n − β {\displaystyle P\left[{Z}_{i}^{r}\right]\leq e^{(-\beta n\log n)/n}=n^{-\beta }} . P [ T > β n log ⁡ n ] ≤ P [ ⋃ i Z i β n log ⁡ n ] ≤ n ⋅ P [ Z 1 ] ≤ n − β + 1 {\displaystyle {\begin{aligned}P\left[T>\beta n\log n\right]\leq P\left[\bigcup _{i}{Z}_{i}^{\beta n\log n}\right]\leq n\cdot P[{Z}_{1}]\leq n^{-\beta +1}\end{aligned}}} 另一種解決贈券收集問題的方法是用生成函數。 觀察得出，贈券收集的過程必然如下： 收集第一張贈券，其出現的機率是 n / n = 1 {\displaystyle n/n=1} 收集了若干張第一種贈券 收集到一張第二種贈券，其出現的機率是 ( n − 1 ) / n {\displaystyle (n-1)/n} 收集了若干張第一種或第二種贈券 收集到一張第三種贈券，其出現的機率是 ( n − 2 ) / n {\displaystyle (n-2)/n} 收集了若干張第一種、第二種或第三種贈券 收集到一張第四種贈券，其出現的機率是 ( n − 3 ) / n {\displaystyle (n-3)/n} … {\displaystyle \ldots } 收集到一張最後一種贈券，其出現的機率是 1 / n {\displaystyle 1/n} 若某一刻已若干種贈券，再收集到一張已重覆的贈券的機率是p，那麼，再收集到m張已重覆的贈券的機率就是 p m {\displaystyle p^{m}} 。則就此部分而言，有關m的概率母函数（PGF）是 G ( z ) = ∑ m = 0 ∞ p m z m = 1 + p z + p 2 z 2 + p 3 z 3 + ⋯ = 1 1 − p z {\displaystyle G(z)=\sum _{m=0}^{\infty }p^{m}z^{m}=1+pz+p^{2}z^{2}+p^{3}z^{3}+\cdots ={\frac {1}{1-pz}}} 若將上述收集過程分割為多個階段，則整個收集過程所花的時間的概率母函数為各部分的乘積，亦即 G ( z ) = n n z ⋅ 1 1 − 1 n z ⋅ n − 1 n z ⋅ 1 1 − 2 n z ⋅ n − 2 n z ⋅ 1 1 − 3 n z ⋅ n − 3 n z ⋯ 1 1 − n − 1 n z ⋅ n − ( n − 1 ) n z . {\displaystyle G(z)={\frac {n}{n}}z\cdot {\frac {1}{1-{\frac {1}{n}}z}}\cdot {\frac {n-1}{n}}z\cdot {\frac {1}{1-{\frac {2}{n}}z}}\cdot {\frac {n-2}{n}}z\cdot {\frac {1}{1-{\frac {3}{n}}z}}\cdot {\frac {n-3}{n}}z\cdots {\frac {1}{1-{\frac {n-1}{n}}z}}\cdot {\frac {n-(n-1)}{n}}z.} 那麼，根據機率生成函數的特性，總收集次數T的期望值是 E ⁡ ( T ) = d d z G ( z ) | z = 1 {\displaystyle \operatorname {E} (T)=\left.{\frac {\mathrm {d} }{\mathrm {d} z}}G(z)\right|_{z=1}} 而某一T的機率則是 Pr ( T = k ) = 1 k ! d k G ( z ) d z k | z = 0 {\displaystyle \Pr(T=k)=\left.{\frac {1}{k!}}{\frac {\mathrm {d} ^{k}G(z)}{\mathrm {d} z^{k}}}\right|_{z=0}} 計算E(T)可先化簡 G ( z ) {\displaystyle G(z)} 為 G ( z ) = z n n − 1 n − z n − 2 n − 2 z n − 3 n − 3 z ⋯ n − ( n − 1 ) n − ( n − 1 ) z {\displaystyle G(z)=z^{n}{\frac {n-1}{n-z}}{\frac {n-2}{n-2z}}{\frac {n-3}{n-3z}}\cdots {\frac {n-(n-1)}{n-(n-1)z}}} 因為 d d z n − k n − k z = k ( n − k ) ( n − k z ) 2 {\displaystyle {\frac {\mathrm {d} }{\mathrm {d} z}}{\frac {n-k}{n-kz}}={\frac {k(n-k)}{(n-kz)^{2}}}} 所以 d d z G ( z ) = G ( z ) ( n z + 1 n − z + 2 n − 2 z + 3 n − 3 z ⋯ + n − 1 n − ( n − 1 ) z ) {\displaystyle {\frac {\mathrm {d} }{\mathrm {d} z}}G(z)=G(z)\left({\frac {n}{z}}+{\frac {1}{n-z}}+{\frac {2}{n-2z}}+{\frac {3}{n-3z}}\cdots +{\frac {n-1}{n-(n-1)z}}\right)} 故此可得出 E ⁡ ( T ) = d d z G ( z ) | z = 1 = G ( 1 ) ( n + 1 n − 1 + 2 n − 2 + 3 n − 3 ⋯ + n − 1 n − ( n − 1 ) ) = n + ∑ k = 1 n − 1 k n − k {\displaystyle {\begin{aligned}\operatorname {E} (T)&=\left.{\frac {\mathrm {d} }{\mathrm {d} z}}G(z)\right|_{z=1}\\&=G(1)\left(n+{\frac {1}{n-1}}+{\frac {2}{n-2}}+{\frac {3}{n-3}}\cdots +{\frac {n-1}{n-(n-1)}}\right)\\&=n+\sum _{k=1}^{n-1}{\frac {k}{n-k}}\end{aligned}}} 其中的連加部分可化簡： ∑ k = 1 n − 1 k n − k = ∑ k = 1 n − 1 ( k n − k − n n − k ) + n H n − 1 = n H n − 1 − ( n − 1 ) {\displaystyle \sum _{k=1}^{n-1}{\frac {k}{n-k}}=\sum _{k=1}^{n-1}\left({\frac {k}{n-k}}-{\frac {n}{n-k}}\right)+nH_{n-1}=nH_{n-1}-(n-1)} 所以得出： E ⁡ ( T ) = n + n H n − 1 − ( n − 1 ) = n H n − 1 + 1 = n H n {\displaystyle \operatorname {E} (T)=n+nH_{n-1}-(n-1)=nH_{n-1}+1=nH_{n}} 用機率生成函數可同時求取變異量。變異量可寫作 Var ⁡ ( T ) = E ⁡ ( T ( T − 1 ) ) + E ⁡ ( T ) − E ⁡ ( T ) 2 {\displaystyle \operatorname {Var} (T)=\operatorname {E} (T(T-1))+\operatorname {E} (T)-\operatorname {E} (T)^{2}} 其中 E ⁡ ( T ( T − 1 ) ) = d 2 d z 2 G ( z ) | z = 1 = [ G ( z ) ( n z + 1 n − z + 2 n − 2 z + 3 n − 3 z ⋯ + n − 1 n − ( n − 1 ) z ) 2 + G ( z ) ( − n z 2 + 1 2 ( n − z ) 2 + 2 2 ( n − 2 z ) 2 + 3 2 ( n − 3 z ) 2 ⋯ + ( n − 1 ) 2 ( n − ( n − 1 ) z ) 2 ) ] | z = 1 = n 2 H n 2 − n + ∑ k = 1 n − 1 k 2 ( n − k ) 2 = n 2 H n 2 − n + ∑ k = 1 n − 1 ( n − k ) 2 k 2 = n 2 H n 2 − n + n 2 H n − 1 ( 2 ) − 2 n H n − 1 + ( n − 1 ) . {\displaystyle {\begin{aligned}\operatorname {E} (T(T-1))=&\left.{\frac {\mathrm {d} ^{2}}{\mathrm {d} z^{2}}}G(z)\right|_{z=1}\\=&\left[G(z)\left({\frac {n}{z}}+{\frac {1}{n-z}}+{\frac {2}{n-2z}}+{\frac {3}{n-3z}}\cdots +{\frac {n-1}{n-(n-1)z}}\right)^{2}\right.\\&\;\left.\left.+G(z)\left(-{\frac {n}{z^{2}}}+{\frac {1^{2}}{(n-z)^{2}}}+{\frac {2^{2}}{(n-2z)^{2}}}+{\frac {3^{2}}{(n-3z)^{2}}}\cdots +{\frac {(n-1)^{2}}{(n-(n-1)z)^{2}}}\right)\right]\right|_{z=1}\\=&n^{2}H_{n}^{2}-n+\sum _{k=1}^{n-1}{\frac {k^{2}}{(n-k)^{2}}}\\=&n^{2}H_{n}^{2}-n+\sum _{k=1}^{n-1}{\frac {(n-k)^{2}}{k^{2}}}\\=&n^{2}H_{n}^{2}-n+n^{2}H_{n-1}^{(2)}-2nH_{n-1}+(n-1).\end{aligned}}} 故得出： Var ⁡ ( T ) = n 2 H n 2 − 1 + n 2 H n − 1 ( 2 ) − 2 n H n − 1 + n H n − 1 + 1 − n 2 H n 2 = n 2 H n − 1 ( 2 ) − n H n − 1 < π 2 6 n 2 {\displaystyle {\begin{aligned}\operatorname {Var} (T)&=\;n^{2}H_{n}^{2}-1+n^{2}H_{n-1}^{(2)}-2nH_{n-1}+nH_{n-1}+1-n^{2}H_{n}^{2}\\&=\;n^{2}H_{n-1}^{(2)}-nH_{n-1}<{\frac {\pi ^{2}}{6}}n^{2}\end{aligned}}} Paul Erdős and Alfréd Rényi, On a classical problem of probability theory, Magyar Tud. Akad. Mat. Kutato Int. Kozl, 1961. William Feller, An introduction to Probability Theory and its Applications, 1957. Michael Mitzenmacher and Eli Upfal, Probability and Computing: Randomized Algorithms and Probabilistic Analysis, Cambridge University Press, 2005 Donald J. Newman and Lawrence Shepp, The Double Dixie Cup Problem, American Mathematical Monthly, Vol. 67, No. 1 (Jan., 1960), pp. 58–61. Philippe Flajolet, Danièle Gardy, Loÿs Thimonier Birthday paradox, coupon collectors, caching algorithms and self-organizing search. （页面存档备份，存于互联网档案馆）, Discrete Applied Mathematics, Vol. 39, (1992), pp. 207–229 "Coupon Collector Problem （页面存档备份，存于互联网档案馆）" by Ed Pegg, Jr., the Wolfram Demonstrations Project. Mathematica package. Coupon Collector Problem （页面存档备份，存于互联网档案馆）, Java applet. How Many Singles, Doubles, Triples, Etc., Should The Coupon Collector Expect? （页面存档备份，存于互联网档案馆）, a short note by Doron Zeilberger. （英文） getir indirim kodu（页面存档备份，存于互联网档案馆）