# Sum to one constraint on a parameter

Hello,

I am working to implement the latent space model of Hoff et. al (2003) and Handcock et al. (2007) where in they have this latent space defined by z and one constraint on this latent space parameter is
\sqrt{\frac{1}{n} \sum\limits_{i=1}^n ||z_i||^2_2}=1

I have the parameter block in my STAN code as below.

parameters {
.
.
.
real z[N,d];
}


and the model block

model {
for(i in 1:N)
{
z[i] ~ normal(zeros, z_prior_sd*ones);
}
.
.
.
}


where ‘zeros’ and ‘ones’ are vectors of 0 and 1 of size d. N and d are user inputs into the STAN code.

I was trying to implement this sum to 1 constraint defined above and would really like some help with how to proceed.

d is 2 and z is basically an ordered pair in the Euclidean space. Thanks in advance.

There’s a unit_vector variable type for this. Using it here with a slight trick to skip the loop:

parameters{
array[N] unit_vector[d] z ;
}
model{
to_vector(z) ~ normal( 0 , z_prior_sd) ; // no need for zeroes & *ones
}


Thank you @mike-lawrence for the reply. I tried to implement what you suggested but if I define the parameter block like that it keeps on throwing the error “Parser Expected <one of the following:”

Do you know what that is about?

Currently my parameter block looks like this

parameters {
vector[N] beta_i;
vector[N] beta_j;
real gam;
real<upper=0> del;
real alpha;           // set to 0
real eps[N,N];//

array[N] unit_vector[d] z ;
}


That means you’re using RStan with an older version of Stan. In that case array keyword is not allowed. The older syntax for an array of unit vectors is

parameters {
...
unit_vector[d] z[N] ;
}

1 Like

Thank you @nhuurre . That fixed the problem.