foundational text-as-data 35 min read

Zipf's and Heaps' Laws: The Statistics of Vocabulary

Why a vocabulary's rank–frequency curve and its growth curve are one power law seen twice — and why a straight line through a log–log plot is the wrong way to measure it

Overview & Motivation

Count the words in any body of text and two shapes appear. Sort the word types from most to least frequent and plot each type’s count against its rank on log–log axes: the points fall near a straight line of negative slope. That is Zipf’s law, f(r)∝r−sf(r) \propto r^{-s}. Now read the same text from the beginning and track how many distinct types we have seen after nn tokens: on log–log axes this growth curve is also nearly straight, with slope below one. That is Heaps’ law, V(n)∝nβV(n) \propto n^{\beta}.

The site’s shared finance corpus — 300 synthetic earnings calls, MD&A sections, and 8-K filings — is a good place to look. It has 63,18263{,}182 tokens and 3,4253{,}425 types, and 54.8%54.8\% of those types occur exactly once. Its ten most frequent words are and, in, the, of, for, percent, to, as, a, was: a handful of function words carries a large share of the text (23.8%23.8\% of tokens; the top hundred words carry 56.3%56.3\%), while most of the vocabulary is rare. The table below samples the curve at log-spaced ranks.

rank rr14198437277916343425
count f(r)f(r)3362106036316029611

These two laws are the first fact about text as data, and almost everything later on this site leans on them. A vocabulary that keeps growing is why subword tokenizers exist; the mass of once-seen words is why language models need smoothing; the heavy head is why stopword lists and IDF weighting work. This topic makes the two laws precise and asks three questions about them:

  1. Are they the same law? Yes, in a precise sense. The rank–frequency curve is the tail of the type-count distribution turned on its side, and under i.i.d. sampling a Zipf exponent ss forces a Heaps exponent β=1/s\beta = 1/s (Karlin’s theorem).
  2. Where could they come from? Two answers: Zipf is the maximum-entropy law at a fixed mean log-rank, and the Pitman–Yor process generates both laws from a single discount parameter dd. That process is exactly how the finance corpus generates its content words.
  3. How do we measure them? Not by drawing a line through a log–log plot. We show how badly that goes, develop the maximum-likelihood alternative, and test both on a corpus where we know the right answer.

Before any algebra, try the laboratory. In panel A, pick different windows of ranks and watch the fitted slope of the same corpus swing from −0.554-0.554 to −1.807-1.807. In panel B, turn the Pitman–Yor discount and watch one knob move both exponents.

A · Fit the line — 63,182 tokens, 3,425 types of the finance corpus
OLS slope, ranks 100–1000
-1.807
implied Zipf s
1.807
MLE ŝ = 1/(α̂ − 1)
1.783

B · Turn the discount — one Pitman–Yor restaurant, θ = 20, n = 20,000
naive Heaps slope
0.615
offset-corrected slope
0.585
planted d
0.600
Pitman–Yor ties the laws together: β = d = 0.6 ⇔ s = 1/d = 1.667. The corpus window above reads s = 1.807; the corpus's content words were planted at d = 0.6, s = 1.667.

One restaurant's level is random — K_n / n^d converges to a random limit — so the points can sit off the exact mean while their slope still tracks d. The naive slope ignores the θ/d offset of Theorem 3 and overshoots.


Rank–frequency is a tail turned sideways

Throughout, a corpus is a token sequence w1,…,wNw_1, \dots, w_N over a vocabulary of word types.

Definition 1 (Counts and ranks).

The count c(w)c(w) of a type ww is its number of tokens. Sorting the VV observed types by decreasing count gives the rank–frequency curve f(1)≥f(2)≥⋯≥f(V)f(1) \ge f(2) \ge \dots \ge f(V), with ties ordered arbitrarily.

Definition 2 (Frequency spectrum and tail count).

The frequency spectrum is Vm=#{w:c(w)=m}V_m = \#\{w : c(w) = m\}, the number of types seen exactly mm times. The tail count is N≥(f)=#{w:c(w)≥f}=∑m≥fVmN_{\ge}(f) = \#\{w : c(w) \ge f\} = \sum_{m \ge f} V_m. Types with c(w)=1c(w) = 1 are the hapax legomena; there are V1V_1 of them.

In the finance corpus V1=1877V_1 = 1877, V2=394V_2 = 394, V3=172V_3 = 172. The tail count is a staircase that starts at N≥(1)=3425N_{\ge}(1) = 3425 (every type) and steps down: N≥(2)=1548N_{\ge}(2) = 1548, N≥(10)=622N_{\ge}(10) = 622, N≥(100)=151N_{\ge}(100) = 151. Draw that staircase with ff on the horizontal axis, then reflect it across the diagonal, and you get the rank–frequency plot. The next proposition says this is not an analogy but an identity.

Proposition 1 (Rank is the tail count).

If ties are ranked last, the rank of any type with count ff is N≥(f)N_{\ge}(f). Equivalently, r↦f(r)r \mapsto f(r) and f↦N≥(f)f \mapsto N_{\ge}(f) are generalized inverses of each other.

Proof.

The types ranked at or above a type ww with c(w)=fc(w) = f are exactly the types with count greater than ff, together with every type whose count equals ff — ranking ties last places ww at the end of its tie block. Their number is #{c>f}+#{c=f}=N≥(f)\#\{c > f\} + \#\{c = f\} = N_{\ge}(f), which is therefore the rank of ww.

∎

The identity converts statements about the spectrum into statements about the rank plot. In particular it ties the two exponents together.

Corollary 1 (The exponent map α = 1 + 1/s).

