Poisson Distribution always return erros

my code in pystan

vv=range(0,10)
my_code = """
data {
    int<lower=0> J; // number of datas
    vector[J] value; // observed values

}
parameters {
    //Arrival rate coefs
    real<lower=1,upper=10> th11;


    //jump dist params
    
    real<lower=1> sigma;
}


model {

    int ku[J];
    for (i in 1:J)
        ku[i] ~ poisson(th11);

    
    for (i in 1:J)
        value[i] ~ normal(ku[i], sigma);
}
"""

schools_dat = {'J': 10,
               'value':vv}

sm = pystan.StanModel(model_code=my_code)
fit = sm.sampling(data=schools_dat, iter=1000, chains=1)

this code always returns same error:

Rejecting initial value:
Error evaluating the log probability at the initial value.
Exception: poisson_lpmf: Random variable is -2147483648, but must be >= 0! (in ā€˜unknown file nameā€™ at line 22)

Initialization between (-2, 2) failed after 100 attempts.

how to avoid this?

Hi! :)

This is not allowed in Stan. The tilde ~ doesnā€™t ā€œsampleā€ from a distribution, but rather adds the data/parameter on the left hand side to the target function with the log distribution function thats on the right hand side (with given parameters). Basically, it is target += lpmf(ku[i], th11), and since ku doesnā€™t have a value, it throws an error.

Unfortunately, this is not the only problem with the model (at least for Stan): You can not have integer parameters in Stan (or any HMC or gradient based MCMC sample).

Youā€™d have to marginalize over all the discreet parameters in your model, which can be a bit painful to do hereā€¦ Sorry, for not being very helpfulā€¦ :/

1 Like