Majid Al-RaimiZipf and Heaps: the shape of vocabulary

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
Understood
0/4 concepts

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

  1. State Zipf's law and compute f(R) from f(1) and α.
  2. Explain why a power law is a straight line on log-log axes, and diagnose the head and tail deviations on a real corpus.
  3. State Heaps' law, compute |V| for given k, β and N, and compare the English and Arabic Wikipedia fits.
  4. Argue from Heaps' law why OOV words are unavoidable and why about half of all types are hapax legomena.
  5. 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.

WordFrequency fRank Rf × R
the3,33213,332
and2,97225,944
a1,77535,325
he877108,770
turned5120010,200
applausive18,0008,000
Tom Sawyer, from Manning and Schütze, Table 1.3 (the book prints 5,235 for "a", a transposition of 1,775 × 3 = 5,325)
f(R)1Rα,α1f(R) \propto \frac{1}{R^{\alpha}}, \qquad \alpha \approx 1
Zipf's law: frequency as a power law in rank

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.

f(R)f(1)Rαf(R) \approx \frac{f(1)}{R^{\alpha}}
The form you compute with

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.

Twenty bars shrinking by 1 / R, then a long tail of hairlines. The top three bars fall short of the teal curve fitted through the bulk: the head of a real corpus is flatter than the law.

Worked example

From f(1) to f(R)

  1. Fix the anchor

    Suppose the top word occurs f(1) = 1,000,000 times in a large Corpus.
  2. Apply α = 1

    f(10) = 1,000,000 / 10 = 100,000 and f(1000) = 1,000,000 / 1000 = 1,000.
  3. 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.
  4. Three exponents side by side

    αf(10)f(100)f(1000)f(100,000)
    1.00100,00010,0001,00010
    0.93117,49013,8041,62222
    1.1079,4336,3105013

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?

With α = 1, about 2,000,000 / 1,000 = 2,000. With α = 0.9, 1000^0.9 = 10^2.7 ≈ 501, so about 2,000,000 / 501 ≈ 3,990. The smaller exponent leaves the rank 1,000 word almost twice as frequent.

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.

logf=logCαlogR\log f = \log C - \alpha \log R
A power law is linear in log-log coordinates

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.

A straight line of slope −α fitted through the bulk on log-log axes, then the empirical curve laid over it: bent at the head, on the line in the middle, and broken into integer steps at 3, 2 and 1 in the tail.

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.

SimulatorZipf explorer

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.

Line anchor
10⁰10¹10²10⁰10¹10²rank Rfrequency fsample corpus441 / R^0.93
0.93
Fit versus sample
rankpredictedobserved
1051.848
1006.16
5001.41

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?

Taking logs of f = C R^−α gives log f = log C − α log R, which is linear in log R. The slope is −α, so the steeper the line, the faster frequency falls with rank.

Recall

Name the two visible departures from a Zipf line on a real corpus and give the cause of each.

Head flattening: the top few words drop off more slowly than R^−α, which Mandelbrot models with a shift (R + ρ); it shows as a bulge above a line pinned to f(1) (the English curve) or a shortfall under a line fitted through the bulk. Tail staircase: counts are integers, so all types with frequency 1, then 2, then 3 share one height and draw as horizontal steps.

Quick check

On a log-log rank-frequency plot, what does the slope of the fitted straight line equal?

Heaps' law: the vocabulary never stops growing

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.

CorpusInstances NTypes |V|
Shakespeare884 thousand29 thousand
Brown corpus1 million38 thousand
Switchboard telephone speech2.4 million20 thousand
COCA440 million2 million
Google n-grams1 trillion13 million
Bigger corpus, bigger vocabulary, no ceiling (SLP3 chapter 2, Figure 2.1; the Google count includes only types seen 40 or more times)

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.

V=kNβ,0<β<1|V| = k N^{\beta}, \qquad 0 < \beta < 1
Heaps' (Herdan's) law

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.

Three fitted vocabulary curves drawn out to a million tokens. The English curve's slope drops from 187 to 27 new types per thousand tokens, and the dotted ceiling fades because there is none.

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.

Corpuskβ|V| at 10⁴|V| at 10⁶|V| at 10⁸New types per 1,000 tokens
English Wikipedia14.9470.5833,21047,049689,531187 at N = 10⁴
Arabic Wikipedia73.3210.5339,936115,6721,346,56849 at N = 2.5 × 10⁵
Undotted Arabic Wikipedia90.4910.5069,56398,3121,010,661not reported
Slide 14 fits with |V| computed at three corpus sizes

Worked example

English fit at one million tokens

  1. Exponent

    N^β = (10^6)^0.583 = 10^(6 × 0.583) = 10^3.498.
  2. Evaluate the power

    10^3.498 ≈ 3,147.8.
  3. Multiply by k

    |V| ≈ 14.947 × 3,147.8 ≈ 47,049.
  4. 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.
SimulatorHeaps explorer

Vocabulary against corpus size

0500,0001,000,000121.5K0tokens Ntypes |V|dotted: straight line to the same endpoint
14.947
0.583
Vocabulary at three corpus sizes
N|V|new types per 1,000
10⁴3,210187.2
10⁶47,04927.4
10⁸689,5314
Formula|V| = 14.947 · N^0.583Faint curves are the three slide fits for comparison.
Growth per 100× more tokens14.7×= 100^0.583Below 100 because β < 1, above 1 because β > 0: slower, never zero.

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.

Sourcekβ
SLP3, current draftnot given0.44 to 0.56
IR book, typical30 to 100≈ 0.5
Reuters-RCV1 (IR book)440.49
Earlier SLP3 drafts, larger corporanot given0.67 to 0.75
Slide 14 fits14.9 to 90.50.506 to 0.583
Reported Heaps' law constants across sources

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?

14.947 × 10^(4 × 0.583) = 14.947 × 10^2.332 ≈ 3,210 and 14.947 × 10^3.498 ≈ 47,049. The ratio is 100^0.583 ≈ 14.7, not 100.

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.

CountZipf predictionTom SawyerSample corpus
Exactly once50%49.8%56%
Exactly twice16.7%16.1%15%
Share of types with a given count: Zipf prediction against two real corpora

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.

One hundred types from a corpus. About half light up as seen once, sixteen as seen twice, and a dashed square outside the grid stands for the type that was never seen at all.

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 unitsNeural, subword units
Unit of the vocabularyWhole wordsSubword pieces, or bytes as the floor
What happens to an unseen wordMapped to <UNK> or given smoothed massComposed from known pieces, so never unknown
Where the fix livesIn the probability estimate: smoothing, backoff, interpolationIn the tokenizer: BPE, WordPiece, byte fallback
CostProbability mass spent on events never seenLonger token sequences, less interpretable units
Covered inLecture 3 and SLP3 chapter 3Part 6 of this lecture
Word-level n-gram models versus subword neural models

Recall

In one sentence, why does Heaps' law guarantee OOV words?

Because |V| = k N^β keeps increasing for every N (its derivative is always positive), new text always brings types absent from any finite training corpus.

Recall

Define hapax legomenon and give the typical share of types it accounts for.

A type occurring exactly once in a corpus. Typically about half of all types: 49.8% in Tom Sawyer, 56% in the three-chapter sample corpus.

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