If N≥(f)=Af−(α−1)N_{\ge}(f) = A f^{-(\alpha - 1)} over a range of counts, then over the corresponding ranks f(r)=(A/r)1/(α−1)f(r) = (A/r)^{1/(\alpha - 1)}: a rank–frequency exponent s=1/(α−1)s = 1/(\alpha - 1). If instead the spectrum itself is a power law, Vm=Bm−αV_m = B m^{-\alpha}, then N≥(f)=Bα−1f−(α−1) (1+O(1/f))N_{\ge}(f) = \frac{B}{\alpha - 1} f^{-(\alpha - 1)}\,(1 + O(1/f)), with the same exponent.

Proof.

The first statement inverts Proposition 1: r=Af−(α−1)r = A f^{-(\alpha-1)} solves to f=(A/r)1/(α−1)f = (A/r)^{1/(\alpha-1)}. For the second, N≥(f)=B∑x≥fx−α=B ζ(α,f)N_{\ge}(f) = B \sum_{x \ge f} x^{-\alpha} = B\,\zeta(\alpha, f), where ζ(α,f)\zeta(\alpha, f) is the Hurwitz zeta function. Since x↦x−αx \mapsto x^{-\alpha} decreases, comparing the sum with integrals gives

∫f∞x−α dx  ≤  ζ(α,f)  ≤  f−α+∫f∞x−α dx,soζ(α,f)=f1−αα−1 (1+O(1/f)).\int_f^\infty x^{-\alpha}\,dx \;\le\; \zeta(\alpha, f) \;\le\; f^{-\alpha} + \int_f^\infty x^{-\alpha}\,dx, \qquad\text{so}\qquad \zeta(\alpha, f) = \frac{f^{1-\alpha}}{\alpha - 1}\,\bigl(1 + O(1/f)\bigr). ∎

So a spectrum exponent α\alpha and a rank exponent ss are two names for one number: α=1+1/s\alpha = 1 + 1/s. The companion harness checks Proposition 1 exactly on all 3,4253{,}425 corpus types, and checks the exponent map on 200,000200{,}000-draw power-law samples with α∈{1.6,2.0,2.5}\alpha \in \{1.6, 2.0, 2.5\}. We will use the map constantly, because the clean estimator in the final section works with counts while the classical picture is drawn with ranks.


Zipf as a probability law

To reason about sampling we need Zipf’s law as a distribution, not just a fitted curve.

Definition 3 (Zipf law).

For s>1s > 1 the Zipf law on the positive integers is pr=Cr−sp_r = C r^{-s} with C=1/ζ(s)C = 1/\zeta(s) and ζ(s)=∑r≥1r−s\zeta(s) = \sum_{r \ge 1} r^{-s}. The truncated Zipf law on {1,…,R}\{1, \dots, R\} is pr=r−s/HR,sp_r = r^{-s}/H_{R,s} with HR,s=∑r≤Rr−sH_{R,s} = \sum_{r \le R} r^{-s}, for any real ss.

Proposition 2 (An infinite Zipf law needs s > 1).

ζ(s)<∞\zeta(s) \lt \infty if and only if s>1s > 1. At s=1s = 1 the partial sums satisfy HR,1=log⁡R+γ+O(1/R)H_{R,1} = \log R + \gamma + O(1/R), where γ\gamma is the Euler–Mascheroni constant.

Proof.

For s>0s > 0 the terms x−sx^{-s} are positive and decreasing, so by the integral test (see Series Convergence) the partial sum ∑r=1Rr−s\sum_{r=1}^{R} r^{-s} lies within 11 of ∫1Rx−s dx\int_1^R x^{-s}\,dx. That integral stays bounded as R→∞R \to \infty exactly when s>1s > 1. For s≤0s \le 0 the terms do not tend to zero, so the series diverges. At s=1s = 1, ∫1Rdx/x=log⁡R\int_1^R dx/x = \log R, and the gap between the sum and the integral decreases to the constant γ\gamma, with remainder O(1/R)O(1/R).

∎

This sets the stage for the first honest caveat. Fitted Zipf exponents for word frequencies in real text sit near s≈1s \approx 1 — on exactly the boundary where an infinite vocabulary cannot be normalized. A Zipf law with s≤1s \le 1 must have a finite vocabulary, or a steeper tail somewhere beyond the ranks we can observe.

Why would word frequencies take this shape at all? One answer comes from information theory. Suppose all we fix is the mean of log⁡r\log r, which we can think of as a budget for how far down the ranking a typical token reaches. The least committal distribution with that budget is Zipf’s.

Theorem 1 (Zipf maximizes entropy at fixed mean log-rank).

Fix R≥2R \ge 2 and μ∈(0,log⁡R)\mu \in (0, \log R). Among distributions qq on {1,…,R}\{1, \dots, R\} with ∑rqrlog⁡r=μ\sum_r q_r \log r = \mu, the Shannon entropy H(q)=−∑rqrlog⁡qrH(q) = -\sum_r q_r \log q_r has a unique maximizer: the truncated Zipf law pr=r−s/HR,sp_r = r^{-s}/H_{R,s}, where ss is the unique real solution of Ep[log⁡r]=μ\mathbb{E}_p[\log r] = \mu.

Proof.

The exponent exists and is unique. Let m(s)=Ep(s)[log⁡r]m(s) = \mathbb{E}_{p^{(s)}}[\log r] for the truncated Zipf law with exponent ss. Since log⁡HR,s\log H_{R,s} is a log-partition function in ss, m(s)=−ddslog⁡HR,sm(s) = -\frac{d}{ds}\log H_{R,s} and m′(s)=−Var⁡p(s)(log⁡r)m'(s) = -\operatorname{Var}_{p^{(s)}}(\log r). The variance is positive because log⁡r\log r is not constant on {1,…,R}\{1, \dots, R\} when R≥2R \ge 2, so mm is continuous and strictly decreasing. As s→−∞s \to -\infty the mass moves to r=Rr = R and m(s)→log⁡Rm(s) \to \log R; as s→∞s \to \infty it moves to r=1r = 1 and m(s)→0m(s) \to 0. Hence mm maps R\mathbb{R} one-to-one onto (0,log⁡R)(0, \log R).

