I am trying to use Stan to obtain the * high-dimensional* posterior distribution of prior x likelihood = (Poisson x Normal distribution) using marginalization. The result should be the posterior distributions of each component of a vector with size =

`nrow`

below (`nrow`

is very large, like `1000`

). But I keep encountering this error message:SAMPLING FOR MODEL ‘stanmodel2’ NOW (CHAIN 1).

Unrecoverable error evaluating the log probability at the initial value.

Exception: []: accessing element out of range. index 0 out of range; expecting index to be between 1 and 875; index position = 1log_weights (in ‘model1cc1651dcead_stanmodel2’ at line 35)

My Stan model is here:

```
data {
int<lower=1> ncol;
int<lower=1> nrow;
vector[ncol] yH;
vector[nrow] x;
#int x[nrow];
matrix[nrow, ncol] A;
vector[nrow] sigma_x;
vector[ncol] sigma_y;
vector[nrow] epsilon;
int big_integer;
}
parameters {
real log_lambda;
}
model {
for (j in 0:ncol) {
vector[big_integer] log_weights;
vector[big_integer] log_dens;
for (i in 0:big_integer) {
log_weights[i] = poisson_log_lpmf(i | log_lambda);
log_dens[i] = normal_lpdf(x[i] | i, sigma_x[i]);
}
target += log_sum_exp(log_weights + log_dens);
}
```

**R code** to feed in the data and call `sampling()`

:

```
for(i in 1:15){
nrow = nrow(rte_m[[i]]);
ncol = ncol(rte_m[[i]]);
A <- as.matrix(rte_m[[i]]);
sigma_x <- as.vector(sample.int(10, nrow(kf_vect[[i]]), replace=TRUE))
sigma_y <- as.vector(eps_vect[[i]])
# sigma_x <- diag(1, nrow)
# sigma_y <- diag(1, nrow)
yH <- as.vector(dh_vect[[i]]$X);
yT <- as.array(rpois(ncol,round(yH + as.vector(eps_vect[[i]]))));
epsilon <- sample.int(15, nrow(kf_vect[[i]]), replace=TRUE);
x <- rnorm(nrow, as.vector(as.matrix(rte_m[[i]])%*%yT) + epsilon, sigma_x);
iterations = 100;
#input it into our Stan model file "stamodeling.stan"
stanmodel1 <- stan_model(file = "poisson.stan",
model_name = "stanmodel2");
#NUTS (No U-Turn) sampler to generate posterior distribution
stanfit <- sampling(stanmodel1, cores = parallel::detectCores(), data = list(ncol = ncol,nrow = nrow,
yH = yH,
x=x, epsilon = epsilon,
A = A, sigma_x = sigma_x, sigma_y = sigma_y, big_integer = nrow) ,iter=iterations, chains = 3, control = list(max_treedepth=13));
```

**My question:** Can anyone please give me some help on what went wrong with my Stan model? I checked all the dimensions of the `poisson_log_lpmf(i | log_lambda)`

and `normal_lpdf(x[i] | i, sigma_x[i])`

to make sure they are correct (and they are!), so I could not see why MCMC keeps failing at the initial state. The matrix `A`

is very sparse one (like 90% of the entries are `0`

), but that should not affect the initial state, isn’t it?