python module for generating random numbers according to a given probability density function
Project description
ReadMe
PDRandom by Ken Leung
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")
Mis:
contact: devyat192002@gmail.com
feel free to contact me if any question, bug, opinion, I will response as soon as possible
PDRandom by Ken Leung
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")
Mis:
contact: devyat192002@gmail.com
feel free to contact me if any question, bug, opinion, I will response as soon as possible
Project details
Release history Release notifications | RSS feed
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.1.zip
(6.1 kB
view hashes)
PDRandom-1.0.1.tar.gz
(5.5 kB
view hashes)