How to calculate the inverse of the normal cumulative distribution function (CDF) in Python?
Method 1: scipy.stats.norm.ppf()
In Excel, NORMSINV is the inverse of the CDF of the standard normal distribution.
In Python’s SciPy library, the
ppf() method of the
scipy.stats.norm object is the percent point function, which is another name for the quantile function. This
ppf() method is the inverse of the
cdf() function in SciPy.
norm.cdf()is the inverse function of
norm.ppf()is the inverse function of
You can see this in the following code snippet:
from scipy.stats import norm print(norm.cdf(norm.ppf(0.5))) print(norm.ppf(norm.cdf(0.5)))
The output is as follows:
An alternative is given next:
Method 2: statistics.NormalDist.inv_cdf()
Python 3.8 provides the
NormalDist object as part of the
statistics module that is included in the standard library. It includes the inverse cumulative distribution function
inv_cdf(). To use it, pass the mean (
mu) and standard deviation (
sigma) into the
NormalDist() constructor to adapt it to the concrete normal distribution at hand.
Have a look at the following code:
from statistics import NormalDist res = NormalDist(mu=1, sigma=0.5).inv_cdf(0.5) print(res) # 1.0
A great resource on the topic is given next.
Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.