SciPy - stats.norm.cdf() Function



scipy.stats.norm.cdf() is a function in the SciPy library that calculates the cumulative distribution function (CDF) of a normal distribution for a given value x. The function belongs to the SciPy stats module where loc represents the mean () and scale is the standard deviation ().

The cumulative distribution function is given by the integral of the probability density function (PDF) from negative infinity to x, mathematically expressed as follows −

F(x) = ⁄√2π ∫-∞x e- ⁄ (t - μ)2 / 2σ2 dt

Syntax

The syntax for using scipy.stats.norm.cdf() is as follows −

scipy.stats.norm.cdf(x, loc=0, scale=1)

Parameters

Following are the parameters of the function scipy.stats.norm.cdf() which is used to calculate the cumulative distribution function (CDF) −

  • x: The input value where the CDF is evaluated.
  • loc (optional): The mean () of the normal distribution. Default value is 0.
  • scale (optional): The standard deviation () of the normal distribution. Default value is 1.

Return Value

This function returns the cumulative probability by indicating the likelihood of obtaining a value less than or equal to x from the normal distribution.

Computing CDF for Standard Normal Distribution

The cumulative distribution function (CDF) of a normal distribution gives the probability that a random variable X will take a value less than or equal to a given value x. In the standard normal distribution where mean () = 0 and standard deviation () = 1, the CDF tells us how much of the distribution lies to the left of x.

In this example we will compute the CDF at x = 1 using the scipy.stats.norm.cdf() function −

import numpy as np
from scipy.stats import norm

# Define parameters for standard normal distribution
x = 1
mean = 0
std_dev = 1

# Compute the CDF
cdf_value = norm.cdf(x, loc=mean, scale=std_dev)

# Print the result
print(f"CDF value at x={x}: {cdf_value}")

Following is the output of the standard normal distribution CDF computed using the function scipy.stats.norm.cdf()

CDF value at x=1: 0.8413447460685429

Computing Probability of a Value Less X

In real-world applications the normal distributions often have different means and standard deviations. The CDF can be used to calculate the probability that a random variable X will be less than or equal to a specific value.

Here in this example, we compute the probability that X 50 in a normal distribution with mean = 40 and standard deviation = 10 using scipy.stats.norm.cdf()

import numpy as np
from scipy.stats import norm

# Define parameters for a normal distribution
x = 50
mean = 40
std_dev = 10

# Compute the CDF
cdf_value = norm.cdf(x, loc=mean, scale=std_dev)

# Print the result
print(f"Probability that X  {x}: {cdf_value}")

Following is the output of the CDF computation −

Probability that X  50: 0.8413447460685429

Plotting the CDF Curve for a Normal Distribution

The cumulative distribution function (CDF) helps visualize the probability of obtaining values up to a given x in a normal distribution. The CDF curve starts from 0 and asymptotically approaches 1 as x increases.

Below is the example in which we plot the CDF of a normal distribution with mean = 0 and standard deviation = 1 using the scipy.stats.norm.cdf() function −

import matplotlib.pyplot as plt
from scipy.stats import norm
import numpy as np
# Define the range of x values
x_values = np.linspace(-5, 5, 100)

# Compute the CDF for each x value
cdf_values = norm.cdf(x_values, loc=0, scale=1)

# Plot the CDF
plt.plot(x_values, cdf_values, label="Standard Normal CDF", color="red")
plt.title("Cumulative Distribution Function (CDF)")
plt.xlabel("x")
plt.ylabel("Cumulative Probability")
plt.legend()
plt.grid(True)
plt.show()

Following is the output of the CDF computation visualized as a cumulative probability curve −

Plotting CDF Curve
scipy_stats.htm
Advertisements