Topic Modeling & Latent Dirichlet Allocation (LDA)
Introduction & Session Context
- Instructor resumes after a 2-week gap; previous unit covered Part-of-Speech tagging (HMM, Maximum Entropy, Bi-LSTM, CRF, neural models).
- New unit: Topic Modeling – automatically discovering the themes a document (or corpus) discusses.
- Central real-world driver: filtering massive news/email/social feeds so that only content matching a user’s topical interests is shown.
Motivation & Use-Cases
- Summarisation (e.g. Gmail “Summarise this thread”).
- Measuring topic coverage and intensity inside a single document.
- Finding topic–topic correlations (e.g. "war" ↔ "arms race", "batting" ↔ "bowling").
- Recommendation systems: given one liked document, find others sharing its hidden topical mix.
- Tracking how topics evolve through time (e.g. Sachin Tendulkar → Virat Kohli in cricket articles).
- Domain examples: book/movie tagging, Research papers, protein–DNA sequence analysis, even image generation (analogy to DALL·E).
Key Terminology & Symbols (will re-appear throughout)
- : size of vocabulary (distinct word types).
- : number of topics we wish/assume.
- : number of documents in corpus.
- : number of word tokens in document .
- : the n-th observed word in document d.
- : latent topic id of that word.
- , (or ) : Dirichlet hyper-parameters – priors over topic mixtures and word mixtures respectively.
- : -dimensional vector – topic proportions for document d.
- : -dimensional vector – word distribution for topic k.
Historical Approaches
- Unigram Model: choose one word → infer topic. Weak due to polysemy ("bat" = cricket bat or mammal).
- Mixture of Unigrams: combine several unigram models; still brittle.
- Latent Semantic Indexing (LSI / pLSI)
- Uses co-occurrence patterns + Singular Value Decomposition to embed documents/words in lower-dimensional space.
- Good when each doc focuses on a single theme; less effective when documents mix themes.
- Latent Dirichlet Allocation (LDA)
- Probabilistic/Bayesian upgrade over LSI.
- Explicitly models documents as mixtures of topics and topics as mixtures of words.
Breaking Down the Name “Latent Dirichlet Allocation”
- Latent: topics are hidden; word “cricket” need not appear for text to be about cricket.
- Dirichlet: the prior used for both and ; convenient because it’s conjugate to the multinomial.
- Allocation: assigning each word token to a topic.
Mathematical Foundations
- Dirichlet PDF (for -vector ):
- Conjugacy: Multinomial likelihood × Dirichlet prior ⇒ Dirichlet posterior (keeps inference tractable).
- Plate Diagram Essence
- Outer plate (repeat over documents ) contains inner plate (repeat over word positions ).
- Arrows: , , , .
- Simplex Geometry
- Any -dimensional probability vector lives on a -simplex ⇒ can visualise 3-word toy case on a triangle.
LDA Generative Process (Top-down description)
- For each topic
• Draw word distribution . - For each document
a. Draw topic mixture . b. For each word position
i. Draw topic .
ii. Draw word .
- This hierarchy allows multiple topics per document and ambiguous words across topics.
- Example narratives:
• "bat", "ball", "over" → cricket topic vs. "bat", "nocturnal", "echolocation" → zoology topic.
• "bank" + "loan" vs. "bank" + "river".
Posterior Inference Challenge
We need but exact marginalisation
is intractable for realistic corpora.
Approximate Inference Techniques
1. Gibbs Sampling (collapsed)
- Integrate out and analytically; iterate over tokens updating conditioned on other assignments.
- Conditional update formula:
• : count of word w assigned to topic k (excluding current position).
• : count of tokens in doc d already assigned to k. - Iterate until Markov chain mixes; sample draws approximate posterior.
- Parameter estimates after convergence:
2. Variational Inference (mentioned, not detailed)
- Replaces sampling with optimisation of a tractable surrogate distribution.
Practical Questions LDA Helps Answer
- What mixture of topics does a given document contain?
- Given one document, find other documents with similar topic vectors.
- Track topic prevalence/intensity across time (diachronic corpora).
- For recommendation: map user interest vector ↔ document topic vectors.
Connections to Earlier Course Material
- Dimensionality Reduction: LDA’s simplex view echoes PCA/SVD; both aim to compress information without losing signal.
- Smoothing: Dirichlet prior plays role similar to add-one or Kneser–Ney smoothing in n-gram models.
- Probabilistic tagging models: Markov-style reasoning (HMM → sequence, LDA → bags-of-words).
Computational & Implementation Considerations
- Must choose (can use CV, non-parametric methods, or business knowledge).
- Hyperparameter tuning for influences sparsity: small ⇒ each doc uses few topics; small ⇒ each topic uses few signature words.
- Large corpora → high-dimensional counts; Gibbs sampling still linear in tokens but needs many iterations.
Ethical & Philosophical Reflections
- Hidden topic discovery can profile users implicitly (privacy concerns).
- Topic ambiguity emphasises need for transparent AI explanations – why did system tag my article as “war”?
- Potential misuse in disinformation: generating plausible documents on sensitive topics at scale.
Formula Recap (Cheat-Sheet)
- Dirichlet prior: .
- Joint model: .
- Collapsed Gibbs conditional (core update):
. - Parameter expectations after sampling: see and formulas above.
Visual & Intuitive Aids Discussed
- Plate diagrams – rectangles denote replication, circles latent vars, shaded circles observed words.
- Simplex triangles – 3-word toy example: corners = pure topics, interior points = document mixtures.
- Heat-map images (25-pixel toy example) – each topic visualised as distinct 5×5 pattern; documents generated by mixing patterns.
Take-Away Summary
- LDA treats each document as a distribution over hidden topics; each topic is a distribution over words.
- Dirichlet priors make Bayesian inference neat (conjugacy) and provide built-in smoothing.
- Exact inference is infeasible; collapsed Gibbs sampling delivers a practical solution by updating one word-assignment at a time.
- Outcomes: interpretable topic–word lists, per-document topic proportions, ability to generate or classify documents, and a gateway to many modern NLP/ML pipelines.