Some problems with predictions of Bayesian multinomial model

208 views
Skip to first unread message

Mira Fedotova

unread,
Apr 10, 2017, 8:09:44 AM4/10/17
to Stan users mailing list
Good afternoon! 
I would like to get the predictions of the model but Stan tells me that something is wrong. Please, help me to understand the problem

predictions_of_bayesian_multi_logit <- "
data {
    int N;                                         // number of individuals 
    int<lower=2> K;                        // number of alternatives
    int<lower=1> D;                        // number of individual predictors
    int<lower=1> C;                        // number of cards for train sample
    int<lower=1> C_tilde;               // number of cards for test sample
    matrix[K, D] X[N, C];                 // individual predictors for train sample
    matrix[K,D] X_tilde[N, C_tilde]; // individual predictors for test sample
    int<lower=1,upper=5> y[N, C];  // outcomes 
}
parameters {
    vector[D] beta;
}
model {
    for (d in 1:D)
        beta[d] ~ normal(0, 5);
    for (n in 1:N)
        for (c in 1:C)
        y[n, c] ~ categorical_logit(X[n, c]*beta);
}
generated quantities {
    int<lower=1,upper=5> y_tilde[N, C_tilde];
    for (n in 1:N)
    for (c in 1:C_tilde)
        y_tilde[n, c] = categorical_logit(X_tilde[n, c]*beta);
}
" 

data_for_aggregate_logit <- list(N = N, y = y_train, X = X_train, X_tilde = X_test, D = D, C = C, C_tilde = C_tilde, K = K)
fit_aggregate_logit <- stan(model_code = predictions_of_bayesian_multi_logit, data = data_for_aggregate_logit, iter = 1000, chains = 4) 

When I`m trying to fit the model, Stan tells me the following:


SYNTAX ERROR, MESSAGE(S) FROM PARSER:

No matches for: 

  categorical_logit(vector)

Function categorical_logit not found.

ERROR at line 26

 24:        for (n in 1:N)
 25:        for (c in 1:C_tilde)
 26:            y_tilde[n, c] = categorical_logit(X_tilde[n, c]*beta);
                                                                     ^
 27:    }


Do you know how the given problem can be solved? Thanks in advance!

Daniel Lee

unread,
Apr 10, 2017, 9:45:38 AM4/10/17
to stan-...@googlegroups.com
Hi Mira,

Use categorical_logit_rng() in generated quantities. rng stands for random number generator. 


Daniel
--
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.

Mira Fedotova

unread,
Apr 10, 2017, 1:15:14 PM4/10/17
to stan-...@googlegroups.com
Hi Daniel! Thanks for your help! 
I`ve changed the model as you said. Now the last row is the following:

y_tilde[n, c] = categorical_logit_rng(X_tilde[n, c]*beta) 

Unfortunately Stan again gives me the error:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:

No matches for: 

  categorical_logit_rng(vector)

Function categorical_logit_rng not found.

ERROR at line 26

 24:        for (n in 1:N)
 25:        for (c in 1:C_tilde)
 26:            y_tilde[n, c] = categorical_logit_rng(X_tilde[n, c]*beta);
                                                                         ^
 27:    }

Please, let me know if you know how to tackle this problem

Kind regards, Mira



To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/nGoDzNtBBmM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+unsubscribe@googlegroups.com.

Stephen Martin

unread,
Apr 10, 2017, 1:30:24 PM4/10/17
to Stan users mailing list
According to the manual, categorical_logit_rng doesn't exist, BUT categorical(softmax(logit-scaled predictions)) = categorical_logit; so you could manually softmax the predicted values and use that in categorical_rng
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.

--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/nGoDzNtBBmM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.

Mira Fedotova

unread,
Apr 10, 2017, 1:54:44 PM4/10/17
to stan-...@googlegroups.com
I`ve read the manual and found the answer.
It should be done this way:
y_tilde[n, c] = categorical_rng(softmax(X_tilde[n, c]*beta))


Thank you so much for helping me! :)

Mira

Mira Fedotova

unread,
Apr 10, 2017, 1:57:15 PM4/10/17
to stan-...@googlegroups.com
Thank you very much! :)

To unsubscribe from this group and all its topics, send an email to stan-users+unsubscribe@googlegroups.com.

Bob Carpenter

unread,
Apr 11, 2017, 12:51:33 PM4/11/17
to stan-...@googlegroups.com
We really need to add categorical_logit_rng to the language.
I created an issue and this would be a super easy place to
write one's first Stan math library function (no derivatives!):

https://github.com/stan-dev/math/issues/517

The explicit softmax is required for now.

- Bob
> To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages