Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21929

Re: Distribution

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'python.': 0.04; 'situation.': 0.04; 'suppose': 0.05; 'type,': 0.07; 'python': 0.08; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'underlying': 0.09; 'output': 0.10; 'am,': 0.12; '10.0': 0.16; '10}': 0.16; 'enigma': 0.16; 'exponential': 0.16; 'kern': 0.16; 'matches.': 0.16; '(i.e.': 0.17; 'wrote:': 0.18; 'skip:[ 20': 0.19; 'header:In-Reply-To:1': 0.22; 'assume': 0.22; 'event,': 0.23; 'module,': 0.23; 'defined': 0.24; 'times,': 0.24; 'code': 0.26; 'figure': 0.26; 'all,': 0.27; 'import': 0.27; 'server.': 0.28; 'variable': 0.28; 'project,': 0.28; 'random': 0.28; 'reaches': 0.28; 'second': 0.28; 'interpret': 0.29; 'mix': 0.29; 'module.': 0.29; 'handling': 0.30; '"in': 0.30; 'second,': 0.30; 'skip:b 30': 0.31; 'requests': 0.32; 'header:User-Agent:1': 0.33; 'object': 0.33; 'header:X-Complaints-To:1': 0.34; 'loop': 0.34; 'rather': 0.34; 'arrival': 0.34; 'to:addr:python-list': 0.35; 'project': 0.35; "we're": 0.36; 'starting': 0.36; 'two': 0.36; 'received:org': 0.36; 'similar': 0.37; 'using': 0.37; 'another': 0.37; 'rate.': 0.38; 'event': 0.38; 'some': 0.38; 'doing': 0.38; 'getting': 0.38; 'should': 0.38; 'same.': 0.39; 'to:addr:python.org': 0.40; 'type': 0.61; 'total': 0.61; 'your': 0.61; 'world': 0.61; 'kind': 0.62; 'received:86': 0.63; 'our': 0.63; 'here': 0.64; 'strategy': 0.64; 'believe': 0.65; 'exact': 0.68; 'sum': 0.74; 'terminology': 0.84; 'uniform': 0.84; '0.0': 0.91; 'eco': 0.91; 'sum,': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robert Kern <robert.kern@gmail.com>
Subject Re: Distribution
Date Tue, 20 Mar 2012 12:52:46 +0000
References <33044d51-c481-4796-a722-406f412cf994@x7g2000pbi.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host86-147-15-181.range86-147.btcentralplus.com
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2
In-Reply-To <33044d51-c481-4796-a722-406f412cf994@x7g2000pbi.googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.827.1332247978.3037.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332247978 news.xs4all.nl 6880 [2001:888:2000:d::a6]:43005
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21929

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Distribution "prince.pangeni" <prince.ram85@gmail.com> - 2012-03-19 21:31 -0700
  Re: Distribution Peter Otten <__peter__@web.de> - 2012-03-20 10:15 +0100
  Re: Distribution Ben Finney <ben+python@benfinney.id.au> - 2012-03-20 22:21 +1100
    Re: Distribution Robert Kern <robert.kern@gmail.com> - 2012-03-20 12:00 +0000
    Re: Distribution Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-03-20 11:29 -0400
    Re: Distribution Laurent Claessens <moky.math@gmail.com> - 2012-03-20 18:00 +0100
    Re: Distribution Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-03-20 13:47 -0400
  Re: Distribution Robert Kern <robert.kern@gmail.com> - 2012-03-20 12:52 +0000
  Re: Distribution duncan smith <buzzard@urubu.freeserve.co.uk> - 2012-03-20 20:19 +0000

csiph-web