Normal Distribution Explained with Python Examples

0

In this post, you will learn about the concepts of Normal Distribution with the help of Python example. As a data scientist, you must get a good understanding of different probability distributions in statistics in order to understand the data in a better manner. Normal distribution is also called as Gaussian distribution or Laplace-Gauss distribution.

Normal Distribution with Python Example

Normal distribution represents a symmetric distribution where most of the observations cluster around the central peak called as mean of the distribution. The parameter used to measure the variability of observations around the mean is called as standard deviation. The probabilities for values occurring near mean are higher than the values far away from the mean. Normal distribution is a probability distribution plot. The parameters of the normal distribution plot defining the shape and the probabilities are mean and standard deviation. The area of the plot between two different points in the normal distribution plot represents the probability of the value occurring between those two points.

Here are some of the properties of normal distribution of the population:

  • The points in the normal distribution are symmetric. Normal distribution can not be used to model skewed distributions.
  • The mean, median and mode of normal distribution are equal.
  • Half of the population is less than the mean and half is greater than the mean.
  • The empirical rule of the normal distribution goes like the following: 68% of the observations fall within +/- 1 standard deviation from the mean, 95% of the observations fall within +/- 2 standard deviation from the mean and 99.7% of the observations fall within +/- 3 standard deviation from the mean.

Here is the probability density function for normal distribution:

Normal distribution probability density function

Fig 1. Normal distribution probability density function

In above function, \(\mu\) represents the mean and \(\sigma\) represents the standard deviation. Given different values of random variable (x), one could calculate the probability using the above probability density function.

Here is a sample probability distribution plot representing normal distribution with a mean of 5 and standard deviation of 10. The plot is created for random variable taking values between -100 and 100. 

Normal Distribution Plot

Fig 2. Normal Distribution Plot

The following code can be used to generate above normal distribution plot.

#
# Create a normal distribution with mean as 5 and standard deviation as 10
#
mu = 5
std = 10
snd = stats.norm(mu, std)
#
# Generate 1000 random values between -100, 100
#
x = np.linspace(-100, 100, 1000)
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -100, 100
#
plt.figure(figsize=(7.5,7.5)) 
plt.plot(x, snd.pdf(x))
plt.xlim(-60, 60)
plt.title('Normal Distribution (Mean = 5, STD = 10)', fontsize='15')
plt.xlabel('Values of Random Variable X', fontsize='15')
plt.ylabel('Probability', fontsize='15')
plt.show()

Here is the code representing multiple normal distribution plots which looks like the following:

Fig 2. Multiple Normal Distribution Plots

The following code can be used to create above shown multiple normal distribution plots having different means and standard deviation.

#
# Values of random variable
#
x = np.linspace(-10, 10, 100)
#
plt.figure(figsize=(7.5,7.5)) 
#
# Normal distribution with mean 0 and std as 1
#
plt.plot(x, stats.norm(0, 1).pdf(x))
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot(x, stats.norm(1, 0.75).pdf(x))
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot(x, stats.norm(2, 1.5).pdf(x))
plt.xlim(-10, 10)
plt.title('Normal Distribution', fontsize='15')
plt.xlabel('Values of Random Variable X', fontsize='15')
plt.ylabel('Probability', fontsize='15')
plt.show()

Standard Normal Distribution with Python Example

Standard Normal Distribution is normal distribution with mean as 0 and standard deviation as 1. 

Here is the Python code and plot for standard normal distribution. Note that the standard normal distribution has a mean of 0 and standard deviation of 1. Pay attention to some of the following in the code below:

Standard Normal Distribution Plot (Mean = 0, STD = 1)
Fig 3. Standard Normal Distribution Plot (Mean = 0, STD = 1)

The following is the Python code used to generate the above standard normal distribution plot. Pay attention to some of the following in the code given below:

  • Scipy Stats module is used to create an instance of standard normal distribution with mean as 0 and standard deviation as 1 (stats.norm)
  • Probability density function pdf() is invoked on the instance of stats.norm to generate probability estimates of different values of random variable given the standard normal distribution
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm(mu, std)
#
# Generate 100 random values between -5, 5
#
x = np.linspace(-5, 5, 100)
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure(figsize=(7.5,7.5)) 
plt.plot(x, snd.pdf(x))
plt.xlim(-5, 5)
plt.title('Normal Distribution', fontsize='15')
plt.xlabel('Values of Random Variable X', fontsize='15')
plt.ylabel('Probability', fontsize='15')
plt.show()

Conclusions

Here is the summary of what you learned in this post in relation to Normal distribution:

  • Normal distribution is a symmetric probability distribution with equal number of observations on either half of the mean.
  • The parameters representing the shape and probabilities of the normal distribution are mean and standard deviation
  • Python Scipy stats module can be used to create a normal distribution with meand and standard deviation parameters using method norm.
  • Standard normal distribution is normal distribution with mean as 0 and standard deviation as 1.
  • In normal distribution, 68% of observations lie within 1 standard deviation, 95% of observations lie within 2 standard deviations and 99.7% observations lie within 3 standard deviations from the mean.
Ajitesh Kumar
Follow me
Share.

Leave A Reply

Time limit is exhausted. Please reload the CAPTCHA.