Most pythonic way of weighted random selection



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dear list,

who's got aesthetic advice for the following problem? I've got some joint probabilities of two distinct events Pr(X=x, Y=y), stored in a list of lists of floats, where every row represents a possible outcome of X and every float in a row a possible outcome of Y (which I will now call my matrix, altough I can't use numpy or numeric for this), so e.g. m = [[0.2, 0.4, 0.05], [0.1, 0.05, 0.2]]. All lists in the list are equally long and the values of the flattened list add up to 1.0, i.e. sum([sum(row) for row in m]) == 1. In practice, this 'matrix' is about 20x40, i.e. a list with 20 lists á 40 floats each.

Now the task is to select one outcome for X and Y based on the joint probabilites, and afterwards check that the outcomes fullfill certain criteria. If it doesn't fulfill some criteria a new pair of outcomes has to be selected, for other criteria it will still be selected with a certain probability. My approach was to choose a random number, and then walk through the list adding the values together until the accumulated sum is greater than my random threshold:

import random
r = random.random()
s = 0.0
p = 0.2 # probability of selecting outcome if it doesn't fulfill criterion 2
break_loop = False
while not break_loop:
for row_index in range(len(m)):
for col_index in range(len(row)):
s += m[row_index][col_index]
if s >= r:
if not fulfills_criterion_a(row_index, col_index):
break_loop = True
elif not fulfills_criterion_b(row_index, col_index):
if random.random() <= p:
return row_index, col_index
else:
break_loop = True
else:
return row_index, col_index
if break_loop: break
if break_loop: break
break_loop = False


Now that looks plain ugly, and I wonder whether you might find a slightly more elegant way of doing it without using numpy and the like.

Bests,
Manuel
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFIuWoncZ70OCIgLecRArV4AJ9ynhC/McegMIYTWOOOW4p44t3rWgCbBjvm
1JRHy5kp1qIGLDaCTXXFcSs=
=X6Sv
-----END PGP SIGNATURE-----
.



Relevant Pages

  • Re: Tom Ogilvys VBA code
    ... One thing I Noticed is that if you Change the Criteria and Run it again ... Enter this in cell A2 ... which is pending in this code is that it also lists duplicates which I ... remove the conditional formatting in the range AC11:AL11 the total time ...
    (microsoft.public.excel.programming)
  • Re: The 60s Rule (was Re: Sitcoms That Defined the Decades)
    ... It's your criteria, WQ. ... have been obvious before that that there were multiple factors ... on several top 10 lists at sitcomsonline, ... It's a sitcom popularity ...
    (rec.arts.tv)
  • Re: default value for combo box based on another combo box
    ... then add the speaker as an extra column in your event selecting ... I think your solution minus the requery will work. ... On the form is a combo box that lists the events ...
    (microsoft.public.access.formscoding)
  • Re: Mail Merge and Outlook Contacts
    ... > use your Outlook distribution lists as mail merge data sources. ... >> You must do your filtering and selecting in Outlook to use the Categories ... I need to have contacts in multiple sub folders. ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Mail Merge and Outlook Contacts
    ... > use your Outlook distribution lists as mail merge data sources. ... >> You must do your filtering and selecting in Outlook to use the Categories ... I need to have contacts in multiple sub folders. ...
    (microsoft.public.outlook.contacts)