Sub topic 6
Fitting LDA: Variational Bayes and Gibbs Sampling
This chapter assumes that you know tokenisation, stop words, TF-IDF, basic probability, and the idea of a vector embedding. You should also know that LDA represents each document as word counts and treats each word occurrence as having a hidden topic assignment.
LDA must infer two unknown distributions:
- The document-topic distribution, which says how likely each topic is in a document.
- The topic-word distribution, which says how likely each word is under a topic.
The exact posterior distribution over these unknowns is difficult to calculate because every word's topic assignment depends on other assignments. LDA therefore uses approximate inference.
Variational Bayes
Variational Bayes replaces the difficult posterior with a simpler distribution that can be optimised. The algorithm chooses parameters for this simpler distribution so that it is as close as possible to the true posterior.
A common factorisation treats document-topic proportions, topic-word proportions, and word-topic assignments as conditionally independent in the approximation. The algorithm then repeats coordinate updates:
- Estimate the probability that each word occurrence belongs to each topic.
- Update the document-topic parameters using those probabilities.
- Update the topic-word parameters using those probabilities.
- Repeat until the objective changes very little or the iteration limit is reached.
The method is deterministic only when its initial state and processing order are fixed. Different initialisations can still produce different topic arrangements. Topic numbers themselves are interchangeable, so topic 0 in one run does not necessarily represent topic 0 in another.
Gensim's LdaModel uses online variational Bayes. It can update the model with batches of documents instead of holding the full corpus in memory. This makes it practical for large or streaming collections.
Gibbs sampling
Collapsed Gibbs sampling estimates the posterior by repeatedly sampling a topic for each word occurrence. It temporarily removes that occurrence from the count tables, then calculates the probability of assigning it to topic k:
P(zᵢ = k | rest) ∝ (nᵈₖ + αₖ) × (nᵏʷ + ηʷ) / (nᵏ + Ση)
Here, nᵈₖ counts topic k in the word's document, nᵏʷ counts word w in topic k, and nᵏ counts all words assigned to topic k. The superscript “rest” means that the current word is excluded from those counts.
After many sweeps through the corpus, the collected assignments approximate the posterior. Early sweeps are called burn-in because the chain may still depend strongly on its starting assignments. Later samples can be averaged to estimate topic distributions.
Choosing between the methods
| Criterion | Variational Bayes | Gibbs sampling |
|---|---|---|
| Main operation | Optimises distribution parameters | Samples topic assignments |
| Large-corpus speed | Usually faster and easier to scale | Often slower |
| Output | One approximate posterior | Samples from an approximate posterior |
| Main concern | Factorisation bias and local optima | Mixing, burn-in, and autocorrelation |
Variational Bayes is usually the practical choice for a large production corpus. Gibbs sampling can be useful when posterior sampling matters or when you want a different inference check. Neither method guarantees meaningful human topics. Always inspect topic words and representative documents after fitting.
Exercise
Why does Gibbs sampling remove a word from the count tables before choosing its topic?
Show answer
Removing it prevents the word from influencing its own conditional probability and makes the update depend only on the other assignments.