Skip to content

Improve numerical precision of discrete uniform and geometric ICDFs #6671

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
Apr 27, 2023
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
11 changes: 10 additions & 1 deletion pymc/distributions/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from pymc.distributions.distribution import Discrete
from pymc.distributions.mixture import Mixture
from pymc.distributions.shape_utils import rv_size_is_none
from pymc.logprob.basic import logp
from pymc.logprob.basic import logcdf, logp
from pymc.math import sigmoid
from pymc.pytensorf import floatX, intX
from pymc.vartypes import continuous_types
Expand Down Expand Up @@ -823,6 +823,10 @@ def logcdf(value, p):

def icdf(value, p):
res = pt.ceil(pt.log1p(-value) / pt.log1p(-p)).astype("int64")
res_1m = pt.maximum(res - 1, 0)
dist = pm.Geometric.dist(p=p)
value_1m = pt.exp(logcdf(dist, res_1m))
res = pt.switch(value_1m >= value, res_1m, res)
res = check_icdf_value(res, value)
return check_icdf_parameters(
res,
Expand Down Expand Up @@ -1060,6 +1064,11 @@ def logcdf(value, lower, upper):

def icdf(value, lower, upper):
res = pt.ceil(value * (upper - lower + 1)).astype("int64") + lower - 1
res_1m = pt.maximum(res - 1, lower)
dist = pm.DiscreteUniform.dist(lower=lower, upper=upper)
value_1m = pt.exp(logcdf(dist, res_1m))
res = pt.switch(value_1m >= value, res_1m, res)

res = check_icdf_value(res, value)
return check_icdf_parameters(
res,
Expand Down