Calculate Inverse of CDF in Python
In Python, the inverse of the Cumulative Distribution Function (CDF) is calculated using the ppf
(percent point function) from the SciPy package.
The inverse of CDF is mostly used for finding the Z-scores corresponding to a given cumulative probability (area under the normal curve to the left of the Z-score).
The inverse of CDF is also useful in calculating the critical Z-scores for confidence interval calculation (e.g. 95% confidence interval)
The following example explains how to calculate the inverse of CDF using the ppf
in Python.
Example 1
We will use the ppf
function to calculate the inverse of CDF i.e. get the Z-score from the given probability.
For example, if you want to calculate the Z-score for a probability of 0.975 for the upper tail, you can use the ppf
function.
# import package
from scipy.stats import norm
norm.ppf(0.975)
# output
1.9599
The value of 1.9599 (~1.96) is a critical Z-score that we typically used for calculating the 95% confidence interval.
The area under the curve to the left of the Z-score value of 1.96 will have a cumulative probability of 0.975.
You can also verify the inverse of the CDF calculated by ppf
function to get the cumulative probability from the Z-score using the cdf
function.
# import package
from scipy.stats import norm
norm.cdf(1.9599)
# output
0.9749
You can see that the value of 0.975 is a cumulative probability for a Z-score of 1.96.
Example 2
We will use the ppf
function to calculate the inverse of CDF i.e. get the Z-score from the given probability.
For example, if you want to calculate the Z-score for a probability of 0.95 for the upper tail, you can use the ppf
function.
# import package
from scipy.stats import norm
norm.ppf(0.95)
# output
1.6448
The value of 1.6448 (~1.65) is a critical Z-score that we typically use for calculating the 90% confidence interval.
The area under the curve to the left of the Z-score value of 1.65 will have a cumulative probability of 0.95.
Example 3
We will use the ppf
function to calculate the inverse of CDF i.e. get the Z-score from the given probability.
For example, if you want to calculate the Z-score for a probability of 0.925 for the upper tail, you can use the ppf
function.
# import package
from scipy.stats import norm
norm.ppf(0.925)
# output
1.4395
The value of 1.4395 (~1.44) is a critical Z-score that we typically use for calculating the 85% confidence interval.
The area under the curve to the left of the Z-score value of 1.44 will have a cumulative probability of 0.925.