Skip to content

Commit

Permalink
Fix multinomial_goodness_of_fit printing (#3115)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzo committed Jul 9, 2022
1 parent dc1351a commit 36d29ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pyro/distributions/testing/gof.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ class InvalidTest(ValueError):


def print_histogram(probs, counts):
max_count = max(counts)
max_count = int(max(counts))
print("{: >8} {: >8}".format("Prob", "Count"))
for prob, count in sorted(zip(probs, counts), reverse=True):
width = int(round(HISTOGRAM_WIDTH * count / max_count))
width = int(round(HISTOGRAM_WIDTH * int(count) / max_count))
print("{: >8.3f} {: >8d} {}".format(prob, count, "-" * width))


Expand Down
20 changes: 20 additions & 0 deletions tests/distributions/testing/test_gof.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright Contributors to the Pyro project.
# SPDX-License-Identifier: Apache-2.0

import torch
import torch.distributions as dist

from pyro.distributions.testing.gof import multinomial_goodness_of_fit


def test_multinomial_goodness_of_fit():
N = 100000
K = 20
logits = torch.randn(K)
probs = (logits - logits.logsumexp(-1)).exp()
d = dist.Categorical(probs)
samples = d.sample((N,))
counts = torch.zeros(K, dtype=torch.long)
counts.scatter_add_(0, samples, torch.ones(N, dtype=torch.long))
gof = multinomial_goodness_of_fit(probs, counts, plot=True)
assert gof > 0.1

0 comments on commit 36d29ee

Please sign in to comment.