Introduction
Given the finite set of all the words in a language, \(\nu\), a sentence in the language is the sequence of words
 Where
 \(n \ge 1\)
 \(x_1 \cdots x_{n1} \in \nu\)
 \(x_n\) is a special symbol STOP \(\require{cancel}\cancel{\in} \nu \)
Set of all the words in a language are assumed to be finite.
Let \(\nu^{\dagger}\) be the infinite set of all sentences with the vocabulary \(\nu\).
A language model consists of a finite set \(\nu\) as a function \(p(x_1, x_2, \cdots, x_n)\), such that:

For any \(⟨ x_1 \cdots x_n ⟩ \in \nu^\dagger, \, p(x_1, x_2, \cdots, x_n) \ge 0\)

\(\sum_{⟨ x_1 \cdots x_n ⟩ \in \nu^\dagger} p(x_1, x_2, \cdots, x_n) = 1\)
So, \(p(x_1, x_2, \cdots, x_n)\) is basically a probability distribution over the sentences \(\nu^\dagger\).
Applications of Language Modeling
A distribution of \(p(x_1 \cdots x_n)\) signifies how probable a sentence is in a language. Such a distribution can prove useful in speech recognition or machine translation. Candidates generated by these algorithms can be run against the language model to check how probable the sentences are.
Frequency Based Modeling
Frequency based modeling is given by,
 Where
 \(c(x_1 \cdots x_n)\) is the number of times \(x_1 \cdots x_n\) occurs in the training corpus
 N is the total number of sentences in the corpus.
One major drawback of such a model is that it would assign probability 0 to any sentence not seen in the training corpus.
Markov Models for Fixed Length Sequences
Consider a sequence of random variables, \(X_1, X_2, \cdots, X_n\), where each random variable can take any value in a finite set \(\nu\). n is assumed to be a fixed number.
Language model aims to find the probability of \(x_1 \cdots x_n\), where \(n\ge1\) and \(x_i \in \nu\) for \(i = 1 \cdots n\), i.e. to model the join probability,
If n is a fixed number there are \(\nu^n\) possible sequences of the form \(x_1 \cdots x_n\), which makes it impossible to list all the possible sequences for a large value of n and \(\nu\). This is where markov models help to build a more compact model.
FirstOrder Markov Process make the assumption that identity of an element in a sequence depends only on the identity of previous element in the sequence, i.e. \(X_i\) is conditionally independent of \( X_1 \cdots X_{i2} \), given the value of \(X_{i1}\).
The first step of the equation above is exact using the chain rule of probability. The second step is a result of firstorder markov model assumption.
Similarly, secondorder Markov models assume that identity of an element in a sequence depends only on the identity of previous two elements in the sequence, i.e. \(X_i\) is conditionally independent of \(X_1 \cdots X_{i3}\), given the value of \(X_{i1}\) and \(X_{i2}\).
It is assumed that \(x_0,\, x_{1} = *\), where * is the special start symbol.
Markov Sequences for Variablelength Sentences
The value n, assumed to be fixed number in previous section, is considered to be a random variable itself and the nth word is always equal to the special symbol STOP.
Using the secondorder markov assumption,
 Where
 \(n \ge 1\)
 \(x_n =\) STOP
 \(x_i \in \nu\) for \(i=1 \cdots (n1)\)
Process of generating a sequence using the above distribution would be as follows:
 Initialize \(i=1\), and \(x_0, x_{1} = *\)
 Generate \(x_i\) from the distribution,
 If \(x_i = \) STOP then return the sequence \(x_1 \cdots x_i\), else set i = i+1 and repeat previous step.
Trigram Language Models
Trigram language models are direct application of secondorder markov models to the language modeling problem. Each sentence is modeled as a sequence of n random variables, \(X_1, \cdots, X_n\) where n is itself a random variable.
A trigram model consists of finite set \(\nu\), and a parameter,
 Where
 u, v, w is a trigram
 \(w \in \nu \cup \{STOP\}\)
 \(u, v \in \nu \cup \{*\}\)
The value of \(q(w u, v)\) can be interpreted as the probability of seeing the word w immediately after bigram u, v.
So, for any sequence \(x_1 \cdots x_n\) where \(x_i \in \nu\) for \(i = 1 \cdots (n1)\) and \(x_n = \) STOP, the probability of the sentence under trigram language model is
 Where \(x_0 = x_{1} = *\)
Trigram assumption: Each word depends on the previous two. This is essentially the secondorder markov assumption used over sentences.
The only step remaining in the trigram language model is the estimation of language parameters, i.e., \(q(wu,v)\). Since the total number of words are \( \nu \) the total number of possible parameters would be \( \nu ^3\). It is a very big number and hence needs some kind of indirect estimation process.
Maximum Likelyhood Estimates
This is the most generic solution to the estimation problem shown above.
For any w, u, v,
 Where
 c(u,v,w) is the number of times the trigram is seen in corpus
 c(u, v) is the number of times the bigram is seen in the corpus
