Recently, I implemented Gibbs sampling for LDA topic model on Python using numpy, taking as a reference some code from a site. In each iteration of Gibbs sampling, we remove one (current) word, sample a new topic for that word according to a posterior conditional probability distribution inferred from the LDA model, and update word-topic counts, as follows:
for m, doc in enumerate(docs): #m: doc id
for n, t in enumerate(doc): #n: id of word inside document, t: id of the word globally
# discount counts for word t with associated topic z
z = z_m_n[m][n]
n_m_z[m][z] -= 1
n_z_t[z, t] -= 1
n_z[z] -= 1
n_m[m] -= 1
# sample new topic for multinomial
p_z_left = (n_z_t[:, t] + beta) / (n_z + V * beta)
p_z_right = (n_m_z[m] + alpha) / ( n_m[m] + alpha * K)
p_z = p_z_left * p_z_right
p_z /= numpy.sum(p_z)
new_z = numpy.random.multinomial(1, p_z).argmax()
# set z as the new topic and increment counts
z_m_n[m][n] = new_z
n_m_z[m][new_z] += 1
n_z_t[new_z, t] += 1
n_z[new_z] += 1
n_m[m] += 1
In the above code, we sample a new (single) z with the multinomial scipy function.
Now, I want to implement a Joint Sentiment Topic model of this paper. Now, I would need the following structures for keeping track of the needed counts:
3D matrix containing # occurrences for a word for each topic, for each sentiment
3D matrix containing # occurrences for a topic, for each sentiment, for each document
2D matrix containing # occurrences for a topic, for each sentiment
2D matrix containing # occurrences for a sentiment for each document
And now comes the problem: in this Gibbs sampler, for each word seen in a document both a new topic and a sentiment label are now sampled from a conditional posterior (page 4 equation 5 of the paper). How could I "sample those 2 values" in Python now ?
Thanks in advance...