My Coding >
Numerical simulations >
Atoms and Molecules >
Tools for molecular modelling >
Generating random distribution for given probability function, 1d case
Generating random distribution for given probability function, 1d caseA very common problem  generate a set of random values in some range in accordance with the probability function, defined over this space. There are three different approaches to dealing with this problem. Numpy.random.choice, projection to cumulative distribution and rejection Sampling process. In this article, I will consider the first two algorithms and the rejection Sampling process I will explain for the 2D case. This problem was also explained in the video. numpy.random.choiceIf this space is discrete, then it is very easy to use numpy function random.choice. But for continuous distribution, this function is not suitable. In the worstcase scenario, you can use a very fine sampling, compatible or exciting a number of required random selections or add some extra noise to the final selection, but both these methods are not ideal. Projection to Cumulative distributionFirst of all, it is necessary to mention, that this method is only working for 1D cases. For "d and more dimensional space, we need to use different techniques. The main Idea of Projection to Cumulative distribution is to use normalized to 1 cumulative sum of the distribution function and then random distribution in the Y direction from 0 to 1 will produce random distribution over the X axis which will represent the given probability function. Python implementationsSo, let's implement these ideas into Python code! First of all boring stuff. Loading libraries, initialization of the probability function  we will use the Gaussian function for the start and number of dots places and number of bins for function calculations.
Next step  we need to calculate the probability value for every range in our r_values set and then make a cumulative function and then normalize it to 1. It is interesting to note, that the cumulative function is an approach to the constant, therefore for normalizing we can divide it into the biggest number which is the last number in this sequence. Or, otherwise, you can apply max() function to find the maximal value.
After all these preparations, we need to apply our main idea of converting the random distribution of N dots with the function intewrp from Numpy library.
Now we can display these randomly placed dots. I think it is more easy to display them by the histogram of their density. And this histogram should be similar to the probability function.
As you can see from the picture, the distribution of the dots is pretty similar to the Gaussian distribution, given as a reference function. More detailed information you can see in the following video: Full Python code

Last 10 artitles
9 popular artitles


© 2020 MyCoding.uk My blog about coding and further learning. This blog was writen with pure Perl and frontend output was performed with TemplateToolkit. 