Hi,

Recently I am writing an Stan code which involves defining the log likelihood of some data which would be used later in the ‘model’ trunk.

First I tried:

```
transformed parameters{
vector[N] log_lik;
for (i in 1:N){
log_lik[i] += normal_lpdf(y|mu, sigma);
}
}
```

But the model returns error like ‘Chain 1: Rejecting initial value:

Chain 1: Log probability evaluates to log(0), i.e. negative infinity.

Chain 1: Stan can’t start sampling from this initial value.’

But when I modified the code as:

```
transformed parameters{
vector[N] log_lik = rep_vector(0,N);
for (i in 1:N){
log_lik[i] += normal_lpdf(y|mu, sigma);
}
}
```

The model could run without any error. May I ask that why this would happen?

Thx!

In the first block, you didn’t initialise `log_lik`

to any value, and so Stan initialises it to `NaN`

by default. Then when you add to it (`+=`

) it doesn’t return a valid number.

In the second block you initialise `log_lik`

to zeros, so the addition returns valid numbers.

The first block would have worked if you had used assignment (i.e., `=`

) instead of addition:

```
transformed parameters{
vector[N] log_lik;
for (i in 1:N){
log_lik[i] = normal_lpdf(y | mu, sigma);
}
}
```

Also, be aware that this loop you’re using is just assigning the exact same value to each element of `log_lik`

, in case that’s not what you want

4 Likes

Thanks so much!

I used to think add a number to NaN by += would return that number but thanks so much for the clarification!

1 Like