Search code examples
pythonpoissonagent-based-modeling

agents arrival according to poisson process


I am trying to achieve agents arrival in my model according to a poisson process. I know from data that on average 230 agents arrive per day (or 9.583 agents/hr or 0.1597/minute). In the simulations, now I need to use this information to add agents. One simulation time step is equal to 5 minutes (real time) and if if we calculate from data, then on average 0.7986 agents should be added to simulation every time step to achieve an average of 230 per day. But how could I do this? I cannot use 0.7986 per time step because I need integer number to add agent. If I round off 0.7986 to 1, then I over estimate this.

It is clear that we cannot add agent every time step but I have no clue how to select a time step in which an agent must be added. If I know which time step I need to select to add an agent, I can do that easily. Does any one know how to do this in Python? I tried the below code but cannot really understand what it is actually

for i in range(1,12): # 1 simulation time step is equal 5min, so this loops covers 1 hour. 
    time=int(random.expovariate(1/0.7986))

I do not really understand the above code as it produces quite different numbers. Any help please.


Solution

  • If agent arrivals is a Poisson process then the time between individual agent arrivals has an exponential distribution. That's what the code you provided generates, but is only useful if you are using continuous time with discrete event scheduling. With time-step as the time advance mechanism, you actually just want to stick with the Poisson distribution, adjusting the rate to match your time-step interval size, which you've already done.

    import numpy
    
    last_step = 12 * 24   # to simulate one day, for example
    rate = 230.0 / last_step
    for time_step in range(1, last_step + 1):
        number_of_new_agents = numpy.random.poisson(rate)
        for new_agent_number in range(number_of_new_agents):
            # do whatever you want at this point
    

    Note that the number_of_new_agents will often be 0, in which case the inner loop will iterate zero times.