How to declare normal distribution for param arrays

stanc

#1

I see from tutorials where you can declare distribution for parameter array using simple mean and variance. For example

parameters {
    real a[M];
}
model {
  a ~ normal(0,sigma);
}

But that’s not working for me.

parameters {
	real xi[S, S];		
	vector[K] beta[S,S];
	real alpha[S, S];
	vector[K] gamma[S,S];
	real eta[S]; 	
	real<lower=0,upper=100> sigma_xi[S, S];  		// sigma for Xi
}
model {
	beta ~ normal(0,100);	
	alpha ~ normal(0,100);
	gamma ~ normal(0,100);
	eta ~ normal(0, 100);
	xi ~ normal(0.0, sigma_xi);
}

All of them got rejected. It dumps a bunch of messages declaring available argument signatures like

real ~ normal(real[], real[])

First, I don’t know how to declare a mean of 0 in an array. Is it like [0,0,…]?

Second, how to do the same when I have 2D array as in case of xi[S, S]?

[Edit: added triple back ticks around code]


#2

You can’t just write

parameters {
    real a[M];
}
model {
  a ~ normal(0,sigma);
}

because the variable sigma is not defined. All variables must be defined before they’re used.


#3

Secondly, this won’t work

vector[K] beta[S,S];
...
beta ~ normal(0,100);

because normal isn’t defined for 2D arrays of vectors. You need to loop down to the basic types.

for (s1 in 1:S)
  for (s2 in 1:S)
    beta[s1, s2] ~ normal(0, 100);

The signatures listed are the only ones you get. For instance,

real ~ normal(real[], real[])

picks out a real and two 1D real arrays. So that’d be:

real y;
real mu[5];
real sigma[5];
...
y ~ normal(mu, sigma);

#4

Thank you. Completely fixed based on your input.