How to Compute the Probability of a Word

Tiago Pimentel, Clara Meister · DOI

Introduction

Most modern large language models (LLMs), including the GPT and Llama families, do not process text as discrete words. Instead, they utilize subword tokenization, breaking down strings into smaller units to handle large vocabularies efficiently. While this is effective for engineering, it creates a significant hurdle for researchers in fields like psycholinguistics and cognitive science. These researchers often rely on surprisal—the negative log-probability of a word—to model human language processing and reading times.

This paper identifies a pervasive mathematical error in how word probabilities are calculated for models using "beginning-of-word" (BOW) tokenizers. This "bug" has likely influenced years of linguistic research, leading to inaccurate surprisal estimates. The authors propose a mathematically rigorous, computationally efficient fix that aligns subword distributions with true word-level probabilities.

The Problem: Prefix vs. Word Probability

In psycholinguistic research, the standard method for calculating the probability of a word $W$ composed of subwords $[t_1, t_2, ..., t_n]$ has been to simply multiply their conditional probabilities:

$$P(W) = P(t_1 | \text{context}) \times P(t_2 | \text{context}, t_1) \times ...$$

The authors demonstrate that while this approach is valid for "end-of-word" marking tokenizers, it is fundamentally incorrect for BOW tokenizers (e.g., those used by GPT-2, Llama, and Mistral). In BOW systems, a special character (like a leading space) denotes the start of a new word.

Multiplying these subword probabilities only yields the prefix probability—the probability that a sequence starts with that specific word. It fails to account for the probability that the word actually terminates at that point. Without accounting for the termination, the resulting value is not a true probability distribution over the lexicon, as the sum of probabilities for all possible words would exceed one.

The Proposed Fix: The Discount Factor

To resolve this, the authors derive a framework based on the probability of the next token. For a word to be complete, the token following its final subword must be a "word-start" token (or an end-of-sequence marker).

The correct word probability is calculated by applying a "discount factor" to the prefix probability. This involves marginalization: summing the probabilities of every token in the model's vocabulary that signifies the beginning of a new word.

Key components of this method include:

  1. Prefix Calculation: Calculate the standard product of subword probabilities.
  2. Marginalization: Sum the probabilities of all potential "next tokens" that would initiate a new word.
  3. Handling Corner Cases: The authors provide specific derivations for the first word in a sequence (which may lack a leading space) and words followed immediately by punctuation, which often requires special handling in tokenization schemes.

Crucially, this fix is "computationally free." It does not require retraining the model or performing additional forward passes; it merely requires a smarter summation of the existing output distribution.

Experimental Evaluation

The authors tested the impact of this correction across several dimensions:

  • Predicting Human Behavior: They utilized four major reading-time datasets—Brown, Dundee, Natural Stories, and Provo—which contain human eye-tracking and self-paced reading data.
  • Multilingual Validation: Using the MECO dataset, they extended the analysis to seven additional languages (Finnish, German, Greek, Hebrew, Italian, Spanish, and Turkish) to ensure the fix was not language-specific.
  • Theoretical Implications: They analyzed a subset of the Wiki-40B English corpus to re-evaluate the "Channel Capacity Hypothesis" versus Zipf’s Law in the context of communicative efficiency.

Key Results and Insights

Improved Predictive Power

The "corrected" surprisal estimates significantly outperformed the "buggy" estimates in predicting human reading times. This improvement was consistent across different model architectures (GPT-2 and Pythia) and across a wide range of model scales, from 70 million to 12 billion parameters. This suggests that the human brain's internal "language model" is better approximated when the math of the artificial model is correctly aligned with word boundaries.

Theoretical Shifts

The correction had a profound impact on the study of communicative efficiency. Previous research using the "buggy" method suggested that word lengths are optimized for "Channel Capacity" (balancing information density). However, once the mathematical error was corrected, the data more strongly supported Zipf’s Law—the simpler idea that word lengths are primarily determined by frequency. This demonstrates how a technical "bug" in probability calculation can lead to incorrect theoretical conclusions in linguistics.

Universal Applicability

The findings held true across all eight languages in the MECO dataset. This confirms that the issue is a fundamental property of how researchers interface with subword tokenizers, regardless of the specific language being modeled.

Limitations and Future Work

While the fix is robust for autoregressive (left-to-right) models, the authors note that masked language models like BERT may require different derivations. Additionally, while common punctuation is addressed, unique orthographic conventions in certain languages (such as inverted punctuation in Spanish) may require further refinement. Finally, the method assumes "segmentation-compatible" tokenizers; niche tokenizers that allow a single subword to span across multiple words would require a more complex approach.

Conclusion

This research provides a vital correction for the computational linguistics community. By identifying a silent but pervasive error in word probability calculation, the authors have provided a path toward more accurate surprisal estimates. As LLMs continue to be used as proxies for human language processing, ensuring the mathematical integrity of the word-level interface is essential for valid scientific inquiry.

Summary updated

BibTeX
@inproceedings{pimentel2024how,
  author = {Tiago Pimentel and Clara Meister},
  title = {{How to Compute the Probability of a Word}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.emnlp-main.1020},
}
Figure