Hello!
Thank you, as always, stan is amazing.
I have this model code:
data {
int<lower=2> K; // num topics
int<lower=2> V; // num words (vocab length)
int<lower=1> M; // num docs
int<lower=1> N; // total word instances
int<lower=1,upper=V> word_vocab[N]; // vocab ID for word n (vocab index)
int<lower=1,upper=M> word_doc[N]; // doc ID for word n
vector<lower=0>[K] topic_prior; // topic prior
vector<lower=0>[V] word_prior_reg; // regular word prior
simplex[V] phi_s[K]; // seed word distributions, topic by words
![]()
vector<lower=0>[K] pi; // the bernoulli selection priors
}
parameters {
simplex[K] theta; // corpus topic prevalence
simplex[V] phi_r[K]; // word dist for topic k reg
}
model {
// instantiate the local variable gamma
real gamma[M,K]; //doc by topic matrix
// instantiate a local variable for doing the logsumexp([seed,reg]) stuff
real guy[2];
// instantiate the local variable for the topic prevalence
theta ~ dirichlet(topic_prior);
// for each topic, a distribution over words for seed and regular
for (k in 1:K)
phi_r[k] ~ dirichlet(word_prior_reg);
// for each document
for (m in 1:M)
for (k in 1:K)
gamma[m,k] = categorical_lpmf(k|theta);
// for each word
for (n in 1:N)
for (k in 1:K)
guy[0] = bernoulli_lpmf(1|pi[k]) + categorical_lpmf(word_vocab[n]|phi_r[k]);
guy[1] = bernoulli_lpmf(0|pi[k]) + categorical_lpmf(word_vocab[n]|phi_s[k]);
gamma[word_doc[n],k] = gamma[word_doc[n],k] + log_sum_exp(guy); // update the likelihood
for (m in 1:M)
target+=log_sum_exp(gamma[m]);
}
The error arises in the red section. In the lines 'guy[0]...' and 'guy[1]...', I get an error "variable 'k' does not exist". This is weird, because 'k' is just the looping index. What's weirder is that, when I comment out those to lines, (i.e. just leave the line 'gamma[...' in there, I don't get the error, even though it's also using 'k'. I attached the whole script I'm using, which includes the generation of fake data.
Any help is much appreciated!
Ben