How to do it...

We will initialize a normal distribution with a given average and standard deviation. Once initialized, we will consider the output that we should be expecting.

Initializing a normal distribution

A normal variable with a given mean and standard deviation can be initialized by using the rvs function in scipy.stats.norm:

  1. Import the relevant packages:
from scipy import stats
  1. Initialize a variable with a given mean and standard distribution:
x = stats.norm.rvs(loc=3, scale=2, size=(1000))

In the preceding line of code, we have initialized a variable x, which has a mean value of 3, a standard deviation of 2, and a total size of 1,000.

Note that the mean is referred to as loc and the standard deviation as scale in the line of code.

  1. Now, we have obtained the values of x, let's go ahead and plot the distribution of x:
import matplotlib.pyplot as plt
%matplotlib inline
plt.hist(x)
plt.show()

In the preceding snippet of code, we have plotted the histogram of the values of x, and the plot looks like the following:

In the preceding plot, we can see that the distribution is centered around 3 (which is what we expect, as we initialized the distribution with a mean of 3).

Given that we have the distribution we expected, let us go ahead and calculate the average, standard deviation, and moments of the distribution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset