Skip to content

fix: Potentially undefined behaviour when initialised with invalid probability distribution. - #16

Open
webbrain-one wants to merge 1 commit into
asmith26:mainfrom
webbrain-one:webbrain/issue-7
Open

fix: Potentially undefined behaviour when initialised with invalid probability distribution.#16
webbrain-one wants to merge 1 commit into
asmith26:mainfrom
webbrain-one:webbrain/issue-7

Conversation

@webbrain-one

Copy link
Copy Markdown

Closes #7

Add a check during initialization to ensure the distribution sums to 1.
This prevents undefined behavior when the sampler is initialized with
an invalid probability distribution.

Closes asmith26#7

def __init__(self, dist, rng=None):
""" (VoseAlias, dict[, RNG]) -> NoneType """
if sum(dist.values()) != 1:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend checking if the sum is close to 1 instead of exactly 1, since we are summing floats, which is unlikely except in simple cases to actually add to exactly 1. See math.isclose(): https://docs.python.org/3/library/math.html#math.isclose

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @austin-bowen, this sounds good to me!

Thanks very much also @webbrain-one for this addition! Should be good to merge once this is added - I've also just added automated checks/GitHub Actions in #17, so you may need to merge main to pick these up.

Thanks again!

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 this pull request may close these issues.

Potentially undefined behaviour when initialised with invalid probability distribution.

3 participants