(corrected )

I am using a model very similar to Noisy Categorical Measurement Model in the Stan reference 14.4.

I have a parameter vector with variant range (theta), so I transformed it to alpha to restrict the range with mu > 0. Mu is the poisson parameter. But when sampling, I got the error:

Error in sampler$call_sampler(c(args, dotlist)) :

Exception: poisson_lpmf: Rate parameter is -1.05972, but must be >= 0! (in ‘modele8162f3d0378_categorical’ at line 20)

How can this happen??

Here is my code:

```
data {
int N;
int K;
int Y[N];
}
parameters {
vector[K] theta;
simplex[K] pi;
vector<lower=0>[K] alpha;
}
transformed parameters {
vector<lower=0>[K] mu;
vector[K] log_q_z[N];
for(n in 1:N){
log_q_z[n] = log(pi);
for(k in 1:K){
mu[k] = 2^k-theta[k];
log_q_z[n,k] = log_q_z[n,k] + poisson_lpmf(Y[n] | mu[k]);
}
}
}
model {
pi ~ dirichlet(alpha);
theta ~ cauchy(0,3);
for(n in 1:N)
target += log_sum_exp(log_q_z[n]);
}
```

Then R code:

```
model_categorical <- stan_model("~/Project/montagna_mar_collab/Shuonan/categorical.stan")
Y <- data_9_Y;
data_temp <- list(N = length(Y), K = 4, Y = Y)
fit_9_Y <- sampling(model_categorical, data = data_temp,
iter = 1000, chains = 4, cores = 4, control = list(adapt_delta = 0.9))
```

I read the reparameterization chapter in the stan reference (20.4) but I am not familiar with Jacobian matrix so would really appreciate if theres a alternative approach :)

Thank you !