API · Python

Python math.erf() Function

Learn what Python's math.erf() function returns and use it to calculate normal-distribution probabilities.

On this page

Python’s math.erf() function evaluates the Gauss error function for a real-valued input. It is useful in probability calculations because the cumulative distribution function (CDF) of a normal distribution can be written in terms of math.erf(). The CDF gives the probability up to a value; it is different from a probability density function, which describes density rather than cumulative probability.

What math.erf() Computes

For a real number x, the error function is defined by an integral from zero to x, scaled by 2 / sqrt(pi). Its value is zero at x = 0, is positive for positive inputs, and is negative for negative inputs. It is an odd function, so erf(-x) = -erf(x). For the exact mathematical function, the value approaches 1 as x tends to positive infinity and -1 as x tends to negative infinity, but neither limit is attained at any finite input. Python returns a floating-point approximation, so for sufficiently large finite inputs its result may round to 1.0 or -1.0. The function accepts real numbers and returns a floating-point value.

Syntax and Return Value

Import the math module, then call math.erf(x), where x is the real value whose error function you need. The return value is a float. The function does not calculate a normal-distribution density or a general integral between arbitrary bounds; its normal-probability use comes from the CDF relationship below.

Basic Usage and Sign Behavior

This example compares two inputs with opposite signs, a small input, and a larger input. It also calculates standard normal cumulative probabilities and the probability between two bounds.

Run this example:

import math

for value in (0.5, -0.5, 0.01, 5.0):
    print(f"erf({value}) = {math.erf(value):.6f}")

def standard_normal_cdf(z):
    return 0.5 * (1.0 + math.erf(z / math.sqrt(2.0)))

lower = -1.0
upper = 1.0
probability = standard_normal_cdf(upper) - standard_normal_cdf(lower)
print(f"P({lower} <= Z <= {upper}) = {probability:.6f}")

Relating math.erf() to the Normal Distribution

For a standard normal variable Z, its CDF is Phi(z) = 0.5 * (1 + erf(z / sqrt(2))). The division by sqrt(2) is part of the relationship; omitting it gives the wrong CDF value. In the example, standard_normal_cdf applies this formula and the final calculation finds the probability that Z falls between -1 and 1 by taking the difference of the upper and lower CDF values.

For a normal variable X with mean mu and positive standard deviation sigma, first standardize a value x as z = (x - mu) / sigma. Then evaluate the standard normal CDF at that z. The mean shifts the center of the distribution, and the standard deviation scales distances from that center. Keep these roles explicit when adapting the formula to measurements that are not already standard normal.

Calculating Probability Between Two Bounds

For bounds a <= b, calculate P(a <= X <= b) as Phi((b - mu) / sigma) - Phi((a - mu) / sigma). This subtracts the probability accumulated through the lower bound from the probability accumulated through the upper bound. For the standard normal case, use mu = 0 and sigma = 1, as in the runnable example. For another normal distribution, pass each standardized bound to the same CDF formula before subtracting.

Use a positive sigma; it represents the distribution’s standard deviation. Reversing the bounds reverses the subtraction and produces a negative result, which is not an interval probability. math.erf() provides the function value used in these formulas, while the CDF and interval calculation give that value its probability interpretation.