Optimality. Let qq be any feasible distribution. Gibbs’ inequality — the nonnegativity of the KL divergence, KL(q ∥ p)≥0\mathrm{KL}(q \,\|\, p) \ge 0 (see Shannon Entropy & Mutual Information) — gives −∑qrlog⁡qr≤−∑qrlog⁡pr-\sum q_r \log q_r \le -\sum q_r \log p_r. Substituting log⁡pr=−slog⁡r−log⁡HR,s\log p_r = -s \log r - \log H_{R,s},

H(q)  ≤  ∑rqr(slog⁡r+log⁡HR,s)  =  sμ+log⁡HR,s.H(q) \;\le\; \sum_r q_r \bigl(s \log r + \log H_{R,s}\bigr) \;=\; s\mu + \log H_{R,s}.

The same computation with q=pq = p shows that H(p)=sμ+log⁡HR,sH(p) = s\mu + \log H_{R,s}, so H(q)≤H(p)H(q) \le H(p). Equality forces KL(q ∥ p)=0\mathrm{KL}(q \,\|\, p) = 0, that is q=pq = p.

∎

The exponent ss is the Lagrange multiplier on the log-rank constraint: the price of spreading probability toward high ranks. The harness solves the constrained problem from scratch with a generic optimizer, for R=40R = 40 and μ=1.1703\mu = 1.1703 (the mean log-rank of Zipf with s=1.3s = 1.3). Starting from the uniform distribution, the optimizer lands on the Zipf law to within 10−410^{-4}, root-finding recovers s=1.3s = 1.3 to 10−910^{-9}, and fifty random feasible perturbations of the Zipf law all lower the entropy. Maximum entropy explains the shape if we accept the constraint; it does not explain why language would fix E[log⁡r]\mathbb{E}[\log r]. We take it as one well-defined route to the power law, not as a theory of language.


Zipf implies Heaps

Now we connect the two laws. Draw nn tokens independently from a Zipf law. Type rr is still unseen after nn draws with probability (1−pr)n(1 - p_r)^n, so the expected vocabulary size is

E V(n)  =  ∑r≥1[1−(1−pr)n].\mathbb{E}\,V(n) \;=\; \sum_{r \ge 1} \Bigl[1 - (1 - p_r)^n\Bigr].

Each term is close to 11 for the common types (they are surely seen) and close to nprn p_r for the rare ones (they are probably unseen). The crossover happens near the rank where npr≈1n p_r \approx 1, that is r≈(Cn)1/sr \approx (Cn)^{1/s}. So the vocabulary should grow like n1/sn^{1/s}. To make this exact we need one tool for comparing sums with integrals.

Lemma 1 (Sums of unimodal functions).

Let φ≥0\varphi \ge 0 be integrable on [0,∞)[0, \infty), nondecreasing on [0,x∗][0, x^\ast] and nonincreasing on [x∗,∞)[x^\ast, \infty), with M=sup⁡φM = \sup \varphi. Then

∫0∞φ  −  M  ≤  ∑r≥1φ(r)  ≤  ∫0∞φ  +  2M.\int_0^\infty \varphi \;-\; M \;\le\; \sum_{r \ge 1} \varphi(r) \;\le\; \int_0^\infty \varphi \;+\; 2M.

If φ\varphi is nonincreasing (x∗=0x^\ast = 0), then ∫0∞φ−∫01φ≤∑r≥1φ(r)≤∫0∞φ\int_0^\infty \varphi - \int_0^1 \varphi \le \sum_{r \ge 1} \varphi(r) \le \int_0^\infty \varphi.

Proof.

Let m=⌊x∗⌋m = \lfloor x^\ast \rfloor. Lower bound: for 1≤r≤m1 \le r \le m, φ\varphi rises on [r−1,r][r-1, r], so φ(r)≥∫r−1rφ\varphi(r) \ge \int_{r-1}^{r} \varphi; for r≥m+1r \ge m + 1, φ\varphi falls on [r,r+1][r, r+1], so φ(r)≥∫rr+1φ\varphi(r) \ge \int_r^{r+1} \varphi. These intervals cover [0,∞)[0, \infty) except [m,m+1][m, m+1], whose integral is at most MM. Upper bound: for r≤m−1r \le m - 1, φ(r)≤∫rr+1φ\varphi(r) \le \int_r^{r+1} \varphi; for r≥m+2r \ge m + 2, φ(r)≤∫r−1rφ\varphi(r) \le \int_{r-1}^{r} \varphi. These intervals are disjoint, and the two remaining terms φ(m)\varphi(m) and φ(m+1)\varphi(m+1) are each at most MM. The monotone case is the same argument with m=0m = 0: the upper bound uses φ(r)≤∫r−1rφ\varphi(r) \le \int_{r-1}^{r}\varphi for every r≥1r \ge 1, and the lower bound uses φ(r)≥∫rr+1φ\varphi(r) \ge \int_r^{r+1}\varphi, which misses only [0,1][0, 1].

∎

Theorem 2 (Zipf implies Heaps (Karlin 1967)).

Let tokens be i.i.d. with pr=Cr−sp_r = C r^{-s}, s>1s > 1, C=1/ζ(s)C = 1/\zeta(s), and set I(n)=Γ(1−1/s) (Cn)1/sI(n) = \Gamma(1 - 1/s)\,(Cn)^{1/s}. Then

