Biogeme: Python Library  2.5
simulateDiscreteDistribution.py
1 import random
2 
3 # probabilities must be a dictionary. If the entries are not summing
4 # up to one, they are normalized.
5 
6 def simulateDiscreteDistribution(probabilities):
7  total = 0
8  for i,p in probabilities.items() :
9  total += p
10  if (total != 1.0) :
11  print('Warning: probabilities do not sum up to one. They will be normalized')
12  for i,p in probabilities.items() :
13  probabilities[i] = p / total
14  r = random.random()
15  total = 0
16  for i,p in probabilities.items() :
17  total += p
18  if (r < total) :
19  return i
Copyright 2016 Michel Bierlaire