Post · 6 min read· pp. 45–49

Maximum Likelihood Estimation of N-Gram Model Parameters

Introduction

A language model is a probability distribution over sequences of words, namely:

p(w1,w2,w3,⋯ ,wn)p(w_1, w_2, w_3, \cdots, w_n)

According to the chain rule,

p(w1,w2,w3,⋯ ,wn)=p(w1)p(w2∣w1)p(w3∣w2,w1)⋯p(wn∣wn−1,wn−2,⋯ ,w1)p(w_1, w_2, w_3, \cdots, w_n) = p(w_1)p(w_2|w_1)p(w_3|w_2,w_1)\cdots p(w_n|w_{n-1},w_{n-2},\cdots,w_1)

However, the parameters for this language model are p(w1)p(w_1), p(w2∣w1)p(w_2|w_1), \cdots, p(wn∣wn−1,⋯ ,w1)p(w_n|w_{n-1},\cdots,w_1), which are usually too computationally expensive to calculate especially for the conditional probability with many conditioning words, even with a small dataset.

To approximate p(w1,w2,w3,⋯ ,wn)p(w_1, w_2, w_3, \cdots, w_n), we could use N-Gram models to approximate the language model, namely:

N-Gram Model

p(w1,w2,w3,⋯ ,wn)≈p(w1)p(w2∣w1)p(w3∣w2,w1)⋯p(wn∣wn−1,wn−2,⋯ ,wn−N)p(w_1, w_2, w_3, \cdots, w_n) \approx p(w_1)p(w_2|w_1)p(w_3|w_2,w_1)\cdots p(w_n|w_{n-1},w_{n-2},\cdots,w_{n-N})

In particular, we usually use unigram model, bigram model and trigram model in language modelings.

Unigram Model

p(w1,w2,w3,⋯ ,wn)≈p(w1)p(w2)p(w3)⋯p(wn)p(w_1, w_2, w_3, \cdots, w_n) \approx p(w_1)p(w_2)p(w_3)\cdots p(w_n)

Bigram Model

p(w1,w2,w3,⋯ ,wn)≈p(w1)p(w2∣w1)p(w3∣w2)⋯p(wn∣wn−1)p(w_1, w_2, w_3, \cdots, w_n) \approx p(w_1)p(w_2|w_1)p(w_3|w_2)\cdots p(w_n|w_{n-1})

Trigram Model

p(w1,w2,w3,⋯ ,wn)≈p(w1)p(w2∣w1)p(w3∣w2,w1)⋯p(wn∣wn−1,wn−2)p(w_1, w_2, w_3, \cdots, w_n) \approx p(w_1)p(w_2|w_1)p(w_3|w_2,w_1)\cdots p(w_n|w_{n-1},w_{n-2})

With the N-Gram model approximations, calculating p(wn∣wn−1,wn−2,⋯ ,wn−N)p(w_n|w_{n-1},w_{n-2},\cdots,w_{n-N}) is usually not too computationally expensive.

Maximum Likelihood Estimation of N-Gram Model Parameters

To estimate p(wn∣wn−1,wn−2,⋯ ,wn−N)p(w_n|w_{n-1},w_{n-2},\cdots,w_{n-N}), an intuitive way is to do maximum likelihood estimation (MLE).

Maximum likelihood estimation estimates the model parameters such that the probability is maximized.

In our case, the parameters are p(wn∣wn−1,wn−2,⋯ ,wn−N)p(w_n|w_{n-1},w_{n-2},\cdots,w_{n-N}), and the probability we maximizes is p(w1)p(w2∣w1)p(w3∣w2,w1)⋯p(wn∣wn−1,wn−2,⋯ ,wn−N)p(w_1)p(w_2|w_1)p(w_3|w_2,w_1)\cdots p(w_n|w_{n-1},w_{n-2},\cdots,w_{n-N})

In practice, we simply count the occurrence of word patterns to calculate the maximum likelihood estimation of p(wn∣wn−1,wn−2,⋯ ,wn−N)p(w_n|w_{n-1},w_{n-2},\cdots,w_{n-N}).

Unigram Model

p(wi)=c(wi)∑wc(w)p(w_i) = \frac{c(w_i)}{\sum_{w}^{} c(w)}

Bigram Model

p(wi∣wi−1)=c(wi−1,wi)∑wc(wi−1,w)p(w_i|w_{i-1}) = \frac{c(w_{i-1},w_i)}{\sum_{w}^{} c(w_{i-1},w)}

Trigram Model

p(wi∣wi−1,wi−2)=c(wi−2,wi−1,wi)∑wc(wi−2,wi−1,w)p(w_i|w_{i-1},w_{i-2}) = \frac{c(w_{i-2},w_{i-1},w_i)}{\sum_{w}^{} c(w_{i-2},w_{i-1},w)}

Now the question becomes why these formulas are the maximum likelihood estimations. Most of the books and online tutorials only give these formulas without showing formal mathematical proof.

Here I am going to rigorously show that these are actually the formulas of maximum likelihood estimation.

Mathematical Derivation of Maximum Likelihood Estimation of N-Gram Model Parameters

Unigram Model

