Simplexes of different dimensions

179 views
Skip to first unread message

bcal...@yahoo.com

unread,
Jul 25, 2016, 11:35:14 AM7/25/16
to Stan users mailing list
Hey all:

New group member and relatively new Stan user here.  I apologize if this has been asked before or if I am asking something obvious.

I have a modeling situation where I want to define several simplex parameter vectors.  They could potentially be of different dimensions, and the dimensions (or, in fact, how many there will be) will not be known until the data are read in.  So I have been trying to think of a way defining them generally, calling each one something like lambda[i], i=1, ..., n for the n simplexes I need.

I was thinking that an array specification might work as in

simplex[J] lambda[n];

except for the fact that J would not be the same for each i = 1, ..., n.

Is there some work-around that you can suggest?  Indeed would this work:

simplex[m[i]] lambda[n];

where m[i] specifies the dimension of the ith simplex?  I did not think so, since all the examples suggest that the array would need a constant "number of columns."  I was thinking that the list type in R would handle this well, but I do not see how I can get an equivalent in Stan.

Thanks in advance,
Bruce Allen

Ben Goodrich

unread,
Jul 25, 2016, 12:03:09 PM7/25/16
to Stan users mailing list
On Monday, July 25, 2016 at 11:35:14 AM UTC-4, bcal...@yahoo.com wrote:
I was thinking that an array specification might work as in

simplex[J] lambda[n];

except for the fact that J would not be the same for each i = 1, ..., n.

Is there some work-around that you can suggest?  Indeed would this work:

simplex[m[i]] lambda[n];

where m[i] specifies the dimension of the ith simplex?

No. We have to deal with this situation in rstanarm and it is a pain in the ass. The easiest way to do it is to declare a long vector

parameters {
  vector
<lower=0>[n * sum(J)] g;
 
...
}

and then in the model block normalize segments of it

model {
 
int pos;
  pos
= 1;
 
for (j in 1:J) {
    vector
[J[j]] lambda;
   
lambda = segment(g, pos, J[j]);
   
lambda = lambda / sum(lambda);
    pos
= pos + J[j];
   
// do the likelihood or whatever with lambda
 
}
  target
+= gamma_lpdf(g | alpha);
}

where alpha is a vector of concentration hyperparameters passed in via the data block. This utilizes the stochastic representation that a Dirichlet random variable can be constructed from unit scale gamma random variables:

https://en.wikipedia.org/wiki/Dirichlet_distribution#Gamma_distribution

Ben

Bob Carpenter

unread,
Jul 25, 2016, 12:12:55 PM7/25/16
to stan-...@googlegroups.com
In the future we'll be doing this, but we
need to add ragged arrays first.

In the interim, we've been meaning to add
functions to do the transforms and compute
the Jacobians. I'll add the Stan functions
for the simple ones in the next release:

https://github.com/stan-dev/stan/issues/1868#issuecomment-235001314

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

bcal...@yahoo.com

unread,
Jul 25, 2016, 12:25:58 PM7/25/16
to Stan users mailing list
Thanks you so much for the quick and helpful response.

Bruce
Reply all
Reply to author
Forward
0 new messages