Skip to content

Commit caa07f3

Browse files
authored
add logcdf, sf and isf (#670)
1 parent 0baa056 commit caa07f3

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

preliz/distributions/distributions.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,36 @@ def logpdf(self, x):
177177
"""
178178
raise NotImplementedError
179179

180+
def logcdf(self, x):
181+
"""Log cumulative distribution function.
182+
183+
Parameters
184+
----------
185+
x : array_like
186+
Values on which to evaluate the logcdf
187+
"""
188+
return np.log(self.cdf(x))
189+
190+
def sf(self, x):
191+
"""Survival function (1 - cdf).
192+
193+
Parameters
194+
----------
195+
x : array_like
196+
Values on which to evaluate the sf
197+
"""
198+
return 1 - self.cdf(x)
199+
200+
def isf(self, x):
201+
"""Inverse survival function (inverse of sf).
202+
203+
Parameters
204+
----------
205+
x : array_like
206+
Values on which to evaluate the inverse of the sf
207+
"""
208+
return self.ppf(1 - np.array(x))
209+
180210
def entropy(self):
181211
"""Entropy."""
182212
raise NotImplementedError

preliz/tests/test_scipy.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,37 @@ def test_match_scipy(p_dist, sp_dist, p_params, sp_params):
255255
else:
256256
assert_almost_equal(actual_cdf, expected_cdf, decimal=6)
257257

258+
actual_logcdf = preliz_dist.logcdf(extended_vals)
259+
expected_logcdf = scipy_dist.logcdf(extended_vals)
260+
261+
if preliz_name in ["HalfStudentT", "LogitNormal"]:
262+
assert_almost_equal(actual_logcdf, expected_logcdf, decimal=2)
263+
else:
264+
assert_almost_equal(actual_logcdf, expected_logcdf, decimal=5)
265+
266+
actual_sf = preliz_dist.sf(extended_vals)
267+
expected_sf = scipy_dist.sf(extended_vals)
268+
269+
if preliz_name in ["HalfStudentT", "LogitNormal"]:
270+
assert_almost_equal(actual_sf, expected_sf, decimal=2)
271+
else:
272+
assert_almost_equal(actual_sf, expected_sf, decimal=6)
273+
258274
x_vals = [-1, 0, 0.25, 0.5, 0.75, 1, 2]
259275
actual_ppf = preliz_dist.ppf(x_vals)
260276
expected_ppf = scipy_dist.ppf(x_vals)
261-
if preliz_name in ["HalfStudentT", "Wald", "LogitNormal", "SkewNormal", "ExGaussian"]:
277+
if preliz_name in ["HalfStudentT", "LogitNormal", "ExGaussian"]:
262278
assert_almost_equal(actual_ppf, expected_ppf, decimal=2)
263279
else:
264280
assert_almost_equal(actual_ppf, expected_ppf)
265281

282+
actual_isf = preliz_dist.isf(x_vals)
283+
expected_isf = scipy_dist.isf(x_vals)
284+
if preliz_name in ["HalfStudentT", "LogitNormal", "ExGaussian"]:
285+
assert_almost_equal(actual_isf, expected_isf, decimal=2)
286+
else:
287+
assert_almost_equal(actual_isf, expected_isf)
288+
266289
actual_logpdf = preliz_dist.logpdf(extended_vals)
267290
if preliz_dist.kind == "continuous":
268291
expected_logpdf = scipy_dist.logpdf(extended_vals)

0 commit comments

Comments
 (0)