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)
  • VV : size of vocabulary (distinct word types).
  • KK : number of topics we wish/assume.
  • DD : number of documents in corpus.
  • NdN_d : number of word tokens in document dd.
  • wd,nw_{d,n} : the n-th observed word in document d.
  • zd,nz_{d,n} : latent topic id of that word.
  • α\alpha, η\eta (or β\beta) : Dirichlet hyper-parameters – priors over topic mixtures and word mixtures respectively.
  • θd\theta_d : KK-dimensional vector – topic proportions for document d.
  • ϕk\phi_k : VV-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 θ\theta and ϕ\phi; convenient because it’s conjugate to the multinomial.
  • Allocation: assigning each word token to a topic.

Mathematical Foundations
  • Dirichlet PDF (for KK-vector θ\theta):
    p(θα)=1B(α)<em>k=1Kθ</em>kαk1p(\theta\mid\alpha)=\frac{1}{B(\alpha)}\prod<em>{k=1}^{K}\theta</em>k^{\alpha_k-1}
  • Conjugacy: Multinomial likelihood × Dirichlet prior ⇒ Dirichlet posterior (keeps inference tractable).
  • Plate Diagram Essence
    • Outer plate (repeat over documents DD) contains inner plate (repeat over word positions NdN_d).
    • Arrows: αθ<em>d\alpha\to\theta<em>d, θ</em>dz<em>d,n\theta</em>d\to z<em>{d,n}, βϕ</em>k\beta\to\phi</em>k, z<em>d,n,ϕw</em>d,nz<em>{d,n},\phi\to w</em>{d,n}.
  • Simplex Geometry
    • Any KK-dimensional probability vector lives on a (K1)(K-1)-simplex ⇒ can visualise 3-word toy case on a triangle.

LDA Generative Process (Top-down description)
  1. For each topic k1..Kk\in{1..K}
    • Draw word distribution ϕkDirichlet(η)\phi_k \sim \text{Dirichlet}(\eta).
  2. For each document d1..Dd\in{1..D}
    a. Draw topic mixture θ<em>dDirichlet(α)\theta<em>d \sim \text{Dirichlet}(\alpha). b. For each word position n1..N</em>dn\in{1..N</em>d}
    i. Draw topic z<em>d,nMultinomial(θ</em>d)z<em>{d,n} \sim \text{Multinomial}(\theta</em>d).
    ii. Draw word w<em>d,nMultinomial(ϕ</em>zd,n)w<em>{d,n} \sim \text{Multinomial}(\phi</em>{z_{d,n}}).
  • 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 p(θ,ϕ,zw,α,η)p(\theta,\phi,z\mid w,\alpha,\eta) but exact marginalisation
<em>θ</em>ϕzp(θ,ϕ,z,wα,η)\int<em>\theta \int</em>\phi \sum_{z} p(\theta,\phi,z,w\mid\alpha,\eta)
is intractable for realistic corpora.


Approximate Inference Techniques
1. Gibbs Sampling (collapsed)
  • Integrate out θ\theta and ϕ\phi analytically; iterate over tokens updating zd,nz_{d,n} conditioned on other assignments.
  • Conditional update formula:
    p(z<em>d,n=kz</em>,w,α,η)n<em>kw</em>d,n,n+ηn<em>k,n+Vη×n</em>dk,n+αn<em>d,n+Kαp(z<em>{d,n}=k \mid z</em>{-}, w, \alpha, \eta) \propto \frac{n<em>{k}^{w</em>{d,n},-n}+\eta}{n<em>{k}^{\cdot,-n}+V\eta}\times\frac{n</em>{d}^{k,-n}+\alpha}{n<em>{d}^{\cdot,-n}+K\alpha}n</em>kw,nn</em>{k}^{w,-n} : count of word w assigned to topic k (excluding current position).
    ndk,nn_{d}^{k,-n} : count of tokens in doc d already assigned to k.
  • Iterate until Markov chain mixes; sample draws approximate posterior.
  • Parameter estimates after convergence:
    ϕ^<em>k,v=n</em>kv+η<em>vn</em>kv+Vη,θ^<em>d,k=n</em>dk+α<em>kn</em>dk+Kα\hat{\phi}<em>{k,v}=\frac{n</em>{k}^{v}+\eta}{\sum<em>{v'} n</em>{k}^{v'}+V\eta},\qquad \hat{\theta}<em>{d,k}=\frac{n</em>{d}^{k}+\alpha}{\sum<em>{k'} n</em>{d}^{k'}+K\alpha}
2. Variational Inference (mentioned, not detailed)
  • Replaces sampling with optimisation of a tractable surrogate distribution.

Practical Questions LDA Helps Answer
  1. What mixture of topics does a given document contain?
  2. Given one document, find other documents with similar topic vectors.
  3. Track topic prevalence/intensity across time (diachronic corpora).
  4. 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 KK (can use CV, non-parametric methods, or business knowledge).
  • Hyperparameter tuning for α,η\alpha,\eta influences sparsity: small α\alpha ⇒ each doc uses few topics; small η\eta ⇒ 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: p(θα)p(\theta \mid \alpha).
  • Joint model: p(w,z,θ,ϕα,η)=p(ϕη)p(θα)p(zθ)p(wz,ϕ)p(w, z, \theta, \phi \mid \alpha, \eta)=p(\phi\mid\eta)p(\theta\mid\alpha)\prod p(z\mid\theta)p(w\mid z,\phi).
  • Collapsed Gibbs conditional (core update):
    p(z<em>i=kz</em>i,w)n<em>kw</em>i,i+ηn<em>k,i+Vηn</em>d<em>ik,i+αn</em>di,i+Kαp(z<em>i=k\mid z</em>{-i},w) \propto \frac{n<em>{k}^{w</em>i,-i}+\eta}{n<em>{k}^{\cdot,-i}+V\eta}\frac{n</em>{d<em>i}^{k,-i}+\alpha}{n</em>{d_i}^{\cdot,-i}+K\alpha}.
  • Parameter expectations after sampling: see ϕ^\hat{\phi} and θ^\hat{\theta} 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.