Home Technology Programming Simulating Popular Distributions in Python
blank

Simulating Popular Distributions in Python

1231
Thumb1

Interest in machine learning and data science has been growing at a rapid rate in recent years. More and more students are enrolling in online data sciences courses that are great at teaching them how to fit machine-learning algorithms to simple data sets. Most of these online courses are fantastic in explaining complex techniques related to machine learning, however, only a few of them delve into the mathematical statistics behind the fancy algorithms. The fundamentals of statistics are grossly undervalued in these courses. For example, there are many so-called data scientists that cannot distinguish between discrete and continuous data. It may seem trivial, but I’ve seen many people simply assume that their data is continuous when in fact it is discrete. The most common being the Poisson distribution. Understanding the properties of various distributions is extremely important in making sense of your data.

To help one understand the properties of a certain distribution, it is always helpful to stimulate the data points and plot them visually. With the help of Python 3, we will go through and simulate the most common simple distributions in the world of data science. We won’t be explaining each distribution in detail, this research can be done in your own time (we provide useful links and resources). Here we will only simulate various popular distributions that can be helpful in many applications. The first step is to install the required libraries.

Standard Normal Distribution

The Normal Distribution contains the word “Normal” because it’s possibly the distribution that explains most types of phenomena. For example, IQ scores, height and shoe sizes are applications of the normal distribution. You can find a detailed explanation of the normal distribution here. It explains the notation used and the central limit theorem.

blank

Binomial Distribution

The Binomial Distribution is discrete and is used to model the number of successes in a given sample size. When we simulate this distribution, it’s useful to indicate the size parameter. The size parameter essentially defines how many times we want to run the experiments. The flipping of a coin is the most intuitive way to think about the binomial distribution.

blank

In the plot above we have flipped a coin 10 times and performed this experiment 100 times. For instance, the probability that the coin lands on heads (denoted as a success) exactly 5 times for all these experiments is 0.30. Note that this is an empirical distribution and not theoretical. Also, as n (number of flips) gets larger, the binomial distribution can be somewhat approximated by the normal distribution. Notice the kernel density (red line), it closely resembles the normal distribution. Have a look at Khan Academy for a detailed explanation of the distribution.

Poisson Distribution

The Poisson Distribution is used to model events that occur at random time points, in which we are interested in the number of occurrences of the event. For example, the number of goals in a match or the number of calls recorded per day. Lambda is defined as the rate of the event multiplied by the time interval of the event. Stat Trek is a good place to get started on the Poisson distribution.

blankNote that in this simulation we plot the frequency and not the density of the distribution. To plot with the density on the y-axis, you’d only need to change ‘kde = False’ to ‘kde = True’ in the code above.

Exponential Distribution

Referring back to the Poisson distribution and the example with the number of goals scored per match, a natural question arises: how would one model the interval of time between the goals? We would use the popular Exponential distribution to provide the result. Here, Lambda is defined as the rate parameter. A lower rate parameter is linked to a flatter curve.

blank

Have a look here for a detailed explanation of the Exponential distribution and its applications.

LEAVE A REPLY

Please enter your comment!
Please enter your name here