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

Inconsistent behaviour of truncate for discrete distributions #1798

Open
lcontento opened this issue Nov 8, 2023 · 1 comment · May be fixed by #1805
Open

Inconsistent behaviour of truncate for discrete distributions #1798

lcontento opened this issue Nov 8, 2023 · 1 comment · May be fixed by #1805

Comments

@lcontento
Copy link

When truncating a Geometric distributions, rand will return different types depending on the bounds chosen. For example, values sampled from truncated(Geometric(0.6); lower = 2) are floats, while values sampled from truncated(Geometric(0.6); upper = 4) are integers.
I guess this is due to

function rand(rng::AbstractRNG, d::Truncated)
d0 = d.untruncated
tp = d.tp
lower = d.lower
upper = d.upper
if tp > 0.25
while true
r = rand(rng, d0)
if _in_closed_interval(r, lower, upper)
return r
end
end
elseif tp > sqrt(eps(typeof(float(tp))))
return quantile(d0, d.lcdf + rand(rng) * d.tp)
else
# computation in log-space fixes numerical issues if d.tp is small (#1548)
return invlogcdf(d0, logaddexp(d.loglcdf, d.logtp - randexp(rng)))
end
end
using the float-returning quantile / invlogcdf in special cases.

@devmotion
Copy link
Member

I think the problem are actually quantile and invlogcdf here. For Geometric they should always return an Int. For other distributions (and in particular discrete distributions) typically we ensure that the return type of quantile etc. and the type of the variates match. For instance, for discrete distributions where such calculations are performed with StatsFuns/Rmath we explicitly convert the result to Int.

@devmotion devmotion linked a pull request Nov 23, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants