Co-occurrence Matrices › Weighting by Distance
Language AI HandbookAll chapters Co-occurrence Matrices › Weighting by DistanceOne section · Sections in this chapter4 / 13 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
A uniform window treats all co-occurrences equally regardless of distance. A word 5 positions away gets the same weight as a word 1 position away. This is simple, but it discards information: words adjacent to the target are usually more semantically informative than words further away.
Why Distance Matters
Think about what the immediate neighbors of a word tell you versus what distant context tells you. The word immediately to the left of a noun is often a determiner, adjective, or possessive, and the word immediately to the right is often a verb or preposition. These tight grammatical constraints are highly specific to the target word. A word 5 positions away is more loosely associated: it is in the same sentence, likely on the same topic, but not necessarily in a tight grammatical relationship.
By giving equal weight to near and far context, a uniform window blurs the distinction between tight semantic relationships and loose topical associations. Distance weighting restores this distinction by making the contribution of each co-occurrence inversely proportional to how far away the context word was.
Harmonic Distance Weighting
Distance weighting addresses this by assigning co-occurrences a weight inversely proportional to the distance between the two words. The most common scheme is harmonic weighting:
where is the distance in tokens between the target word and the context word. So a word 1 position away contributes 1 to the co-occurrence count, a word 2 positions away contributes 0.5, a word 3 positions away contributes 0.33, and so on.
For a target word at position in a sentence, the weighted co-occurrence count with word is:
where the sum runs over all positions within the window where word appears, and is the distance from the target to that occurrence.
The harmonic function has a specific property that makes it appealing: it assigns weight 1 to the nearest context, then diminishes smoothly. The weights for distances 1 through 5 are: 1.0, 0.5, 0.33, 0.25, 0.20. The first neighbor gets five times the weight of the fifth neighbor. This is not arbitrary: it reflects the empirical observation that the first few context words are typically the most informative about the target word's meaning.
Alternative Weighting Schemes
Harmonic weighting is not the only option. Researchers have explored several alternatives.
Linear weighting uses weights that decrease linearly with distance: for a window of size . This gives a fixed weight of to distance 1 and a weight of to distance . Linear weighting is simpler to reason about than harmonic weighting and works well in practice.
Exponential weighting uses for some decay parameter . This drops off more steeply than harmonic weighting for small and can be tuned to emphasize very tight context more aggressively.
Binary weighting (the uniform window) is the simplest: all context words get weight 1 regardless of distance. While it ignores distance information, it is the default in many implementations and often works well enough that the added complexity of distance weighting is not justified.
The choice of weighting scheme matters less than the choice of window size, but distance weighting consistently improves word vector quality in downstream tasks, at the cost of slightly more complex construction. GloVe, which we will discuss later in the book, implicitly captures distance weighting through its training objective: the GloVe loss function weights co-occurrence events, and words that appear very frequently near each other (often because they are grammatically adjacent) receive higher weight in training.