# Estimating vocabulary size with Heaps’ law

Heaps’ law says that the number of unique words in a text of n words is approximated by

V(n) = K nβ

where K is a positive constant and β is between 0 and 1. According to the Wikipedia article on Heaps’ law, K is often between 10 and 100 and β is often between 0.4 an 0.6.

(Note that it’s Heaps’ law, not Heap’s law. The law is named after Harold Stanley Heaps. However, true to Stigler’s law of eponymy, the law was first observed by someone else, Gustav Herdan.)

## Demonsrating Heaps law

For a collection of related texts, you can estimate the parameters K and β from data. I decided to see how well Heaps’ law worked in predicting the number of unique words in each book of the Bible. I used the King James Version because it is easy to download from Project Gutenberg.

I converted each line to lower case, replaced all non-alphabetic characters with spaces, and split the text on spaces to obtain a list of words. This gave the following statistics:

```    |------------+-------+------|
| Book       |     n |    V |
|------------+-------+------|
| Genesis    | 38520 | 2448 |
| Exodus     | 32767 | 2024 |
| Leviticus  | 24621 | 1412 |
...
| III John   |   295 |  155 |
| Jude       |   609 |  295 |
| Revelation | 12003 | 1283 |
|------------+-------+------|
```

The parameter values that best fit the data were K = 10.64 and β = 0.518, in keeping with the typical ranges of these parameters.

Here’s a sample of how the actual vocabulary size and predicted vocabulary size compare.

```    |------------+------+-------|
| Book       | True | Model |
|------------+------+-------|
| Genesis    | 2448 |  2538 |
| Exodus     | 2024 |  2335 |
| Leviticus  | 1412 |  2013 |
...
| III John   |  155 |   203 |
| Jude       |  295 |   296 |
| Revelation | 1283 |  1387 |
|------------+------+-------|
```

Here’s a visual representation of the results.

It looks like the predictions are more accurate for small books, and that’s true on an absolute scale. But the relative error is actually smaller for large books as we can see by plotting again on a log-log scale.

## Hapax logemenon

In linguistics, a hapax logemenon is a word that only appears once in a given context. The term comes comes from a Greek phrase meaning something said only once. The term is often shortened to just hapax.

I thought it would be interesting to look at the number of hapax logemena in each book since I could do it with a minor tweak of the code I wrote for the first part of this post.

Normally if someone were speaking of hapax logemena in the context of the Bible, they’d be looking at unique words in the original languages, i.e. Hebrew and Greek, not in English translation. But I’m doing statistics here, not Bible scholarship, so I’ll look at the text I have at hand.

Here’s a plot of the number of haxap as a function of the total number of words in a book.

This looks a lot like the plot of vocabulary size and total words, suggesting the number of hapax also follow a power law like Heaps law. This is clear when we plot again on a logarithmic scale and see a linear relation.

Just to be clear on the difference between the two major sections of this post, in the first we looked at vocabulary size, the number of distinct words in each book. In the second section we looked at words that only appear once. In both cases we’re counting unique words, but unique in different senses. In the first section, unique means that each word only counts once, no matter how many times it’s used. In the second section, unique means that a work only appears once.

# Estimating vocabulary size with Heaps’ law

Heaps’ law says that the number of unique words in a text of n words is approximated by

V(n) = K nβ

where K is a positive constant and β is between 0 and 1. According to the Wikipedia article on Heaps’ law, K is often between 10 and 100 and β is often between 0.4 an 0.6.

(Note that it’s Heaps’ law, not Heap’s law. The law is named after Harold Stanley Heaps. However, true to Stigler’s law of eponymy, the law was first observed by someone else, Gustav Herdan.)

## Demonsrating Heaps law

For a collection of related texts, you can estimate the parameters K and β from data. I decided to see how well Heaps’ law worked in predicting the number of unique words in each book of the Bible. I used the King James Version because it is easy to download from Project Gutenberg.

I converted each line to lower case, replaced all non-alphabetic characters with spaces, and split the text on spaces to obtain a list of words. This gave the following statistics:

```    |------------+-------+------|
| Book       |     n |    V |
|------------+-------+------|
| Genesis    | 38520 | 2448 |
| Exodus     | 32767 | 2024 |
| Leviticus  | 24621 | 1412 |
...
| III John   |   295 |  155 |
| Jude       |   609 |  295 |
| Revelation | 12003 | 1283 |
|------------+-------+------|
```

The parameter values that best fit the data were K = 10.64 and β = 0.518, in keeping with the typical ranges of these parameters.

Here’s a sample of how the actual vocabulary size and predicted vocabulary size compare.

```    |------------+------+-------|
| Book       | True | Model |
|------------+------+-------|
| Genesis    | 2448 |  2538 |
| Exodus     | 2024 |  2335 |
| Leviticus  | 1412 |  2013 |
...
| III John   |  155 |   203 |
| Jude       |  295 |   296 |
| Revelation | 1283 |  1387 |
|------------+------+-------|
```

Here’s a visual representation of the results.

It looks like the predictions are more accurate for small books, and that’s true on an absolute scale. But the relative error is actually smaller for large books as we can see by plotting again on a log-log scale.

## Hapax logemenon

In linguistics, a hapax logemenon is a word that only appears once in a given context. The term comes comes from a Greek phrase meaning something said only once. The term is often shortened to just hapax.

I thought it would be interesting to look at the number of hapax logemena in each book since I could do it with a minor tweak of the code I wrote for the first part of this post.

Normally if someone were speaking of hapax logemena in the context of the Bible, they’d be looking at unique words in the original languages, i.e. Hebrew and Greek, not in English translation. But I’m doing statistics here, not Bible scholarship, so I’ll look at the text I have at hand.

Here’s a plot of the number of haxap as a function of the total number of words in a book.

This looks a lot like the plot of vocabulary size and total words, suggesting the number of hapax also follow a power law like Heaps law. This is clear when we plot again on a logarithmic scale and see a linear relation.

Just to be clear on the difference between the two major sections of this post, in the first we looked at vocabulary size, the number of distinct words in each book. In the second section we looked at words that only appear once. In both cases we’re counting unique words, but unique in different senses. In the first section, unique means that each word only counts once, no matter how many times it’s used. In the second section, unique means that a work only appears once.