Latent Dirichlet Allocation (Blei, Ng and Jordan, 2003) explains each document as a mixture of topics and each topic as a distribution over words. Collapsed Gibbs sampling (Griffiths and Steyvers, 2004) resamples the topic of one word token at a time. For that token, with its own assignment already removed from the counts, the conditional is
p(z = k | rest) proportional to (n_dk + alpha) * (n_kw + beta) / (n_k + V beta).
Here n_dk counts the other tokens in the document assigned to topic k, n_kw counts occurrences of word w in topic k, n_k is the total count in topic k, and V is the vocabulary size.
Implement topic_conditional(doc_counts, topic_word, topic_totals, alpha, beta, word).
doc_counts is (K,). topic_word is (K, V). topic_totals is (K,). word is an index in 0..V-1.K.ValueError.alpha > 0 and beta > 0. Otherwise raise ValueError.ValueError.word must be in range. Otherwise raise ValueError.Multiply the three factors elementwise over topics, then divide by their sum. The denominator n_k + V beta is a vector over k, not a scalar.
No results yet
Run your code to check it against the examples, or submit to run every test.