A term's collection frequency falls roughly as the inverse of its rank (Zipf's law, $\mathit{cf}_i \approx c/i$), and a collection's vocabulary grows as a power of its size (Heaps' law, $M = k\,T^{b}$, typically $b \approx 0.5$). On the 10,788 Reuters-21578 stories (1,330,267 tokens, 29,187 terms) the middle ranks fit a slope of $-0.974$, with a flatter head and a steeper tail; 109 terms carry half the tokens and 36 percent of terms occur once. Heaps' law fits as $M = 17.6\,T^{0.527}$, 1.2 percent high at full size, but extrapolations overshoot: by 28 percent from the first tenth of Reuters, 24 percent for RCV1 from a million tokens. For independently drawn text with Zipf exponent $s > 1$, the expected vocabulary provably grows as $T^{1/s}$.
Tokenize the 10,788 news stories of Reuters-21578, the collection this topic uses for its measurements, and count. There are 1,330,267 tokens and 29,187 distinct terms. The single word the accounts for 5.2 percent of all the tokens. Half of all the tokens come from just 109 terms, 0.37 percent of the vocabulary. At the other end, 10,559 terms occur exactly once. Is every collection this lopsided, and how many distinct terms should a collection of a given size be expected to contain? Two empirical regularities answer: a term's frequency is roughly inversely proportional to its rank, and the vocabulary grows roughly as the square root of the number of tokens.
The first regularity is Zipf's law, which describes how frequencies are distributed across the vocabulary. The second is Heaps' law, which describes how the vocabulary grows as the collection grows. Neither is exact, but both hold well enough across languages and collections to guide the design of nearly every component in this topic: which words to treat as stop words, how large the dictionary will become, and why compression works.
Here is the route. First we separate two ways of counting a term, by occurrences and by documents. Then we state Zipf's law, derive what it predicts for an idealized collection, and test it on Reuters, including its long tail of rare terms. Next we do the same for Heaps' law, and see how far its predictions can be trusted. Finally we show that the two laws are two views of one fact, and prove it for an idealized model of text. Worked problems follow, and the code that fits both laws is at the end.
Three kinds of statement appear below and are kept apart. Some are derived from a model, such as the shares under an exact Zipf law. Some are measured on Reuters-21578 by the script tools/experiments/indexing/reuters_laws.py. Some are cited from the course text or the original sources.
Two kinds of frequency
What does "how common is a term" mean? Two frequency counts recur throughout retrieval, and they behave differently:
- The collection frequency $\mathit{cf}_t$ of a term $t$ is the total number of times it occurs in the collection, counting every token.
- The document frequency $\mathit{df}_t$ is the number of documents that contain $t$ at least once.
They can diverge sharply. Manning, Raghavan, and Schütze (2008, §6.2.1) give an example from the Reuters collection. Try and insurance have almost the same collection frequency, 10,422 and 10,440 occurrences. But try occurs in 8,760 documents and insurance in only 3,997.
Example (which word says more). Of the two words, insurance is the better clue to what a document is about. Try is spread thinly across many documents, about once each. Insurance is concentrated in fewer documents that use it repeatedly, which is the pattern of a word that indicates a document's subject. Document frequency captures that difference and collection frequency does not. That is why inverse document frequency is computed from $\mathit{df}$.
Zipf's law is usually stated in terms of collection frequency.
The two counts in Reuters-21578
The rest of this article measures both laws on one real collection, so it helps to see the two counts there first. Reuters-21578 (Lewis, 1997) is an older and much smaller Reuters newswire collection than RCV1, the one the textbook uses. In the version distributed with the NLTK toolkit, it has 10,788 news stories from 1987. Lowercasing the text and taking every maximal run of letters as a token gives the 1,330,267 tokens and 29,187 distinct terms counted above. Some of its terms (ratios rounded to one decimal):
| Term | $\mathit{cf}$ | $\mathit{df}$ | $\mathit{cf}/\mathit{df}$ |
|---|---|---|---|
| the | 69,277 | 6,951 | 10.0 |
| said | 25,383 | 6,784 | 3.7 |
| mln | 18,623 | 4,849 | 3.8 |
| dlrs | 12,417 | 4,226 | 2.9 |
| oil | 3,272 | 1,038 | 3.2 |
| zinc | 115 | 50 | 2.3 |
The ratio $\mathit{cf}/\mathit{df}$ is the average number of occurrences in the documents that contain the term.
- The occurs in 64 percent of the stories and about ten times in each.
- Said is nearly as widespread, since almost every news story reports what someone said, but appears fewer times per story.
- Mln (million) and dlrs (dollars) are the house abbreviations of a financial newswire.
A term's rank by $\mathit{cf}$ and its rank by $\mathit{df}$ can therefore differ. The choice matters whenever a list of "most common words" is drawn up.
Zipf's law
How fast does frequency fall as we move down the ranking? Sort the terms of a collection by collection frequency, so that the most frequent has rank 1, the next rank 2, and so on. George Kingsley Zipf (1949) observed that frequency is roughly inversely proportional to rank:
$$\mathit{cf}_i \propto \frac{1}{i},$$
where $\mathit{cf}_i$ is the collection frequency of the term at rank $i$. In plain words, the second most frequent term occurs about half as often as the first, the third about a third as often, and so on.
The model. Written as an equation, the law is $\mathit{cf}_i = c/i$ for a constant $c$ that depends on the collection. Taking logarithms gives $\log \mathit{cf}_i = \log c - \log i$. On a plot with logarithmic axes, the terms therefore fall close to a straight line with slope $-1$. The course text states the law in this form, as a power law $\mathit{cf}_i = c\,i^{k}$ with exponent $k = -1$ (Manning, Raghavan, and Schütze, 2008, §5.1.2, equation 5.2 and the text after it). A Zipf-like law allows another exponent, $\mathit{cf}_i = c\,i^{-s}$ with $s > 0$, a line of slope $-s$.
Fitting it. The law is fitted to a collection by least squares on the logarithms: the line $\log_{10} \mathit{cf}_i = a - s \log_{10} i$ is chosen to minimize the sum of squared vertical distances from the points $(\log_{10} i, \log_{10} \mathit{cf}_i)$, one point for each term in a chosen range of ranks. The slope gives $s$, and the intercept gives $c = 10^{a}$. Which ranks are included changes the result a great deal, as the Reuters fits below show.
Real collections deviate from the line at both ends, with the very top ranks and the long tail usually falling below it. Mandelbrot proposed a better-fitting generalization, $\mathit{cf}_i = P(i + \rho)^{-B}$, with parameters $P$, $\rho$, and $B$ fitted to each collection (Manning and Schütze, 1999, §1.4.3, equation 1.16). The course text judges the fit of the simple form to RCV1 "not particularly good, but good enough to serve as a model" for the calculations in its section on postings compression (Manning, Raghavan, and Schütze, 2008, §5.1.2).
What the law predicts for an ideal collection
How much of the text do the top terms carry, if the law holds exactly? Here is the idea in one sentence: under $\mathit{cf}_i = c/i$, every share is a ratio of two partial sums of $1 + \tfrac{1}{2} + \tfrac{1}{3} + \dots$, so it depends only on the vocabulary size.
The formal version. If $\mathit{cf}_i = c/i$ for a vocabulary of $M$ terms, the total number of tokens is
$$T = \sum_{i=1}^{M} \frac{c}{i} = c \, H_M,$$
where $H_M = 1 + \tfrac{1}{2} + \dots + \tfrac{1}{M}$ is the $M$-th harmonic number, approximately $\ln M + 0.577$. The fraction of all tokens contributed by the top $k$ terms is therefore $H_k / H_M$.
Take a vocabulary the size of RCV1's, $M = 400{,}000$, for which $H_M \approx 13.48$:
| Top $k$ terms | $H_k$ | Share of all tokens |
|---|---|---|
| 1 | 1.00 | 7.4% |
| 10 | 2.93 | 21.7% |
| 100 | 5.19 | 38.5% |
| 1,000 | 7.49 | 55.5% |
So the single most frequent term would account for about 7 percent of all tokens. The ten most frequent would account for about 22 percent, and the hundred most frequent for nearly 39 percent. The cumulative share first reaches one half at rank 474. Half of all the tokens in the collection would come from just 474 terms, about a tenth of a percent of the vocabulary. The other 399,500 terms would share the remaining half, and most of them would occur only a handful of times.
These figures describe an idealized collection, but real ones are similar. A short list of function words such as the, of, and, and to accounts for a large fraction of all text.
Two steps in this calculation need a justification.
- The constant is not free. Once the vocabulary is fixed, setting the total to $T$ forces $c = T / H_M$. The most frequent term's share of the collection is therefore $c/T = 1/H_M$. A larger vocabulary means a larger $H_M$ and a smaller share for every term.
- The approximation for $H_M$. It comes from comparing the sum with the integral $\int_1^M dx/x = \ln M$. Since $1/x$ decreases, $1/i$ is larger than $\int_i^{i+1} dx/x$ and smaller than $\int_{i-1}^{i} dx/x$. Adding the first inequality for $i = 1, \dots, M$ and the second for $i = 2, \dots, M$ gives $\ln(M+1) < H_M \le 1 + \ln M$, with equality on the right only for $M = 1$. So the sum overshoots $\ln M$ by an amount greater than 0 and at most 1. That amount tends to the Euler–Mascheroni constant $\gamma \approx 0.5772$ as $M$ grows (Knuth, 1997, §1.2.7). For $M = 400{,}000$ the approximation gives $12.899 + 0.577 = 13.476$, the same as the exact sum to three decimals.
Example (a smaller vocabulary). Under an ideal Zipf law, the most frequent term of Reuters-21578 would carry a larger share of the tokens than the 7.4 percent of the table, namely 9.2 percent. Reuters-21578 has a vocabulary of 29,187 terms, not 400,000. The share is $1/H_M$, and a smaller vocabulary has a smaller harmonic number. For $M = 29{,}187$, $H_M = 10.859$, so the share is $1/10.859 = 9.2$ percent. The next section compares that with what the actually carries.
Zipf's law on a real collection
Back to Reuters. Sorting its 29,187 terms by collection frequency and plotting frequency against rank on logarithmic axes gives the figure below.
Three features of the plot recur in nearly every collection:
- The middle is close to Zipf. A least-squares line through the 991 terms at ranks 10 to 1,000 has slope $-0.974$ and intercept $c = 149{,}285$, which is $0.112\,T$. Over that range the product $i \cdot \mathit{cf}_i$ is roughly constant, which is what $\mathit{cf}_i = c/i$ says. Divided by $T$ to make it a share, the product is $6 \times 25{,}383 / 1{,}330{,}267 = 0.114$ for said (rank 6), $0.116$ for oil (rank 47), and $0.114$ for zinc (rank 1,314).
- The head is flatter. The has a share of $0.052$, not $0.11$. The most frequent words occur less often than a line through the middle predicts.
- The tail is steeper. Beyond about rank 1,000 the points fall away faster than slope $-1$; a fit to ranks 3,000 to 10,000 has slope $-1.69$. The tail ends in horizontal steps at frequencies 3, 2, and 1, since many terms share each small integer frequency. These tied terms are so numerous that a line fitted through every point is dominated by them and has slope $-1.557$. That is why the fit in the code at the end depends so much on which ranks it includes.
The cumulative shares show the same picture. The ideal column uses $H_k / H_M$ with the collection's own vocabulary size, $M = 29{,}187$, for which $H_M = 10.859$ (all shares rounded to one decimal):
| Top $k$ terms | Ideal Zipf | Reuters-21578 |
|---|---|---|
| 1 | 9.2% | 5.2% |
| 10 | 27.0% | 22.3% |
| 100 | 47.8% | 49.1% |
| 1,000 | 68.9% | 79.2% |
The real head is lighter than the ideal one, and the real tail is lighter still, so the middle ranks carry more of the text than Zipf predicts. Half of all tokens come from the 109 most frequent terms, 0.37 percent of the vocabulary.
The long tail
The head carries the tokens. Where does the vocabulary sit? In the tail: Zipf's law also predicts how many terms are rare. Here is the plan of the derivation. Turned around, the law says how many terms have at least a given frequency. Subtracting two such counts gives the number with exactly that frequency.
The formal version. The model is the law read with whole-number frequencies: the term at rank $i$ occurs $\lfloor c/i \rfloor$ times, and the vocabulary consists of the ranks at which that is at least 1.
- Terms with frequency at least $f$. For a whole number $f \ge 1$, $\lfloor c/i \rfloor \ge f$ holds exactly when $c/i \ge f$, that is, when $i \le c/f$. So $\lfloor c/f \rfloor$ terms have frequency at least $f$, which is $c/f$ to within 1.
- The vocabulary. With $f = 1$, the terms are the ranks $i \le c$. The vocabulary has $M = \lfloor c \rfloor$ terms: in this model the constant is the vocabulary size.
- Terms with frequency exactly $f$. These are the terms with frequency at least $f$ that do not have frequency at least $f + 1$. To within 1 at each end, their number is
$$\frac{c}{f} - \frac{c}{f+1} = \frac{c\,(f+1) - c f}{f(f+1)} = \frac{c}{f(f+1)}.$$
- As a share. Dividing by the vocabulary size $M = c$, the proportion of the vocabulary occurring exactly $f$ times is about $1/(f(f+1))$: one half for $f = 1$, one sixth for $f = 2$, one twelfth for $f = 3$.
The shares add up to the whole vocabulary, as they must. Since $\frac{1}{f(f+1)} = \frac{1}{f} - \frac{1}{f+1}$, the sum over $f = 1, \dots, F$ telescopes to $1 - \frac{1}{F+1}$, which tends to 1. The same identity gives the share of terms occurring at most $F$ times, $F/(F+1)$: five sixths of the vocabulary occurs at most five times.
This is an idealized estimate, and it is not the model of the previous section. There the vocabulary size was given and the constant followed, $c = T/H_M$. Here the constant is the vocabulary size. The two agree only for a collection with $T = M H_M$ tokens. For $M = 29{,}187$ that would be about 317,000 tokens, and Reuters-21578 has 4.2 times as many. (With the whole-number frequencies of this section the total is $\sum_{i} \lfloor M/i \rfloor = 304{,}602$ tokens, and Reuters-21578 has 4.4 times as many.) Seen the other way, the middle of the Reuters curve has $i \cdot \mathit{cf}_i \approx 0.114\,T \approx 152{,}000$. An exact Zipf law with that constant would run on to rank 152,000 before reaching frequency 1, and the real vocabulary ends at rank 29,187, a fifth of the way there. That is the steep tail of the figure in another form. The exact law does not describe the tail of this collection, so the shares it predicts for the rarest terms should be read as rough guides. The tail of a finite collection is also shaped by sampling and by text processing. Croft, Metzler, and Strohman (2010, §4.2) use this argument to conclude that about half of the distinct terms in a large collection occur only once. Such terms are called hapax legomena. They include genuine rare words, but also names, misspellings, codes, and tokenization accidents. They make up much of the dictionary while contributing very little to the postings.
Reuters-21578 can be compared with the prediction directly (proportions of the vocabulary, rounded to three decimals):
| Frequency $f$ | Terms | Share of vocabulary | Ideal $1/(f(f+1))$ |
|---|---|---|---|
| 1 | 10,559 | 0.362 | 0.500 |
| 2 | 4,212 | 0.144 | 0.167 |
| 3 | 2,347 | 0.080 | 0.083 |
| 4 | 1,639 | 0.056 | 0.050 |
| 5 | 1,081 | 0.037 | 0.033 |
From $f = 3$ on, the prediction is close. At $f = 1$ it is too high, the same shortfall seen in the 36 percent of this wiki's terms that occur once, reported at the end. Terms occurring at most five times are 19,838 of the 29,187, or 68.0 percent, against the predicted five sixths (83.3 percent). The 10,559 hapax legomena are 36 percent of the dictionary but only 0.8 percent of the tokens.
A random sample of 25 of them shows the mixture described above:
- misspellings (certian, microgrpahics);
- surnames and company names (haarmann, jepson, sobey);
- ticker-like codes (cvsi, rhds);
- ordinary but uncommon words (augers, custody, unflappable).
Consequences of Zipf's law for indexing
What does the shape of the distribution mean for an index? It explains several design decisions:
- Stop words. The handful of terms at the top of the ranking are mostly function words. They occur in nearly every document and have the longest postings lists. Removing them was historically the cheapest way to shrink an index. The stop words article discusses why that trade is less attractive today.
- Luhn's significance band. Hans Peter Luhn (1958) argued that the words most useful for representing a document's content lie in the middle of the frequency range. The most frequent words are too common to discriminate between documents, and the rarest are too infrequent to be reliable. Term weighting schemes such as TF-IDF later replaced fixed frequency cut-offs with continuous weights that express the same idea.
- Skewed postings. A few postings lists are enormous and most are tiny. In Reuters-21578, the 97 terms that occur in at least 1,000 stories hold 33 percent of all postings. The 12,769 terms that occur in exactly one story hold under 2 percent. Compression methods are designed around this: the long lists of frequent terms contain many small gaps between document numbers, which can be stored in very few bits. The index compression article measures the effect on the same collection. Queries are skewed in the same way, a few repeated very often, which is what makes caching work: caching in search engines derives the hit rate of a cache under a Zipf law of query popularity.
Heaps' law
Zipf's law describes a collection of fixed size. What happens to the vocabulary as the collection grows? Harold Heaps (1978) described the relationship that now bears his name:
$$M = k \, T^{b},$$
where $M$ is the vocabulary size, $T$ is the number of tokens in the collection, and $k$ and $b$ are parameters fitted to the collection. Typical values are $30 \le k \le 100$ and $b \approx 0.5$ (Manning, Raghavan, and Schütze, 2008, §5.1.1). On logarithmic axes this too is a straight line, $\log_{10} M = \log_{10} k + b \log_{10} T$, with slope $b$ and intercept $\log_{10} k$.
Fitting it. The collection is read in a fixed order, and the vocabulary size $M_j$ is recorded at checkpoints $T_j$. The parameters are those of the least-squares line through the points $(\log_{10} T_j, \log_{10} M_j)$: the slope is $b$, and $k$ is 10 raised to the intercept. The course text's line for RCV1 is also a least-squares fit in log space, $\log_{10} M = 0.49 \log_{10} T + 1.64$, from which $k = 10^{1.64} \approx 44$ (§5.1.1, Figure 5.1). The law is an empirical description. The course text motivates it only as the simplest possible relationship between the two sizes, a straight line in log-log space.
For the Reuters RCV1 collection, Manning, Raghavan, and Schütze fit $k = 44$ and $b = 0.49$. For the first 1,000,020 tokens the law predicts
$$M = 44 \times 1{,}000{,}020^{0.49} \approx 38{,}323$$
distinct terms. The actual count is 38,365, a close fit. They report that the fit is excellent for $T > 10^{5}$. Extrapolating to the whole collection is less reassuring, and the extrapolation is this article's, not the textbook's. The book's Table 4.2 lists RCV1 as $10^{8}$ tokens, which gives
$$M = 44 \times (10^{8})^{0.49} \approx 365{,}976$$
terms, close to the vocabulary of about 400,000 in the same table. But that round figure stands for 96,969,056, the number the table's caption gives, and in the book's Table 5.1 that is the count of nonpositional postings after case folding. The same line of Table 5.1 counts 179,158,204 tokens and 391,523 terms. For that many tokens the law predicts
$$M = 44 \times 179{,}158{,}204^{0.49} \approx 487{,}000$$
terms, 24 percent more than the 391,523 counted.
Heaps' law on a real collection
Back to Reuters-21578. Reading its stories in order of their Reuters identifiers, which is roughly the order in which they were filed, and recording the vocabulary size after every 1,000 tokens gives 1,330 checkpoints. A least-squares line through their logarithms gives
$$M = 17.6 \, T^{0.527}.$$
The exponent is close to the textbook's typical value. The constant is below the typical range of 30 to 100. The two parameters are not independent. A line in log space that is slightly steeper must start lower to pass through the same points, so a larger $b$ comes with a smaller $k$, as the fit to part of the collection below also shows.
With $T = 1{,}330{,}267$ the fit predicts 29,550 terms, against 29,187 observed, 1.2 percent too many. That prediction uses the unrounded parameters, $k = 17.6163$ and $b = 0.52656$. The rounded values printed above do not reproduce it: $17.6 \times 1{,}330{,}267^{0.527} \approx 29{,}705$. The reason is that the exponent multiplies $\ln T = 14.1$, so the 0.0004 gained in rounding $b$ up to 0.527 raises the prediction by $14.1 \times 0.0004$, about 0.6 percent (rounding $k$ down takes back 0.1 percent). Every prediction in this section is computed from the unrounded parameters.
Example (a collection twice as large). If Reuters-21578 were twice as large, with the same kind of text, the fit predicts that its vocabulary would grow by a factor of about 1.44. Since $M$ grows as $T^{b}$, multiplying the collection by a factor $m$ multiplies the vocabulary by $m^{b}$, and $2^{0.527} \approx 1.44$. The table extends the prediction further:
| Collection size | Tokens | Predicted vocabulary | Growth factor $m^{0.527}$ |
|---|---|---|---|
| Reuters-21578 | 1.33 million | 29,550 | 1 |
| twice as large | 2.66 million | 42,567 | 1.44 |
| ten times | 13.3 million | 99,341 | 3.36 |
| a hundred times | 133 million | 333,959 | 11.30 |
A hundred times the text yields only about eleven times the vocabulary.
How far can the law be trusted?
This kind of extrapolation is also where the law is weakest. Fitting only the first 10 percent of the collection (133,000 tokens) gives $k = 7.6$ and $b = 0.603$. That predicts 37,442 terms for the whole collection, instead of the 29,187 observed.
Why is the exponent fitted on a small sample too large? Because the curve is not quite straight on logarithmic axes. Its slope is steeper over the first few thousand tokens, when even common words are still appearing for the first time. It flattens as the collection grows, as the right-hand panel of the figure shows. Extrapolations over two orders of magnitude deserve the same caution. The RCV1 one above, from a million tokens to 179 million, came out 24 percent too high.
The order of the text matters as well. Shuffling all the tokens of the collection into a random order destroys the grouping of words into stories and of stories into weeks of news. Refitting then gives $b = 0.465$ (median over 30 shuffles with different random seeds, interquartile range 0.463 to 0.467). Halfway through the shuffled stream, the vocabulary already holds a median of 22,387 terms, against 20,553 in the original order.
In real text, rare words cluster. A story about a company names it several times, and a news event introduces its vocabulary at the time it happens. New terms are therefore revealed later than random sampling would reveal them, and the real curve keeps climbing longer.
Consequences of Heaps' law for indexing
The important property of Heaps' law is that $b < 1$ but $b > 0$. Vocabulary grows more slowly than the collection, roughly with its square root, but under the law it never stops growing. Doubling a collection's size increases its vocabulary by a factor of about $2^{0.5} \approx 1.4$. New names, products, technical terms, and misspellings keep appearing however much text has already been seen. This has practical consequences:
- Dictionary size. The dictionary of a large collection does not level off at the size of a natural-language vocabulary. With the RCV1 parameters the law gives about 487,000 terms at 179 million tokens and several million at a hundred times that, and the dictionary must be designed to hold them. That is one motivation for dictionary compression.
- Dynamic indexes. A system that adds documents over time will keep encountering terms it has never seen, so an indexer cannot fix its vocabulary in advance. The index construction algorithms are designed with this in mind.
- Out-of-vocabulary queries. A query term absent from the dictionary is common, not exceptional. Systems respond with spelling correction, or, in neural models, with subword tokenization. A learned sparse index such as SPLADE takes the second route: its terms are the 30,522 word pieces of its encoder's vocabulary, a dictionary that does not grow with the collection.
How the two laws relate
Are these two separate facts about text? No. A vocabulary with a long tail of rare terms is exactly a vocabulary that keeps growing, because every new stretch of text draws some terms from that tail for the first time. Under suitable assumptions, Heaps' law can be derived from a Zipf-like frequency distribution (van Leijenhorst and van der Weide, 2005).
A version of the argument that can be proved in a few lines shows where the exponent comes from. In one sentence: after $T$ tokens we have seen roughly the terms we expected to see at least once, and how many those are depends on how fast the Zipf curve falls.
The model. Text is generated by drawing each token independently, with the term of rank $r$ drawn with probability $p_r = r^{-s}/Z$, for $r = 1, 2, 3, \dots$ without end. Here $s > 1$ is the Zipf exponent, and $Z = \sum_{r \ge 1} r^{-s}$ makes the probabilities sum to one. The sum is finite exactly because $s > 1$. Let $M_T$ be the number of distinct terms among the first $T$ tokens.
Theorem. Let $r^{*} = (T/Z)^{1/s}$, and suppose $r^{*} \ge 1$. Then the expected vocabulary satisfies
$$\left(1 - \tfrac{1}{e}\right) \lfloor r^{*} \rfloor \;\le\; E[M_T] \;\le\; \frac{s}{s-1}\, r^{*} + 1.$$
Both bounds are constant multiples of $T^{1/s}$, apart from the rounding and the added 1. So in this model the expected vocabulary grows as Heaps' law says, with exponent $b = 1/s$, and, as $T$ grows, the constant $k$ lies between $0.63\,Z^{-1/s}$ and $\frac{s}{s-1} Z^{-1/s}$.
Proof. The plan: write the expected vocabulary as a sum over ranks, and bound the ranks up to $r^{*}$ and the ranks beyond it separately.
- The expectation as a sum. The term of rank $r$ is missing from the first $T$ tokens with probability $(1 - p_r)^{T}$, since the draws are independent. It is present with probability $q_r = 1 - (1 - p_r)^{T}$. The number of distinct terms is the sum, over all ranks, of an indicator that is 1 when the term is present, and the expectation of a sum is the sum of the expectations. So $E[M_T] = \sum_{r \ge 1} q_r$.
- The dividing rank. A term of rank $r$ is expected to occur $T p_r$ times. The number $r^{*}$ is where that expectation equals 1: solving $T (r^{*})^{-s} / Z = 1$ gives $r^{*} = (T/Z)^{1/s}$. Since $p_r$ decreases with $r$, $T p_r \ge 1$ for $r \le r^{*}$ and $T p_r < 1$ for $r > r^{*}$.
- Lower bound. For every real $x$, $1 - x \le e^{-x}$, so $(1 - p_r)^{T} \le e^{-T p_r}$. For a rank $r \le r^{*}$ this is at most $e^{-1}$, so $q_r \ge 1 - 1/e$. There are $\lfloor r^{*} \rfloor$ such ranks, and the remaining $q_r$ are not negative. Hence $E[M_T] \ge (1 - 1/e) \lfloor r^{*} \rfloor$.
- Upper bound, ranks up to $r^{*}$. Each $q_r$ is at most 1, so the ranks $r \le \lfloor r^{*} \rfloor$ contribute at most $r^{*}$, and the single rank $\lfloor r^{*} \rfloor + 1$ contributes at most 1.
- Upper bound, ranks beyond. A term is present only if at least one of the $T$ draws produces it, and each draw does so with probability $p_r$, so $q_r \le T p_r$. For $r \ge \lfloor r^{*} \rfloor + 2$ we have $r - 1 > r^{*}$. Since $x^{-s}$ decreases, $r^{-s} \le \int_{r-1}^{r} x^{-s}\,dx$, and adding these over all such $r$ gives
$$\sum_{r \ge \lfloor r^{*} \rfloor + 2} T p_r \;\le\; \frac{T}{Z} \int_{r^{*}}^{\infty} x^{-s}\,dx = \frac{T}{Z} \cdot \frac{(r^{*})^{1-s}}{s-1} = \frac{r^{*}}{s-1},$$
where the last step uses $T (r^{*})^{-s}/Z = 1$ from step 2.
- Together. $E[M_T] \le r^{*} + 1 + \frac{r^{*}}{s-1} = \frac{s}{s-1} r^{*} + 1$. $\blacksquare$
Step 5 is where the tail of rare terms enters. The terms beyond $r^{*}$ that have been seen anyway add a quantity of the same form as $r^{*}$ itself, so they change the constant and not the exponent.
Example (steep and shallow curves). A steep Zipf curve, in which rare terms are very rare, gives slower vocabulary growth than a shallow one. A larger $s$ means a smaller $b = 1/s$. When rare terms are rarer, fewer of them turn up in each new stretch of text.
Two computations check the theorem. The first evaluates the sum of step 1 exactly, for $T = 1{,}000{,}000$ and a vocabulary cut off at two million ranks, with $Z$ summed over those ranks. The proof applies to such a cutoff unchanged, as long as it lies beyond $r^{*}$: every step bounds a sum over ranks, and removing ranks beyond $r^{*}$ removes only terms that are not negative from the sums of steps 3 and 5.
| $s$ | $Z$ | $r^{*}$ | Lower bound | $E[M_T]$ | Upper bound |
|---|---|---|---|---|---|
| 1.5 | 2.611 | 5,274 | 3,333 | 13,586 | 15,823 |
| 2 | 1.645 | 780 | 492 | 1,381 | 1,560 |
The second is a simulation. Drawing a million tokens from such a distribution over two million ranks, and fitting Heaps' law by least squares in log space as for Reuters, gives $b = 0.661$ for $s = 1.5$ and $b = 0.502$ for $s = 2$. These are medians over 30 seeds, with interquartile ranges 0.658 to 0.664 and 0.495 to 0.507, against predictions of 0.667 and 0.5.
Example (a common misstatement). "Heaps' law follows from Zipf's law, with $b = 1/s$" is true of the model above and needs two qualifications. First, it needs $s > 1$. For $s \le 1$ the sum $\sum r^{-s}$ has no finite value, so the model exists only with a finite vocabulary, and then the vocabulary cannot grow without limit. For $s < 1$ the value $b = 1/s > 1$ would in any case be impossible for large $T$, since a collection of $T$ tokens has at most $T$ distinct terms. At $s = 1$ it would mean a vocabulary growing in proportion to the text. Zipf's law in its exact form, with $s = 1$, is such a case: the worked problems show that it gives a vocabulary of fixed size $c$ for a given collection. Second, the relation does not predict the exponent of Reuters-21578. The slope of its rank-frequency curve is $-0.974$ through the middle ranks and $-1.69$ over ranks 3,000 to 10,000. The first is below 1 in size, where the theorem does not apply. The second would give $b = 1/1.69 = 0.59$, against the fitted 0.527.
Real text is not drawn independently, as the shuffling experiment showed, and its rank-frequency curve is not one straight line. So the relation explains the form of Heaps' law better than it predicts a particular collection's exponent.
Worked problems
The first problem is Exercise 5.18 of the course text (Manning, Raghavan, and Schütze, 2008, at the end of §5.3). The other three are posed on the Reuters-21578 measurements of this article. Every Reuters-21578 number is recomputed and checked in tools/experiments/indexing/reuters_laws.py.
Problem 1 (Exercise 5.18: finite and infinite vocabularies). (i) Show that the size of the vocabulary is finite according to Zipf's law and infinite according to Heaps' law. (ii) Decide whether Heaps' law can be derived from Zipf's law.
Solution. (i) Zipf's law gives the term at rank $i$ the collection frequency $c/i$, for a constant $c$ of the collection. A term that is in the vocabulary occurs at least once, so its rank satisfies $c/i \ge 1$, that is, $i \le c$. The vocabulary therefore has at most $c$ terms, a finite number. This is step 2 of the derivation in the section on the long tail.
Heaps' law gives $M = k\,T^{b}$ with $k > 0$ and $b > 0$. For any number $V$, however large, $k\,T^{b} > V$ as soon as $T > (V/k)^{1/b}$. So the law sets no ceiling on the vocabulary: as the collection grows without limit, so does $M$. For a collection of a given size both laws give a finite number. The difference is that Zipf's law, read for one collection, fixes the vocabulary at $c$, while Heaps' law says how it changes when the collection grows.
(ii) Not from the exact law with exponent 1, and yes from a Zipf-like law with exponent $s > 1$.
Assume that each collection, whatever its size, follows the exact law down to frequency 1. Then the vocabulary is $M = c$ and the number of tokens is $T = \sum_{i=1}^{M} c/i = M H_M$. With $H_M \approx \ln M + 0.577$ this reads $T \approx M(\ln M + 0.577)$: the number of tokens per distinct term, $T/M = H_M$, grows only as the logarithm of the vocabulary. A power law $M = k\,T^{b}$ with $0 < b < 1$ gives $T = (M/k)^{1/b}$, and so $T/M = M^{1/b - 1}/k^{1/b}$, a positive power of $M$ (for $b = 0.5$ it is $M/k^{2}$). A positive power of $M$ outgrows $\ln M$, so the two cannot both hold over a wide range of collection sizes. Read this way, the exact law makes the vocabulary grow roughly as $T/\ln T$, faster than any $T^{b}$ with $b < 1$. On Reuters-21578 the difference is visible: $M H_M = 29{,}187 \times 10.859 \approx 317{,}000$, and the collection has 1,330,267 tokens, 4.2 times as many.
For a Zipf-like law with exponent $s > 1$, the theorem of the section on how the two laws relate gives the derivation, for text drawn independently: the expected vocabulary lies between two constant multiples of $T^{1/s}$, which is Heaps' law with $b = 1/s$. Van Leijenhorst and van der Weide (2005) give a derivation under more general assumptions.
Problem 2 (a Heaps fit from two points). Reading Reuters-21578 in order, the vocabulary is 9,084 terms after 133,000 tokens and 29,185 terms after 1,330,000 tokens. Find the Heaps' law through these two points. Use it to predict the vocabulary after 665,000 tokens, where the actual count is 20,553.
Solution. Two points determine the two parameters. Dividing $M_2 = k\,T_2^{b}$ by $M_1 = k\,T_1^{b}$ removes $k$:
$$\frac{M_2}{M_1} = \left(\frac{T_2}{T_1}\right)^{b}, \qquad b = \frac{\log_{10}(M_2/M_1)}{\log_{10}(T_2/T_1)} = \frac{\log_{10}(29{,}185/9{,}084)}{\log_{10} 10} = \log_{10} 3.2128 = 0.5069.$$
Then $k = M_1 / T_1^{b} = 9{,}084 / 133{,}000^{0.5069} = 22.97$. The law through the two points is $M = 22.97\,T^{0.5069}$.
At $T = 665{,}000$ it predicts $22.97 \times 665{,}000^{0.5069} \approx 20{,}539$ terms (computed from the unrounded parameters). The actual count is 20,553, so the prediction is 14 terms short, less than 0.1 percent. A tenfold increase in tokens multiplied the vocabulary by 3.21, not by 10.
The two-point parameters differ from the least-squares fit to all 1,330 checkpoints, $k = 17.6$ and $b = 0.527$. The least-squares line also has to pass near the early checkpoints, where the curve is steeper (the fit to the first 10 percent alone has $b = 0.603$), and its slope is the higher of the two. At 665,000 tokens the two-point law is the closer: the least-squares fit predicts about 20,510 terms there. Outside the range of the two points the laws disagree more: at ten times the collection the two-point law predicts about 93,800 terms and the least-squares fit 99,341. Neither is to be trusted there, for the reason given in the section on how far the law can be trusted.
Problem 3 (half of the tokens under an ideal Zipf law). Under an exact Zipf law over the 29,187 terms of Reuters-21578, find the number of top terms that carry half of all tokens, and compare it with the 109 terms measured.
Solution. The top $k$ terms carry the share $H_k / H_M$, so we need the smallest $k$ with $H_k \ge H_M / 2$. Here $H_M = 10.859$ and $H_M/2 = 5.4294$. With the approximation $H_k \approx \ln k + 0.5772$, the condition is $\ln k \ge 5.4294 - 0.5772 = 4.8522$, or $k \ge e^{4.8522} = 128.0$. The exact harmonic numbers settle it: $H_{127} = 5.4253$, just below the target, and $H_{128} = 5.4331$, just above. So 128 terms carry half of the tokens under the ideal law, 0.44 percent of the vocabulary.
The measured number is 109. The real collection reaches one half sooner, although its first few terms carry less than the ideal law gives them, because its ranks from about 10 to 100 carry more: the table of cumulative shares shows the ideal law ahead at 10 terms (27.0 against 22.3 percent) and behind at 100 (47.8 against 49.1 percent).
Problem 4 (how many terms occur ten times). Use the long-tail estimate to predict how many of the 29,187 terms of Reuters-21578 occur exactly ten times, and the share that occur at most five times. Compare with the collection.
Solution. The estimate gives the share $1/(f(f+1))$ for frequency $f$. For $f = 10$ that is $1/110$, or 0.91 percent, which is $29{,}187/110 \approx 265$ terms. The collection has 369 such terms, 1.26 percent, about 1.4 times the prediction.
The share occurring at most five times is the telescoping sum
$$\sum_{f=1}^{5} \frac{1}{f(f+1)} = \left(1 - \tfrac12\right) + \left(\tfrac12 - \tfrac13\right) + \left(\tfrac13 - \tfrac14\right) + \left(\tfrac14 - \tfrac15\right) + \left(\tfrac15 - \tfrac16\right) = 1 - \tfrac16 = \tfrac56,$$
or 83.3 percent. The collection has $10{,}559 + 4{,}212 + 2{,}347 + 1{,}639 + 1{,}081 = 19{,}838$ such terms, 68.0 percent. The estimate puts too much of the vocabulary at frequency 1 (50 percent against 36.2) and too little at frequencies such as 10. Both differences say the same thing: the real vocabulary is less concentrated at the very lowest frequencies than the estimate makes it.
What we have learned
Together the two laws describe the basic statistical situation every indexer faces. A small set of terms accounts for most of the text and the longest postings lists: in Reuters, 109 terms carry half the tokens. A vast and still-growing set of rare terms accounts for most of the vocabulary: 36 percent of the Reuters dictionary occurs once, and a collection a hundred times larger would hold about eleven times the vocabulary. The inverted index, its construction, and its compression are all shaped by having to handle both at once.
The head of the ranking has an immediate consequence. Its top terms are the, of, to, and also said and mln, words that appear in most stories and say little about any one of them. The next article, Stop Words, measures what removing such words from an index saves and what it costs, and sets out why practice moved away from removing them.
Measuring them on a collection
Both laws are straight lines on logarithmic axes, so their parameters can be estimated by linear regression on the logarithms. A least-squares fit in log space is a quick diagnostic, not a rigorous estimate of a power-law exponent. Its result depends on which ranks are included, and the many tied low-frequency terms in the tail pull on the slope. The following code fits both to a list of texts large enough to give several Heaps' law checkpoints:
import re
from collections import Counter
import numpy as np
def tokens(text):
return re.findall(r"[a-z]+", text.lower())
def zipf_slope(counts):
freqs = np.array(sorted(counts.values(), reverse=True), dtype=float)
ranks = np.arange(1, len(freqs) + 1)
slope, _ = np.polyfit(np.log10(ranks), np.log10(freqs), 1)
return slope
def heaps_fit(stream, every=1000):
seen, xs, ys = set(), [], []
for i, tok in enumerate(stream, 1):
seen.add(tok)
if i % every == 0:
xs.append(i)
ys.append(len(seen))
b, log_k = np.polyfit(np.log10(xs), np.log10(ys), 1)
return 10 ** log_k, b
texts = [...] # the documents, as a list of strings
stream = [t for text in texts for t in tokens(text)]
counts = Counter(stream)
print(zipf_slope(counts), heaps_fit(stream))
Applied to the articles of this wiki, at a point when it held 28 articles and 51,444 tokens, the code found 4,985 distinct terms. The ten most frequent were the, and, a, of, in, is, to, that, for, and it, all function words. 36 percent of the terms occurred exactly once. The fitted Zipf slope was $-1.18$, close to the ideal $-1$.
Heaps' law fit with $k \approx 3.8$ and $b \approx 0.67$, outside the typical ranges given above. That is what one should expect from a collection this small and this uniform in subject and style. Parameters fitted on a small sample are poor guides to a large collection, which is one reason textbooks quote values from collections such as RCV1.
References
- Croft, W. B., Metzler, D., and Strohman, T. (2010). Search Engines: Information Retrieval in Practice, section 4.2. Addison-Wesley. record · 12 pages
- Heaps, H. S. (1978). Information Retrieval: Computational and Theoretical Aspects. Academic Press. record
- Knuth, D. E. (1997). The Art of Computer Programming, Volume 1: Fundamental Algorithms, 3rd edition, section 1.2.7. Addison-Wesley. record
- Lewis, D. D. (1997). Reuters-21578 text categorization test collection, distribution 1.0. record · 11 pages
- Luhn, H. P. (1958). The automatic creation of literature abstracts. IBM Journal of Research and Development, 2(2), 159–165. record · 4 pages
- Manning, C. D., Raghavan, P., and Schütze, H. (2008). Introduction to Information Retrieval, sections 4.2, 5.1, 5.3 and 6.2. Cambridge University Press. record · 102 pages
- Manning, C. D., and Schütze, H. (1999). Foundations of Statistical Natural Language Processing, section 1.4.3. MIT Press. record · 2 pages
- van Leijenhorst, D. C., and van der Weide, T. P. (2005). A formal derivation of Heaps' Law. Information Sciences, 170(2–4), 263–272. record
- Zipf, G. K. (1949). Human Behavior and the Principle of Least Effort. Addison-Wesley. record
Reviewed: verified, 16 Nov 2026 · checked against sources, 23 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 12 revisions