How to calculate and plot the Normal CDF in Python

First, we will import some libraries numpy, scipy, and Matplotlib. numpy for numerical operations,scipy. stats for statistical functions and

matplot for plotting the plot.

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

then we will define a range of x and calculate cdf using stats. norm.cdf

How stats. norm.cdf work-

The Cumulative Distribution Function (CDF) for the standard normal distribution is computed using the error function (erf), which is closely related to the Gaussian integral. For a standard normal distribution, the mean and standard deviation are set to 0 and 1.

F(x) = 0.5 * [1 + erf(x / sqrt(2))]

In this equation, x represents the standardized value for calculating the CDF. CDF of a random variable X is defined as F(x)=P(X≤x) which is the probability of X taking a value less than or equal to x.

This formula directly calculates the area under the standard normal curve from negative infinity to x, which the CDF represents. The result is always a value between 0 and 1

 # range of x values
x = np.linspace(-3, 3, 100)

# Calculate the normal CDF
cdf = stats.norm.cdf(x)

Now we will plot the result

plt.plot(x, cdf)
plt.title('Normal Cumulative Distribution Function')
plt.xlabel('x')
plt.ylabel('CDF')
plt.grid(True)
plt.show()

output-

 

Leave a Reply

Your email address will not be published. Required fields are marked *