Skip to main content
Join the official Python Developers Survey 2018 and win valuable prizes: Start the survey!

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

Project description

ReadMe
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

Logic/Method:
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

argument:
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
[3,0]
[4,0]

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

#===================================================================

Example:
1-Dim case:
def cosine (input):
return abs(math.cos(input))
gen = PDRandom.PDRandom(cosine, -1, math.pi, 10,dimension=1)
gen.OutputGenCountList(100000,50,"consine2")

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))
gen.OutputGenCountList(100000,[50,50,50],"sine3")


misc:
contact: devyat192002@gmail.com
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.

Filename, size & hash SHA256 hash help File type Python version Upload date
PDRandom-1.0.2.tar.gz (6.9 kB) Copy SHA256 hash SHA256 Source None Apr 28, 2015
PDRandom-1.0.2.zip (7.6 kB) Copy SHA256 hash SHA256 Source None Apr 28, 2015

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page