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
unexpected behaviour of random.choices with zero weights #83062
Comments
Hi, hits= []
for i in range(100):
hits.append(random.choices(["A","B","C","D"], [0, 0, 0, 0])[0])
print (set(hits))
I guess that most users would expect that in case of zero weights it will default into a random.choice behaviour and select one option at random since this is what happens in cases when all weights are equal. Alternatively, it should return an empty array if the assumption was that all choices have a zero probability of being selected. Either way, if it is consistently choosing one option, this may be potentially difficult to spot in situations when a sequence of weights all equal to zero only happen sporadically. |
Given non-sensical input, that behavior is as reasonable as any other (fwiw, the same is also observed with all negative weights, even if the negative weights are unequal). The documentation currently says, "weights are assumed to be non-negative." Perhaps it should say, "weights are assumed to be non-negative and have at least one positive weight." |
Alternatively, we could raise an exception if the weight total isn't positive. Am not sure that has any real worth, but it could be done with only a small additional cost to the critical path. |
Dear Raymond, I understand that passing all zero weights may look nonsensical but random.choices is an implementation of the roulette wheel which is widely used across different scientific disciplines and the situation of passing all zeros is completely plausible. In genetics: In political sciences or cultural evolution: In engineering: You are absolutely right that negative weights make no sense (how can you choose option A with a -10% chance. But a 0% chance is entirely possible. I consulted with colleagues working in other languages and it looks that the default for roulette wheel with zero weights is choosing at random. |
IIUC, there was a similar discussion on negative weights and all weights being zero to raise ValueError at https://bugs.python.org/issue18844#msg196252 |
xtreak, the other discussion isn't similar at all. The OP is proposing that weights all equal to zero be a well-defined way to specify an equiprobable selection. That is a completely new proposal which is easy to implement, but doesn't make much sense to me. The current concept is that the weights express an odds ratio where a zero weight means that an event has no chance of being selected. This view implies that if all weights are zero, the result is undefined (or an error). Iza, it seems to me that the provided examples are conflating a zero incremental preference with a zero chance of occurrence. |
Hi, Many thanks for engaging with this. Raymond, I agree with you that this is conflating incremental preference with zero chance of occurring. From a standard user perspective, if the [0, 0, 0, 0.1] sequence is passed as weights the first three options have a zero probability of selection thus that interpretation (even if in your opinion erroneous) is very likely to happen for most of the users. I think we all agree that an output that always chooses the last element of the sequence is not ok. We differ in opinion as to what should happen instead: raising an error or returning a value at random. My arguments for the latter are:
I see the logic of the second solution, i.e., raising an error. It may make it more difficult to catch the issue for those doing simulations but at least it's not giving a wrong result. As mentioned this is a key algorithm for many scientific applications with predominantly non-computer science users like myself. So please do take into consideration that it will be often used naively. Many thanks. |
There are a number of "obvious" properties that should obtain, given the intended meaning of weights:
Especially because of the 3rd, the only sensible thing to do is to treat an input with weights all 0 much like an empty population - although, in context, ValueError would make more immediate sense for "all weights are 0" than the IndexError raised for an empty population. Anything other than that is "too clever" by half, and I just don't believe would be of real use often enough to be worth violating the "obvious" properties above. |
Either raising, or treating a zero-weight-sum as undefined behaviour and documenting that the sum of the weights should be positive sounds fine to me. -1 on the suggestion to (deliberately, by documented design) choose at random in this case. Mathematically, this situation doesn't make sense: as Tim said, it's analogous to choosing from an empty population. Ex: you have random.choices(["red", "blue"], [nred, nblue]) does the job. But in the case where |
Thanks for the suggestions. |
Many thanks for patching it! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: