Help with the explanation of drawing sample from the loop - Vector verses Real

Hello, stan users,

I have a quick question:

Suppose I have a vector rho[J] which is a normally distributed variable for J groups.

Now I have another variable,Y that depends upon rho (I know I am highly simplifying the whole model).

Suppose I have two ways of drawing sample/samples from rho:

real Y;
for (j in 1:J) {
Y= (1-rho[j])*(1+rho[j]);

now this gives me one draw of Y from j rho’s.

I can have another way of sampling

vector[J] Y;
for (j in 1:J) {
Y[j]= (1-rho[j])*(1+rho[j]);

and this will give me j samples of Y.

I want to ask the modellers out there, is the one value of Y I got from the first sampling can be considered as the average of J Y samples I obtained from second procedure?

I know I have simplied it a lot and do not talk about the overall model.

I just want opinions.


No, because, in the first for loop

Y is assigned as (1-rho[J])*(1+rho[J]). Why is J here, because you assign Y and re-assign again and again until j=J. In the second for loop, it gives a vector (as you already mentioned). And of course, mean(Y) is (generally) different from Y[J].

You can insert a statement such as mean_y = mean(Y) to compute the mean.

1 Like

Thank you kindly.