Let us warm up with unigram model.

We have a collection of unique words, w1,w2,⋯ ,wnw_1, w_2, \cdots, w_n.

For any given sequence of words w\mathbf{w} of length NN (w=(w1,w2,w1,w5,w7,w2)\mathbf{w} = (w_1, w_2, w_1, w_5, w_7, w_2) for example), we have

p(w)=p(w1)c(w1)p(w2)c(w2)p(w3)c(w3)⋯p(wn)c(wn)=∏i=1np(wi)c(wi)\begin{aligned} p(\mathbf{w}) & = p(w_1)^{c(w_1)} p(w_2)^{c(w_2)} p(w_3)^{c(w_3)}\cdots p(w_n)^{c(w_n)}\\ & = \prod_{i=1}^{n}p(w_i)^{c(w_i)} \end{aligned}

where c(wi)c(w_i) is the count of word wiw_i in the sentence.

We take the log of p(w)p(\mathbf{w}), we then have:

log⁡p(w)=c(w1)log⁡p(w1)+c(w2)log⁡p(w2)+c(w3)log⁡p(w3)+⋯+c(wn)log⁡p(wn)=∑i=1nc(wi)log⁡p(wi)\begin{aligned} \log{p(\mathbf{w})} & = c(w_1)\log{p(w_1)} + c(w_2)\log{p(w_2)} + c(w_3)\log{p(w_3)} + \cdots + c(w_n)\log{p(w_n)}\\ & = \sum_{i=1}^{n}c(w_i)\log{p(w_i)} \end{aligned}

To maximize p(w)p(\mathbf{w}), equivalently we have the following optimization problem:

Maximize log⁡p(w)\log{p(\mathbf{w})}, subject to ∀i∈[1…N]\forall i \in [1 \dotsc N], ∑i=1np(wi)=1\sum_{i = 1}^{n} p(w_i) = 1.

Equivalently, we introduce auxilliary optimization function using Lagrange multiplier ∑i=1np(wi)−1=0\sum_{i=1}^{n}p(w_i)-1 = 0:

L=∑i=1nc(wi)log⁡p(wi)+λ(∑i=1np(wi)−1)\mathcal{L} = \sum_{i=1}^{n}c(w_i)\log{p(w_i)} + \lambda\left(\sum_{i=1}^{n}p(w_i)-1\right)

The Hessian matrix of L\mathcal{L} is as follows:

H(L)=[∂2L∂p(w1)2∂2L∂p(w1)∂p(w2)⋯∂2L∂p(w1)∂p(wn)∂2L∂p(w2)∂p(w1)∂2L∂p(w2)2⋯∂2L∂p(w2)∂p(wn)⋮⋮⋱⋮∂2L∂p(wn)∂p(w1)∂2L∂p(wn)∂p(w2)⋯∂2L∂p(wn)2]=[c(w1)p(w1)20⋯00c(w2)p(w2)2⋯0⋮⋮⋱⋮00⋯c(wn)p(wn)2]\begin{align} \mathbf{H}(\mathcal{L}) &= \begin{bmatrix} \frac{\partial^2 \mathcal{L}}{\partial p(w_1)^2} & \frac{\partial^2 \mathcal{L}}{\partial p(w_1) \partial p(w_2)} & \cdots & \frac{\partial^2 \mathcal{L}}{\partial p(w_1) \partial p(w_n)}\\ \frac{\partial^2 \mathcal{L}}{\partial p(w_2) \partial p(w_1)} & \frac{\partial^2 \mathcal{L}}{\partial p(w_2)^2} & \cdots & \frac{\partial^2 \mathcal{L}}{\partial p(w_2) \partial p(w_n)}\\ \vdots & \vdots & \ddots & \vdots\\ \frac{\partial^2 \mathcal{L}}{\partial p(w_n) \partial p(w_1)} & \frac{\partial^2 \mathcal{L}}{\partial p(w_n) \partial p(w_2)} & \cdots & \frac{\partial^2 \mathcal{L}}{\partial p(w_n)^2} \end{bmatrix} \\ &= \begin{bmatrix} \frac{c(w_1)}{p(w_1)^2} & 0 & \cdots & 0\\ 0 & \frac{c(w_2)}{p(w_2)^2} & \cdots & 0\\ \vdots & \vdots & \ddots & \vdots\\ 0 & 0 & \cdots & \frac{c(w_n)}{p(w_n)^2} \end{bmatrix} \end{align}

It is straightforward to see that the Hessian matrix is positive semi-definite, therefore the optimization problem is convex.

For any p(wj)p(w_j), we take the derivatives of L\mathcal{L} respective to p(wj)p(w_j):

∂L∂p(wj)=c(wj)p(wj)+λ=0\frac{\partial \mathcal{L}}{\partial p(w_j)} = \frac{c(w_j)}{p(w_j)} + \lambda = 0

p(wj)=−c(wj)λp(w_j) = -\frac{c(w_j)}{\lambda}

Because ∑i=1np(wi)=1\sum_{i=1}^{n}p(w_i) = 1, we have:

