categorical_logit that shouldn't compile?

59 views
Skip to first unread message

Allen Riddell

unread,
Jul 28, 2014, 3:38:47 PM7/28/14
to stan...@googlegroups.com
Hi stan-dev,

I feel like this model definitely shouldn't compile -- but it does. I've got
a statement:

target ~ categorical_logit(x_beta[i]); // note lack of [i] on LHS

where

int<lower=1, upper=K> target[N];
vector[K] x_beta[N];

Is this a bug or am I missing something?

Here's the full model

model_code = """
data {
int<lower=1> N; // number of texts
int<lower=1> K; // number of target categories
int<lower=1> J; // number of groups
int<lower=1, upper=K> target[N];
int<lower=1, upper=J> group[N];
}
parameters {
vector[K] alpha[J]; // group intercepts
}
model {
vector[K] x_beta[N]; // pre-calculate x_beta for performance speedup

for (j in 1:J)
alpha[j] ~ normal(0, 5); // NOTE: each alpha[j] is K-dimensional

for (i in 1:N)
x_beta[i] <- alpha[group[i]];

for (i in 1:N)
target ~ categorical_logit(x_beta[i]); // NOTE: missing [i] on LHS
}
"""

Thanks,

-a

Bob Carpenter

unread,
Jul 28, 2014, 4:04:50 PM7/28/14
to stan...@googlegroups.com

On Jul 28, 2014, at 3:38 PM, Allen Riddell <a...@ariddell.org> wrote:

> Hi stan-dev,
>
> I feel like this model definitely shouldn't compile -- but it does. I've got
> a statement:
>
> target ~ categorical_logit(x_beta[i]); // note lack of [i] on LHS
>
> where
>
> int<lower=1, upper=K> target[N];
> vector[K] x_beta[N];
>
> Is this a bug or am I missing something?

The declaration of categorical_logit_log is

real categorical_logit_log(ints, vector);

where ints can be either an int or an array of int.

That is, it's vectorized. It just adds the log prob for each
of the entries. And is MUCH more efficient because the
equivalent log softmax only needs to be computed once.

There's a description of how vectorization work and the notation
for it at the start of the part of the manual on distributions.
But nobody ever finds it, because it's in the middle of what
looks like a plain old list of functions.

- Bob

Andrew Gelman

unread,
Jul 28, 2014, 4:07:18 PM7/28/14
to stan...@googlegroups.com
Obviously we have to just put the important stuff in the manual in all-caps, bold, and in red. Maybe a larger font too, just to make sure. And, if necessary, we could underline and have a special icon on the margin that means, “Hey—this is really important.” Flashing, too, if that can be done in pdf. And, once the technology is available, something in Smellorama.

Allen Riddell

unread,
Jul 28, 2014, 4:25:04 PM7/28/14
to stan...@googlegroups.com
Hi Bob,

I see your point. Although I feel like I've got something that's at least worth
thinking about: I'm worried about someone forgetting the [i] on the LHS since

target ~ categorical_logit(x_beta[i]); // note lack of [i] on LHS

and

target[i] ~ categorical_logit(x_beta[i]);

both compile without error but mean something completely different -- the first
one is an error.

It seems like the error could be detected if the upper limit on target is lower
than the dimension of the parameter vector.

Best,

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

Andrew Gelman

unread,
Jul 28, 2014, 4:27:00 PM7/28/14
to stan...@googlegroups.com, glae...@uke.de
Oddly enough, someone just sent in a typo in the manual that has a similar form:


On Jul 28, 2014, at 9:51 PM, Jan Gläscher <glae...@uke.de> wrote:

Dear Prof. Gelman,

Im an in the middle of reading the Stan Reference Manual (version 2.4.0). First of all, thank you for your inspiring work on Bayesian modeling and developing such a great piece of software.

I noticed what I think is a typo in the Reference Manual (top of page 24 "Regression Models'). There it lists:

for (n in 1:N)
y ~ normal(x[n] * beta, sigma);

 but if I am not mistaken I think this should read:

for (n in 1:N)
y[n] ~ normal(x[n] * beta, sigma);

Apologies for taking up your time with such a small issue, but for newcomers to Stan this might be a bit confusing.

Thanks again and all the best,
Jan

P.S. to Jan:  Thanks for thanking me but please remember that I’m only one of the people on the large Stan development team!

Bob Carpenter

unread,
Jul 28, 2014, 4:48:59 PM7/28/14
to stan...@googlegroups.com
[changed subject]

I'll fix it for the next manual:

https://github.com/stan-dev/stan/issues/786#issuecomment-50397891

- Bob

P.S. We need to work on keeping our e-mail threads more coherent!

Bob Carpenter

unread,
Jul 28, 2014, 5:16:55 PM7/28/14
to stan...@googlegroups.com

On Jul 28, 2014, at 4:25 PM, Allen Riddell <a...@ariddell.org> wrote:

> Hi Bob,
>
> I see your point. Although I feel like I've got something that's at least worth
> thinking about: I'm worried about someone forgetting the [i] on the LHS since
>
> target ~ categorical_logit(x_beta[i]); // note lack of [i] on LHS
>
> and
>
> target[i] ~ categorical_logit(x_beta[i]);
>
> both compile without error but mean something completely different -- the first
> one is an error.
>
> It seems like the error could be detected if the upper limit on target is lower
> than the dimension of the parameter vector.

I think the potential coding error is that there might be an outer
loop over i so you're inadvertently repeating the probability.
(And now I see why Andrew responded to this thread.)

If so, I don't understand how we could detect this programming error,
because it's a semantic error not a syntactic one.

- Bob
Reply all
Reply to author
Forward
0 new messages