-
-
Notifications
You must be signed in to change notification settings - Fork 987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sampling from truncated Gaussian #790
Comments
hello, you might take a look here: |
Hi @bmazoure you could also define a Rejector distribution for a truncated normal. This would require you to implement the total probability of acceptance class TruncatedMVN(dist.Rejector):
def __init__(self, loc, covariance_matrix, min_x0):
propose = dist.MultivariateNormal(loc, covariance_matrix)
def log_prob_accept(x):
return (x[0] > min_x0).type_as(x).log()
scale_0 = torch.sqrt(covariance_matrix[0, 0])
log_scale = torch.log(1 - dist.Normal(loc[0], scale_0).cdf(min_x0))
super(TruncatedMVN, self).__init__(propose, log_prob_accept, log_scale) (Note this is available on Pyro dev branch, but not in the 0.1.2 release) |
Will try these suggestions, thanks! |
I'm trying to use a rejector to limit a Pareto distribution, which is wrapped from a torch distribution: class TruncatedPareto(Rejector):
def __init__(self, scale, alpha, upper_limit, validate_args=None):
propose = Pareto(scale, alpha, validate_args=validate_args)
def log_prob_accept(x):
return (x < upper_limit).type_as(x).log()
log_scale = torch.Tensor(alpha) * torch.log(torch.Tensor([scale / upper_limit]))
super(TruncatedPareto, self).__init__(propose, log_prob_accept, log_scale) I can sample from it by calling
|
@dobos It looks like |
Were truncated distributions ever added? |
@GlastonburyC I believe @alicanb was working on |
Is there a way to implement a Distribution for sampling from a multivariate Gaussian truncated on a hyperplane? for a example, making sure that the sampled array sums to zero? @fritzo |
@zoj613 You could easily implement a multivariate Gaussian truncated along a single hyperplane passing through the center by generalizing FoldedDistribution to a |
@fritzo How can I sample from a normal but only have negative support? |
@GlastonburyC For a negative Gaussian with zero mode, you could use a transformed class NegativeHalfNormal(dist.TransformedDistribution):
support = constraints.less_than(0)
def __init__(self, scale):
base_dist = dist.HalfNormal(scale)
transform = dist.transforms.AffineTransform(0., -1.)
super().__init__(base_dist, transform) If your In practice I prefer class NegativeFoldedNormal(TransformedDistribution):
support = constraints.less_than(0)
def __init__(self, loc, scale):
base_dist = dist.FoldedDistribution(dist.Normal(loc, scale))
transform = dist.transforms.AffineTransform(0., -1.)
super().__init__(base_dist, transform) |
Thanks @fritzo, that's super helpful of you. :) |
Is there currently a way to sample from new distributions which are similar to the ones already implemented in Pyro?
For instance, in the case of a multivariate truncated Gaussian we would need to define how to compute the gradient as a piecewise function and I am not sure where to do so.
The text was updated successfully, but these errors were encountered: