Skip to main content

python module for generating random numbers according to a given probability density function

Project description

PDRandom by Ken Leung
The MIT License (MIT)
Copyright (c) 2015 Ken Leung
###Update 1.0.2####
New Feature:
1. support multiprocessing
for function: RandList, GetCountList, GenCountList, OutputRawRandom, OutputGenCountList,
You can set argument nproc=NUM_OF_PROCESS
e.g. gen.OutputGenCountList(int(1e6),[100,100],"cosine2",nproc=4,foption='a')
**for windows users, u should start the program with if __name__=="__main__":

2. you can append the existing data files
set foption='a' for appending
*for appending a countlist file, the binNum should be matched with the file

3. new function: GenCountList(number,binNum,nproc=1) ---- directly generate and return a countlist

misc: tidy up

*any bugs, tell me via email or github

#===== Information =======
Generate random numbers according to your probability function, e.g. sin(x), cos(x) , sin(x)sin(y)
f: R^n -> R

Use acceptance-rejection method, Step Function to be the bound function

How TO Use:
*** for dimension > 1 (multivariable function) , please pass list arguments

1. Define Your probability density function, e.g.
def test(input):
return abs(math.sin(input))

*If it is multivariable function, please pass a list argument. e.g.
test([1,2]) # for 2D function

2. create a PDRandom object
ranObject = PDRandom.PDRandom(test, lowerBound, UpperBound, NumDiv, NumSubDiv, dimension )

# test: your density function
# lowerBound, UpperBound: x ϵ [lowerBound,UpperBound)
# for dim >1 : x0 ϵ [lowerBound[0],UpperBound[0]), x1 ϵ [lowerBound[1],UpperBound[1]) , ..............
# NumDiv: divide the [UpperBound - LowerBound] into numbers of divisions for the Step function (bound function)
# NumDiv >=1 . NumDiv increases -> Rejection Rate decreases (i.e. increases the acceptance chance of random numbers) ,
# BUT large NumDiv would cause performance hit
# * this parameter related to the performance(Speed)
# NumSubDiv (Optional): used to find the maximum value in a division, related to the accuracy, and initialization time.
# e.g. 1000 or [10,10,10]
# Dimension: specify how many input varibles

3. Some Class Function You can Use:
Next(): return one random Number

RandList(num): Return a list of random numbers

GetCountList(binNum, randlist):
#To count the numbers of random numbers within the range for bins (divisions)
# if binLowerBound <= randomNum < binUpperBound, randomNum will be counted for the bin with value = binLowerBound
#inclusive lowerbound, exclusive upper

binNum: divide the range into binNum bins
randlist: generated by RandList(num)

return a countlist,e.g. [ [bin1x, bin1y ,count], [bin2x,bin2y,count] ....... ] for two variable function
e.g. randlist = [1.1, 2.4] # lowerBound = 0, UpperBound =5 , binNum = 5 , dimension=1
countlist= [ [0,0]
[1,1] # 1.1 counted for 1
[2,1] # 2.4 counted for 2

] # total 5 number of bins

OutputCountList(countlist, filename) : output to a file with filename (space sperate format)

OutputRawRandom(number, filename): generate rand numbers to a file with filename

OutputGenCountList (number , binNum, filename): generate and directly output the count list


1-Dim case:
def cosine (input):
return abs(math.cos(input))
gen = PDRandom.PDRandom(cosine, -1, math.pi, 10,dimension=1)

n-Dim case: n>1
def sine3(input):
return abs(math.sin(input[0])*math.sin(input[1])*math.sin(input[2]))
gen = PDRandom.PDRandom(sine3, [-1,0,1], [math.pi,math.pi,math.pi], [10,10,10],dimension=3)
lis = gen.RandList(1000)
print (gen.GetCountList([10,10,10], lis))

feel free to contact me if any question, bug, opinion, I will response as soon as possible

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions (7.6 kB view hashes)

Uploaded source

PDRandom-1.0.2.tar.gz (6.9 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page