# Order of statments in model block

Hi all,

I’ve just started using Stan some days ago.
Reading the Stan reference 2.17.1, I see that one need to take care of order of statements, not like in BUGS.

I’ve a question of the order of statements in model block.
On the page 144, the model block of 1PL (Rasch) Model is written as

model {
alpha ~ normal(0, 1); // informative true prior
beta ~ normal(0, 1); // informative true prior
delta ~ normal(0.75, 1); // informative true prior
for (n in 1:N)
y[n] ~ bernoulli_logit(alpha[jj[n]] - beta[kk[n]] + delta);
}

Since y[n] need to use the values of alpha, beta, delta, we need to declare them first.

But on the page 145, for the Multilevel 2PL Model, I see that while beta and gamma need the values of sigma_beta and sigma_gamma, beta and gamma are “written” before sigma_beta and sigma_gamma:

model {
alpha ~ normal(0, 1);
beta ~ normal(0, sigma_beta);
gamma ~ lognormal(0, sigma_gamma);
mu_beta ~ cauchy(0, 5);
sigma_beta ~ cauchy(0, 5);
sigma_gamma ~ cauchy(0, 5);
for (n in 1:N)
y[n] ~ bernoulli_logit(gamma[kk[n]]* (alpha[jj[n]] - (beta[kk[n]] + mu_beta)));
}

Could you please explain to me why, and can we assign values for sigma_beta and sigma_gamma before feeding them to alpha and beta, e.g:

model {
alpha ~ normal(0, 1);

mu_beta ~ cauchy(0, 5);
sigma_beta ~ cauchy(0, 5);
sigma_gamma ~ cauchy(0, 5);

beta ~ normal(0, sigma_beta);
gamma ~ lognormal(0, sigma_gamma);

for (n in 1:N)
y[n] ~ bernoulli_logit(gamma[kk[n]]* (alpha[jj[n]] - (beta[kk[n]] + mu_beta)));
}

Thank you very much in advance!

I think the confusion here comes from the fact that:

1. You always need to assign values to variables before you use them.
2. The sampler assigns values to all parameters before the start of the model block.
3. The `~` operator does not assign values to ANY of the variables involved in its expression

As an aside:

`a ~ f(b, c)` is the same as `target += f(a | b, c)` and both modify the log-density of the distribution implied by the model.

4 Likes

Doing `sigma_beta ~ cauchy(0,5);` does not assign a value to the symbol `sigma_beta`, which was declared in the parameters block and has a leapfrog proposal given to it. Essentially, `sigma_beta ~ cauchy(0,5);` is an alias for `target += cauchy_lpdf(sigma_beta | 0, 5);` which says to evaluate the logarithm of the Cauchy PDF at the proposed value of `sigma_beta` with location 0 and scale 5 and add that to the keyword `target` which keeps track of all such incrementation for all the terms in the posterior distribution. The only difference between the `~` form and the `target +=` form is that the former drops constants (such as `-log(pi)`) and is more confusing for people coming from BUGS. This is also discussed in the an appendix to the Stan User Manual and at

3 Likes