I(n)−1  ≤  E V(n)  ≤  I(n)+εn,εn  ≤  1(1−C) n[1s Γ(2−1s)(Cn)1/s+8e−2]=O(n1/s−1).I(n) - 1 \;\le\; \mathbb{E}\,V(n) \;\le\; I(n) + \varepsilon_n, \qquad \varepsilon_n \;\le\; \frac{1}{(1 - C)\,n}\Bigl[\tfrac{1}{s}\,\Gamma\bigl(2 - \tfrac{1}{s}\bigr)(Cn)^{1/s} + 8e^{-2}\Bigr] = O\bigl(n^{1/s - 1}\bigr).

In particular E V(n)∼I(n)\mathbb{E}\,V(n) \sim I(n) and log⁡E V(n)=1slog⁡n+const+o(1)\log \mathbb{E}\,V(n) = \tfrac{1}{s}\log n + \text{const} + o(1): Heaps’ law holds with β=1/s\beta = 1/s, and the absolute error is at most about one word type.

Proof.

Step 0: the integral. Let g(x)=1−e−nCx−sg(x) = 1 - e^{-nCx^{-s}} and substitute u=nCx−su = nCx^{-s}, so x=(nC/u)1/sx = (nC/u)^{1/s} and dx=−1s(nC)1/su−1−1/s dudx = -\tfrac{1}{s}(nC)^{1/s}u^{-1-1/s}\,du:

∫0∞g(x) dx  =  (nC)1/ss∫0∞(1−e−u) u−1−1/s du.\int_0^\infty g(x)\,dx \;=\; \frac{(nC)^{1/s}}{s}\int_0^\infty \bigl(1 - e^{-u}\bigr)\,u^{-1-1/s}\,du.

Integrate by parts with antiderivative −s u−1/s-s\,u^{-1/s} for u−1−1/su^{-1-1/s}. The boundary term −s u−1/s(1−e−u)-s\,u^{-1/s}(1 - e^{-u}) vanishes as u→∞u \to \infty, and as u→0u \to 0 because 1−e−u≤u1 - e^{-u} \le u and s>1s > 1. What remains is s∫0∞u−1/se−u du=s Γ(1−1/s)s\int_0^\infty u^{-1/s}e^{-u}\,du = s\,\Gamma(1 - 1/s) (see Improper Integrals & Special Functions). Hence ∫0∞g=Γ(1−1/s)(Cn)1/s=I(n)\int_0^\infty g = \Gamma(1 - 1/s)(Cn)^{1/s} = I(n).

Step 1: Poissonize. Replace (1−p)n(1-p)^n by e−npe^{-np}. The function gg is decreasing with 0≤g≤10 \le g \le 1, so the monotone case of Lemma 1 gives I(n)−1≤∑r≥1g(r)≤I(n)I(n) - 1 \le \sum_{r \ge 1} g(r) \le I(n).

Step 2: de-Poissonize. Since 1−p≤e−p1 - p \le e^{-p}, we have (1−p)n≤e−np(1 - p)^n \le e^{-np}, so E V(n)≥∑rg(r)≥I(n)−1\mathbb{E}\,V(n) \ge \sum_r g(r) \ge I(n) - 1. For the other side, −log⁡(1−p)=∑k≥1pk/k≤p+p2/(1−p)-\log(1-p) = \sum_{k \ge 1} p^k/k \le p + p^2/(1-p), hence (1−p)n≥e−np e−np2/(1−p)≥e−np(1−np2/(1−p))(1-p)^n \ge e^{-np}\,e^{-np^2/(1-p)} \ge e^{-np}\bigl(1 - np^2/(1-p)\bigr) and

0  ≤  e−npr−(1−pr)n  ≤  npr2 e−npr1−pr  ≤  h(npr)(1−C) n,h(u)=u2e−u,0 \;\le\; e^{-np_r} - (1 - p_r)^n \;\le\; \frac{n p_r^2\, e^{-np_r}}{1 - p_r} \;\le\; \frac{h(np_r)}{(1 - C)\,n}, \qquad h(u) = u^2 e^{-u},

using pr≤p1=C<1p_r \le p_1 = C \lt 1. As a function of xx, h(nCx−s)h(nCx^{-s}) is unimodal with maximum h(2)=4e−2h(2) = 4e^{-2}, and the substitution of Step 0 gives ∫0∞h(nCx−s) dx=1sΓ(2−1s)(Cn)1/s\int_0^\infty h(nCx^{-s})\,dx = \tfrac{1}{s}\Gamma(2 - \tfrac{1}{s})(Cn)^{1/s}. Lemma 1 bounds ∑rh(npr)\sum_r h(np_r) by that integral plus 8e−28e^{-2}, so E V(n)−∑rg(r)≤εn\mathbb{E}\,V(n) - \sum_r g(r) \le \varepsilon_n. Combining with Step 1 gives both inequalities. Taking logarithms, log⁡E V(n)=log⁡I(n)+O(1/I(n))\log \mathbb{E}\,V(n) = \log I(n) + O(1/I(n)), and log⁡I(n)=1slog⁡n+log⁡(Γ(1−1/s)C1/s)\log I(n) = \tfrac{1}{s}\log n + \log\bigl(\Gamma(1 - 1/s)C^{1/s}\bigr).

∎

The theorem is sharper than its usual statement: the error is about one type, not a vanishing fraction. The harness computes E V(n)\mathbb{E}\,V(n) on the full infinite Zipf law (an exact sum over the first two million ranks plus the tail’s Taylor series) and checks each step: the Step 1 sandwich, the sign and size of the Step 2 gap, and the slope. The ratio E V(n)/I(n)\mathbb{E}\,V(n)/I(n) looks like this:

ssn=102n = 10^210310^310410^410510^510610^6
1.250.99150.99860.99981.00001.0000
1.50.98470.99660.99920.99981.0000
2.00.96510.98870.99640.99890.9996
3.00.91650.96090.98180.99150.9961

Convergence is slowest for large ss. That is what the bound predicts: a one-type error weighed against I(n)∼n1/sI(n) \sim n^{1/s} types, which is a small vocabulary when ss is large.

Remark.

Heaps’ law needs an open vocabulary. Theorem 2 uses the infinite support in an essential way. A Zipf law truncated at RR types has V(n)≤RV(n) \le R, so its growth curve must flatten. With R=10,000R = 10{,}000 and s=1.25s = 1.25, the expected vocabulary is 270.4270.4, 1358.41358.4, 9808.29808.2, and 10000.010000.0 at n=103,104,106,107n = 10^3, 10^4, 10^6, 10^7. The local log–log slope falls from above 0.6/s0.6/s to below 0.25/s0.25/s. A fixed word list cannot keep up with text whose vocabulary keeps growing — the problem that subword tokenization, a later topic, solves.

The same argument says how much of the vocabulary we have seen only once.

Corollary 2 (The hapax share estimates β).

Under the hypotheses of Theorem 2, the expected number of types seen exactly once is E V1(n)=1sΓ(1−1s)(Cn)1/s+O(1)\mathbb{E}\,V_1(n) = \tfrac{1}{s}\Gamma(1 - \tfrac{1}{s})(Cn)^{1/s} + O(1). Consequently E V1(n)/E V(n)→1/s=β\mathbb{E}\,V_1(n)/\mathbb{E}\,V(n) \to 1/s = \beta.

Proof.

E V1(n)=∑rnpr(1−pr)n−1\mathbb{E}\,V_1(n) = \sum_r n p_r (1 - p_r)^{n-1}. In the Poissonized version each term is φ(r)\varphi(r) with φ(x)=ue−u\varphi(x) = u e^{-u}, u=nCx−su = nCx^{-s}. As xx grows, uu falls from ∞\infty to 00 and ue−uue^{-u} first rises and then falls, so φ\varphi is unimodal with maximum e−1e^{-1}. The substitution of Step 0 gives ∫0∞φ=1s(Cn)1/s∫0∞u−1/se−u du=1sΓ(1−1s)(Cn)1/s\int_0^\infty \varphi = \tfrac{1}{s}(Cn)^{1/s}\int_0^\infty u^{-1/s}e^{-u}\,du = \tfrac{1}{s}\Gamma(1 - \tfrac{1}{s})(Cn)^{1/s}, and Lemma 1 bounds the sum to within O(1)O(1). To de-Poissonize,

∣(1−p)n−1−e−np∣  ≤  p (1−p)n−1+[e−np−(1−p)n]  ≤  e p e−np+np2e−np1−p,\bigl|(1-p)^{n-1} - e^{-np}\bigr| \;\le\; p\,(1-p)^{n-1} + \bigl[e^{-np} - (1-p)^n\bigr] \;\le\; e\,p\,e^{-np} + \frac{np^2 e^{-np}}{1 - p},

using (1−p)n−1≤e−(n−1)p≤e⋅e−np(1-p)^{n-1} \le e^{-(n-1)p} \le e\cdot e^{-np}. Multiplying by nprnp_r and summing, the total error is at most en∑rur2e−ur+1(1−C)n∑rur3e−ur\frac{e}{n}\sum_r u_r^2 e^{-u_r} + \frac{1}{(1-C)n}\sum_r u_r^3 e^{-u_r} with ur=npru_r = np_r. By Lemma 1 and the same substitution, both sums are O(n1/s)O(n^{1/s}), so the error is O(n1/s−1)O(n^{1/s - 1}). Dividing by Theorem 2’s E V(n)=Γ(1−1/s)(Cn)1/s+O(1)\mathbb{E}\,V(n) = \Gamma(1 - 1/s)(Cn)^{1/s} + O(1) gives the limit 1/s1/s.

∎

At n=106n = 10^6 the harness finds hapax shares 0.66670.6667, 0.50020.5002, and 0.33460.3346 for s=1.5s = 1.5, 22, and 33, against 1/s=0.66671/s = 0.6667, 0.50.5, and 0.33330.3333. Once-seen words are not noise to discard. Their share measures the growth rate of the vocabulary, and their mass is what smoothing, a later topic, must reserve for words not yet seen.


Pitman–Yor plants both laws

Theorem 2 assumes a Zipf law. A generative model that produces one is more satisfying, and the Pitman–Yor process is the standard choice. It is also exactly how the site’s finance corpus generates its content words.

Definition 4 (Pitman–Yor restaurant).

Fix a discount 0≤d<10 \le d \lt 1 and a strength θ>0\theta > 0. Customer 11 sits at table 11. Given KnK_n occupied tables with occupancies c1,…,cKnc_1, \dots, c_{K_n}, customer n+1n + 1

  • opens a new table with probability θ+dKnθ+n\dfrac{\theta + d K_n}{\theta + n}, or
  • joins table kk with probability ck−dθ+n\dfrac{c_k - d}{\theta + n}.

Tables are word types and customers are tokens, so KnK_n is the vocabulary after nn tokens. At d=0d = 0 this is the Chinese restaurant process of the Dirichlet process (see Bayesian Nonparametrics). The process is also defined for −d<θ≤0-d \lt \theta \le 0, but we need only θ>0\theta > 0.

The discount takes a little from every occupied table and gives it to the new-table probability. The more tables already exist, the more likely another one opens — rich vocabularies get richer. At d=0d = 0 new tables open at the rate θ/(θ+n)\theta/(\theta + n), so the vocabulary grows only logarithmically. With d>0d > 0 it grows as a power.

Theorem 3 (Exact expected type count).

For 0<d<10 \lt d \lt 1 and θ>0\theta > 0,

