Skip to content
Open
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
2 changes: 2 additions & 0 deletions vose_sampler/vose_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class VoseAlias(object):

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!

raise ValueError("Probability distribution must sum to 1.")
self.dist = dist
self.rng = rng or random.Random()
self.alias_initialisation()
Expand Down