How do I calculate the inverse of the cumulative distribution function (CDF) of the normal distribution in Python?
Which library should I use? Possibly scipy?
NORMSINV (mentioned in a comment) is the inverse of the CDF of the standard normal distribution. Using
scipy, you can compute this with the
ppf method of the
scipy.stats.norm object. The acronym
ppf stands for percent point function, which is another name for the quantile function.
In : from scipy.stats import norm In : norm.ppf(0.95) Out: 1.6448536269514722
Check that it is the inverse of the CDF:
In : norm.cdf(norm.ppf(0.95)) Out: 0.94999999999999996
norm.ppf uses mean=0 and stddev=1, which is the "standard" normal distribution. You can use a different mean and standard deviation by specifying the
scale arguments, respectively.
In : norm.ppf(0.95, loc=10, scale=2) Out: 13.289707253902945
If you look at the source code for
scipy.stats.norm, you'll find that the
ppf method ultimately calls
scipy.special.ndtri. So to compute the inverse of the CDF of the standard normal distribution, you could use that function directly:
In : from scipy.special import ndtri In : ndtri(0.95) Out: 1.6448536269514722