Math Foundations — Lecture 12
Light on math: retrieval scoring (cosine similarity over embeddings), Chain-of-Thought formalism, and a comparison of dense vs sparse retrieval.
Chain-of-Thought as conditional generation
A plain LM directly samples an answer \(y\) given a prompt \(x\):
A Chain-of-Thought prompt introduces an intermediate reasoning trace \(z\):
In practice we do not marginalise — we sample one trace \(\hat{z}\sim p(z\mid x)\) and read off \(\hat{y}=\arg\max p(y\mid x,\hat{z})\). The trace acts as scratch memory: extra tokens that condition the answer.
Self-consistency
Sample \(K\) reasoning traces, then majority-vote over the extracted answers:
This Monte-Carlo estimate of the marginal \(p(y\mid x)\) often beats greedy CoT.
Retrieval scoring for RAG
Given a query \(q\) and a corpus of documents \(\{d_1,\dots,d_N\}\), we return the top-\(k\) by some score \(s(q,d_i)\).
Dense retrieval (cosine similarity)
Encode query and documents with an embedding model \(E_q,E_d:\text{text}\to\mathbb{R}^{D}\):
If the embeddings are L2-normalised (\(\lVert\mathbf{q}\rVert=\lVert\mathbf{d}_i\rVert=1\)) this reduces to the dot product. Top-\(k\) retrieval is then an approximate nearest-neighbour lookup in a vector store.
Sparse retrieval (BM25)
BM25 is a tf–idf variant over bag-of-words:
where \(f(t,d)\) is term frequency, \(|d|/\bar{|d|}\) the length normaliser, and \(k_1,b\) are hyperparameters. Pure lexical match — fast and strong baseline, but blind to paraphrase.
Dense vs sparse: when to use what?
| Aspect | Sparse (BM25) | Dense (cosine) |
|---|---|---|
| Match type | exact lexical | semantic / paraphrase |
| Cold start | works out-of-the-box | needs an embedding model |
| Rare names / IDs | strong | can miss |
| Synonyms | misses | handles |
| Index | inverted index | ANN vector store |
In practice, RAG systems often combine the two (hybrid retrieval) and re-rank the union with a cross-encoder.
ReAct loop (pseudo-formal)
At step \(t\) the agent has a history \(h_t = (x, \tau_1, a_1, o_1, \dots, \tau_{t-1}, a_{t-1}, o_{t-1})\) of thoughts \(\tau\), actions \(a\), observations \(o\). It samples the next thought-action pair from a single LM:
The loop terminates when \(a_t\) is a special Finish[y] action; the agent returns \(y\). No parameters are learned — the entire behaviour is induced from few-shot demonstrations of \((\tau, a, o)\) traces.