[issue38881] unexpected behaviour of random.choices with zero weights
Iza Romanowska added the comment: Many thanks for patching it! Much appreciated. -- ___ Python tracker <https://bugs.python.org/issue38881> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue38881] unexpected behaviour of random.choices with zero weights
Iza Romanowska added the comment: Hi, Many thanks for engaging with this. I agree that we should very clearly separate negative weights from zero weights. A negative number is illegal and that's the end of it. However, a zero weight is not illegal, e.g., [0, 0, 0, 0.1] is a legal sequence to pass as weight. 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: - this seems to be the standard for other programming languages (I've checked for R and NetLogo but this should be confirmed by others); - a weight sequence [1, 1, 1, 1] is equivalent to [10, 10, 10, 10] so if we don't want to make [0, 0, 0, 0] 'a special case' it should give the same behaviour (equal probability); - when a weight sequence is not provided (i.e., there are no odds given) a random selection is made. One can argue that the odds [] are similar to [0, 0, 0, 0 ]. Perhaps the zero weights option could be pushed into the if-loop of no weights? 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. -- ___ Python tracker <https://bugs.python.org/issue38881> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue38881] unexpected behaviour of random.choices with zero weights
Iza Romanowska added the comment: 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: A genome may consist of a set of genes none of which increases fitness thus their relative probability of being copied over other genes is all zero. In political sciences or cultural evolution: A voter may hate all parties (ie. their individual preference for any one party is zero). An agent may happen to have no preference for either of the options. In engineering: All solutions may carry zero increase in performance. 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. This should probably be consulted with a mathematician who knows the definition of the algorithm. -- ___ Python tracker <https://bugs.python.org/issue38881> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue38881] unexpected behaviour of random.choices with zero weights
New submission from Iza Romanowska : Hi, When zero weights are given, the last element of a sequence is always chosen. Example: hits= [] for i in range(100): hits.append(random.choices(["A","B","C","D"], [0, 0, 0, 0])[0]) print (set(hits)) >> {'D'} 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. -- components: Library (Lib) messages: 357185 nosy: IRomanowska priority: normal severity: normal status: open title: unexpected behaviour of random.choices with zero weights type: behavior versions: Python 3.7 ___ Python tracker <https://bugs.python.org/issue38881> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com