Skip to content

Commit 982bb4d

Browse files
authored
Remove redundant log1pexp reimplementation in Logistic CDF (#4406)
Also: Reorder logp and random to be in line with rest of the library
1 parent e783106 commit 982bb4d

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

pymc3/distributions/continuous.py

+24-34
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
)
4646
from pymc3.distributions.distribution import Continuous, draw_values, generate_samples
4747
from pymc3.distributions.special import log_i0
48-
from pymc3.math import invlogit, log1mexp, logdiffexp, logit
48+
from pymc3.math import invlogit, log1mexp, log1pexp, logdiffexp, logit
4949
from pymc3.theanof import floatX
5050

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

3890-
def logp(self, value):
3891-
"""
3892-
Calculate log-probability of Logistic distribution at specified value.
3893-
3894-
Parameters
3895-
----------
3896-
value: numeric
3897-
Value(s) for which log-probability is calculated. If the log probabilities for multiple
3898-
values are desired the values must be provided in a numpy array or theano tensor
3899-
3900-
Returns
3901-
-------
3902-
TensorVariable
3903-
"""
3904-
mu = self.mu
3905-
s = self.s
3906-
3907-
return bound(-(value - mu) / s - tt.log(s) - 2 * tt.log1p(tt.exp(-(value - mu) / s)), s > 0)
3908-
39093890
def random(self, point=None, size=None):
39103891
"""
39113892
Draw random values from Logistic distribution.
@@ -3929,17 +3910,33 @@ def random(self, point=None, size=None):
39293910
stats.logistic.rvs, loc=mu, scale=s, dist_shape=self.shape, size=size
39303911
)
39313912

3913+
def logp(self, value):
3914+
"""
3915+
Calculate log-probability of Logistic distribution at specified value.
3916+
3917+
Parameters
3918+
----------
3919+
value: numeric
3920+
Value(s) for which log-probability is calculated. If the log probabilities for multiple
3921+
values are desired the values must be provided in a numpy array or theano tensor
3922+
3923+
Returns
3924+
-------
3925+
TensorVariable
3926+
"""
3927+
mu = self.mu
3928+
s = self.s
3929+
3930+
return bound(
3931+
-(value - mu) / s - tt.log(s) - 2 * tt.log1p(tt.exp(-(value - mu) / s)),
3932+
s > 0,
3933+
)
3934+
39323935
def logcdf(self, value):
39333936
r"""
39343937
Compute the log of the cumulative distribution function for Logistic distribution
39353938
at the specified value.
39363939
3937-
References
3938-
----------
3939-
.. [Machler2012] Martin Mächler (2012).
3940-
"Accurately computing :math: `\log(1-\exp(- \mid a \mid<))` Assessed by the Rmpfr
3941-
package"
3942-
39433940
Parameters
39443941
----------
39453942
value: numeric or np.ndarray or theano.tensor
@@ -3952,14 +3949,7 @@ def logcdf(self, value):
39523949
"""
39533950
mu = self.mu
39543951
s = self.s
3955-
a = -(value - mu) / s
3956-
return -tt.switch(
3957-
tt.le(a, -37),
3958-
tt.exp(a),
3959-
tt.switch(
3960-
tt.le(a, 18), tt.log1p(tt.exp(a)), tt.switch(tt.le(a, 33.3), tt.exp(-a) + a, a)
3961-
),
3962-
)
3952+
return -log1pexp(-(value - mu) / s)
39633953

39643954

39653955
class LogitNormal(UnitContinuous):

0 commit comments

Comments
 (0)