I have N processes and a different Poisson rate for each. I would like to simulate arrival times from all N processes. If N =1 I can do this
t = 0
N = 1
for i in range(1,10):
t+= random.expovariate(15)
print N, t
However if I have N = 5 and a list of rates
rates = [10,1,15,4,2]
I would like somehow for the loop to output the arrival times of all N processes in the right order. That is I would still like only two numbers per line (the ID of the process and the arrival time) but globally sorted by arrival time.
I could just make N lists and merge them afterwards but I would like the arrival times to be outputted in the right order in the first place.
Update. One problem is that if you just sample a fixed number of arrivals from each process, you get only early times from the high rate processes. So I think I need to sample from a fixed time interval for each process so the number of samples varies depending on the rate.
If I'm understanding you correctly:
import random
import itertools
def arrivalGenerator(rate):
t = 0
while True:
t += random.expovariate(rate)
yield t
rates = [10, 1, 15, 4, 2]
t = [(i, 0) for i in range(0, len(rates))]
arrivals = []
for i in range(len(rates)):
t = 0
generator = arrivalGenerator(rates[i])
arrivals += [(i, arrival) \
for arrival in itertools.takewhile(lambda t: t < 100, generator)]
sorted_arrivals = sorted(arrivals, key=lambda x: x[1])
for arrival in sorted_arrivals:
print arrival[0], arrival[1]
Note that your initial logic was generating a fixed number of arrivals for each process. What you really want is a specific time window, and to keep generating for a given process until you exceed that time window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With