(BackBack)
ML//20 min read

Latent Dirichlet Allocation - Part.1

LDA's main function is not dimension reduction but Topic Modeling, and the definition of Topic Modeling, borrowing from the expression in the paper, is as follows

In this post, we will explore Latent Dirichlet Allocation (LDA).

It's interesting that LDA is used as an acronym for two different concepts, but fortunately, both are related to 'dimension reduction'.

  1. Latent Dirichlet Allocation

  2. Linear Discriminant Analysis (To avoid confusion, I will refer to this as LDAs)

LDAs' main function, like PCA, is dimension reduction. This function, of course, means that it can be used in other situations, but its inherent purpose is dimension reduction. On the other hand, LDA's main function is not dimension reduction but Topic Modeling, and the definition of Topic Modeling, borrowing from the expression in the paper, is as follows:

"To find short descriptions of the members of a collection that enable efficient processing of large collections while preserving the essential statistical relationships that are useful."

In other words, it's about representing vast and numerous documents with simple descriptions, and finding the 'Topic' that can represent those documents. Therefore, it can also serve the role of dimension reduction.

By using LDA, you can explore the following:

We will unravel these one by one as the explanation progresses. LDA has made significant efforts to semantically reflect these in their formula.

LDA's objective is very simple. However, the formulas used and their background principles are quite complex, and if you fully understand LDA, it means you have a solid foundation in Bayesian statistics or probability. Therefore, materials explaining LDA often try to deliver the overall framework while leaving out difficult concepts. But in this post, we will look at the original paper,

"Latent Dirichlet allocation - David M. Blei, Andrew Y. Ng, Michael I.Jordan,"

and aim to explore its background and deeper principles. However, I can't explain everything about LDA from A to Z due to the complexity of the technique. While studying, I felt that some crucial concepts in the paper disappeared in explanatory materials, and as these were adopted by others, the basic background began to fade away. Therefore, I will try to reflect these points as much as I read through the paper. For this purpose, I will follow the order of the paper as much as possible, but I will add explanations where necessary.

Then, let's first look at the background of LDA's birth.

Introduction

LDA's Origin, Information Retrieval

LDA's purpose, as explained in the introduction, is to "simply" represent a multitude of documents. This attempt was not first started in the machine learning field but in the Information Retrieval (IR) field. What IR aimed for was to represent numerous documents on the internet as simple numbers based on "frequency". To this end, they used the concept of TF-IDF, which goes beyond the simple frequency of words appearing in a document to reflect the frequency of words in the entire document collection.

wi,j=tfi,j×log(Ndfi)w_{i,j}=tf_{i,j}\times \log(\frac{N}{df_i})

tfi,j=tf_{i,j}= number of occurrences of ii in jj

dfi=df_i = number of documents containing ii

N=N = total number of documents

However, the dimension reduction capability of TF-IDF was not very significant. To show better performance than this, Latent Semantic Indexing (LSI) appeared. LSI applies Singular Value Decomposition (SVD) to a Matrix that represents word frequencies and documents, aiming to find a Subspace that maintains as much variance as possible. When using LSI, it is said that it represents linear combinations of TF-IDF, and it has the advantage of being able to capture the relationship of synonyms and polysemes.

But LSI also could not satisfy researchers, and Hofmann (1999) proposed pLSI. The concept closest to the principle of LDA is pLSI. pLSI assumes that each word in the document comes from a Mixture Model (Multinomial) of topics. Thus, the words that make up a document will consist of words from various topics (Mixture Components), and if the distribution of these topics is expressed, it becomes the topic distribution of the document. If you have studied LDA even a little, you will feel that this concept is very similar to LDA. However, pLSI is different from LDA, and the part pointed out as a limitation of pLSI is that direct topic assignment is possible for words, but the document derives topics from these words, which is an indirect method.

Then, is there a way to build a probability model for both words and documents? This is where LDA, which overcomes the limitations of pLSI, was born.

  1. Notation and Terminology Now that we have looked at the techniques before LDA, we will soon need to look at the procedure of the LDA technique

. But before that, let's organize the notation once as done in the paper.

Since it will be uncomfortable to use the paper's expression when explaining, let's express Word as w, Document as d, and Corpus as c.

Latent Dirichlet Allocation

LDA: Generative probabilistic model of a corpus

