yat011 / pdrandom Goto Github PK
View Code? Open in Web Editor NEWpython script for generating random numbers according to a given probability density function
License: MIT License
python script for generating random numbers according to a given probability density function
License: MIT License
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') 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: [email protected] feel free to contact me if any question, bug, opinion, I will response as soon as possible
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.