Stats 102C - Lecture 22
Overview
Miles Chen connects Gibbs sampling to unsupervised document clustering, modeling 19 passages from Green Eggs and Ham and Go, Dog. Go! with two clusters and a 59-word vocabulary. The method combines a beta prior for cluster proportions with Dirichlet-multinomial word models, then updates each passage's cluster assignment in turn; interactive demos show how different initializations can produce different results after one sweep.
Key takeaways
- Bag-of-words clustering can group documents by word frequencies, but it loses word order and treats texts with identical counts as equivalent even when their meanings differ.
- The beta distribution models a two-category proportion, while the Dirichlet generalizes it to a vocabulary-sized probability vector; a uniform Dirichlet prior adds one pseudo-count per word.
- In the 19-document example, Gibbs sampling reassigns one passage at a time using both cluster-size priors and Dirichlet-smoothed word predictive probabilities.
- Removing a document before scoring its possible clusters prevents its own word counts from directly influencing the cluster profiles used to classify it.
- A single Gibbs sweep may recover the intended groups for one initialization but not another, so clustering quality depends on initialization and repeated updates.
Chapters
- Miles Chen introduces animations he built for the course and previews a lesson on document clustering.
- The demonstrations are hosted at mileschen.github.io and include random number generators and a substitution-cipher decoder.
- The linear congruential generator uses seed 1, multiplier 5, increment 1, and modulus 32.
- Each step applies the recurrence and wraps the result to its remainder modulo 32, producing a sequence from a 32-value state space.
- The animation simplifies the Mersenne Twister to 7 state values of 5 bits each; the full generator uses 624 values of 32 bits.
- The update combines adjacent words, shifts bits, applies a twisting mask with XOR, and uses an offset of 3 words.
- The final 5-bit result can take one of 32 values, illustrating the full generator's state-update logic at smaller scale.
- The decoder proposes swaps between letters in a substitution key and compares the resulting word combinations using word weights.
- Proposals with higher likelihood are accepted, while less likely swaps can be rejected, allowing the search to explore candidate keys.
- One run approaches the phrase “To be or not to be,” demonstrating how repeated Metropolis updates can recover readable text.
- Unlike supervised classification, document clustering starts with unlabeled passages and infers groups from word frequencies.
- The example uses excerpts from Green Eggs and Ham and Go, Dog. Go!, with words such as egg and ham distinguishing one book and dog and hat distinguishing the other.
- Preprocessing lowercases text, removes punctuation and numbers, drops stop words, and stems variants such as egg/eggs and jump/jumped.
- The resulting representation contains 19 documents and 59 vocabulary terms, but discards word order and therefore cannot distinguish sentences with identical word counts but opposite meanings.
- A beta distribution models the proportion of documents assigned to either of two clusters; its parameters act like pseudo-counts.
- A Dirichlet distribution generalizes the beta to proportions across multiple categories, such as yes, no, and maybe, or the 59 words in the vocabulary.
- Each cluster has its own word-probability vector, and a multinomial model generates document word counts from that vector.
- With a uniform prior of 1 per word, observing 8 counts of Sam and 3 each of egg and ham updates the corresponding pseudo-counts to 9, 4, and 4.
- The sampler maintains one unknown cluster label for each of the 19 documents, begins from an alternating assignment, and updates labels one document at a time.
- For each update, it temporarily removes the document, computes cluster-size priors, and scores each cluster using the document's words and the cluster's current counts.
- Dirichlet smoothing adds one pseudo-count for each of the 59 vocabulary terms; predictive probabilities for repeated words are multiplied, with log scores used to manage small values.
- The first document's three egg counts make cluster 2 about 99% likely in the worked example; the document is then added back to that cluster and its counts are updated.
- Chen's interactive display shows each document's stemmed words, cluster term counts, temporary removal, assignment probabilities, and restored counts.
- Some initializations recover the book-based grouping after one sweep, while another leaves documents 1, 2, and 4 grouped with documents 11 through 19 after the first sweep.
- Further Gibbs updates can correct imperfect assignments, and the demonstration also extends the clustering setup from two categories to three books.
- The lecture closes with an announcement of an optional online class meeting at 8 a.m. on Wednesday.
Transcript excerpt
0:03 All right. Happy Monday, everybody. Happy It's week eight. No, nine. Eight. What is it? Nine. It's Thanksgiving week. I feel like I'm barely holding it together, too. I don't know. Uh I'm like behind. I haven't I haven't even finished grading your exams. I'm like, "Okay, I'm going to do all everything over Thanksgiving break." I've got uh a bunch of letters of recommendation to write. I've got uh and then um and then I have to like finish writing everyone the final exams and all of that. And then but this past weekend instead of working on that stuff I was like I was vibe coding and uh because it was fun and I'm like oh this is still
0:51 like my job and educational and um so so I made a bunch of animations um and stuff and that that like uh but anyway if you say oh it helped me learn and understand then I'll feel like Okay, I'm still doing my I'm still doing my job here. It's some of it's related directly related to our lecture today on document clustering, but I'm so excited. I wanted to show you some other stuff. Oh, wait. What happened here? Why am I Why do I have Why do I have crosshairs? That's disturbing. Okay. Okay. All right. So, smileschen.github.io.
1:39 Okay. So I have uh I created a couple animations on random number generators. So this is the linear congruential generator and you can change. So this one I think we talked about this early early early on in the course and I just like this is how we used to generate random numbers and then now we have better ways to do it and um and so anyway um this will generate like with the modulus being 32, you can generate 32
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Miles Chen.