Quant Memo
Core

Word Embeddings and Word2Vec

A way of turning words into coordinates so that words used in similar contexts land near each other. Word2Vec learns those coordinates from raw text alone, and the geometry it produces turns out to encode meaning.

Prerequisites: Bag of Words and TF-IDF, Text Preprocessing and Tokenization

Count-based text features like Bag of Words and TF-IDF treat every word as its own unrelated column. To that representation, profit and earnings are as different as profit and lawsuit — each is just a separate slot in a very wide, very sparse table. So a model trained on filings that say "strong profit" has learned nothing about filings that say "strong earnings". You end up needing enormous amounts of labelled text to relearn, one synonym at a time, things a human reader already knows.

Embeddings fix this by giving each word a position instead of a slot. Picture a map where every word in the vocabulary is a pin. Words that get used in the same kinds of sentences are pinned close together; words used in unrelated contexts are pinned far apart. Once words have coordinates, "close in meaning" becomes "close in distance", and a model that has seen one word gets partial credit for the others nearby.

The rule that builds the map is old and simple: you shall know a word by the company it keeps. If two words repeatedly appear surrounded by the same neighbours, they probably mean similar things. Nobody has to label anything — the raw text supplies its own supervision.

An embedding is a short list of numbers, typically 100 to 300 of them, standing in for a word. It is learned entirely from which words appear near which, and it converts a question about meaning into a question about geometry.

How Word2Vec learns the map

Word2Vec turns the "company it keeps" idea into a prediction task. In the skip-gram version, you slide a window over the text — say five words wide — and for each centre word you ask the model to predict its neighbours. Given the centre word dovish, predict that Fed, statement, rates are nearby.

Each word gets two vectors, one for when it is the centre and one for when it is a neighbour, and the model scores a (centre, neighbour) pair by their dot product:

score(c,n)=vcvn\text{score}(c, n) = v_c \cdot v_n

In plain English: two words score highly together when their coordinate lists point in the same direction. Those scores are turned into probabilities and the vectors are nudged by gradient descent so that pairs which really co-occur score high, and randomly sampled pairs which do not score low. That last trick — training against a handful of random "negative" words instead of the whole vocabulary — is called negative sampling, and it is what makes the whole thing fast enough to run on billions of words.

Nothing in that procedure knows what dovish means. The meaning is an emergent property of where the word lands.

profit earnings revenue lawsuit probe recall share shares bond bonds clusters carry topic; the repeated offset carries a relationship
Related words gather into neighbourhoods, and consistent relationships — here singular to plural — show up as the same displacement applied in different parts of the map.

Worked example: measuring similarity

Distance alone is a poor measure because common words get long vectors. What matters is direction, so similarity is measured by the cosine of the angle between two vectors:

cos(u,v)=uvuv\cos(u, v) = \frac{u \cdot v}{\lVert u \rVert \, \lVert v \rVert}

In plain English: divide out the lengths and ask only whether the two arrows point the same way. The result runs from 1-1 to 11.

Take three toy three-dimensional embeddings: profit=(2,1,0)\text{profit} = (2,\, 1,\, 0), earnings=(3,2,0.5)\text{earnings} = (3,\, 2,\, 0.5), and lawsuit=(1,2,3)\text{lawsuit} = (-1,\, 2,\, 3).

For profit and earnings, the dot product is 2(3)+1(2)+0(0.5)=82(3) + 1(2) + 0(0.5) = 8. The lengths are profit=5=2.236\lVert \text{profit} \rVert = \sqrt{5} = 2.236 and earnings=13.25=3.640\lVert \text{earnings} \rVert = \sqrt{13.25} = 3.640, so

cos=82.236×3.640=88.139=0.983.\cos = \frac{8}{2.236 \times 3.640} = \frac{8}{8.139} = 0.983 .

Nearly parallel — the model treats them as close to interchangeable. For profit and lawsuit, the dot product is 2(1)+1(2)+0(3)=02(-1) + 1(2) + 0(3) = 0, giving a cosine of exactly 00: perpendicular, meaning no relationship at all. That single number is what powers duplicate-headline detection, peer-firm mapping, and the retrieval step in Vector Databases and Embedding Search.

Worked example: relationships as arrows

The famous property of Word2Vec is that consistent relationships become consistent displacements. Using two-dimensional stand-ins: share=(1,1)\text{share} = (1,1), shares=(1,2)\text{shares} = (1,2), bond=(3,1)\text{bond} = (3,1).

The singular-to-plural move is sharesshare=(0,1)\text{shares} - \text{share} = (0,\, 1). Apply the same move elsewhere:

(3,1)+(0,1)=(3,2).(3,\,1) + (0,\,1) = (3,\,2) .

Now look up the nearest actual word to (3,2)(3,2) in the vocabulary. If the embedding is any good, it is bonds. The model was never told about plurals; it discovered that plural contexts differ from singular ones in a consistent way, and encoded that difference as a direction. The same trick recovers company-to-ticker and currency-to-country pairs in financial corpora.

Word2Vec gives each word one vector for all time. In finance that is a real limitation: charge in "a charge to earnings" and charge in "criminal charge" get the identical embedding, averaged across both senses. Context-dependent models like BERT and Masked Language Models were built specifically to fix this, and for anything involving legal or accounting language the difference matters.

Do not train your own embeddings on a small corpus of filings. Word2Vec needs hundreds of millions of tokens before the geometry stabilises. Start from published vectors, then adapt them if your vocabulary is genuinely unusual.

Embeddings are also the point where noise enters quietly. Two headlines about different companies can sit close together simply because they share boilerplate, so cosine similarity on raw text needs the same care as any other alpha feature.

Related concepts

Practice in interviews

Further reading

  • Mikolov et al., Efficient Estimation of Word Representations in Vector Space (2013)
  • Jurafsky & Martin, Speech and Language Processing (ch. 6)
ShareTwitterLinkedIn