Skip to content

Remove redundant log1pexp reimplementation in Logistic CDF #4406

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

Merged
merged 1 commit into from
Jan 4, 2021
Merged
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
58 changes: 24 additions & 34 deletions pymc3/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
)
from pymc3.distributions.distribution import Continuous, draw_values, generate_samples
from pymc3.distributions.special import log_i0
from pymc3.math import invlogit, log1mexp, logdiffexp, logit
from pymc3.math import invlogit, log1mexp, log1pexp, logdiffexp, logit
from pymc3.theanof import floatX

__all__ = [
Expand Down Expand Up @@ -3887,25 +3887,6 @@ def __init__(self, mu=0.0, s=1.0, *args, **kwargs):
self.mean = self.mode = mu
self.variance = s ** 2 * np.pi ** 2 / 3.0

def logp(self, value):
"""
Calculate log-probability of Logistic distribution at specified value.

Parameters
----------
value: numeric
Value(s) for which log-probability is calculated. If the log probabilities for multiple
values are desired the values must be provided in a numpy array or theano tensor

Returns
-------
TensorVariable
"""
mu = self.mu
s = self.s

return bound(-(value - mu) / s - tt.log(s) - 2 * tt.log1p(tt.exp(-(value - mu) / s)), s > 0)

def random(self, point=None, size=None):
"""
Draw random values from Logistic distribution.
Expand All @@ -3929,17 +3910,33 @@ def random(self, point=None, size=None):
stats.logistic.rvs, loc=mu, scale=s, dist_shape=self.shape, size=size
)

def logp(self, value):
"""
Calculate log-probability of Logistic distribution at specified value.

Parameters
----------
value: numeric
Value(s) for which log-probability is calculated. If the log probabilities for multiple
values are desired the values must be provided in a numpy array or theano tensor

Returns
-------
TensorVariable
"""
mu = self.mu
s = self.s

return bound(
-(value - mu) / s - tt.log(s) - 2 * tt.log1p(tt.exp(-(value - mu) / s)),
s > 0,
)

def logcdf(self, value):
r"""
Compute the log of the cumulative distribution function for Logistic distribution
at the specified value.

References
----------
.. [Machler2012] Martin Mächler (2012).
"Accurately computing :math: `\log(1-\exp(- \mid a \mid<))` Assessed by the Rmpfr
package"

Parameters
----------
value: numeric or np.ndarray or theano.tensor
Expand All @@ -3952,14 +3949,7 @@ def logcdf(self, value):
"""
mu = self.mu
s = self.s
a = -(value - mu) / s
return -tt.switch(
tt.le(a, -37),
tt.exp(a),
tt.switch(
tt.le(a, 18), tt.log1p(tt.exp(a)), tt.switch(tt.le(a, 33.3), tt.exp(-a) + a, a)
),
)
return -log1pexp(-(value - mu) / s)


class LogitNormal(UnitContinuous):
Expand Down