In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use inverse transformation in Python to generate random variables". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to generate random variables using inverse transformation in Python".
target
In simulation theory, generating random variables is one of the most important "building blocks", and most of these random variables are generated by uniformly distributed random variables. One of the methods that can be used to generate random variables is inverse transformation. In this article, I'll show you how to generate random variables (both discrete and continuous) using the inverse transformation method in Python.
Concept
A random variable U is given, where U is uniformly distributed in (0 ~ 1). Suppose we want to generate a random variable X, where the cumulative distribution function (CDF) is:
The idea of inverse transformation is to generate a random number from any probability distribution by using its inverse CDF as follows.
For discrete random variables, the steps are slightly different. Suppose we want to generate the value of a discrete random variable X with a probabilistic quality function (PMF).
In order to generate the value of X, you need to generate a random variable U _
Through the above steps, we can create the algorithm of the inverse transformation method as follows.
Code implementation of continuous random numbers
First, we implement this method to generate continuous random variables. Suppose we want to simulate a random variable X that follows the exponential distribution of the mean λ (that is, X~EXP (λ)). We know that the probability distribution function (PDF) of exponential distribution is
CDF is as follows
Then, we can write the inverse CDF using the following method
In Python, we can simply implement it by writing these lines of code as follows.
# Generate exponential distributed random variables given the mean # # and number of random variables def exponential_inverse_trans: U=uniform.rvs (size=n) X=-mean*np.log (1muru) actual=expon.rvs (size=n,scale=mean) plt.figure (figsize= (12L9)) plt.hist (X, bins=50, alpha=0.5, label= "Generated r.v.") Plt.hist (actual, bins=50, alpha=0.5, label= "Actual r.v.") Plt.title ("Generated vs Actual i Exponential Random Variables" n) plt.legend () plt.show () return X
We can try the above code by running the following example. Note that because we are generating random variables, the results may be different.
Cont_example1=exponential_inverse_trans (nasty 100) cont_example2=exponential_inverse_trans (nasty 500) cont_example3=exponential_inverse_trans (nasty 1000)
It looks interesting. If we compare it with the actual variables, we can see that the generated random variables have very similar results. You can adjust the mean (note that the mean I defined for the expon.rvs () function is a proportional parameter in the exponential distribution) and / or the number of random variables generated to see different results.
Discrete random number implementation code
For the case of discrete random variables, suppose we want to simulate the case X of discrete random variables with the following distribution.
First, we write functions to use this code to generate discrete random variables using a sample.
# Generate arbitary discrete distributed random variables given # # the probability vector def discrete_inverse_trans (prob_vec): U=uniform.rvs (size=1) if U
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.