Many of the frequencies c(u,v,w) and c(u,v) would be 0. This would effect the estimation and present the following flaws:
 \(q(wu,v) = 0\) because c(u,v,w) is 0 which would underestimate many trigram probabilities which is unreasonable
 If the denominator is 0, then \(q(wu,v)\) would be undefined.
Perplexity
It is one of the evaluation metrics for the language models and is calculated on a heldout data after the model is trained on some corpus. The heldout data is not used for parameter estimation of the language model.
Consider a test dataset consisting of sentences, \(s_1, \cdots, s_m\), then \(p(s_i)\) gives probability for sentence \(s_i\) in the language model.
A basic measure of quality of language model would be the probability it assigns to the entire test set, give by
So, higher the quantity is, the better the language model is at modeling unseen sentences.
Perplexity is a direct transformation of this basic defination. Let M be the total number of words in the corpus, then average log probability under the model is
which is the log probability of the entire corpus, divided by the total number of words in the corpus. Again the higher the value of this, the better the language model.
Then, Perplexity is defined as
 Where
The perplexity is a positive number. The smaller the value of perplexity, the better the language model is at modeling unseen data. Perplexity becomes a minimization parameter because of the negative power that is applied to the defination.
Intuition for Perplexity
Let the vocabulary, \(\nu\) have N words, and the model predicts uniform distribution over the vocabulary, i.e.,
Then evaluating (3) using (1),
 Where \(n_1, n_2, \cdots, n_m\) are the number of words in each sentence in the test sample and,
Using (4) in (2),
So, under a uniform distribution model, the perplexity is equal to the vocabulary size. Perplexity can be considered the effective vocabulary size under the model.
Properties of perplexity:

If for any trigram u, v, w, the estimated probability \(q(wu, v) = 0\) then the perplexity will be \(\infty\) which is consistent with the rule stating that a good model should not predict probability zero for an unseen dataset and perplexity is low for good models.

If perplexity is the measure of language model, then 0 estimates should be avoided at all costs.
Linear Interpolation
The following trigram, bigram and unigram maximumlikelihood estimates are defined,
 Where
 \(c(u,v,w)\) is the number of times trigram u, v, w occurs
 \(c(v,w)\) is the number of times bigram v, w occurs
 \(c(w)\) is the number of times unigram w occurs
 \(c()\) is the total number of words seen in the training
Properties of these models:
 The unigram model will never face the issue of number or denominator being 0, so the estimate is always well defined and greater than 0. But it completely ignores the context and hence discards valuable information.
 The trigram model use the context better than the unigram model, but has the problem of many of its counts being 0 rendering estimate value 0 or undefined.
 The bigram model falls between the two extremes.
Linear Interpolation uses all three of these estimates to define the trigram estimate as follows,
 Where
i.e. (6) is a weighted average of the three estimates.
Discounting Methods and Katz Backoff
An alternative estimation method commonly used in practice.
Consider a bigram language model where the following parameter is to be found,
 Where
 \(w \in \nu \cup \) {STOP}
 \(v \in \nu \cup\) {*}
Discounted Counts are used to reflect the intuition that if the counts are taken from the training corpus, there would be a systematic overestimation of probability of bigrams seen in the corpus and hence underestimate the bigrams not seen in the corpus. So, discounted count is given by,
 Where
 \(c^*(v, w)\) is the discounted count
 \(c(v,w)\) is the count of bigrams, such that, \(c(v,w) \gt 0\)
 0.5 is the discount value
Using the discounted count, (7) can be written as,
i.e., use the discounted count in the numberator and regular count in the denominator.
For any context \(v\), there is a missing mass, defined as,
The intuition behind discounted methods is to divide the missing mass among words \(w\), such that \(c(v,w) = 0\).
Formally, for any \(v\), there exist sets
Then the estimate is given by,
i.e if \(c(v,w) \gt 0\) return \({c^*(v, w) \over c(v)}\) else divide the remaining probability mass \(\alpha(v)\) in proportion to the unigram estimates \(q_{ML}(w)\).
This method can be generalized to trigram language model in a recursive way, i.e., for any bigram (u, v) define,
 Where 0.5 is the discount value and hence discounted count is given by,
Then the trigram model is given by,
where
\(\alpha(u, v)\) is the missing probability mass of the bigram. It can be noted that the missing probability is distributed in proportion to the bigram estimaes \(q_{BO}(wv)\) given in (8).