473,805 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

random numbers according to user defined distribution ??

Hi everybody,

I wonder if it is possible in python to produce random numbers
according to a user defined distribution?
Unfortunately the random module does not contain the distribution I
need :-(

Many thanks

axel
Aug 6 '08 #1
11 11573
On Thursday 07 August 2008 00:02, Alex <ax*********@ru b.dewrote:
Hi everybody,

I wonder if it is possible in python to produce random numbers
according to a user defined distribution?
Unfortunately the random module does not contain the distribution I
need :-(

Many thanks

axel
I'm not aware of any module with that specific function, but it's
algorithmically not too complex I'd think. If you're writing an application
that does this I'll assume that you have a basic gist of how to implement
it ;). It's been a while since I messed with the subject, so I'm not
getting any further than graphs in my head right now. Good luck!

--
Dominic van Berkel
"Bi-la Kaifa"
Aug 6 '08 #2
On Wed, 06 Aug 2008 15:02:37 -0700, Alex wrote:
Hi everybody,

I wonder if it is possible in python to produce random numbers according
to a user defined distribution? Unfortunately the random module does not
contain the distribution I need :-(

This is a strange question. Of course you can -- just write a function to
do so! Here's some easy ones to get you started:

from __future__ import division
import random, maths

def unbounded_rand( p=0.5):
"""Return a random integer between 0 and infinity."""
if not (0 < p <= 1):
raise ValueError
n = 0
while random.random() < p:
n += 1
return n

def pseudonorm():
"""Return a random float with a pseudo-normal distribution.

The probability distribution is centered at 0 and bounded
by -1 and +1.
"""
return (sum([random.random() for i in range(6)])-3)/3

def triangular(min= 0, max=1, mode=0.5):
"""Return a random float in the range (min, max) inclusive
with a triangular histogram, and the peak at mode.
"""
u = random.random()
if u <= (mode-min)/(max-min):
return min + math.sqrt(u*(ma x-min)*(mode-min))
else:
return max - math.sqrt((1-u)*(max-min)*(max-mode))

def linear():
"""Return a random float with probability density
function pdf(x)=2x.
"""
return math.sqrt(rando m.random())

There's no general way to create a random function for an arbitrary
distribution. I don't think there's a general way to *describe* an
arbitrary random distribution. However, there are some mathematical
techniques you can use to generate many different distributions. Google
on "transforma tion method" and "rejection method".

If you have a specific distribution you are interested in, and you need
some help, please ask.

--
Steven
Aug 7 '08 #3
On Aug 6, 8:26*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
On Wed, 06 Aug 2008 15:02:37 -0700, Alex wrote:
Hi everybody,
I wonder if it is possible in python to produce random numbers according
to a user defined distribution? Unfortunately the random module does not
contain the distribution I need :-(

This is a strange question. Of course you can -- just write a function to
do so! Here's some easy ones to get you started:
...
>
There's no general way to create a random function for an arbitrary
distribution. I don't think there's a general way to *describe* an
arbitrary random distribution.
What about the quantile function?
Aug 7 '08 #4
Alex <ax*********@ru b.dewrites:
I wonder if it is possible in python to produce random numbers
according to a user defined distribution?
That can mean a lot of things. The book "Numerical Recipes" (there
are editions for various languages, unfortunately not including Python
last time I looked) has some discussion about how to do it.

Aug 7 '08 #5
On Wed, 06 Aug 2008 21:09:30 -0700, Dan Bishop wrote:
>There's no general way to create a random function for an arbitrary
distribution . I don't think there's a general way to *describe* an
arbitrary random distribution.

What about the quantile function?

Well, sure, if you can write down the quantile function, c.d.f or p.d.f.
of a distribution, I suppose that counts as describing it, in some sense.
But even if we limit ourselves to distributions which are actually
useful, as opposed to arbitrary distributions that can't be described in
terms of any known mathematical function, there are serious practical
difficulties. I quote from the Wikipedia article on quantile functions:

"The quantile functions of even the common distributions are relatively
poorly understood beyond the use of simple lookup tables, which is at
odds with their importance in Monte Carlo sampling, where a sample from a
given distribution may be obtained in principle by applying its quantile
function to a sample from a uniform distribution. The exponential case
above is one of the very few distributions where there is a simple
formula."

http://en.wikipedia.org/wiki/Quantile_function
--
Steven
Aug 7 '08 #6
On Aug 6, 3:02*pm, Alex <axel.kow...@ru b.dewrote:
I wonder if it is possible in python to produce random numbers
according to a user defined distribution?
Unfortunately the random module does not contain the distribution I
need :-(
Sure there's a way but it won't be very efficient. Starting with an
arbitrary probability density function over some range, you can run it
through a quadrature routine to create a cumulative density function
over that range. Use random.random() to create a uniform variate x.
Then use a bisecting search to find x in the cumulative density
function over the given range.

from __future__ import division
from random import random

def integrate(f, lo, hi, steps=1000):
dx = (hi - lo) / steps
lo += dx / 2
return sum(f(i*dx + lo) * dx for i in range(steps))

def make_cdf(f, lo, hi, steps=1000):
total_area = integrate(f, lo, hi, steps)
def cdf(x):
assert lo <= x <= hi
return integrate(f, lo, x, steps) / total_area
return cdf

def bisect(target, f, lo, hi, n=20):
'Find x between lo and hi where f(x)=target'
for i in range(n):
mid = (hi + lo) / 2.0
if target < f(mid):
hi = mid
else:
lo = mid
return (hi + lo) / 2.0

def make_user_distr ibution(f, lo, hi, steps=1000, n=20):
cdf = make_cdf(f, lo, hi, steps)
return lambda: bisect(random() , cdf, lo, hi, n)

if __name__ == '__main__':
def linear(x):
return 3 * x - 6
lo, hi = 2, 10
r = make_user_distr ibution(linear, lo, hi)
for i in range(20):
print r()

--
Raymond
Aug 7 '08 #7
Thanks for the many answers.

So basically I have to get the inverse of the CDF and use this to
transform my uniformly distributed random numbers. If my desired
distribution is simple I can get an analytical solution for the
inverse, otherwise I have to use numerical methods.

Okay, things are now much clearer.

Many thanks,

Alex
Aug 7 '08 #8
Alex wrote:
Thanks for the many answers.

So basically I have to get the inverse of the CDF and use this to
transform my uniformly distributed random numbers. If my desired
distribution is simple I can get an analytical solution for the
inverse, otherwise I have to use numerical methods.

Okay, things are now much clearer.
It's worth noting that unless if the PPF (the inverse of the CDF) is very
straightforward , this method is not very good. The numerical errors involved
cause very poor results in the tails of many distributions. Typically, rejection
sampling, if done well, will work much better. There are techniques for doing
this on nearly-arbitrary distributions:

http://statmath.wu-wien.ac.at/projects/arvag/index.html

If you implement any of these techniques in Python, I would love to see them.

--
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

Aug 7 '08 #9
Alex wrote:
I wonder if it is possible in python to produce random numbers
according to a user defined distribution?
Unfortunately the random module does not contain the distribution I
need :-(
There exist some very general algorithms to generate random numbers
from arbitrary distributions.

The most notable of these are "Markov Chain Monte Carlo", e.g. the
Metropolis-Hastings algorithm. It is very easy to implement in any
programming language. The nature MCMC algorithms makes it inefficient
when implemented in pure Python. But you can get tremendous speedup by
simulating multiple Markov chains in parallel, by means of vectorizing
with NumPy.

A relative of Metropolis-Hastings which may also be applicable to your
problem is pure "rejection sampling". It is far less efficient, but
produces no autocorrelation in the samples.

If you can generate random deviates from the marginal distributions,
but need to sample the joint distribution, look into using the Gibbs
sampler. It is an efficient version of Metropolis-Hastings for this
special case.

http://en.wikipedia.org/wiki/Metropo...ings_algorithm
http://en.wikipedia.org/wiki/Rejection_sampling
http://en.wikipedia.org/wiki/Gibbs_sampling

Aug 8 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
13116
by: mescaline | last post by:
hi, i'm new to C++ could anyone refer me to a good site / good examples of random numbers? in particular including: 1) the commnds to obtain normally and exponenetially distributed r numbers
2
3396
by: abhinay07 | last post by:
Hi, I would like to generate random values which are in bimodal distribution. If I can give the two peak means and the variance values, can I get any function to generate them from the uniform random values. I have seen how to generate gaussian by BoxMuller method. But am not finding any way to generate the numbers using bimodal distribution..
70
6291
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like using the high-order bits returned by rand() instead of the low-order bits, avoiding using rand() for anything that wants decently random numbers, not using rand() if you want more than approx. UINT_MAX total different sequences, and so on. So I...
21
3030
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX. Thank you for your help
13
4240
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number between 0 and 100? (call rand() only 10 times...) Thanks, qq
13
3619
by: Jon Agiato | last post by:
Hello, I am sure this problem is easy to spot but I have been at this project all day and the frustration has finally overcome me. I am using this function in order to produce a standard normal distribution random number generator and then using the function in another part of my program in order to use the value. This function is called for many iterations. The problem is, through each run I am getting the exact same number generated...
14
9914
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up to 1 . I am not sure how to do this. Any idea? Thanks.
9
9959
by: MyInfoStation | last post by:
Hi all, I am a newbie to Python and would like to genereate some numbers according to geometric distribution. However, the Python Random package seems do not have implemented functionality. I am wondering is there exist any other libraries that can do this job? Thanks a lot, Da
13
2819
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num )
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.