log likelihood in generated quantities

356 views
Skip to first unread message

Chris

unread,
Feb 29, 2016, 10:38:51 AM2/29/16
to Stan users mailing list
Hi,

I'm still learning Stan and this is my Negative Binomial GLM model.
In the generated quantities part I want to compute the log likelihood.
However, I get this error:

SAMPLING FOR MODEL 'bbdb253aba6deb87bc89c325cfa3735c' NOW (CHAIN 1).
[1] "Error : Exception thrown at line 21: stan::math::neg_binomial_2_log_rng: Random number that came from gamma distribution is 3.46472e+11, but must be less than 1.07374e+09"
error occurred during calling the sampler; sampling not done

I think I need to constrain the samples somehow, but how?


data {
int<lower=0> N; // observations
int<lower=0> K; // number of columns
matrix[N,K] X; // predictor matrix
int<lower=0> y[N]; // response
}
parameters {
vector[K] betas;
real<lower=0.00001, upper=50> phi;
}
model {
  phi ~ cauchy(0,2.5);
  for(k in 1:K)
    betas[k] ~ normal(0,100);
  y ~ neg_binomial_2_log(X*betas, phi);
}
generated quantities {
  int log_lik[N]; // log likelihood
  for (n in 1:N)   // could be vectorized maybe
    log_lik[n] <- neg_binomial_2_log_rng(X[n]*betas, phi);
}


Thanks
Chris

Bob Carpenter

unread,
Feb 29, 2016, 11:56:58 AM2/29/16
to stan-...@googlegroups.com
You're generating random variates, not computing the log
likelihood. If you wnat the log likelihood, it's just

real log_lik;
log_lik <- neg_binomial_2_log(y, X * betas, phi);

And you probably want to vectorize that betas statement

betas ~ normal(0, 100);

and put the scale on the scale you expect --- do you really
expect betas in (-200, 200)?

You should only need a lower=0 constraint on phi.

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

Chris

unread,
Feb 29, 2016, 12:43:12 PM2/29/16
to Stan users mailing list
if I change the model to

stan_code <- "
data {
int<lower=0> N; // observations
int<lower=0> K; // number of columns
matrix[N,K] X; // predictor matrix
int<lower=0> y[N]; // response
}
parameters {
vector[K] betas;
real<lower=0.00001, upper=50> phi;
}
model {
  phi ~ cauchy(0,2.5);
  for(k in 1:K)
    betas[k] ~ normal(0,100);
  y ~ neg_binomial_2_log(X*betas, phi);
}
generated quantities {
  real log_lik[N];
  for (n in 1:N)
    log_lik[n] <- neg_binomial_2_log(y[n], X[n]*betas, phi);
}
"

I get this error:

SAMPLING FOR MODEL 'd03cab7d46de4a92cfea096c7f1696cc' NOW (CHAIN 1).
[1] "Error : Exception thrown at line 21: stan::math::neg_binomial_2_log: Location parameter is -58.3978, but must be > 0!"
error occurred during calling the sampler; sampling not done

thanks

Bob Carpenter

unread,
Feb 29, 2016, 1:13:59 PM2/29/16
to stan-...@googlegroups.com
Our notation's super confusing (even to me; we're changing it,
by the way).

This:

> log_lik[n] <- neg_binomial_2_log(y[n], X[n]*betas, phi);


needs to be neg_binomial_2_log_log(...).

The first log is to say that the location parameter X[n] * betas
is on the log scale and the second to indicate that this is
a function.

NOTE about the future of Stan:

We're going to change this so that in the future it'll be possible
to write neg_binomial_2_log_lpdf(...) --- so the _lpdf indicates
it's a log pdf, whereas the _log will indicate the scale of the parameters.

- Bob

> On Feb 29, 2016, at 12:43 PM, Chris <wel...@gmail.com> wrote:
>
> if I change the model to
>
> stan_code <- "
> data {
> int<lower=0> N; // observations
> int<lower=0> K; // number of columns
> matrix[N,K] X; // predictor matrix
> int<lower=0> y[N]; // response
> }
> parameters {
> vector[K] betas;
> real<lower=0.00001, upper=50> phi;
> }
> model {
> phi ~ cauchy(0,2.5);
> for(k in 1:K)
> betas[k] ~ normal(0,100);
> y ~ neg_binomial_2_log(X*betas, phi);
> }
> generated quantities {
> real log_lik[N];
> for (n in 1:N)
> log_lik[n] <- neg_binomial_2_log_(y[n], X[n]*betas, phi);

Chris

unread,
Feb 29, 2016, 1:51:17 PM2/29/16
to Stan users mailing list
that works, thank you
Reply all
Reply to author
Forward
0 new messages