13.1 Math for Topic Modeling

DATA 441/641 · Week 13 · PDF · Slides

Documents as mixtures, and the linear algebra underneath.

These notes walk through the lecture slides. The original deck is unchanged; use (slides) on the course hub if you want that PDF.

1. Topics are a matrix problem

Topic modeling finds themes in a large collection without document labels. A news archive is not 10,000 unrelated files. It is a mix of elections, sports, markets, and weather, and each article is a mix of those themes.

Topics in a corpus

The starting object is a document-term matrix (DTM): rows are documents, columns are terms, cells are counts or TF–IDF. Everything this module does is an operation on that matrix, or on a close cousin of it.

Linear algebra is not decoration here. SVD, NMF, PCA, and the inference inside LDA are all matrix factorizations or eigenvalue problems.


2. Why the linear algebra shows up

Idea Role in topic modeling
Document-term matrix The data. Rows = documents, columns = terms
Matrix factorization (SVD, NMF) Split the DTM into "terms × topics" and "topics × documents"
Eigenvalues / eigenvectors Directions of variation; importance of those directions
PCA A ranked set of uncorrelated axes; a way to shrink the DTM
LDA A probabilistic model; linear algebra still runs the inference

You do not need a full linear-algebra course. You need vectors, matrix multiply, inverse and transpose, and the eigenvalue equation.


3. Vectors and matrices

A vector x∈ℝn is a list of n numbers. Three operations you will use:

  • Scalar: αx=[αx1,…,αxn]
  • Dot product: x·y=x⊤y=∑ixiyi
  • Euclidean norm: ‖x‖2=∑ixi2

A matrix A∈ℝm×n is the DTM shape. Pointwise +, −, and scalar multiply are entrywise. Matrix–vector multiply Ax is a new vector whose ith entry is the dot product of row i with x. Matrix–matrix multiply AB is defined only when the inner dimensions match. AB is not BA.

The identity In has ones on the diagonal. The inverse satisfies AA−1=I when it exists. The transpose A⊤ swaps rows and columns. Symmetric matrices (A=A⊤) are the ones whose eigenvectors we trust first.


4. Eigenvalues, in one picture

For a square matrix A,

Av=λv.

v is an eigenvector: a direction that A does not rotate. λ is the eigenvalue: how much that direction is stretched. λ>1 stretches, 0<λ<1 shrinks, λ<0 flips.

Geometry of an eigenpair

In a term–term or document–document covariance, a large λ is a direction where the corpus actually varies. Those directions are the candidates for "topics" or for the axes PCA will keep.


5. PCA as a recipe

PCA turns correlated features into a new orthogonal basis, ordered by variance.

  1. Center. Subtract the column mean: Xcentered=X−X¯.
  2. Covariance. C=1n−1Xcentered⊤Xcentered.
  3. Eigendecompose. Cv=λv. Large λ = a principal component that explains more variance.
  4. Keep the top k. Columns of Vk are those eigenvectors.
  5. Project. Xreduced=XcenteredVk.

Each document is now a vector in k dimensions. You can cluster those vectors (k-means, DBSCAN) or pass them to LDA or NMF. Words and documents that load on the same component are a first draft of a topic.

PCA is a tool, not the whole topic model. Note 13.2 is SVD / LSA, which is the factorization people actually run on a DTM. Week 14 is LDA and NMF: same matrix picture, different constraints (probabilities, non-negativity). Learn the multiply and the eigen-equation well enough to read those notes. You will not invert a 20,000-column matrix by hand.


6. Practice

  1. A DTM is 5,000 documents by 20,000 terms. What are m and n if we write A∈ℝm×n as documents × terms?

  2. Why is AB a different size from BA in the 3×2 times 2×3 example?

  3. In PCA, what does a small eigenvalue tell you about that direction, and why might you drop it before clustering?

If you can compute Av and compare it to λv on a 2×2 example, you are ready for SVD.