Probability theory introduces the concept of a probability density function (PDF), which expresses the likelihood of a continuous random variable taking on a particular value. We can leverage powerful libraries like NumPy, SciPy, and Matplotlib to plot the PDF of a continuous random variable in Python. The mathematical model for the PDF is as follows:
This equation, which expresses the likelihood that lies inside the range , is created by integrating the PDF over the interval.
The PDF has two essential criteria:
Non-negativity: The PDF shouldn’t be negative to all values of inside the range of . The probability values supplied by the PDF are always non-negative.
Normalization: Over the whole range of , the PDF integrates to 1. This characteristic guarantees that the overall probability for the entire range of is 1. It denotes that a value within the scope of the random variable will permanently be assigned to it.
For instance, look at this code snippet that utilizes NumPy and Matplotlib to visualize the PDF of a normal distribution.
import numpy as npimport matplotlib.pyplot as plt# Define the parameters of the normal distributionmu = 0 # meansigma = 1 # standard deviation# Generate random numbers from the normal distributionx = np.random.normal(mu, sigma, 10000)# Plot the PDF of the normal distributionplt.hist(x, bins=50, density=True, alpha=0.6, color='g')plt.xlabel('Value')plt.ylabel('Probability density')plt.title('Normal distribution PDF')plt.show()
To generate a histogram plot of a normal distribution’s probability density function (PDF), we use the following steps:
Lines 5–6: We define the mean and standard deviation parameters using the mu
and sigma
variables.
Line 9: We use the np.random.normal()
function to generate 10,000 random numbers from the distribution.
Line 12: We use the plt.hist()
function from matplotlib
with the following parameters to plot the PDF:
density=True
: This normalizes the histogram area under the curve to represent a probability density.alpha
: This is used to adjust the transparency level of the histogram bars.Lines 13–15: We add the labels and title to the graph.
The histogram will show a bell curve centered around the mean (mu
) with a spread determined by the standard deviation (sigma
). This code generates a professional quality plot portraying the PDF of a normal distribution.
Free Resources