How to specify a sum-to-one vector with individual component has support on full range from [0,1] ?

707 views
Skip to first unread message

sreenath am

unread,
Oct 5, 2016, 9:22:38 AM10/5/16
to Stan users mailing list
Hi,

I am trying to specify a sum-to-one vector with individual component has support on the full range [0,1] including 0 and 1.
Unit simplex would be a natural choice how ever, from what I understood from the manual, simples is not defined at 0 and 1.
Putting constrain doesn't seems to work because they are transformed using scaled logit(), behind the scenes, in Stan.

I am using this vector as weights. Below is a simple representation of the model.
Basically I am trying to define a weighted sum of exposures happened at different time points, this weighted sum would be the effective exposure. 

data{
  int N;                                    //Number of observations
  vector[N] X1;                        //Exposure variables at three time points
  vector[N] X2;
  vector[N] X3;
  vector[N] Y;                         //Outcome
}

parameters{
  real alpha;                         //intercept
  real beta;                          //Overall effect for exposure
  vector[3] w;                      //Weights for exposure at three time points (Sum-to-one vector needed!)
  real<lower=0> sigma        //Scale parameter
}

transformed parameters{
  vector[N] effEx;                            //Effective exposure
  effEx = X1*w[1] + X2*w[2] + X3*w[3];
}
model{
//priors
  alpha ~ cauchy(0,10);
  beta ~ cauchy(0,5);

for(n in 1:N){
  Y[n] ~ normal( alpha + beta*effEx[n] , sigma);
}
}

The sum-to-one vector has some interesting interpretation, it is the relative contribution of exposures happened at different times to the effective exposure.
So if one time point exposure is not contributing anything the corresponding weight should be zero. Also the vise-versa, if there is only one time point contributing everything then the weight for that period should be 1 and all others zero.

I have tried several things will little success. Is there a easy way to do this?
I am not a statistician, but a fast learner.

Note: I have not vectorized the model just for clarity.
I am using rstan 2.11.


Thank you in advance.
Sreenath





Bob Carpenter

unread,
Oct 5, 2016, 1:29:29 PM10/5/16
to stan-...@googlegroups.com
The boundaries shouldn't matter because they have measure 0.
That is,

Pr[theta[n] = 1 OR theta[n] = 0

for any continuous posterior density p(theta | y).

There's a section in the manual on coding sum-to-one constraints.

The simplex type allows 0 or 1 values. You will only get them
through underflow or overflow in the inverse-logit transforms from
unconstrained space---the manual has a chapter on constraint
transforms near the end that explains how the transforms work for
parameters like the simplex.

- 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.

sreenath am

unread,
Oct 5, 2016, 3:59:32 PM10/5/16
to Stan users mailing list
Thank you very much Bob. 

If I define the weight as simplex[3], with a flat Dirichlet prior on it, do you meant to say that theoretically a sample from posterior can be (1,0,0) or (0,0,1)?

-Sreenath

Daniel Lee

unread,
Oct 5, 2016, 4:47:29 PM10/5/16
to stan-users mailing list
On Wed, Oct 5, 2016 at 3:59 PM, sreenath am <natha...@gmail.com> wrote:
Thank you very much Bob. 

If I define the weight as simplex[3], with a flat Dirichlet prior on it, do you meant to say that theoretically a sample from posterior can be (1,0,0) or (0,0,1)?

Yes. If you write a model as:

parameter {
  simplex[3] theta;
}
model {
}

You'll just sample from 3-simplexes. (1,0,0) has the same probability as every other valid simplex.

(Feel free to throw that into a .stan file and run it.)



Daniel

 
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

Bob Carpenter

unread,
Oct 5, 2016, 5:04:29 PM10/5/16
to stan-...@googlegroups.com
Not from a Dirichlet. See the Wikipedia page.

But due to the vagaries of rounding with floating point,
we will get (1, 0, 0) values out by mistake due to roundoff
during inverse-logit transforms.

Our simplex data type allows values to be 0 or 1. But it's
not useful as it doesn't provide full support for a categorical
distribution---it puts all the mass on one outcome.

So what I'm saying is that you should be able to ignore the boundaries.

Why do you think you need 1 or 0 values?

- Bob

sreenath am

unread,
Oct 5, 2016, 6:47:39 PM10/5/16
to Stan users mailing list

Thank you Bob and Daniel,
 
I understood that with Dirichlet prior for simplex it is a rounding off phenomena. 
From my understanding I am not using simplex as the parameter for a categorical distribution. Instead, simplex as a way to get a non-negative vector of length K which sum-to-one. 
Or may be my understanding is wrong, but that is my intention in the model in the first my-post.

I searched in the manual but couldn't find an entry for sum-to-one constrain except for unit-simplex. 

Is there anything I am missing. 
OR any other way to do this?

Appreciate all you feedback

- Sreenath 

Bob Carpenter

unread,
Oct 21, 2016, 3:11:27 PM10/21/16
to stan-...@googlegroups.com
Sorry, missed that follow up.

The categorical distribution requires a simplex parameter.
The categorical_logit distribution applies softmax to its
parameter vector to produce a simplex.

See section 6.7: Parameterizing Centered Vectors

- Bob
Reply all
Reply to author
Forward
0 new messages