# N-Gram Language Models, Laplace/Kneser-Ney Smoothing & Perplexity
Exam: GATE Data Science & Artificial Intelligence (DA) (GATE DA)
Subject: Natural Language Processing (NLP) & LLMs
Canonical URL: https://gateandnet.com/exams/gate-da/natural-language-processing/ngram-language-models-smoothing-and-perplexity

> Markov chain N-gram MLE probabilities, Add-1 (Laplace) & Add-k smoothing formulas, Interpolation vs Backoff, Kneser-Ney continuation probability, and Cross-Entropy / Perplexity numericals.

## 1. Chain Rule & $N$-Gram Markov Assumption

Exact joint probability of a token sequence $W = (w_1, w_2, \dots, w_T)$ factors via the chain rule $P(W) = \prod_{t=1}^T P(w_t \mid w_1, \dots, w_{t-1})$. An **$N$-gram model** applies a $(N-1)$-th order Markov assumption, looking back only $N - 1$ tokens:
- **Unigram ($N=1$):** $P(w_t)$
- **Bigram ($N=2$):** $P(w_t \mid w_{t-1})$
- **Trigram ($N=3$):** $P(w_t \mid w_{t-2}, w_{t-1})$

## 2. Handling Zero Counts: Smoothing & Kneser-Ney

Any unseen test $N$-gram gives $P_{\text{MLE}} = 0 \implies P(W) = 0 \implies \operatorname{PPL}(W) = \infty$!
1. **Add-$k$ Smoothing:** $P_{\text{add-}k}(w_i \mid w_{i-1}) = \frac{C(w_{i-1}, w_i) + k}{C(w_{i-1}) + k|V|}$ ($k=1$ is Laplace). Note $\sum_{w \in V} P_{\text{add-}k}(w \mid w_{i-1}) = \frac{C(w_{i-1}) + k|V|}{C(w_{i-1}) + k|V|} = 1$.
2. **Linear Interpolation:** $\hat{P}(w_i \mid w_{i-2}, w_{i-1}) = \lambda_3 P_3 + \lambda_2 P_2 + \lambda_1 P_1$ with $\sum \lambda_j = 1$.
3. **Absolute Discounting & Kneser-Ney:** Subtracts a fixed discount $d \in (0,1)$ (e.g., $0.75$) from non-zero counts and backs off to **continuation probability** $P_{\text{cont}}(w) \propto |\{u : C(u, w) > 0\}|$ (how versatile $w$ is across *distinct preceding contexts* — solves the *"San Francisco"* trap where *"Francisco"* has high raw frequency but only ever follows *"San"*!).

## 3. Worked Perplexity Numerical

> [!FORMULA]
> Suppose a language model assigns probabilities $P(w_1)=\frac{1}{2},\; P(w_2\mid w_1)=\frac{1}{4},\; P(w_3\mid w_1,w_2)=\frac{1}{4},\; P(\text{EOS}\mid \dots)=\frac{1}{2}$ over $N = 4$ tokens:
> $$P(W) = \frac{1}{2} \times \frac{1}{4} \times \frac{1}{4} \times \frac{1}{2} = \frac{1}{64} = 2^{-6} \implies \operatorname{PPL}(W) = \left(2^{-6}\right)^{-1/4} = 2^{6/4} = 2^{1.5} = 2\sqrt{2} \approx \mathbf{2.83}$$
> Intuitively, Perplexity equals the **weighted average branching factor** of the language model ($\operatorname{PPL} = |V|$ for a uniform model $P = 1/|V|$).

## Solved Practice Questions & PYQs

### [GATE DA NLP | MCQ]
A corpus has vocabulary size $|V| = 1,000$. The unigram `data` occurs $C(\text{data}) = 200$ times, and the bigram `data science` occurs $C(\text{data, science}) = 19$ times. Under **Laplace (Add-1) Smoothing**, what is $P(\text{science} \mid \text{data})$?
**Correct Answer:** B
**Solution:** $$P_{\text{Laplace}}(\text{science} \mid \text{data}) = \frac{C(\text{data, science}) + 1}{C(\text{data}) + |V|} = \frac{19 + 1}{200 + 1000} = \frac{20}{1200} = \mathbf{\frac{1}{60} \approx 0.0167}$$

### [GATE DA NLP | MCQ]
A language model achieves a per-token **Cross-Entropy** of $H(W) = 5\text{ bits/token}$ (base-$2$ log) on a held-out evaluation corpus. What is the **Perplexity** ($\text{PPL}$) of the language model?
**Correct Answer:** C
**Solution:** Perplexity is related to base-$2$ cross-entropy $H(W)$ by $\operatorname{PPL}(W) = 2^{H(W)} = 2^5 = \mathbf{32}$.
