Distribution

Robert Kern robert.kern at gmail.com
Tue Mar 20 08:52:46 EDT 2012


On 3/20/12 4:31 AM, prince.pangeni wrote:
> Hi all,
>     I am doing a simulation project using Python. In my project, I want
> to use some short of distribution to generate requests to a server.
> The request should have two distributions. One for request arrival
> rate (should be poisson) and another for request mix (i.e. out of the
> total requests defined in request arrival rate, how many requests are
> of which type).
>     Example: Suppose the request rate is - 90 req/sec (generated using
> poisson distribution)

Just a note on terminology to be sure we're clear: a Poisson *distribution* 
models the number of arrivals in a given time period if the events are from a 
Poisson *process* with a given mean rate. To model the inter-event arrival 
times, you use an exponential distribution. If you want to handle events 
individually in your simulation, you will need to use the exponential 
distribution to figure out the exact times for each. If you are handling all of 
the events in each second "in bulk" without regard to the exact times or 
ordering within that second, then you can use a Poisson distribution.

> at time t and we have 3 types of requests (i.e.
> r1, r2, r2). The request mix distribution output should be similar to:
> {r1 : 50 , r2 : 30 , r3 : 10} (i.e. out of 90 requests - 50 are of r1
> type, 30 are of r2 type and 10 are of r3 type).
>     As I an new to python distribution module, I am not getting how to
> code this situation. Please help me out for the same.

I am going to assume that you want to handle each event independently. A basic 
strategy is to keep a time variable starting at 0 and use a while loop until the 
time reaches the end of the simulation time. Increment it using a draw from the 
exponential distribution each loop. Each iteration of the loop is an event. To 
determine the kind of event, you will need to draw from a weighted discrete 
distribution. What you want to do here is to do a cumulative sum of the weights, 
draw a uniform number from 0 to the total sum, then use bisect to find the item 
that matches.

import bisect
import random


# Use a seeded PRNG for repeatability. Use the methods on the Random
# object rather than the functions in the random module.
prng = random.Random(1234567890)

avg_rate = 90.0  # reqs/sec

kind_weights = [50.0, 30.0, 10.0]
kind_cumsum = [sum(kind_weights[:i+1]) for i in range(len(kind_weights))]
kind_max = kind_cumsum[-1]

max_time = 10.0  # sec
t = 0.0  # sec
events = []  # (t, kind)
while t < max_time:
     dt = prng.expovariate(avg_rate)
     u = prng.uniform(0.0, kind_max)
     kind = bisect.bisect_left(kind_cumsum, u)
     events.append((t, kind))
     t += dt


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list