LDA is a generative probability model of a corpus. The fact that it generates means that, assuming the corpus is all the words in the world, it uses these words to create documents. Alternatively, it can be expressed differently, meaning that documents are made through the distribution of latent topics, and each topic is made up of a distribution of words. Let's give a simple example.

Let's say a writer named Leo has to quickly write about any topic. Therefore, she will choose a topic of interest from the 'topic distribution', and when she sits down at her desk and starts writing, the words she will use will naturally reflect that topic. If Leo writes about the topic of 'diet', she will use many words like sweet potato, shake, salad, etc., to describe the diet.

Mixture model

Here, we need to stop and go over the concept of the Mixture Model. A representative example of the Mixture Model is the Gaussian Mixture Model (GMM), which is often used in Clustering. That is, the concept of the Mixture Model itself originally represents the problem of which distribution a certain data belongs to (Cluster). Mixture Model is often used interchangeably with Mixture Distribution, but these two are concepts with different "purposes".

Just as GMM tries to identify subgroups (Clusters) from the entire distribution, LDA assumes that there are latent topics (Latent Topics) and judges which subtopics the currently viewed document belongs to. And it means that we will view the distribution of Latent Topics as a Dirichlet distribution. If we follow the name LDA once again,

This is what it means.

Dirichlet Distribution

The Dirichlet distribution is the posterior probability distribution of the conjugate prior distribution. The conjugate prior distribution means that when the posterior probability distribution p(θx)p(\theta|x) and the prior distribution p(θ)p(\theta) are tied to the same family group. This concept is used in Bayesian statistics to update the posterior using the prior, and when the prior and posterior distributions are tied to the same family group, it is convenient to calculate, so the conjugate prior distribution is used. It can be expressed in a formula as P(θX)=P(Xθ)P(θ)P(\theta|X) = P(X|\theta) * P(\theta)

and the purpose of the formula is to find the left-hand term P(θX)P(\theta|X), which means the probability distribution of the parameters when data are given. P(Xθ)P(X|\theta) on the right-hand side represents the distribution of data when the probability distribution is given, and P(θ)P(\theta) represents the probability distribution of the parameters. If we organize this in words following the formula (starting from the right-hand term),

Multiplying the (probability distribution of parameters) and the (distribution of data when that probability distribution is given) yields the (distribution of parameters when data exist). In other words,

Multiplying the (Prior Distribution) and the (Likelihood) yields the (Posterior Distribution), and if we apply this tothe (kinetic) flow of LDA, it can be expressed as:

(The predefined Topic Distribution - [Prior] Dirichlet) * (Distribution of words when the Prior is set) = (Topic Distribution when the distribution of words exists - [Posterior] Multinomial).

Thus, there is a clear reason for using Conjugate Prior / Posterior in LDA.

LDA Procedure

LDA follows this procedure to create each Document dd from the entire Corpus cc :

  1. Choose NN ~ Poisson(ξξ).

  2. Choose θθ ~ Dir(αα).

  3. For each of the N words wnw_n: (a) Choose a topic znz_n ~ Multinomial(θθ). (b) Choose a word wnw_n from p(wnzn,β)p(w_n|z_n,β), a multinomial probability conditioned on the topic znz_n.

This procedure has additional details:

Simplex

A kk-dimensional Dirichlet random variable θθ can take values in the (k1)(k-1) simplex.

The above sentence, taken directly from the paper, means that a random variable extracted from a kk-dimensional Dirichlet distribution exists on a k1k-1 dimensional simplex. But what exactly is a simplex? While simplex has various definitions in different fields, in probability theory, it means "Space of all probabilities distributions with support N". Depending on kk, you can define simplex as follows:

Although I don't fully grasp it yet, I will guide you on how to apply this to the Dirichlet Distribution.

A point I missed when mentioning the Dirichlet Distribution earlier is that Dirichlet is a 'distribution of distributions'. Specifically, it contains distributions of Multinomials, and we sample these distributions. The best example to visualize this is a Dirichlet distribution with k=3k = 3, i.e., a 2-simplex, which we will look at.

k=3k = 3 means that we are dealing with 3 Topics, and we obtain the probability Prior Distribution for these 3 Topics from the Dirichlet Distribution. At this time, you can contain a 3-dimensional probability distribution in the form of a triangle simplex.

