-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Reintroduce Bernoulli logitp parametrization #4620
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,15 @@ | |
import aesara.tensor as at | ||
import numpy as np | ||
|
||
from aesara.tensor.random.basic import bernoulli, binomial, categorical, nbinom, poisson | ||
from aesara.tensor.random.basic import ( | ||
BernoulliRV, | ||
binomial, | ||
categorical, | ||
nbinom, | ||
poisson, | ||
) | ||
from scipy import stats | ||
from scipy.special import expit | ||
|
||
from pymc3.aesaraf import floatX, intX, take_along_axis | ||
from pymc3.distributions.dist_math import ( | ||
|
@@ -32,7 +39,7 @@ | |
normal_lcdf, | ||
) | ||
from pymc3.distributions.distribution import Discrete | ||
from pymc3.math import log1mexp, logaddexp, logsumexp, sigmoid, tround | ||
from pymc3.math import log1mexp, log1pexp, logaddexp, logit, logsumexp, sigmoid, tround | ||
|
||
__all__ = [ | ||
"Binomial", | ||
|
@@ -332,6 +339,19 @@ def logcdf(self, value): | |
) | ||
|
||
|
||
class BernoulliLogitRV(BernoulliRV): | ||
name = "bernoulli_logit" | ||
_print_name = ("BernLogit", "\\operatorname{BernLogit}") | ||
|
||
@classmethod | ||
def rng_fn(cls, rng, logitp, size=None): | ||
p = expit(logitp) | ||
return stats.bernoulli.rvs(p, size=size, random_state=rng) | ||
|
||
|
||
bernoulli_logit = BernoulliLogitRV() | ||
|
||
|
||
class Bernoulli(Discrete): | ||
R"""Bernoulli log-likelihood | ||
|
||
|
@@ -368,16 +388,29 @@ class Bernoulli(Discrete): | |
---------- | ||
p: float | ||
Probability of success (0 < p < 1). | ||
logit_p: float | ||
Alternative logit of sucess probability. | ||
""" | ||
rv_op = bernoulli | ||
rv_op = bernoulli_logit | ||
|
||
@classmethod | ||
def dist(cls, p=None, logit_p=None, *args, **kwargs): | ||
p = at.as_tensor_variable(floatX(p)) | ||
# mode = at.cast(tround(p), "int8") | ||
return super().dist([p], **kwargs) | ||
logit_p = cls.get_logitp(p=p, logit_p=logit_p) | ||
logit_p = at.as_tensor_variable(floatX(logit_p)) | ||
return super().dist([logit_p], **kwargs) | ||
|
||
def logp(value, p): | ||
@classmethod | ||
def get_logitp(cls, p=None, logit_p=None): | ||
if p is not None and logit_p is not None: | ||
raise ValueError("Incompatible parametrization. Can't specify both p and logit_p.") | ||
elif p is None and logit_p is None: | ||
raise ValueError("Incompatible parametrization. Must specify either p or logit_p.") | ||
|
||
if logit_p is None: | ||
logit_p = logit(p) | ||
return logit_p | ||
|
||
def logp(value, logit_p): | ||
r""" | ||
Calculate log-probability of Bernoulli distribution at specified value. | ||
|
||
|
@@ -391,19 +424,15 @@ def logp(value, p): | |
------- | ||
TensorVariable | ||
""" | ||
# if self._is_logit: | ||
# lp = at.switch(value, self._logit_p, -self._logit_p) | ||
# return -log1pexp(-lp) | ||
# else: | ||
lp = at.switch(value, -logit_p, logit_p) | ||
return bound( | ||
at.switch(value, at.log(p), at.log(1 - p)), | ||
value >= 0, | ||
-log1pexp(lp), | ||
0 <= value, | ||
value <= 1, | ||
p >= 0, | ||
p <= 1, | ||
~at.isnan(logit_p), | ||
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. Invalid |
||
) | ||
|
||
def logcdf(value, p): | ||
def logcdf(value, logit_p): | ||
""" | ||
Compute the log of the cumulative distribution function for Bernoulli distribution | ||
at the specified value. | ||
|
@@ -422,12 +451,11 @@ def logcdf(value, p): | |
return bound( | ||
at.switch( | ||
at.lt(value, 1), | ||
at.log1p(-p), | ||
-log1pexp(logit_p), | ||
0, | ||
), | ||
0 <= value, | ||
0 <= p, | ||
p <= 1, | ||
~at.isnan(logit_p), | ||
) | ||
|
||
def _distr_parameters_for_repr(self): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -731,10 +731,15 @@ def test_beta_binomial(self): | |
def _beta_bin(self, n, alpha, beta, size=None): | ||
return st.binom.rvs(n, st.beta.rvs(a=alpha, b=beta, size=size)) | ||
|
||
@pytest.mark.skip(reason="This test is covered by Aesara") | ||
def test_bernoulli(self): | ||
pymc3_random_discrete( | ||
pm.Bernoulli, {"p": Unit}, ref_rand=lambda size, p=None: st.bernoulli.rvs(p, size=size) | ||
pm.Bernoulli, {"p": Unit}, ref_rand=lambda size, p: st.bernoulli.rvs(p, size=size) | ||
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. This will be replaced following #4608 |
||
) | ||
|
||
pymc3_random_discrete( | ||
pm.Bernoulli, | ||
{"logit_p": R}, | ||
ref_rand=lambda size, logit_p: st.bernoulli.rvs(expit(logit_p), size=size), | ||
) | ||
|
||
@pytest.mark.skip(reason="This test is covered by Aesara") | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should it inherit from BernoulliRV or RandomVariable directly?
Is the
name
and_print_name
change just confusing and not necessary?