Giving lower bound and upper bound as a matrix for a matrix (same dimensions)

Hello,

I would like to give upper and lower bounds to a parameter matrix with a matrix of same size provided in the data. Is this possible ? The code below is not working with error “Expression denoting real required; found type=matrix.”

‘’'stan
data {
int<lower=1> N; // total number of trials
matrix[N,2] Stimuli; // stimulus per trial, in radian from -pi to pi, for color and shape
}

parameters {
matrix<lower=Stimuli-pi(), upper=Stimuli+pi()>[N,2] perceived_stimulus_in_rad ;
}
‘’’

Later in the model block, I declare the perceived stimuli as being :
perceived_stimulus_in_rad[i,1] ~ von_mises(0, kappa);
If instead I was doing perceived_stimulus_in_rad[i,1] ~ von_mises(Stimuli[i,1], kappa); with perceived_stimulus_in_rad bounded between fixed -pi() and pi(), as I wanted initially, the chains would sometimes get stuck to pi or -pi, something seems to be wrong in the circularity of the von mises function (cf https://www.dropbox.com/s/s7c2667tl6laayp/param_5_Rhat_11.69.png?dl=0)

thank you in advance

No. You would have to declare a primitive matrix in the parameters block with scalar bounds like

parameters {
  matrix<lower = -pi(), upper = pi()>[N, 2] rad;
}

and then shift it in transformed parameters like

transformed parameters {
  matrix[N, 2] perceived_stimulus_in_rad = Stimuli + rad;
}

thank you. Then is there a difference between declaring perceived_stimulus_in_rad in a transformed parameter block, and just declaring perceived_stimulus_in_rad in the model block ? Example in the model block ;
matrix[N,2] perceived_stimulus_in_rad;
perceived_stimulus_in_rad[i,1] = rad[i,1]+Stimuli[i,1];
perceived_stimulus_in_rad[i,2] = rad[i,2]+Stimuli[i,2];

and another related question, sorry if this is obvious : Can I now just have a single real value for the rad parameter so :
‘’’
parameters {
real<lower = -pi(), upper = pi()> rad;
}
‘’’
then what you said in the transformed parameters :
‘’’
transformed parameters {
matrix[N, 2] perceived_stimulus_in_rad = Stimuli + rad;
}
‘’’

and then, in the model block, at each trial i, having rad ~ von_mises(0, kappa)

that would permit evaluate just one ‘rad’ instead of the whole sequence of trials values for rad, but obviously they need to have a different value from each other at each trial because the perceived_stimulus_in_rad are different from one trial to another, and the sampling procedure has to take into account this whole sequence of trial differences

If you just declare a scalar rad that is a much more restrictive model then declaring rad as a matrix, in which case all the elements are going to be different from each other but between -\pi and \pi. You can declare perceived_stimulus_in_rad in either the transformed parameters or the model block, depending on whether you want to store it in the output.

thank you very much !

ah but then : why is the von_mises function not truly circular ?
In the end, why can’t you write
parameters {
matrix<lower = -pi(), upper = pi()>[N, 2] rad;
}

and then in the model block : rad[i,1] ~ von_mises(Stimuli[i,1], kappa)
without having this boundary issue ?

I think that is legal syntax, but I am not sure it makes sense. rad[i,1] ~ von_mises(Stimuli[i,1], kappa) just adds the log-prior onto target.

sorry what does “If the location µ is not in
the center of the support, the density is circularly translated and there will be a second
local maximum at the boundary furthest from the mode. Ideally, the parameterization
and support will be set up so that the bulk of the probability mass is in a continuous
interval around the mean µ.” in the functions reference 2_22 document mean then ?

Not sure. Maybe it bunches the mass at the far boundary instead of wrapping around?

yes it seems that it’s exactly what it does, which is a problemparam_3_Rhat_13.74
example in this image, where some chains were stuck at -pi