Skip to content

Commit c01f2fb

Browse files
committed
fix cdf normalization
1 parent 65ef80f commit c01f2fb

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

petab/v1/distributions.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(
6464
self._cd_high = self._cdf_transformed_untruncated(
6565
self.trunc_high
6666
)
67-
# normalization factor for the PDF of the transformed
67+
# normalization factor for the PDF/CDF of the transformed
6868
# distribution to account for truncation
6969
self._truncation_normalizer = 1 / (
7070
self._cd_high - self._cd_low
@@ -172,7 +172,11 @@ def cdf(self, x):
172172
:param x: The value at which to evaluate the CDF.
173173
:return: The value of the CDF at ``x``.
174174
"""
175-
return self._cdf_transformed_untruncated(x) - self._cd_low
175+
if self._trunc is None:
176+
return self._cdf_transformed_untruncated(x)
177+
return (
178+
self._cdf_transformed_untruncated(x) - self._cd_low
179+
) * self._truncation_normalizer
176180

177181
def _cdf_transformed_untruncated(self, x):
178182
"""Cumulative distribution function of the transformed, but untruncated

tests/v1/test_distributions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13
import numpy as np
24
import pytest
35
from numpy.testing import assert_allclose
@@ -56,6 +58,21 @@ def cdf(x):
5658

5759
assert p > 0.05, (p, distribution)
5860

61+
# check min/max of CDF at the bounds
62+
assert np.isclose(
63+
distribution.cdf(
64+
distribution.trunc_low
65+
if not distribution.logbase
66+
else max(sys.float_info.min, distribution.trunc_low)
67+
),
68+
0,
69+
atol=1e-16,
70+
rtol=0,
71+
)
72+
assert np.isclose(
73+
distribution.cdf(distribution.trunc_high), 1, atol=1e-14, rtol=0
74+
)
75+
5976
# Test samples match scipy CDFs
6077
reference_pdf = None
6178
if distribution._trunc is None and distribution.logbase is False:

0 commit comments

Comments
 (0)