E Kn+θd  =  θd Γ(θ+n+d) Γ(θ)Γ(θ+d) Γ(θ+n)  ∼  Γ(θ+1)d Γ(θ+d)  nd.\mathbb{E}\,K_n + \frac{\theta}{d} \;=\; \frac{\theta}{d}\,\frac{\Gamma(\theta + n + d)\,\Gamma(\theta)}{\Gamma(\theta + d)\,\Gamma(\theta + n)} \;\sim\; \frac{\Gamma(\theta + 1)}{d\,\Gamma(\theta + d)}\; n^{d}.

As d→0d \to 0, E Kn→∑i=0n−1θθ+i=θ [ψ(θ+n)−ψ(θ)]∼θlog⁡n\mathbb{E}\,K_n \to \sum_{i=0}^{n-1} \frac{\theta}{\theta + i} = \theta\,[\psi(\theta + n) - \psi(\theta)] \sim \theta \log n, the Dirichlet-process rate, where ψ\psi is the digamma function.

Proof.

By Definition 4, E[Kn+1∣Kn]=Kn+θ+dKnθ+n\mathbb{E}[K_{n+1} \mid K_n] = K_n + \frac{\theta + dK_n}{\theta + n}. The right side is affine in KnK_n, so taking expectations gives

E Kn+1=E Kn+θ+d E Knθ+n.\mathbb{E}\,K_{n+1} = \mathbb{E}\,K_n + \frac{\theta + d\,\mathbb{E}\,K_n}{\theta + n}.

Put Mn=E Kn+θ/dM_n = \mathbb{E}\,K_n + \theta/d. Then θ+d E Kn=dMn\theta + d\,\mathbb{E}\,K_n = d M_n, and the recursion becomes Mn+1=Mn(1+dθ+n)=Mn θ+n+dθ+nM_{n+1} = M_n\bigl(1 + \frac{d}{\theta + n}\bigr) = M_n\,\frac{\theta + n + d}{\theta + n} with M0=θ/dM_0 = \theta/d. Telescoping,

Mn=θd∏i=0n−1θ+i+dθ+i=θd Γ(θ+n+d) Γ(θ)Γ(θ+d) Γ(θ+n),M_n = \frac{\theta}{d}\prod_{i=0}^{n-1}\frac{\theta + i + d}{\theta + i} = \frac{\theta}{d}\,\frac{\Gamma(\theta + n + d)\,\Gamma(\theta)}{\Gamma(\theta + d)\,\Gamma(\theta + n)},

using Γ(x+1)=x Γ(x)\Gamma(x + 1) = x\,\Gamma(x). For the asymptotic, Lemma 2 below with x=θ+nx = \theta + n gives Γ(θ+n+d)/Γ(θ+n)∼(θ+n)d∼nd\Gamma(\theta + n + d)/\Gamma(\theta + n) \sim (\theta + n)^d \sim n^d, and θ Γ(θ)=Γ(θ+1)\theta\,\Gamma(\theta) = \Gamma(\theta + 1). At d=0d = 0 the recursion reads E Kn+1−E Kn=θ/(θ+n)\mathbb{E}\,K_{n+1} - \mathbb{E}\,K_n = \theta/(\theta + n), which sums to the stated series and its digamma form.

∎

