The error function and the normal distribution#

Recovers the 68-95-99.7 rule from erf, and shows why erfc is needed in the far tail, where 1 - erf(x) rounds to zero.

import math

import matplotlib.pyplot as plt
import numpy as np

from mathematicskit.special_functions import complementary_error_function, error_function

The 68-95-99.7 rule#

for k in (1, 2, 3):
    print(f"P(|X - mu| < {k} sigma) = erf({k}/sqrt 2) = {error_function(k / math.sqrt(2)):.6f}")
P(|X - mu| < 1 sigma) = erf(1/sqrt 2) = 0.682689
P(|X - mu| < 2 sigma) = erf(2/sqrt 2) = 0.954500
P(|X - mu| < 3 sigma) = erf(3/sqrt 2) = 0.997300

erf and erfc#

x = np.linspace(-3, 3, 300)
fig, ax = plt.subplots()
ax.plot(x, error_function(x), label="erf(x)")
ax.plot(x, complementary_error_function(x), label="erfc(x)")
ax.set_xlabel("x")
ax.legend()
ax.set_title("The error function")
The error function
Text(0.5, 1.0, 'The error function')

The far tail#

for x0 in (3.0, 6.0, 9.0):
    print(f"x = {x0:g}: 1 - erf(x) = {1 - error_function(x0):.3e}, erfc(x) = {complementary_error_function(x0):.3e}")
x = 3: 1 - erf(x) = 2.209e-05, erfc(x) = 2.209e-05
x = 6: 1 - erf(x) = 0.000e+00, erfc(x) = 2.152e-17
x = 9: 1 - erf(x) = 0.000e+00, erfc(x) = 4.137e-37

Total running time of the script: (0 minutes 0.023 seconds)

Gallery generated by Sphinx-Gallery