Skip to content
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

Added ICDF for the discrete uniform distribution. #6617

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions pymc/distributions/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,15 @@ def logcdf(value, lower, upper):
msg="lower <= upper",
)

def icdf(value, lower, upper):
res = pt.ceil(value * (upper - lower + 1)).astype("int64") + lower - 1
res = check_icdf_value(res, value)
return check_icdf_parameters(
res,
lower <= upper,
msg="lower <= upper",
)


class Categorical(Discrete):
R"""
Expand Down
10 changes: 9 additions & 1 deletion tests/distributions/test_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import pymc as pm

from pymc.distributions.discrete import Geometric, _OrderedLogistic, _OrderedProbit
from pymc.logprob.abstract import logcdf
from pymc.logprob.abstract import icdf, logcdf
from pymc.logprob.joint_logprob import logp
from pymc.logprob.utils import ParameterValueError
from pymc.pytensorf import floatX
Expand Down Expand Up @@ -118,13 +118,21 @@ def test_discrete_unif(self):
Domain([-10, 0, 10], "int64"),
{"lower": -Rplusdunif, "upper": Rplusdunif},
)
check_icdf(
pm.DiscreteUniform,
{"lower": -Rplusdunif, "upper": Rplusdunif},
lambda q, lower, upper: st.randint.ppf(q=q, low=lower, high=upper + 1),
skip_paramdomain_outside_edge_test=True,
)
# Custom logp / logcdf check for invalid parameters
invalid_dist = pm.DiscreteUniform.dist(lower=1, upper=0)
with pytensor.config.change_flags(mode=Mode("py")):
with pytest.raises(ParameterValueError):
logp(invalid_dist, 0.5).eval()
with pytest.raises(ParameterValueError):
logcdf(invalid_dist, 2).eval()
with pytest.raises(ParameterValueError):
icdf(invalid_dist, np.array(1)).eval()

def test_geometric(self):
check_logp(
Expand Down