Skip to content
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

Add a SoftLaplace distribution #2791

Merged
merged 4 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/source/distributions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,13 @@ Rejector
:undoc-members:
:show-inheritance:

SoftLaplace
-------------
.. autoclass:: pyro.distributions.SoftLaplace
:members:
:undoc-members:
:show-inheritance:

SpanningTree
------------
.. autoclass:: pyro.distributions.SpanningTree
Expand Down
2 changes: 2 additions & 0 deletions pyro/distributions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
RelaxedBernoulliStraightThrough,
RelaxedOneHotCategoricalStraightThrough,
)
from pyro.distributions.softlaplace import SoftLaplace
from pyro.distributions.spanning_tree import SpanningTree
from pyro.distributions.stable import Stable
from pyro.distributions.torch import __all__ as torch_dists
Expand Down Expand Up @@ -127,6 +128,7 @@
"Rejector",
"RelaxedBernoulliStraightThrough",
"RelaxedOneHotCategoricalStraightThrough",
"SoftLaplace",
"SpanningTree",
"Stable",
"TorchDistribution",
Expand Down
66 changes: 66 additions & 0 deletions pyro/distributions/softlaplace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright Contributors to the Pyro project.
# SPDX-License-Identifier: Apache-2.0

import math

import torch
from torch.distributions import constraints
from torch.distributions.utils import broadcast_all

from .torch_distribution import TorchDistribution


class SoftLaplace(TorchDistribution):
"""
Smooth distribution with Laplace-like tail behavior.

This distribution corresponds to the log-convex density::

z = (value - loc) / scale
log_prob = log(2 / pi) - log(scale) - logaddexp(z, -z)

Like the Laplace density, this density has the heaviest possible tails
(asymptotically) while still being log-convex. Unlike the Laplace
distribution, this distribution is infinitely differentiable everywhere,
and is thus suitable for constructing Laplace approximations.

:param loc: Location parameter.
:param scale: Scale parameter.
"""

arg_constraints = {"loc": constraints.real, "scale": constraints.positive}
support = constraints.real
has_rsample = True

def __init__(self, loc, scale, *, validate_args=None):
self.loc, self.scale = broadcast_all(loc, scale)
super().__init__(self.loc.shape, validate_args=validate_args)

def expand(self, batch_shape, _instance=None):
new = self._get_checked_instance(SoftLaplace, _instance)
batch_shape = torch.Size(batch_shape)
new.loc = self.loc.expand(batch_shape)
new.scale = self.scale.expand(batch_shape)
super(SoftLaplace, new).__init__(batch_shape, validate_args=False)
new._validate_args = self._validate_args
return new

def log_prob(self, value):
if self._validate_args:
self._validate_sample(value)
z = (value - self.loc) / self.scale
return math.log(2 / math.pi) - self.scale.log() - torch.logaddexp(z, -z)
Copy link
Member

@fehiepsi fehiepsi Apr 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make senses to me: logaddexp(z, -z) ~ |z| in the tail and wolfram alpha shows the correct normalization factor.


def rsample(self, sample_shape=torch.Size()):
shape = self._extended_shape(sample_shape)
u = self.loc.new_empty(shape).uniform_()
return self.icdf(u)

def cdf(self, value):
if self._validate_args:
self._validate_sample(value)
z = (value - self.loc) / self.scale
return z.exp().atan().mul(2 / math.pi)
fritzo marked this conversation as resolved.
Show resolved Hide resolved

def icdf(self, value):
return value.mul(math.pi / 2).tan().log().mul(self.scale).add(self.loc)
11 changes: 11 additions & 0 deletions tests/distributions/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,17 @@ def scale(self):
{'concentration': [0., 0., 0.], 'test_data': [1., 0., 0.]},
{'concentration': [-1., 2., 3.], 'test_data': [0., 0., 1.]},
]),
Fixture(pyro_dist=dist.SoftLaplace,
examples=[
{'loc': [2.0], 'scale': [4.0],
'test_data': [2.0]},
{'loc': [[2.0]], 'scale': [[4.0]],
'test_data': [[2.0]]},
{'loc': [[[2.0]]], 'scale': [[[4.0]]],
'test_data': [[[2.0]]]},
{'loc': [2.0, 50.0], 'scale': [4.0, 100.0],
'test_data': [[2.0, 50.0], [2.0, 50.0]]},
]),
]

discrete_dists = [
Expand Down