And if you look closely, do you see numbers on the triangle? These numbers represent αα in "Choose θθ ~ Dir(αα)". In LDA, the Dirichlet Distribution used is symmetric. That is, one αα is used equally regardless of how many dimensions the distribution has. As αα increases, the 3-dimensional probability distribution gathers in the center, meaning that each probability has many points with equal values. Conversely, as αα decreases, they scatter, gathering at the vertices, which means that the probability of each Topic appearing is concentrated in one Topic. Depending on the form of your data, choosing αα is a smarter way to use LDA.

LDA, as Bayesian Network

When parameters αα and ββ exist, the joint distribution formula for the mixture θθ of topics,

NN topics zz, and NN words ww can be expressed as follows:

p(θ,z,wα,β)=p(θα)i=1Np(znθ)p(wnzn,β)p(θ, z, w|α, β) = p(θ|α) ∏_{i=1}^{N} p(z_n|θ) p(w_n|z_n, β)

Reading the right-hand side of the formula, it surprisingly makes sense. However, there is doubt about whether such a joint distribution interpretation is appropriate or forced. Reading it:

Therefore, the left-hand side joint distribution p(θ,z,wα,β)p(θ, z, w|α, β) multiplies the Dirichlet distribution θθ given αα with the distribution of topics znz_n and words wnw_n in the document. However, the usual factorization of joint distributions is done as follows:

p(x1,x2,,xK)=p(xKx1,,xk1)p(x2x1)p(x1)p(x_1, x_2, …, x_K) = p(x_K|x_1, …, x_{k-1}) … p(x_2|x_1) p(x_1)

Thus, the joint distribution interpretation used in LDA is different from the standard form and can be explained as a Bayesian Network. However, the paper only examines Bayesian Statistical Modeling, i.e., prior/posterior distributions, so some parts were not clear.

Bayesian Network is well explained in Professor Moon Il-Chul's lectures at KAIST. I will briefly describe it.

Bayesian Network is a directional graph model where the links in the graph have direction and are represented with arrows. This indicates that the arrows represent conditional probabilities. Below is a Bayesian Network of my recent mood and state, and we will use it to solve the joint distribution.

Thus, even if a joint distribution contains numerous variables, if it is represented or representable as a Bayesian Network, it becomes easier to factorize the joint distribution. But the LDA representation equivalent to the above example of my mood exists.

Latent Dirichlet allocation - Wikipedia

If we consider the above Plate Notation as a Bayesian Network and extract elements to solve the joint distribution p(θ,z,wα,β)p(θ, z, w|α, β), we can express:

Final Formula Summary with Luis Serrano

When I couldn't quite grasp the 'intuitive' understanding of LDA, I watched a YouTube video by Luis Serrano, who explained the Generative Model of LDA well through diagrams.

We said the Generative Model's Joint Distribution of LDA could be expressed as:

p(θ,z,wα,β)=p(θα)i=1Np(znθ)p(wnzn,β)p(θ, z, w|α, β) = p(θ|α) ∏_{i=1}^{N} p(z_n|θ) p(w_n|z_n, β)

However, in this video, ββ is described as a parameter, and the φφ Dirichlet distribution using this parameter is clearly depicted (a formula not appearing in the original paper, and I guess it might have been included in subsequent research following the original). If we transform the formula to represent the entire Corpus cc, it becomes:

Explaining the new formula:

p(Cα,β)=j=1MP(θj;α)i=1KP(φi;β)t=1NP(Zj,tθj)P(Wj,kφZj,k)p(C|α, β) = ∏_{j=1}^{M} P(θ_j; α) ∏_{i=1}^{K} P(φ_i; β) ∏_{t=1}^{N} P(Z_{j, t}|θ_j) P(W_{j, k}|φ_{Z_{j, k}})

LDA and Exchangeability

“What is the key to LDA's success?” “It’s Exchangeability…”

The theoretical background of LDA includes Bayesian Statistics, Mixture Models, and more. However, one of the most fundamental theories, which constitutes an entire chapter in the paper, is De Finetti's Exchangeability.

When random variables z1,z2,,zNz_1, z_2, …, z_N exist, if the probability remains the same even when their order is varied in different ways, and always remains the same regardless of the order, we can say that Exchangeability exists. This can be expressed as:

p(z1,,zN)=p(zπ(1),,zπ(N))p(z_1, …, z_N) = p(z_{π(1)}, …, z_{π(N)}), where ππ is a permutation.

And if every subsequence in an infinite sequence of random variables is exchangeable, then the entire sequence is exchangeable.

Initially, I didn't quite grasp this concept because in probabilities like p(a,b,c)=p(b,a,c)=p(c,a,b)p(a, b, c) = p(b, a, c) = p(c, a, b), the order of elements in the joint distribution doesn't matter. However, the joint distribution of word sequences in a sentence p(w1,,wN)p(w_1, …, w_N) is a probability that should guarantee the order. For example:

p("our,teams,jungle,is,trash")p("teams,our,is,trash,jungle")p("our, team's, jungle, is, trash") ≠ p("team's, our, is, trash, jungle").

This shows that the probability changes with the order.

De Finetti’s Theorem

De Finetti's theorem, using Exchangeability, provides the following theorem:

p(X1,X2,,XN)=(i=1Np(Xiθ))dπ(θ)p(X_1, X_2, …, X_N) = ∫(∏_{i=1}^{N} p(X_i|θ))dπ(θ)

This theorem simplifies the joint distribution significantly, showing that when θθ is given, the random variables derived from the distribution θθ are independent and identically distributed.

Why is this included in LDA's assumptions?

LDA also ignores the order. In other words, it ignores the order of words. Unlike the previous sentence example where p("our,teams,jungle,is,trash")p("teams,our,is,trash,jungle")p("our, team's, jungle, is, trash") ≠ p("team's, our, is, trash, jungle"), in LDA, they are considered equal due to the Bag of Words assumption. Since all words in the entire Corpus are in one Bag, their order is ignored, enabling Exchangeability. This provides computational advantages in LDA’s model:

p(θ,z,wα,β)=p(θα)i=1Np(znθ)p(wnzn,β)p(θ, z, w|α, β) = p(θ|α) ∏_{i=1}^{N} p(z_n|θ) p(w_n|z_n, β)

Here, i=1N∏_{i=1}^{N} benefits from this. Even using Bayesian Network, there was no theoretical guarantee for i=1N∏_{i=1}^{N}. However, knowing that words and topics in a document are exchangeable, we can represent probabilities in the document not as a joint distribution but as numerous multiplications.

