Skip to main content

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.

Source Distributions

PDRandom-1.0.2.zip (7.6 kB view details)

Uploaded Source

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

Uploaded Source

File details

Details for the file PDRandom-1.0.2.zip.

File metadata

  • Download URL: PDRandom-1.0.2.zip
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PDRandom-1.0.2.zip
Algorithm Hash digest
SHA256 6c0e4054ac7e641e4125dffdfaa92d197238543fff348e382a1113bef5cc4de6
MD5 e990dc2a252a971510e088692d58e859
BLAKE2b-256 5b7e1e71f1cd1083a3e11e38d1352d544b2312ecef528fa5cd2fb47ddcd8bc57

See more details on using hashes here.

File details

Details for the file PDRandom-1.0.2.tar.gz.

File metadata

  • Download URL: PDRandom-1.0.2.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PDRandom-1.0.2.tar.gz
Algorithm Hash digest
SHA256 6627350e985968bf907f8ace54b46376a2e68b81108582ee6738bd707cce2fbc
MD5 3bfd560d8fb5b456ed7bd810b14d5290
BLAKE2b-256 2b86c6da44904f64338cafd05e614cf5914b68b6c54141f67f68ea22a1988959

See more details on using hashes here.

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