ML0177 LDA Topic Modeling

Explain Latent Dirichlet Allocation as a generative model: what do the document-topic and topic-word distributions look like, and how do you choose the number of topics?

Three-panel dashed scenario card. The first panel describes 40,000 support tickets of about 120 tokens each, turned into bags of words over an 8,000-term vocabulary and fit with LDA by collapsed Gibbs sampling at K equal to 10, 40 and 150. The second panel lists held-out perplexity of 1,420 at K equal to 10, 1,180 at K equal to 40 and 1,050 at K equal to 150, plus two K equal to 150 topics, 87 and 112, that both start with refund, charge, card. The third panel asks what each ticket's topic mix and each topic's word distribution should look like, and whether to ship K equal to 10, 40 or 150, under the constraint that support ops must be able to name every topic from its top 10 words.

The Problem: A support team fit LDA to 40,000 tickets with an 8,000-term vocabulary at K = 10, 40 and 150, and held-out perplexity kept falling from 1,420 to 1,050 while the 150-topic run began splitting refunds into near-duplicate topics. They need to know what the per-ticket and per-topic distributions should look like and which K to ship.

Answer

Latent Dirichlet Allocation (LDA) treats every document as a bag of words generated by a mixture of K topics, where each topic is a probability distribution over the whole vocabulary. To write document d, the model draws topic proportions \theta_d \sim \mathrm{Dir}(\alpha), then for each word slot picks a topic z from \theta_d and a word from that topic’s distribution \phi_z. The document-topic vectors are points on the (K-1)-simplex, and with \alpha below 1 they are sparse, so a typical document uses 2 to 4 topics. The topic-word vectors \phi_k \sim \mathrm{Dir}(\beta) are long-tailed distributions over all V words, where a few dozen words carry most of the mass. The number of topics is a hyperparameter the model does not learn. You sweep it and pick the value where topic coherence peaks and topics stay distinct, because held-out perplexity usually keeps improving long after topics stop being interpretable.

(1) Generative Story: Each topic draws a word distribution \phi_k \sim \mathrm{Dir}(\beta) once for the whole corpus. Each document draws \theta_d \sim \mathrm{Dir}(\alpha), and each word slot draws a topic z_{dn} from \theta_d and then the word from \phi_{z_{dn}}.
(2) Plate Notation: A K plate repeats \phi_k, a D plate repeats \theta_d, and an N_d word plate nested inside it repeats z_{dn} and w_{dn}. The word is the only observed node, so inference means running this story backwards to recover \theta, \phi and z.
(3) Document-Topic Distribution: \theta_d lives on the (K-1)-simplex with prior mean \alpha_k / \sum_j \alpha_j. With \alpha below 1 it is sparse, so a real document loads on a handful of topics rather than spreading across all K.
(4) Topic-Word Distribution: \phi_k gives every vocabulary word nonzero probability but is heavily skewed, and a small \beta sharpens the head. Its top words name the topic, and one word can rank high in several topics, which is how LDA handles polysemy.
(5) Admixture, Not Clustering: Topics are assigned per word, so documents mix topics. Shrinking \alpha toward 0 pushes LDA toward a one-topic-per-document mixture of unigrams, whose hard-assignment fit is k-means with KL divergence.
(6) Choosing K: Sweep K over several seeds and pick the peak of coherence (UMass, NPMI, C_v), backed by topic diversity and a human read of the top words. Held-out perplexity usually keeps falling and is a poor guide to interpretability.

Mathematical Formulation:
\theta_d \sim \mathrm{Dir}(\alpha)
\phi_k \sim \mathrm{Dir}(\beta)
z_{dn} \sim \mathrm{Cat}(\theta_d)
w_{dn} \sim \mathrm{Cat}(\phi_{z_{dn}})
p(w_{dn} = v \mid \theta_d, \Phi) = \sum_{k=1}^{K} \theta_{dk}\,\phi_{kv}
p(z_{dn} = k \mid \cdot) \propto (n_{dk}^{\neg} + \alpha)\,\frac{n_{kv}^{\neg} + \beta}{n_{k}^{\neg} + V\beta}
\mathrm{PPL} = \exp\left(-\frac{\sum_d \log p(\mathbf{w}_d)}{\sum_d N_d}\right)
C_{\mathrm{UMass}} = \sum_{m=2}^{M} \sum_{l=1}^{m-1} \log \frac{D(v_m, v_l) + 1}{D(v_l)}

Where:

  • \theta_d \in \Delta^{K-1} is the topic mixture of document d \in \{1, \ldots, D\}, and \alpha is its concentration, either a symmetric scalar or a vector (\alpha_1, \ldots, \alpha_K).
  • \phi_k \in \Delta^{V-1} is the word distribution of topic k \in \{1, \ldots, K\}, V is the vocabulary size, \beta is the topic-word concentration, and \Phi stacks the \phi_k into a K \times V matrix.
  • z_{dn} is the latent topic of word slot n \in \{1, \ldots, N_d\}, w_{dn} is the observed word, and \mathrm{Cat} is a categorical draw.
  • The marginal word probability is a convex combination of topic rows, so the D \times V matrix of word probabilities factorizes as \Theta \Phi with rank at most K.
  • In the collapsed Gibbs update, \theta and \Phi are integrated out: n_{dk}^{\neg} counts tokens of document d assigned to topic k, n_{kv}^{\neg} counts corpus-wide assignments of word v to k, and n_k^{\neg} is all tokens in k, each excluding the token being resampled.
  • \mathrm{PPL} is held-out perplexity, where p(\mathbf{w}_d) is the marginal likelihood of an unseen document and N_d its length; lower is better, and the marginal must be estimated (importance sampling, left-to-right, or document completion).
  • C_{\mathrm{UMass}} scores one topic from its top-M words v_1, \ldots, v_M ranked by \phi_{kv}. D(v) counts documents containing v, D(v_m, v_l) counts documents containing both, the +1 avoids \log 0, and scores closer to 0 mean more co-occurring top words.

What the two distributions look like is set by the concentration parameters and the data together. A symmetric Dirichlet with \alpha = 1 is uniform over the simplex, values below 1 push draws toward corners and edges, and values above 1 pull draws toward the even mixture 1/K. The posterior mean \hat{\theta}_{dk} = (n_{dk} + \alpha)/(N_d + K\alpha) shows the prior acting as K\alpha pseudo-tokens. At K = 40, \alpha = 0.1 adds 4 pseudo-tokens to a 120-token ticket, while the older heuristic \alpha = 50/K = 1.25 adds 50 and visibly flattens every ticket’s mixture. On the word side, \beta = 0.01 over V = 8{,}000 words makes each \phi_k Zipf-like, which is why a topic is read and named from its top 10 to 20 words. Learning an asymmetric \alpha helps in practice, because a few broad topics can then absorb greetings and sign-offs that would otherwise leak into every specific topic.

Three-panel figure. Panel a: LDA plate notation with hyperparameter alpha pointing to theta_d, theta_d pointing to z_dn, z_dn pointing to the shaded observed word w_dn, and beta pointing to phi_k, which points up into w_dn; a word plate N_d surrounds z_dn and w_dn, a document plate D surrounds theta_d and the word plate, and a topic plate K surrounds phi_k. Panel b: bar chart of one ticket's topic proportions over 40 topics with alpha equal to 0.1, where billing holds 0.62, login 0.24, shipping 0.09 and the other 37 topics share the remaining 0.05. Panel c: horizontal bar chart of the billing topic's top 12 words from refund at 0.071 down to fee at 0.013, with the remaining 7,988 words sharing 0.605 of the probability.

Figure 1: (a) Plate notation, with w_{dn} as the only observed node and \phi_k shared by every document. (b) One ticket’s \theta_d under \alpha = 0.1, where 3 of 40 topics hold 95% of the mass. (c) The billing topic’s \phi_k, whose top 12 words carry 0.395 while the other 7,988 words share a long tail of 0.605.

Choosing K is a model-selection problem that the likelihood answers badly. Held-out perplexity almost always keeps improving as K grows, because extra topics give the model more parameters to soak up residual co-occurrence. Human word-intrusion studies found that better perplexity often comes with less interpretable topics. Practitioners therefore sweep K on a grid such as 10, 20, 40, 80 and 150 with several seeds and score each run with topic coherence. UMass uses document co-occurrence in the training corpus, while NPMI and C_v use sliding windows over a reference corpus and track human ratings more closely. Pair coherence with topic diversity (the share of unique words among all topics’ top 25) and seed stability, read the topics at the two or three best values, and let downstream accuracy break ties. The hierarchical Dirichlet process (HDP) infers the number of topics instead, though it moves the choice into its own concentration parameters.

Worked Example:
Score two candidate topics with UMass coherence on a 1,000-ticket reference sample, using each topic’s top three words in rank order and natural logs. Topic A is {refund, charge, card} with D(\mathrm{refund}) = 200, D(\mathrm{charge}) = 150, and co-document counts of 90 for charge with refund, 60 for card with refund, and 80 for card with charge. Its score is \log(91/200) + \log(61/200) + \log(81/150) = -0.787 - 1.187 - 0.616 = -2.590. Topic B is {refund, password, shipping} with D(\mathrm{password}) = 120 and co-document counts of 4, 10 and 2. Its score is \log(5/200) + \log(11/200) + \log(3/120) = -3.689 - 2.900 - 3.689 = -10.278. Topic A’s words genuinely appear together, Topic B is a mixed topic, and the average of this score over all K topics is what a K sweep compares.

The cleanest way to see what LDA adds is to compare it with clustering. A mixture of unigrams draws one topic per document and generates every word from it. Fitting it with hard assignments is k-means on word-count vectors with KL divergence in place of squared Euclidean distance, the same way a Gaussian mixture collapses to k-means as its variances shrink. LDA relaxes the one-topic constraint to a per-word assignment, so a ticket about a double charge after a password reset can be 60% billing and 30% login. In matrix terms LDA is a Bayesian, non-negative factorization of document-word probabilities into \Theta times \Phi. pLSA is the same factorization without the Dirichlet priors, which is why it overfits and cannot score a new document without refitting.

PropertyLDAMixture of unigramsk-means on word counts
Topics per documentA mixture over all K topicsExactly oneExactly one cluster
What gets assignedEvery word token gets its own topicThe whole documentThe whole count vector
Document representationTopic proportions on the simplexPosterior over K topicsNearest centroid index
PriorsDirichlet on document mixtures and topic-word rowsOptional Dirichlet on mixing weightsNone
Fit byCollapsed Gibbs or variational BayesEMLloyd iterations
New documentFold in its mixture with topics held fixedBayes rule over K topicsAssign the nearest centroid
Limit relationApproaches the mixture of unigrams as alpha shrinks toward 0Hard-assignment EM becomes k-means with KL divergenceEuclidean special case of hard clustering

Login to view more content
Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *