I am testing a variant of LDA (Latent Dirichlet Allocation) algorithm on some text data. Given some documents $d$ where each document is expressed as a distribution over words $w$, the algorithm effectively expresses the same documents over topics $z$. I am dropping the hyperparameters like the number of topics and the priors.
My dataset has 185311 documents with 26464 words in total. I picked the number of topics 10.
The algorithm computes the following:
- $p(w|z)$ - matrix of size (10 x 26464)
- $p(z)$ - matrix of size (1 x 10)
- $p(z|d)$ - matrix of size (185311 x 10)
For my experiments I need to compute $p(z|w)$. My very first idea was simply to use the columns of the $p(w|z)$ by normalizing to sum to one. After little thought I grew suspicious and recalled the Bayes theorem:
$p(z|w) = \frac{p(w|z)p(z)} {\sum_{i=1}^{K}p(w|z_i)p(z_i)}$$p(z|w) = \frac{p(w|z)p(z)} {\sum_{i=1}^{K}p(w|z_i)p(z_i)} = \frac{p(w|z)p(z)} {p(w)}$
I understand that the $p(z|w)$ must be of size (26464 x 10) or (10 x 26464). Looking at the equation, I need $p(w|z)$ and $p(z)$ which I have. It looks like $p(z)$ serves as a kind of weighting while the whole thing in denominator makes sure that $p(z|w)$ is a probability distribution.
What I would do is to multiply elementwise the matrix $p(w|z)$ with a transposed matrix $p(z)$ and replicated along the columns to match the size of the first matrix. Then finally normalize the resulting matrix such that the sum along rows equals 1. Is this correct? Am I making a mistake somewhere?
I could easily write the expression but it is not clear for me how to practically compute it using the data I have. I am really willing to understand it; any pointers to some tutorials are welcome.