∑i=1np(wi)=∑i=1n−c(wi)λ=∑i=1nc(wi)−λ=1\sum_{i=1}^{n}p(w_i) = \sum_{i=1}^{n} -\frac{c(w_i)}{\lambda} = \frac{\sum_{i=1}^{n} c(w_i)}{-\lambda} = 1

λ=−∑i=1nc(wi)\lambda = - \sum_{i=1}^{n} c(w_i)

Because p(wj)=−c(wj)/λp(w_j) = -c(w_j)/{\lambda}, therefore

p(wj)=c(wj)∑i=1nc(wi)p(w_j) = \frac{c(w_j)}{\sum_{i=1}^{n} c(w_i)}

This concludes the proof.

Bigram Model

Now let us move on to bigram model to see what is different.

We have a collection of unique words, w1,w2,⋯ ,wnw_1, w_2, \cdots, w_n.

For the conditional probabilities, we have n×nn \times n possibilities.

For any given sequence of words w\mathbf{w} of length NN (w=(w1,w2,w1,w5,w7,w2)\mathbf{w} = (w_1, w_2, w_1, w_5, w_7, w_2) for example), we have

p(w)=∏i=1np(wi)s(wi)∏i=1n∏j=1np(wj∣wi)c(wi,wj)\begin{aligned} p(\mathbf{w}) & = \prod_{i=1}^{n} p(w_i)^{s(w_i)} \prod_{i=1}^{n} \prod_{j=1}^{n} p(w_j|w_i)^{c(w_i, w_j)} \end{aligned}

where c(wi,wj)c(w_i, w_j) is the count of word sequence wi,wjw_i, w_j in the sentence and

s(wi)={1,if wi is the first word0,otherwises(w_i) = \begin{cases} 1, & \text{if $w_i$ is the first word}\\ 0, & \text{otherwise} \end{cases}

We take the log of p(w)p(\mathbf{w}), we then have:

log⁡p(w)=∑i=1ns(wi)log⁡p(wi)+∑i=1n∑j=1nc(wi,wj)log⁡p(wj∣wi)\begin{aligned} \log{p(\mathbf{w})} & = \sum_{i=1}^{n}s(w_i)\log{p(w_i)} + \sum_{i=1}^{n}\sum_{j=1}^{n}c(w_i,w_j)\log{p(w_j|w_i)} \end{aligned}

To maximize p(w)p(\mathbf{w}), equivalently we have the following optimization problem:

Maximize log⁡p(w)\log{p(\mathbf{w})}, subject to ∀i∈[1…N]\forall i \in [1 \dotsc N], ∑j=1np(wj∣wi)=1\sum_{j = 1}^{n} p(w_j|w_i) = 1.

Equivalently, we introduce auxiliary optimization function using Lagrange multiplier ∑j=1np(wj∣wi)−1=0\sum_{j = 1}^{n} p(w_j|w_i)-1 = 0:

L=∑i=1ns(wi)log⁡p(wi)+∑i=1n∑j=1nc(wi,wj)log⁡p(wj∣wi)+∑i=1nλi((∑j=1np(wj∣wi))−1)\mathcal{L} = \sum_{i=1}^{n}s(w_i)\log{p(w_i)} + \sum_{i=1}^{n}\sum_{j=1}^{n}c(w_i,w_j)\log{p(w_j|w_i)} + \sum_{i=1}^{n} \lambda_i \left( \left(\sum_{j = 1}^{n} p(w_j|w_i) \right) - 1 \right)

For any p(wk∣wi)p(w_k|w_i), we take the derivatives of L\mathcal{L} respective to p(wk∣wi)p(w_k|w_i):

∂L∂p(wk∣wi)=c(wi,wk)p(wk∣wi)+λi=0\frac{\partial \mathcal{L}}{\partial p(w_k|w_i)} = \frac{c(w_i, w_k)}{p(w_k|w_i)} + \lambda_i = 0

p(wk∣wi)=−c(wi,wk)λip(w_k|w_i) = -\frac{c(w_i, w_k)}{\lambda_i}

Because ∑j=1np(wj∣wi)=1\sum_{j = 1}^{n} p(w_j|w_i) = 1, we have:

∑j=1np(wj∣wi)=∑j=1n−c(wi,wj)λi=∑j=1nc(wi,wj)−λi=1\sum_{j = 1}^{n} p(w_j|w_i) = \sum_{j = 1}^{n} -\frac{c(w_i, w_j)}{\lambda_i} = \frac{\sum_{j = 1}^{n} c(w_i, w_j)}{-\lambda_i} = 1

λi=−∑j=1nc(wi,wj)\lambda_i = -\sum_{j = 1}^{n} c(w_i, w_j)

Because p(wk∣wi)=−c(wi,wk)/λip(w_k|w_i) = -c(w_i, w_k)/\lambda_i, therefore

p(wk∣wi)=c(wi,wk)∑j=1nc(wi,wj)p(w_k|w_i) = \frac{c(w_i, w_k)}{\sum_{j = 1}^{n} c(w_i, w_j)}

This concludes the proof.

N-Gram Model

Without losing generality, the maximum likelihood estimation of n-gram model parameters could also be proven in the same way.

References