0

I have an excel file with different cases and about 75 events for each case. There is a probability of each event happening given each case. So the excel file looks like this:

       event1  event2 ...   event75    

case1  0.0343  0.0234 ...   0.1194

case2  0.0924  0.0532 ...   0.0023

I want to generate a random number in python, ranging from 0 to 75 with the probabilities of the given case. When I worked with cases with only 2 events that could occur, I used a code like this:

excel_cases = pd.read_csv('/.../cases.csv')

def event_probabilities(case):
event_probability = excel_cases.loc[excel_cases['Case'] == case, 'event1'].values[0]
return event_probability

event1_probability = event_probabilities("case1")
np.random.choice(np.arange(1, 3), p=[event1_probability, (1 - event1_probability)])
# can also do an event2_probability instead of 1-event1_probability

However when the range of events get bigger I don't know what would be a more efficient way of doing this. Thanks for the help.

*Not same as the other question as I'm getting the probabilities from Excel

memokerobi
  • 143
  • 10

1 Answers1

0

So it looks like you are going to 4 decimals of precision right? So what you could do is get a random number between 0 and 9999:

from random import randint
randomNum = randint(0,9999))

And then multiply each probability by 10000 and set up a bunch of elif statements for each switch. So if event_1 has a probability of 0.0045 and event 2 has a probability of .0100, the elif would look something like this.

if randomNum < 45:
    event_1()
elif randomNum > 45 && randomNum < 145:
    event_2()
..........

and so on. This shouldn't require anywhere near as many calls to random. Though setting it up might not be trivial.

Benjamin Commet
  • 350
  • 3
  • 8
  • It would take a long time to do this, it's not very practical, just like the method I used. I am looking for a simpler way, don't know if there is one. – memokerobi May 31 '17 at 21:04
  • It wouldn't take too long if you generated the code programmatically, but that is a whole different task all together. – Benjamin Commet May 31 '17 at 21:09