-
Notifications
You must be signed in to change notification settings - Fork 1
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 Cumulative Distribution Function, Inverse CDF methods to Distributions #122
Changes from 2 commits
e4e58e2
d9263a6
ee55d13
f76114a
d0bc72c
864d190
a9d74ce
24c2461
2932ffe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2309,6 +2309,28 @@ def test_variance_stddev(self): | |
self.assertEqual(pytorch_dist.variance, scipy_dist.var(), allow_inf=True, message=pytorch_dist) | ||
self.assertEqual(pytorch_dist.stddev, scipy_dist.var() ** 0.5, message=pytorch_dist) | ||
|
||
def test_cdf(self): | ||
set_rng_seed(0) # see Note [Randomized statistical tests] | ||
for pytorch_dist, scipy_dist in self.distribution_pairs: | ||
samples = pytorch_dist.sample((5,)) | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's safer to enclose as little as needed in a try-except. Could you refactor to try:
cdf = pytorch_dist.cdf(samples)
except NotImplementedError:
continue
self.assertEqual(cdf, scipy_dist.cdf(samples), message=pytorch_dist) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yes. I saw the discussion in TruncatedNormal. I will modify it accordingly. |
||
self.assertEqual(pytorch_dist.cdf(samples), | ||
scipy_dist.cdf(samples), | ||
message=pytorch_dist) | ||
except NotImplementedError: | ||
pass | ||
|
||
def test_icdf(self): | ||
set_rng_seed(0) # see Note [Randomized statistical tests] | ||
for pytorch_dist, scipy_dist in self.distribution_pairs: | ||
samples = Variable(torch.rand((5,) + pytorch_dist.batch_shape)) | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto, enclose as little as possible in try-except There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. |
||
self.assertEqual(pytorch_dist.icdf(samples), | ||
scipy_dist.ppf(samples), | ||
message=pytorch_dist) | ||
except NotImplementedError: | ||
pass | ||
|
||
|
||
class TestTransforms(TestCase): | ||
def setUp(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,5 +55,13 @@ def log_prob(self, value): | |
self._validate_log_prob_arg(value) | ||
return -torch.log(2 * self.scale) - torch.abs(value - self.loc) / self.scale | ||
|
||
def cdf(self, value): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Laplace's .cdf is a piecewise function. I was doubtful about adding an inverse, and later realized that the inverse could be piecewise as well. Will update this too.. |
||
self._validate_log_prob_arg(value) | ||
term = torch.exp((value - self.loc) / self.scale) | ||
result = value.new() | ||
result[value < self.loc] = 0.5 * term | ||
result[value >= self.loc] = 1 - 0.5 / term | ||
return result | ||
|
||
def entropy(self): | ||
return 1 + torch.log(2 * self.scale) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have an additional test that did not rely on scipy, e.g.
or you could get even fancier by using
grad()
like