Smarter way of doing this?

Anton Vredegoor anton at vredegoor.doge.nl
Thu Feb 5 05:09:22 EST 2004


Max M <maxm at mxm.dk> wrote:

>I solved it by using acumulated probabilities instead. So the final 
>version is here, if anybody cares.
>
>I used it in a Markov chain class, that is now pretty fast.

Yes it's fast, but I don't think it's smart :-) Unfortunately I
haven't got a working solution as an alternative, but only some code
that "explains" what I mean:


    def choice_generator(population,probabilities):
        PC = zip(probabilities,population)
        while 1:
            p,c = max([(p*random(),c) for p,c in PC])
            yield c

This is short and fast. However, the distribution of the outcomes is
wrong, because the list of probabilities should be "adjusted" so that
in the end the *outcomes* are distributed according to the
"probabilities". Or should that be proportions? 

My statistics skills are a bit rusty, but I think it's an interesting
problem. I'll get back when I have some more ideas. In the meantime
more mathematically educated readers are given a chance to beat me to
it :-), or prove it can't be done.

Anton



More information about the Python-list mailing list