ICS 582Lecture 02Part 02
Zipf and Heaps: the shape of vocabulary
Two empirical laws that explain why a few words are everywhere, most words are rare, and vocabulary never stops growing, and why that pushes NLP toward subword units.
- Concepts
- 4
- Slides
- 11-16
- Reading
- 24 min
Why this part matters
Every tokenizer, every n-gram smoother and every embedding table you will build for Arabic NLP is a response to two curves. One says that a handful of words carry most of any text while almost everything else is rare. The other says that no matter how much text you read, new words keep arriving.
Part 01 defined types and tokens and showed that corpora vary wildly in size. This part asks what the relationship between the two looks like, and the answer turns out to be the same for English, Arabic and undotted Arabic Wikipedia, with only the constants changing. Those constants are numbers you can quote in your own research on Arabic text, and the two formulas behind them are the kind of thing an exam asks you to compute with. By the end you should be able to look at a vocabulary size and say whether it is surprising, and to explain in one sentence why subword units exist.
By the end you can
- State Zipf's law and compute f(R) from f(1) and α.
- Explain why a power law is a straight line on log-log axes, and diagnose the head and tail deviations on a real corpus.
- State Heaps' law, compute |V| for given k, β and N, and compare the English and Arabic Wikipedia fits.
- Argue from Heaps' law why OOV words are unavoidable and why about half of all types are hapax legomena.
- Connect sparsity and OOV to smoothing, backoff and subword tokenization.
Take a novel, count every word, and sort the counts from largest to smallest. Manning and Schütze did exactly this for Mark Twain's Tom Sawyer: 71,370 tokens and 8,018 distinct types. The most frequent type is "the" with 3,332 occurrences, then "and" with 2,972, then "a" with 1,775. At the other end sit thousands of words like "applausive" that appear exactly once. The hundred most common types alone cover 50.9% of every token in the book.
Give each type a rank R, with R = 1 for the most frequent word. Now multiply frequency by rank. For most of the list the product hovers around a constant, roughly 8,000 to 10,000, a relationship Estoup noticed in 1916 and George Kingsley Zipf made famous in the 1930s and 1940s, so it carries Zipf's name. Zipf's law says that frequency falls off as a power of rank.
| Word | Frequency f | Rank R | f × R |
|---|---|---|---|
| the | 3,332 | 1 | 3,332 |
| and | 2,972 | 2 | 5,944 |
| a | 1,775 | 3 | 5,325 |
| he | 877 | 10 | 8,770 |
| turned | 51 | 200 | 10,200 |
| applausive | 1 | 8,000 | 8,000 |
Because the law is a proportionality, the useful working form anchors it to the top word. If you know how often the most frequent word occurs, everything else follows from the rank alone.
With α = 1 the arithmetic is nothing more than division. The word at rank 10 occurs about a tenth as often as the top word, the word at rank 1,000 about a thousandth as often. Slide 12 will show that natural language has α slightly below one, so the fall is a little gentler and the rare words a little less rare than the pure 1 / R rule predicts.
Worked example
From f(1) to f(R)
Fix the anchor
Suppose the top word occurs f(1) = 1,000,000 times in a large Corpus.Apply α = 1
f(10) = 1,000,000 / 10 = 100,000 and f(1000) = 1,000,000 / 1000 = 1,000.Apply the slide's α = 0.93
1000^0.93 = 10^(3 × 0.93) = 10^2.79 ≈ 617, so f(1000) ≈ 1,000,000 / 617 ≈ 1,622. At rank 10, 10^0.93 ≈ 8.51 gives f(10) ≈ 117,490.Three exponents side by side
α f(10) f(100) f(1000) f(100,000) 1.00 100,000 10,000 1,000 10 0.93 117,490 13,804 1,622 22 1.10 79,433 6,310 501 3
Read the table across a row and you see the fall; read it down a column and you see what α does. A smaller exponent leaves more mass in the tail, which for a language model means more probability spread across rare words. A larger exponent concentrates the text in fewer types. The exponent is a property of the corpus, so it differs slightly between languages, genres and preprocessing choices such as case folding.
Recall
The most frequent word in a corpus occurs 2,000,000 times. Under Zipf with α = 1, roughly how often does the rank 1,000 word occur? And with α = 0.9?
Quick check
A corpus follows Zipf's law with α = 1 and its top word occurs 500,000 times. About how often does the word at rank 50 occur?
Slide 12 plots rank against frequency for three corpora, English Wikipedia, Arabic Wikipedia and an undotted Arabic Wikipedia in which the dots that distinguish letters such as ب, ت and ث have been removed. Both axes are logarithmic, labelled from 10^0 to 10^6 in rank (the Arabic curve runs on to about 2 × 10^6) and to 10^7 in frequency, and two dashed straight lines, each pinned to the top word of its corpus, are drawn with α = 0.9252 for Arabic and α = 0.9290 for English. The first thing to understand is why anyone would expect a straight line at all.
Write Zipf's law as an equation with a constant, f(R) = C · R^−α, and take the logarithm of both sides. A product becomes a sum and a power becomes a multiplier, which is the whole trick.
On axes that show log R and log f, this is the equation of a straight line with slope −α and intercept log C, which is log f(1) since log 1 = 0. The Stanford IR book states the special case directly: log cf_i = log c + k log i with k = −1. So the two fitted slopes on the slide, −0.9252 and −0.9290, are the exponents of the two Zipf fits, and the fact that they are nearly parallel says Arabic and English share the same kind of head-and-tail shape.
Two places where the real curve leaves the line
Look at the slide again and the solid curves do not follow the dashed lines everywhere. They deviate in two characteristic ways, and both are diagnostic rather than noise. At the head, the curve is flatter than the power law: the top few words are common, but the drop from rank 1 to rank 10 is smaller than R^−α demands. This is the same shortfall the Tom Sawyer table showed: f × R was 3,332 for "the" against 8,000 to 10,000 further down, and it is why Mandelbrot added the shift term (R + ρ).
How that flat head looks on a plot depends on where the line is anchored. On slide 12 both fits pass through f(1). The blue English curve bulges slightly above the purple line only for ranks 2 to about 15, then runs just under it. The Arabic curves behave differently: they dip below the red line for ranks 3 to about 50 and cross back above it. At Wikipedia scale the head deviation is small, and the head is not uniformly flatter. The flat head is clearest in small texts such as Tom Sawyer and the explorer's sample; on slide 12 the dominant departure is the tail.
At the tail, beyond about rank 10^4, the curves steepen sharply below both lines and then turn into a staircase with horizontal steps at frequencies 3, 2 and 1, stretching out to about rank 2 × 10^6 for Arabic. The reason is that a frequency is a count, and counts are integers. There is no word that occurs 1.5 times. If 400,000 types each occur exactly once, they occupy 400,000 consecutive ranks at height f = 1, which draws as one long flat step. The step at f = 2 is shorter because fewer types occur exactly twice, and so on upward until the counts are large enough that adjacent ranks rarely share a value and the curve looks smooth. The bottom step is the longest because types seen once are the largest single group, the hapax legomena that concept four returns to.
What to read off a log-log rank-frequency plot
- Slope of the fitted line
- −α, the Zipf exponent
- Intercept at log R = 0
- log f(1), the frequency of the top word
- Rightmost rank on the curve
- |V|, the number of types in the corpus
- Length of the bottom step
- The number of hapax legomena
- Bend at the head relative to the line
- How much flatter the top few words are than the law; above a line pinned to f(1), below a line fitted through the bulk
One more thing the slide shows without saying it. In the tail, the Arabic curves sit above the English one: at any low count, Arabic has more types occurring at least that often. Templatic morphology and attached clitics mean that a single Arabic lexeme surfaces as many distinct spelled forms, each of which is a separate type for a whitespace tokenizer. That is a preview of the next concept, where the same fact shows up as a larger k in Heaps' law.
Rank against frequency
The teal curve is the real count of every type in Pride and Prejudice, chapters 1 to 3 (3,438 tokens, 845 types). The dashed line is f(R) = C / R^α for the α you choose. With the best fit, C is the least-squares intercept over the top 200 ranks, currently 3.5× the top count f(1) = 125, so the head falls short of the line and the tail steps run under it.
| rank | predicted | observed |
|---|---|---|
| 10 | 51.8 | 48 |
| 100 | 6.1 | 6 |
| 500 | 1.4 | 1 |
The tail is a staircase because counts are integers: 475 types share the step at f = 1, 129 share f = 2, and 63 share f = 3.
Recall
Why is a power law a straight line on log-log axes, and what does the slope tell you?
Recall
Name the two visible departures from a Zipf line on a real corpus and give the cause of each.
Quick check
On a log-log rank-frequency plot, what does the slope of the fitted straight line equal?
Zipf describes a Corpus frozen at one size. Now let the corpus grow. Read the first thousand words of Pride and Prejudice and you meet 361 distinct types. After ten thousand words the count is 1,698; after fifty thousand, 4,304; at the end of the novel, 123,627 tokens have produced 6,298 types. Each new stretch of text adds fewer new words than the last, but it never adds none. The same pattern holds across corpora of every size.
| Corpus | Instances N | Types |V| |
|---|---|---|
| Shakespeare | 884 thousand | 29 thousand |
| Brown corpus | 1 million | 38 thousand |
| Switchboard telephone speech | 2.4 million | 20 thousand |
| COCA | 440 million | 2 million |
| Google n-grams | 1 trillion | 13 million |
The rule behind these numbers is Heaps' law, also called Herdan's law after the linguist who described it in 1960, nearly two decades before Heaps (1978). The Vocabulary size is a power of the token count, with an exponent strictly between zero and one.
The two halves of the constraint on β say two different things. Because β < 1, growth is sublinear: doubling the corpus does not double the vocabulary. Because β > 0, growth is unbounded: the derivative d|V| / dN = k β N^(β − 1) is positive for every N, so the curve keeps rising however far you extend it. SLP3 phrases the second half bluntly: no matter how big our vocabulary, we will never have a vocabulary that captures all the possible words that might occur.
The slide's three fits, and what the constants mean
Slide 14 fits the law to the same three corpora as before and reports k and β for each. The constants are worth reading as a pair. k is a multiplicative scale (the predicted |V| at N = 1) that shifts the whole curve up, and β sets the slope on log-log axes: the ratio between two corpus sizes, (N2 / N1)^β, depends on β alone. Plugging in three corpus sizes makes the comparison concrete.
| Corpus | k | β | |V| at 10⁴ | |V| at 10⁶ | |V| at 10⁸ | New types per 1,000 tokens |
|---|---|---|---|---|---|---|
| English Wikipedia | 14.947 | 0.583 | 3,210 | 47,049 | 689,531 | 187 at N = 10⁴ |
| Arabic Wikipedia | 73.321 | 0.533 | 9,936 | 115,672 | 1,346,568 | 49 at N = 2.5 × 10⁵ |
| Undotted Arabic Wikipedia | 90.491 | 0.506 | 9,563 | 98,312 | 1,010,661 | not reported |
Worked example
English fit at one million tokens
Exponent
N^β = (10^6)^0.583 = 10^(6 × 0.583) = 10^3.498.Evaluate the power
10^3.498 ≈ 3,147.8.Multiply by k
|V| ≈ 14.947 × 3,147.8 ≈ 47,049.Result
About 47 thousand types. For comparison, the Brown corpus has 38 thousand types at one million tokens (SLP3 Figure 2.1), and the IR book's Reuters fit, 44 × 1,000,020^0.49, gives 38,323 against an actual 38,365. Same ballpark, different corpus.
Vocabulary against corpus size
| N | |V| | new types per 1,000 |
|---|---|---|
| 10⁴ | 3,210 | 187.2 |
| 10⁶ | 47,049 | 27.4 |
| 10⁸ | 689,531 | 4 |
Now compare languages. At a million tokens Arabic Wikipedia reaches 115,672 types, about two and a half times the English figure, and undotted Arabic reaches 98,312. Arabic gets there because of its k, 73 against 15: templatic morphology and attached clitics such as wa-, bi- and -hu multiply the surface forms of every lexeme, so the vocabulary is large from the very first pages. Its β is lower, though, so the curve bends sooner: a hundredfold larger corpus multiplies the English vocabulary by 100^0.583 ≈ 14.7 but the Arabic vocabulary by only 100^0.533 ≈ 11.6. Removing the dots collapses distinct letters into one shape, and the fit agrees that this shrinks the vocabulary: undotted Arabic is below dotted Arabic at every size in the table. Its larger k (90 against 73) is offset by its smaller β, and the two curves cross below about 2,400 tokens, so k alone never tells you which vocabulary is bigger; evaluate k N^β at the N you care about. Parts 03 and 04 explain the morphology.
Typical values, and how the slide compares
The constants reported across five reference points are easiest to read side by side.
| Source | k | β |
|---|---|---|
| SLP3, current draft | not given | 0.44 to 0.56 |
| IR book, typical | 30 to 100 | ≈ 0.5 |
| Reuters-RCV1 (IR book) | 44 | 0.49 |
| Earlier SLP3 drafts, larger corpora | not given | 0.67 to 0.75 |
| Slide 14 fits | 14.9 to 90.5 | 0.506 to 0.583 |
SLP3 summarizes the law as vocabulary growing a little faster than the square root of the corpus length in words, with β depending on corpus size and genre. The Arabic exponents, 0.533 and 0.506, fall inside SLP3's 0.44 to 0.56 band; the English 0.583 is just above it, which SLP3's "or even higher" allows. The English k of 14.9 is on the low side, which is what you get when a fit trades a smaller k for a larger β; the two constants are not independent estimates.
Recall
Using the English Wikipedia fit (k = 14.947, β = 0.583), how many types would you expect at 10^4 tokens and at 10^6 tokens?
Quick check
With k = 73.321 and β = 0.533, roughly how many types does a one-million-token Arabic corpus have under the slide's Heaps fit?
Quick check
Which statement about vocabulary growth under Heaps' law is correct?
Put the two laws together and a design problem falls out. In Tom Sawyer, 3,993 of the 8,018 types occur exactly once, which is 49.8%. In the three chapters of Pride and Prejudice inside the explorer, 475 of 845 types occur once, or 56%. A type seen exactly once is a Hapax legomenon, from the Greek for "read only once", and in corpus after corpus these are about half of everything a model must store.
That one half is not a coincidence; it falls out of Zipf's law. With α = 1, the type at rank R has count C / R, so the number of types with count at least m is the rank at which the count drops to m, namely C / m. The types with count exactly m are then C / m − C / (m + 1) = C / (m (m + 1)), and dividing by the total C gives a share of 1 / (m (m + 1)): one half seen once, one sixth seen twice, one twelfth seen three times.
| Count | Zipf prediction | Tom Sawyer | Sample corpus |
|---|---|---|---|
| Exactly once | 50% | 49.8% | 56% |
| Exactly twice | 16.7% | 16.1% | 15% |
Baayen (2001) develops this connection between the frequency spectrum and vocabulary growth in full, and it is why the bottom step of the staircase on slide 12 is the longest.
Slides 15 and 16 list the consequences, and they form a chain rather than a list. Start with size. Heaps' law says a word-level Vocabulary on a large corpus runs to hundreds of thousands or millions of types, and every one of them needs a row in an embedding matrix and a column in the output softmax. Memory grows with |V| × d, where d is the embedding width. Then comes sparsity, which is Zipf's contribution: since half of those types belong to hapax legomena, half of the parameters in that matrix have been trained on a single example. Whatever the model learned about "applausive" it learned from one sentence.
Next comes the guarantee. Because Heaps' law has a positive derivative at every N, any finite training corpus stops somewhere on a curve that is still rising. New text therefore always contains types that were never seen, and those types are out-of-vocabulary. This is not a defect of a small corpus that a bigger one would cure; a bigger corpus only pushes the OOV rate down along the same curve, never to zero. Finally, evaluation inherits the problem. Earlier SLP3 drafts warned that mapping unseen words to an <UNK> special token lets a model buy a lower perplexity simply by choosing a smaller vocabulary, since more of the test set collapses onto one easy symbol. The current chapter 3 sidesteps the issue by making the vocabulary subword tokens, so that, in its words, the test set can never contain unseen tokens.
- Huge word-level vocabularies: memory in the embedding and output layers scales with |V|.
- Sparsity: about half of all types are hapax legomena, so about half of the rows have one training example.
- Guaranteed OOV: Heaps' law keeps rising, so new text always brings unseen types.
- Evaluation sensitivity: mapping unseen words to <UNK> changes what a perplexity number means.
Two answers to the same problem
The two families of language model respond to the chain at different points. N-gram models keep the word as the unit and repair the probability estimate: smoothing moves a little probability mass from seen events to unseen ones, and backoff falls back to a shorter context when the longer one was never observed. Lecture 3 and SLP3 chapter 3 develop both. Neural models change the unit instead. With Subword tokenization, learned by byte-pair encoding as Sennrich, Haddow and Birch showed for machine translation in 2016, a word never seen in training is spelled out of pieces that were, and with byte-level fallback there is nothing left that cannot be represented. The vocabulary becomes a design choice of tens of thousands of units rather than a fact about the corpus, which is exactly the trade part 6 examines.
| N-gram, word units | Neural, subword units | |
|---|---|---|
| Unit of the vocabulary | Whole words | Subword pieces, or bytes as the floor |
| What happens to an unseen word | Mapped to <UNK> or given smoothed mass | Composed from known pieces, so never unknown |
| Where the fix lives | In the probability estimate: smoothing, backoff, interpolation | In the tokenizer: BPE, WordPiece, byte fallback |
| Cost | Probability mass spent on events never seen | Longer token sequences, less interpretable units |
| Covered in | Lecture 3 and SLP3 chapter 3 | Part 6 of this lecture |
Recall
In one sentence, why does Heaps' law guarantee OOV words?
Recall
Define hapax legomenon and give the typical share of types it accounts for.
Quick check
A model trained on 10 million tokens meets a word absent from its training data. Which mechanism lets a modern neural model represent it anyway?
Recap
If you remember nothing else
- Rank types by count. Zipf: f(R) is proportional to 1 / R^α with α near 1 (0.9252 for Arabic and 0.9290 for English Wikipedia on slide 12).
- log f = log C − α log R is a line with slope −α. Real corpora flatten at the head (above a line pinned to f(1), below a line fitted through the bulk) and form integer steps at the tail.
- Heaps: |V| = k N^β with 0 < β < 1, sublinear but unbounded. The English fit 14.947 N^0.583 gives 47,049 types at one million tokens; Arabic 115,672; undotted Arabic 98,312.
- The larger k for Arabic reflects templatic morphology and clitics. Typical β is 0.44 to 0.56 in the current SLP3 draft, and b ≈ 0.5 with 30 ≤ k ≤ 100 in the IR book.
- Slide 14 plots the fitted formula out to 10^19 tokens. That is an extrapolation, not data.
- About half of all types are hapax legomena (49.8% in Tom Sawyer, 56% in the sample corpus). Word vocabularies are huge and sparse, and OOV words are guaranteed.
- N-gram models respond with smoothing and backoff. Neural models respond with subwords (BPE, part 6). Evaluation depends on how unseen items are treated.
- Heaps' law is a consequence of Zipf's law (Lü, Zhang and Zhou 2010), not an independent law.
Sources
- Speech and Language Processing, 3rd edition draft, chapter 2: Words and TokensBookJurafsky and Martin, Stanford UniversityCorpus size table (Figure 2.1), Heaps' law (Equation 2.5), β range 0.44 to 0.56, two-regime growth (Figure 2.2)(opens in a new tab)
- Speech and Language Processing, 3rd edition draft, chapter 3: N-gram Language ModelsBookJurafsky and Martin, Stanford UniversitySmoothing, interpolation and backoff; the current draft notes that subword vocabularies mean the test set can never contain unseen tokens (the <UNK> discussion is from earlier drafts)(opens in a new tab)
- Foundations of Statistical Natural Language Processing, chapter 1 excerptBookManning and Schütze, MIT PressTom Sawyer counts, 49.8% hapax legomena, Table 1.3 f × R products, Zipf and Mandelbrot(opens in a new tab)
- Introduction to Information Retrieval: Heaps' law, estimating the number of termsBookManning, Raghavan and Schütze, Cambridge University Press30 ≤ k ≤ 100 and b ≈ 0.5; Reuters-RCV1 fit k = 44, b = 0.49(opens in a new tab)
- Introduction to Information Retrieval: Zipf's law, modeling the distribution of termsBookManning, Raghavan and Schütze, Cambridge University Presslog cf_i = log c + k log i with k = −1; the fit is 'not particularly good, but good enough'(opens in a new tab)
- Human Behavior and the Principle of Least EffortBookZipf, Addison-Wesley, 1949Zipf's own account of the rank-frequency law and its proposed cause(opens in a new tab)
- Information Retrieval: Computational and Theoretical AspectsBookHeaps, Academic Press, 1978The original statement of the vocabulary growth law(opens in a new tab)
- Word Frequency DistributionsBookBaayen, Kluwer, 2001LNRE models and the derivation of vocabulary growth from frequency distributions(opens in a new tab)
- Zipf's Law Leads to Heaps' Law: Analyzing Their Relation in Finite-Size SystemsPaperLü, Zhang and Zhou, PLoS ONE, 2010Heaps as a derivative of Zipf; asymptotic β = 1 / α and the finite-size caveat(opens in a new tab)
- Zipf's word frequency law in natural language: A critical review and future directionsPaperPiantadosi, Psychonomic Bulletin and Review, 2014Zipf-Mandelbrot form and competing explanations(opens in a new tab)
- Zipf's, Heaps' and Taylor's Laws are Determined by the Expansion into the Adjacent PossiblePaperTria, Loreto and Servedio, Entropy, 2018Two-regime Heaps' law figure cited by SLP3(opens in a new tab)
- Neural Machine Translation of Rare Words with Subword UnitsPaperSennrich, Haddow and Birch, ACL 2016Byte-pair encoding as the subword answer to rare and unseen words(opens in a new tab)
- Wikipedia: Size of WikipediaArticleWikipediaEnglish Wikipedia at about 5.27 billion words, used to scale the slide 14 axes(opens in a new tab)
- Pride and Prejudice, plain text (eBook 1342)DocsProject GutenbergPublic domain text behind the explorer's built-in corpus and the growth figures(opens in a new tab)