How to implement probability density function using Python

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:

F(X)=P(axb)=abf(x)dx0F(X) = P(a \le x \le b) = \int_{a}^{b} f(x) dx \ge 0

This equation, which expresses the likelihood that XX lies inside the range [a,b][a, b], is created by integrating the PDF f(x)f(x) over the interval.

The PDF f(x)f(x) has two essential criteria:

  1. Non-negativity: The PDF shouldn’t be negative to all values of xx inside the range of XX. The probability values supplied by the PDF are always non-negative.

  2. Normalization: Over the whole range of XX, the PDF integrates to 1. This characteristic guarantees that the overall probability for the entire range of XX is 1. It denotes that a value within the scope of the random variable XX will permanently be assigned to it.

Example

For instance, look at this code snippet that utilizes NumPy and Matplotlib to visualize the PDF of a normal distribution.

import numpy as np
import matplotlib.pyplot as plt
# Define the parameters of the normal distribution
mu = 0 # mean
sigma = 1 # standard deviation
# Generate random numbers from the normal distribution
x = np.random.normal(mu, sigma, 10000)
# Plot the PDF of the normal distribution
plt.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

Copyright ©2025 Educative, Inc. All rights reserved