# Order of statments in model block

**URL:** <https://discourse.mc-stan.org/t/order-of-statments-in-model-block/3036>\
**Category:** Modeling\
**Created:** [January 18, 2018, 7:39pm UTC](https://discourse.mc-stan.org/t/order-of-statments-in-model-block/3036 "2018-01-18T19:39:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmcuong88](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nmcuong88/32/1663_2.png) [@nmcuong88](https://discourse.mc-stan.org/u/nmcuong88)\
**Post date:** [January 18, 2018, 7:39pm UTC](https://discourse.mc-stan.org/t/order-of-statments-in-model-block/3036/1 "2018-01-18T19:39:51Z")

</div>

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.  
[https://github.com/stan-dev/stan/releases/download/v2.17.1/stan-reference-2.17.1.pdf](https://github.com/stan-dev/stan/releases/download/v2.17.1/stan-reference-2.17.1.pdf)

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!

---

<div class="post-metadata">

**Author:** ![Krzysztof\_Sakrejda](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Krzysztof\_Sakrejda](https://discourse.mc-stan.org/u/Krzysztof_Sakrejda)\
**Post date:** [January 18, 2018, 8:13pm UTC](https://discourse.mc-stan.org/t/order-of-statments-in-model-block/3036/2 "2018-01-18T20:13:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [January 18, 2018, 10:59pm UTC](https://discourse.mc-stan.org/t/order-of-statments-in-model-block/3036/3 "2018-01-18T22:59:55Z")

</div>

> [@nmcuong88](#):
>
> 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,

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

> **[Home](https://github.com/stan-dev/stan/wiki/)**
>
> Stan development repository. The master branch contains the current release. The develop branch contains the latest stable development. See the Developer Process Wiki for details. - stan-dev/stan
