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

Respect log_density in kl of delta #1625

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions numpyro/distributions/kl.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ def kl_divergence(p, q):

@dispatch(Delta, Distribution)
def kl_divergence(p, q):
return -q.log_prob(p.v)
return -q.log_prob(p.v) + p.log_density


@dispatch(Delta, ExpandedDistribution)
def kl_divergence(p, q):
return -q.log_prob(p.v)
return -q.log_prob(p.v) + p.log_density


@dispatch(Independent, Independent)
Expand Down
9 changes: 9 additions & 0 deletions test/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2814,6 +2814,15 @@ def test_kl_delta_normal_shape(batch_shape):
assert kl_divergence(p, q).shape == batch_shape


def test_kl_delta_normal():
v = np.random.normal()
loc = np.random.normal()
scale = np.exp(np.random.normal())
p = dist.Delta(v, 10.0)
q = dist.Normal(loc, scale)
assert_allclose(kl_divergence(p, q), 10.0 - q.log_prob(v))


@pytest.mark.parametrize("batch_shape", [(), (4,), (2, 3)], ids=str)
@pytest.mark.parametrize("event_shape", [(), (4,), (2, 3)], ids=str)
def test_kl_independent_normal(batch_shape, event_shape):
Expand Down