(p(θ,z,wα,β)=p(θα)p(wn,zn,d)( p(θ, z, w|α, β) = p(θ|α) p(w_n, z_n, d)p(θ,z,wα,β)=p(θα)i=1Np(znθ)p(wnzn,β)p(θ, z, w|α, β) = p(θ|α) ∏_{i=1}^{N} p(z_n|θ) p(w_n|z_n, β)

Relationship with Other Latent Variable Models

This section compares LDA with simpler latent variable models, such as the Unigram Model and Mixture of Unigrams.

Unigram Model / Mixture of Unigrams, pLSI

Unigram Model

The Unigram model is the simplest model in language models. It assumes each word is independent and calculates the joint distribution as a product of probabilities.

P(w1,w2,,wn)=i=1nP(wi)P(w_1, w_2, …, w_n) = ∏_{i=1}^{n} P(w_i) or P(WordsinDocument)=n=1Np(wn)P(Words in Document) = ∏_{n=1}^{N} p(w_n).

This means that each word is drawn from a single Multinomial Distribution.

Mixture of Unigrams

The Mixture of Unigrams (Unigram + random topic variable zz) is structured as

follows:

  1. First, choose a Topic zz,

  2. Then, independently draw N words from the Conditional Multinomial p(wz)p(w|z).

Thus, the formula is structured as follows:

p(d)=zp(z)n=1Np(wnz).p(d) = ∑_{z} p(z) ∏_{n=1}^{N} p(w_n|z).

Here, p(z)p(z) appears as a weight, and n=1Np(wnz)∏_{n=1}^{N} p(w_n|z) acts as the sub-model. The Mixture of Unigrams assumes that only one Topic exists in a document, making the word distribution p(d)p(d) effectively represent the Topic.

In the above figures, the left Topic Multinomial Distribution represents p(z)p(z), and the chosen zz's p(wz)p(w|z) is represented by the right Multinomial Distribution. However, in the Mixture of Unigrams, since zp(z)∑_{z} p(z) aggregates each topic one by one, it represents a Single Topic, whereas LDA constructs the formula to include multiple Topics in one document.

Inference and Parameter Estimation

So far, we have learned about the Latent Topic Model, which is how a document or a corpus is created. However, this procedure is not yet Topic Modeling.

When we actually face the goal of Topic Modeling, what do we have in our hands? It's the documents. We only have the documents and the frequency of words contained in them. Therefore, θθ and zz are unknown to us, and we need to infer and update these parameters through Inference, finding out how the topics in our documents are composed. The word 'Latent' itself means 'hidden', so the process of finding these out is Latent Dirichlet Allocation.

θθ and zz are Hidden or Latent Variables. That is, except for ww, nothing is visible. The formula for obtaining the Posterior Distribution to find these is

p(θ,zw,α,β)=p(θ,z,wα,β)p(wα,β)p(θ, z|w, α, β) = \frac{p(θ, z, w|α, β)}{p(w|α, β)}

However, according to the paper and various materials, calculating this formula for Estimation is Intractable, i.e., impossible. Therefore, we resort to approximation methods for calculation.

The approximation method mentioned in the paper is 'Variational Inference'. I still lack the ability to fully understand Variational Inference, but its goal seems to be setting a Lower Bound through Jenkin's Inequality. However, the formulas look quite complex and are filled with numerous complicated expressions.

An easier and simpler method for implementation is Gibbs Sampling, which I will explain.

(Collapsed) Gibbs Sampling

Once Gibbs Sampling starts, it no longer seems like solving a probability problem but rather continuously updating frequency calculations, simplifying the problem. It is widely used and the easiest and most certain method among various approximation techniques. The Python-implemented 'lda' also applies Gibbs Sampling.

I will start by explaining Gibbs Sampling.

Gibbs Sampling involves quite a bit of formula calculation, and it would be better to watch Professor Gang Pil-Seong's 'Topic Modeling - Part 3' rather than me explaining it line by line. Therefore, I will only cover the basic concept here, and we will look at the code implementation together.

The basic idea of Gibbs Sampling is, "By fixing other variables and changing only one variable and updating it, all variables are automatically updated." Based on this, the distribution we fundamentally want to calculate is:

p(zi=jz(i),w)p(z_i = j|z_{(-i)}, w)

This is the probability that the topic of the ii-th word in a document is jj after erasing the topic information of the ii-th word. To derive this, the most probable topic from the remaining topic distribution is inserted into the erased topic information after erasing the topic information. Repeating this process, similar to the EM algorithm, will converge to the optimal Topic Distribution.

But here's a question: Isn't Topic Modeling about calculating

Why, then do we only iterate with zz, the topic of words, as the subject of interest? Shouldn't there be direct calculations for the above two distributions?

This is because if you calculate zz through a process like Gibbs Sampling, the actual goal of the two distributions above is derived automatically. If we calculate zz for all words in the Corpus, we know the topics of each word. Then, we can determine the topic distribution of each document (θθ) by aggregating the topics of words in each document. Similarly, by selecting a topic and examining the distribution of words contained in it, we can determine the word distribution within a topic (φφ). The appearance of (Collapsed) in the title refers to the fact that θθ and φφ do not need to be inferred directly in the process of inference, as the calculations revolve around z.

Ultimately, after several complex formulas, the following final formula is derived:

P(Z,W;α,β)(nd^,()k^(d^,n^))+αk(k=1Knd^,()k^(d^,n^))+αk×(n(),v^k^(d^,n^))+βv(v=1Vn(),v^k^(d^,n^))+βvP(Z,W;\alpha,\beta)\propto \frac{(n^{\hat{k}-(\hat{d},\hat{n})}_{\hat{d},(\cdot)})+\alpha_k}{(∑^{K}_{k=1}n^{\hat{k}-(\hat{d},\hat{n})}_{\hat{d},(\cdot)})+\alpha_k} \times \frac{(n^{\hat{k}-(\hat{d},\hat{n})}_{(\cdot),\hat{v}})+\beta_v}{(∑^{V}_{v=1}n^{\hat{k}-(\hat{d},\hat{n})}_{(\cdot),\hat{v}})+\beta_v}

\propto Topic weight over document ×\times Word weight over Topic

This formula guides how updates are carried out and how conclusions are reached.