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
Add an implementation of alias method for weighted indices #692
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c2bed15
Added an implementation of alias method for weighted indices
zroug 1feb633
Added tests for AliasMethodWeightedIndex
zroug 002a001
Get rid of the extra VecDeque during creation of AliasMethodWeightedI…
zroug ea83974
Made implementation details of AliasMethodWeightedIndex more generic
zroug f392fb7
Made AliasMethodWeightedIndex generic
zroug 2af10fa
Added documentation for AliasMethodWeightedIndex
zroug 9c44b6a
Addressed documentation issues from review
zroug 8641a9b
Use pairwise sum only for floating point weights
zroug 5b29341
Reorganized distributions::weighted module
zroug 950afb3
Use a instead of an when appropriate
zroug File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively we could make the module public and name these
weighted::{AliasMethodWeight, AliasMethod, Error, BinarySearch}
. Thoughts?(obviously a breaking change)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that would be better and more like it is done in the standard library (
std::io::Error
). Again, let me know if you want to do that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But maybe the module must be renamed because with the proposed naming the 'index' part is lost.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too sure we need to keep
Index
in the name anyway; it's the only type of weighted sampling we have. It's not the best idea to break this stuff again, but still better to get it right than leave a mess IMO.But lets wait for @vks to comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned above, I think we might get rid of our current implementation and only have the alias method. Then there wouldn't be naming issues.
Alternatively, if we decide to keep both, I would prefer to drop the common
AliasMethod
prefix and instead have analias_method
module. This would make it much easier for users to switch between the two implementations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current benchmarks show the alias method to always be in the lead or only slightly behind, however if you move the set-up time into the measurement loop, then the binary-search method can be significantly faster (three times faster on the large_set bench with 1000 samples; nine times faster with 100 samples, and a little faster on the smaller sets).
Memory usage will be a little higher with the Alias method due to the extra
Vec<usize>
; mostly this is unimportant I think (unless memory constrained and having a large set of weights in a small type).The Alias method has some extra requirements on the type, notably
Copy
. Should we useClone
instead?I think there is room for both implementations, though the current presentation and documentation is not ideal. So what do you think about the following structure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed that. In that case we should probably have both, and working with
Clone
would be nicer.This is what I would suggest as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good. @zroug would you make these changes please?
The module documentation should give advice something like the following:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will make these changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did these changes in 5b29341 but I used
alias_method
instead ofalias
as module name. Justalias
didn't sound right to me and the word alias has a much broader meaning. Are you okay with this?I wasn't sure if I should keep the reexports for
WeightedIndex
. I have kept them.