"variable does not exist" error

938 views
Skip to first unread message

Benjamin Feifke

unread,
Jan 24, 2017, 10:19:16 AM1/24/17
to Stan users mailing list
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
simple_topicmodel_seeded.R

Michael Betancourt

unread,
Jan 24, 2017, 10:39:40 AM1/24/17
to Stan users mailing list
You need braces — only the first line is currently in scope of the loops,
the last two are being evaluated after the loops.

Benjamin Feifke

unread,
Jan 24, 2017, 10:43:53 AM1/24/17
to Stan users mailing list
Holy crap! I feel silly, thank you; fixed it right up!

Bob Carpenter

unread,
Jan 24, 2017, 4:40:12 PM1/24/17
to stan-...@googlegroups.com
This is another thing to add to our lint mode when we get around
to doing that --- scope that doesn't match indentation. I added
a note at the end:

https://github.com/stan-dev/stan/wiki/Stan-Parser-Pedantic-Mode

An editor like emacs with the Stan mode makes the scope more apparent
with auto-indenting.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages