-
Notifications
You must be signed in to change notification settings - Fork 204
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
choosing a random element with weighted probability? #123
Comments
I think what you're describing is part of the random module: https://docs.python.org/3/library/random.html#random.choices |
I don't think that will work very well in my case. I have a large collection of objects, each with a weight associated with them. I am continually adding and removing things to this collection. The version you've linked to will recalculate the cumulative sum each time it is called, or I have to update the cumulative sum each time I add/remove things:
This is too slow for my purposes. I've got a better approach than that already, but I know an even better algorithm if I can guarantee that my weights are in decreasing order. |
Hmm, I was just trying to implement my "better algorithm" which was an adaptation of "reservoir sampling" to take advantage of the fact that we know the weights are ordered. There is an unexpected subtlety that is blocking my initial attempt. I think it can be fixed, but I'm going to have to set it aside for a bit before I can devote the time it'll need. If I manage to work it out, I'll try to share it with you for sortedcontainers. I think it would be a valuable contribution. |
Okay, so I've got some working code, and comparisons with I believe it does almost as well as My example does not need to know the cumulative sum to work, so it is clearly better in cases where we cannot keep track of the cumulative sum.
|
I don’t think this belongs in sortedcontainers. It’s an interesting use case but the topic is niche and result can be achieved with the existing api. There might be a place for it in sortedcollections or some other recipes-oriented package/post. |
Reference #110 |
For reference, sorted collections is at https://github.com/grantjenks/python-sortedcollections |
Let's say I have a collection of many objects, each with a weight associated with it. I'd like to be able to choose one of these at random, with probability proportional to its weight.
Is this currently built in to sortedcontainers? Basically given an ordered list of (non-negative) numbers, I'd like to return a random index chosen with probability proportional to the corresponding number.
I have some ideas about how this could be done, but first I want to check if it's doable already before I put in the effort to see if my ideas actually work (and are faster than what I've already implemented using a different data structure).
The text was updated successfully, but these errors were encountered: