I’d like to define a ragged array of ordered reals.
Similar to what suggested by @Christopher-Peterson in the solution for Ragged array of simplexes is there a way to define “unordered” vectors and transform them to ordered with a function that does the transformation and automatically performs the Jacobian adjustment?
thanks in advance for suggestions
Yes, a very similar technique can be used. An unordered vector would just be a normal vector of parameters, and the transform is given in Constraint Transforms
thanks @WardBrian
I wrote this:
vector ordered_constrain_lp(vector v) {
int K = size(v);
vector[K] v0 = append_row(0, segment(v,2,K-1));
// Jacobian
target += sum(v0);
return cumulative_sum(exp(v0)) + rep_vector(v[1]-1,K);
}
and sample in model
with
{
int pos=1;
for(k in 1:M){
ordered_constrain_lp(segment(mu_unconstrained,pos, sizes[k])) ~ normal(segment(mu0,pos, sizes[k]),sigma_mu);
pos += sizes[k];
}
}
does it look ok?
Can it be made more efficient?
thanks,
Davide