Lemma 2 (Wendel's inequality).

For x>0x > 0 and 0<d<10 \lt d \lt 1: (xx+d)1−d≤Γ(x+d)xd Γ(x)≤1\displaystyle \Bigl(\frac{x}{x + d}\Bigr)^{1-d} \le \frac{\Gamma(x + d)}{x^d\,\Gamma(x)} \le 1. In particular Γ(x+d)/Γ(x)∼xd\Gamma(x + d)/\Gamma(x) \sim x^d as x→∞x \to \infty.

Proof.

Write tx+d−1e−t=(tx−1e−t)1−d(txe−t)dt^{x+d-1}e^{-t} = \bigl(t^{x-1}e^{-t}\bigr)^{1-d}\bigl(t^{x}e^{-t}\bigr)^{d} and apply Hölder’s inequality with exponents 11−d\frac{1}{1-d} and 1d\frac{1}{d}: Γ(x+d)≤Γ(x)1−d Γ(x+1)d=xd Γ(x)\Gamma(x + d) \le \Gamma(x)^{1-d}\,\Gamma(x+1)^{d} = x^d\,\Gamma(x). Applying the same inequality at the point x+dx + d with step 1−d1 - d gives Γ(x+1)≤(x+d)1−d Γ(x+d)\Gamma(x + 1) \le (x + d)^{1-d}\,\Gamma(x + d), that is Γ(x+d)≥x Γ(x)/(x+d)1−d\Gamma(x + d) \ge x\,\Gamma(x)/(x + d)^{1-d}. Dividing by xdΓ(x)x^d\Gamma(x) gives the lower bound, and both bounds tend to 11.

∎

Read Theorem 3 carefully: it is E Kn+θ/d\mathbb{E}\,K_n + \theta/d, not E Kn\mathbb{E}\,K_n, that is a clean power law, and its variable is n+θn + \theta, not nn. Write c=Γ(θ+1)/(d Γ(θ+d))=θd Γ(θ)/Γ(θ+d)c = \Gamma(\theta + 1)/\bigl(d\,\Gamma(\theta + d)\bigr) = \frac{\theta}{d}\,\Gamma(\theta)/\Gamma(\theta + d) for Theorem 3’s constant. Then the ratio of the shifted count to c (n+θ)dc\,(n + \theta)^d is exactly Lemma 2’s middle term at x=θ+nx = \theta + n, so it lies between ((θ+n)/(θ+n+d))1−d\bigl((\theta + n)/(\theta + n + d)\bigr)^{1-d} and 11 at every nn. As a power of nn itself it is clean only asymptotically. At d=0.6d = 0.6 and θ=20\theta = 20 the shifted count is 0.9990.999 of c (n+θ)dc\,(n + \theta)^d at n=102n = 10^2 and 1.0001.000 from n=104n = 10^4 on, but 11.5%11.5\% above c ndc\,n^d at n=102n = 10^2 and 1.2%1.2\% above at n=103n = 10^3. The offset vanishes relative to ndn^d eventually, but it is large at corpus scale — θ/d=33.3\theta/d = 33.3 for the finance corpus’s restaurants (d=0.6d = 0.6, θ=20\theta = 20). There, E Kn=64.8\mathbb{E}\,K_n = 64.8, 321.5321.5, 1364.21364.2, and 22090.822090.8 at n=102,103,104,106n = 10^2, 10^3, 10^4, 10^6, which is only 0.73600.7360, 0.91680.9168, 0.97730.9773, and 0.99850.9985 of the asymptote c ndc\,n^d. The harness confirms the closed form solves the recursion to 10−1010^{-10}, matches Monte-Carlo means over 200 restaurants, and agrees with the corpus module’s own sampler.

What about Zipf? The rank–frequency limit is deeper, and we cite it rather than prove it.

Theorem 4 (Pitman–Yor limits (Pitman 2006)).

For a Pitman–Yor restaurant with 0<d<10 \lt d \lt 1, almost surely:

  1. Kn/nd→SdK_n / n^d \to S_d, a strictly positive, finite random limit (the dd-diversity);
  2. the ranked table frequencies satisfy Pi↓∼Z i−1/dP^{\downarrow}_i \sim Z\, i^{-1/d} for a random 0<Z<∞0 \lt Z \lt \infty — a Zipf law with s=1/ds = 1/d;
  3. the share of tables with exactly mm customers converges, Kn,m/Kn→pd(m)=d Γ(m−d)Γ(1−d) m!K_{n,m}/K_n \to p_d(m) = \dfrac{d\,\Gamma(m - d)}{\Gamma(1 - d)\, m!} (the Sibuya law), whose tail is pd(m)∼dΓ(1−d) m−(1+d)p_d(m) \sim \frac{d}{\Gamma(1-d)}\, m^{-(1+d)}.

These are Theorem 3.8, Corollary 3.9, and Lemma 3.11 of Pitman’s Combinatorial Stochastic Processes (2006). The proof runs through Kingman’s representation of exchangeable partitions and regular variation, which is beyond this topic. The harness checks the statements on restaurants of 200,000200{,}000 customers with d∈{0.4,0.6,0.8}d \in \{0.4, 0.6, 0.8\}. The block-size shares for m=1,…,5m = 1, \dots, 5 fall within four binomial standard errors of pd(m)p_d(m). For d=0.6d = 0.6 the limits are 0.60.6, 0.120.12, 0.0560.056, 0.03360.0336, 0.02280.0228. The fitted power-law spectrum exponent lands within 0.10.1 of 1+d1 + d.

Everything now fits together. The Sibuya tail has spectrum exponent α=1+d\alpha = 1 + d. By Corollary 1 that is a rank exponent s=1/ds = 1/d — exactly item 2. Theorem 3 says β=d=1/s\beta = d = 1/s — exactly what Theorem 2 requires of a Zipf law with that ss. And pd(1)=dp_d(1) = d is Corollary 2’s hapax share. One parameter sets both laws. Panel B of the laboratory turns that parameter. Item 1 also explains something you will see there: a single restaurant’s type count follows Sd ndS_d\, n^d with a random constant. Its points can sit visibly off the exact mean — at d=0.6d = 0.6 the displayed restaurant has 15271527 types at n=20,000n = 20{,}000, against an expected 2083.72083.7 — while their slope still tracks dd.


Estimating the exponent

The classical recipe for “measuring Zipf’s law” is to fit a least-squares line to the log–log rank plot and report minus its slope. On the finance corpus, the answer depends heavily on which ranks we fit:

ranks1–1010–100100–10001000–34251–10001–3425
dense OLS slope−0.871−0.554−1.807−0.821−1.300−1.464

These are the windows in panel A of the laboratory. The slope over ranks 100100–10001000 is more than three times the slope over 1010–100100. Nothing in the recipe says which window is correct. Least squares also assumes independent, equal-variance errors around the line, while neighboring points on a rank plot are strongly dependent and the tail is a staircase of tied counts. The method has no valid standard error to report.

The alternative, developed by Clauset, Shalizi, and Newman (2009), works with counts rather than ranks. By Corollary 1, a rank exponent ss corresponds to a spectrum exponent α=1+1/s\alpha = 1 + 1/s. So we model each type’s count as a draw from a discrete power law and estimate α\alpha by maximum likelihood.

Definition 5 (Discrete power law).

For an integer xmin⁡≥1x_{\min} \ge 1 and α>1\alpha > 1, the discrete power law is P(X=x)=x−α/ζ(α,xmin⁡)P(X = x) = x^{-\alpha}/\zeta(\alpha, x_{\min}) for x≥xmin⁡x \ge x_{\min}, normalized by the Hurwitz zeta function ζ(α,xmin⁡)=∑x≥xmin⁡x−α\zeta(\alpha, x_{\min}) = \sum_{x \ge x_{\min}} x^{-\alpha}.

Proposition 3 (The power-law MLE exists and is unique).

Let x1,…,xn≥xmin⁡x_1, \dots, x_n \ge x_{\min} be type counts with log-likelihood ℓ(α)=−nlog⁡ζ(α,xmin⁡)−α∑ilog⁡xi\ell(\alpha) = -n\log\zeta(\alpha, x_{\min}) - \alpha\sum_i \log x_i. Then ℓ\ell is strictly concave on (1,∞)(1, \infty) and

ℓ′(α)=n(Eα[log⁡X]−log⁡x‾),log⁡x‾=1n∑ilog⁡xi.\ell'(\alpha) = n\bigl(\mathbb{E}_\alpha[\log X] - \overline{\log x}\bigr), \qquad \overline{\log x} = \tfrac{1}{n}\textstyle\sum_i \log x_i.

If log⁡x‾>log⁡xmin⁡\overline{\log x} > \log x_{\min} — that is, not every count equals xmin⁡x_{\min} — the maximum-likelihood estimate α^\hat\alpha exists, is unique, and solves the score equation log⁡x‾=−∂αlog⁡ζ(α,xmin⁡)\overline{\log x} = -\partial_\alpha \log\zeta(\alpha, x_{\min}).

Proof.

log⁡ζ(α,xmin⁡)=log⁡∑x≥xmin⁡e−αlog⁡x\log\zeta(\alpha, x_{\min}) = \log\sum_{x \ge x_{\min}} e^{-\alpha\log x} is a log-partition function. Its first derivative is −Eα[log⁡X]-\mathbb{E}_\alpha[\log X] and its second is Var⁡α(log⁡X)>0\operatorname{Var}_\alpha(\log X) > 0. Hence ℓ′′(α)=−nVar⁡α(log⁡X)<0\ell''(\alpha) = -n\operatorname{Var}_\alpha(\log X) \lt 0, and the score has the stated form. The map α↦Eα[log⁡X]\alpha \mapsto \mathbb{E}_\alpha[\log X] is continuous and strictly decreasing. As α→∞\alpha \to \infty the mass concentrates on xmin⁡x_{\min}, so it tends to log⁡xmin⁡\log x_{\min}. As α↓1\alpha \downarrow 1 it tends to +∞+\infty: for any level LL, the finitely many x<eLx \lt e^L carry bounded total weight while ζ(α,xmin⁡)→∞\zeta(\alpha, x_{\min}) \to \infty, so the probability of log⁡X≥L\log X \ge L tends to 11. Hence ℓ′\ell' has exactly one root when log⁡x‾>log⁡xmin⁡\overline{\log x} > \log x_{\min}, and by concavity it is the maximum.

∎

With xmin⁡x_{\min} fixed, this is a one-parameter exponential family with sufficient statistic ∑ilog⁡xi\sum_i \log x_i. Consistency and asymptotic normality, with Fisher information Var⁡α(log⁡X)\operatorname{Var}_\alpha(\log X), follow from the general theory (see Maximum Likelihood Estimation). Clauset et al.’s working standard error is (α^−1)/n(\hat\alpha - 1)/\sqrt{n}. The cutoff xmin⁡x_{\min} is chosen by minimizing the Kolmogorov–Smirnov distance between the empirical and fitted tail distributions — a sensible heuristic, not a theorem.

The harness checks the estimator against a reference implementation. On 20,00020{,}000-draw samples with α∈{1.6,2.2}\alpha \in \{1.6, 2.2\} the MLE lands within four standard errors of the truth and agrees with scipy.stats.fit to 10−410^{-4}. On the corpus, the score equation holds to 10−510^{-5} for xmin⁡∈{1,5,10}x_{\min} \in \{1, 5, 10\}. It also measures the least-squares alternatives on 40 random power-law samples with α=1.6\alpha = 1.6 (so s=5/3s = 5/3). Least squares on the log–log frequency histogram is badly biased, with mean error more than twenty times the MLE’s. Least squares on the rank plot does better, but its error is still more than twice the MLE’s (Simple & Multiple Linear Regression covers the assumptions both fits break). On the corpus’s full text, the KS rule selects xmin⁡=2x_{\min} = 2 and gives α^=1.5608\hat\alpha = 1.5608, so s^=1/(α^−1)=1.7833\hat s = 1/(\hat\alpha - 1) = 1.7833. In panel A that estimate is the fixed dashed line through (r(2),2)=(1548,2)(r(2), 2) = (1548, 2), placed there by Proposition 1.


Finance case study: a corpus with a planted answer


Where this goes next

  • Open vocabulary → subword units. The Remark after Theorem 2 shows that no fixed word list keeps up with Heaps growth. Subword tokenization, the next topic in this track, responds by making the vocabulary a set of reusable pieces.
  • Once-seen mass → smoothing. Corollary 2 says roughly a fraction 1/s1/s of the types seen so far are hapaxes. Their count divided by the number of tokens, V1(n)/nV_1(n)/n, estimates how often the next token will be a word never seen before — the missing mass that Good–Turing estimation, in the later n-gram smoothing topic, assigns to unseen words. Kneser–Ney smoothing turns out to approximate a hierarchical Pitman–Yor model (Teh 2006) — the same discount dd, now at every context.
  • The head → stopwords and IDF. The few types that dominate the rank plot are the stopword list. The long tail is where inverse document frequency is large (see The Vector Space Model and TF-IDF).

Honest caveats


Implementation

The companion notebook (notebookPath) imports zipf_heaps_vocabulary_statistics.py, which owns every number on this page. Running it as a script executes a harness of 27 assertion-based tests, one or more per claim, in about three seconds on a laptop CPU. The tests cover:

  • Proposition 1 exactly, on every corpus type, and the exponent map of Corollary 1 on random samples.
  • The convergence of ζ(s)\zeta(s), and the maximum-entropy theorem against a generic constrained optimizer.
  • Each step of Karlin’s proof, the saturation of a finite vocabulary, and the hapax-share corollary.
  • Theorem 3’s closed form against its recursion, Monte-Carlo means, and the finance corpus’s own sampler, plus the Sibuya shares of Theorem 4.
  • The MLE against scipy.stats.fit, its score equation, and its advantage over least squares.
  • The three beats of the finance case study.

The laboratory reads its data from a JSON file that one of those tests requires to equal the module’s viz_payload() exactly, so the figure cannot drift from the code.

References & Further Reading