-
-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathlda.stan
More file actions
30 lines (30 loc) · 843 Bytes
/
lda.stan
File metadata and controls
30 lines (30 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
data {
int<lower=2> K; // num topics
int<lower=2> V; // num words
int<lower=1> M; // num docs
int<lower=0> corpus[M,V]; // word freq matrix, doc x word
vector<lower=0>[K] alpha; // topic prior
vector<lower=0>[V] beta; // word prior
}
parameters {
simplex[K] theta[M]; // topic dist for doc m
simplex[V] phi[K]; // word dist for topic k
}
model {
for (m in 1:M)
theta[m] ~ dirichlet(alpha); // prior
for (k in 1:K)
phi[k] ~ dirichlet(beta); // prior
for (i in 1:M) {
for (j in 1:V) {
int count = corpus[i,j];
real gamma[K];
if (count > 0) {
for (k in 1:K) {
gamma[k] <- (log(theta[i,k]) + log(phi[k,j]))*count;
}
increment_log_prob(log_sum_exp(gamma)); // likelihood
}
}
}
}