preface
Random numbers are commonly used. Random functions of various languages are usually used directly, such as random() function in c++, random class in Java, and random module in python. All of them can easily generate random variables, but they have one characteristic, that is, they all follow uniform distribution, while some scenarios need to generate random variables with different distributions.
A random variable
A random variable is a random function that generates a value for each possible event. Let’s say we roll dice and each time we generate a value with a certain probability. There are two types of variables,
- A discrete random variable can have a finite number or an infinite number of different values.
- Continuous random variable, the value can be continuous, in a certain interval can take any real number.
Probability density function
It is mainly used to represent the probability of different points, that is, the probability of each unit length on the X-axis. The probability is equal to the area, and it becomes a point as the length of some interval approaches 0, and the probability is f(x).
The common distribution
- Bernoulli distribution, 0-1 distribution, random variable 0 or 1, probability P and 1-p.
- Binomial distribution, n Bernoulli experiments, relative to the probability of different successes.
- Poisson distribution is the limiting form of the binomial distribution.
- Uniformly distributed, equally likely at the same length interval.
- Normal distribution, also known as gaussian distribution, the normal curve is bell-shaped, low at both ends and high in the middle.
- Exponential distribution, which describes the probability distribution of processes in which events occur continuously and independently at a constant average rate.
- Logistic distribution, growing distribution, looks a little bit like a normal distribution.
Generative distribution algorithm
- Inverse Transform Method: firstly, a uniformly distributed random number is generated, then the distribution function F(x) of the specified distribution is obtained, and then the Inverse function G(x) of F(x) is obtained. The random number is inserted into the Inverse function to obtain the random number of the specified distribution.
- Acceptance Rejection Method: firstly, a uniformly distributed random number A is generated, and the probability density function is f(x), and then a uniformly distributed random number B is generated. If B <=f(a), the random number with a specified distribution is successfully obtained, otherwise, the random number with a specified distribution is successfully obtained.
implementation
Both algorithms are relatively easy to understand, the first one requires an inverse function, which is sometimes hard to find, and the second one is pretty much unconstrained, and it works pretty well for any distribution. Below use the second method to achieve the generation of several distribution of random numbers.
Normal distribution
def normal_pdf(x, mu=0, sigma=1):
return (1 / (math.sqrt(2 * math.pi) * sigma)) * (math.exp(-math.pow(x - mu, 2) / (2 * math.pow(sigma, 2))))
def standard_normal_rand():
whileTrue: a = random. Uniform (-4.0, 4.0) b = random. Uniform (0.0, 3.0)if b < normal_pdf(a):
return a, b
Copy the code
An index distribution
def exponential_pdf(x, lam=1):
return lam * math.exp(-lam * x)
def exponential_rand():
whileTrue: a = random. Uniform (0.0, 100.0) b = random. Uniform (0.0, 3.0)if b <= exponential_pdf(a):
return a, b
Copy the code
Poisson distribution
def poisson_pdf(x, lam=1):
return (math.pow(lam, x) / math.factorial(x)) * math.exp(-lam)
def poisson_rand():
whileTrue: a = random. Randint (0, 50) b = random. Uniform (0.0, 1.0)if b <= poisson_pdf(a):
return a, b
Copy the code
github
https://github.com/sea-boat/MachineLearning_Lab/blob/master/distribution_gen.py
————- Recommended reading ————
My 2017 article summary – Machine learning
My 2017 article summary – Java and Middleware
My 2017 article summary – Deep learning
My 2017 article summary — JDK source code article
My 2017 article summary – Natural Language Processing
My 2017 Article Round-up — Java Concurrent Article
Talk to me, ask me questions:
The public menu has been divided into “distributed”, “machine learning”, “deep learning”, “NLP”, “Java depth”, “Java concurrent core”, “JDK source”, “Tomcat kernel” and so on, there may be a suitable for your appetite.
Why to write “Analysis of Tomcat Kernel Design”
Welcome to: