You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Potentially undefined behaviour when initialised with invalid probability distribution. #7
Ran into potentially undefined behaviour (?) when initialised with a probability distribution that does not sum to 1.
Unless there is a misunderstanding (which should be clarified in the description), I propose to add a simple check such as sum(dist) == 1).
FWIW, in some of my own code I have subclassed VoseAlias and overrode __init__ to perform this check like so:
importvose_samplerfrommathimportiscloseclassMyVoseAlias(vose_sampler.VoseAlias):
def__init__(self, dist, rng=None, prob_sum_tol: float=1e-9):
prob_sum=sum(dist.values())
ifnotisclose(prob_sum, 1., rel_tol=0, abs_tol=prob_sum_tol):
raiseValueError(f'Probabilities did not add up to 1.0 (+/- {prob_sum_tol}). prob_sum={prob_sum}')
super().__init__(dist, rng)
In my testing, I didn't start getting the ValueError thrown until I put about 100 million distinct items -- each with probability 1 / 100M -- into the VoseAlias.
I would be a little wary of putting that code right into VoseAlias without a major version bump, as it could be a breaking change for some people who might be relying (knowingly or otherwise) on the lack of probability sum checking.
If you do put this into VoseAlias, I'd probably replace the raise ValueError with warnings.warn so it doesn't break anyone's existing code.
Might also want to check that all probabilities are between 0 and 1, cuz you could still get around the sum check with something insane like {'H': 1.1, 'T': -0.1} 😂
Thanks for flagging this! I’ve opened a PR to address it: #16. It adds a quick validation check to ensure the provided probability distribution sums to 1, which should prevent the undefined behavior you noticed. Let me know if you have any feedback or if the description needs any additional clarification. Appreciate the catch!
Activity
asmith26 commentedon Nov 11, 2019
Hi @s-ilyas,
Good catch, you are correct that
sum(dist) == 1.Would you like to a submit a PR request?
Many thanks for your contribution!
s-ilyas commentedon Nov 11, 2019
Sure, will do sometime today.
austin-bowen commentedon Feb 13, 2023
FWIW, in some of my own code I have subclassed
VoseAliasand overrode__init__to perform this check like so:In my testing, I didn't start getting the
ValueErrorthrown until I put about 100 million distinct items -- each with probability 1 / 100M -- into the VoseAlias.I would be a little wary of putting that code right into
VoseAliaswithout a major version bump, as it could be a breaking change for some people who might be relying (knowingly or otherwise) on the lack of probability sum checking.If you do put this into
VoseAlias, I'd probably replace theraise ValueErrorwithwarnings.warnso it doesn't break anyone's existing code.austin-bowen commentedon Feb 13, 2023
Might also want to check that all probabilities are between 0 and 1, cuz you could still get around the sum check with something insane like
{'H': 1.1, 'T': -0.1}😂austin-bowen commentedon Feb 13, 2023
Something like:
webbrain-one commentedon Aug 12, 2026
Thanks for flagging this! I’ve opened a PR to address it: #16. It adds a quick validation check to ensure the provided probability distribution sums to 1, which should prevent the undefined behavior you noticed. Let me know if you have any feedback or if the description needs any additional clarification. Appreciate the catch!