Sum to zero constraint?


#1

Hi, I am wondering how I can constrain a set of variables to sum to 0?

I am currently trying:

a = pyro.sample(“a”, dist.Normal(0, 1).expand([5]).to_event(1))
a -= a.mean()

However, this produces the error on the 2nd line:
“a leaf Variable that requires grad has been used in an in-place operation”

I have also tried using plate, and a variety of other options to no avail.

Is this possible in Pyro? My apologies for what may be a trivial question.


#3

It might help to avoid in-place operations (I now always avoid in-place PyTorch ops):

a = a - a.mean()

If this doesn’t work, could you try pasting more code so we can see how a is being used. Another thing you could do is to sample from the n-1 dimensional space and manually inject into the n dimensional space, similar to the code in torch.distributions.constraints.


#4

Is this compositional data?

Sample from a Dirichlet distribution (with alpha equal 1 for each component if you want a uniform compositional distribution). After that just subtract 1 the resultant vector.