Python Math.erf() Method

Junaid khan Jan 30, 2023 Oct 05, 2022

We use Python’s `math` module to perform mathematical calculations and standards work. This module provides us with many different methods, such as the `math.erf()` method, which takes a number and returns the error function for the provided argument.

This method was newly introduced in Python version 3.2. These error methods are usually related to computational problems and machine learning techniques.

The method accepts the value range from -Infinity to +Infinity. We use this method to solve various mathematical problems such as Gaussian and integrals of the numbers.

Syntax of Python `math.erf()` Method

``````math.erf(number)
``````

Parameter

`number` This is the number to find the error function and takes from -Infinity to +Infinity.

Return

This method returns the floating value and shows the error value for the provided argument.

Example Code: Use of the `math.erf()` Method in Python

In Python version 3.2 and later, we use the method `math.erf()` to check the error function of the provided number.

This method is used to compute the distribution function for the standard normal distribution. It is actually integral to the normal distribution.

This method accepts a number ranging from -Infinity to +Infinity and returns a value ranging from -1 to +1. The below example shows the output variation of the different arguments provided.

``````# import to use the mathematics functions
import math

def find_error_function_value(number):
return math.erf(number)

# shows the error function for different value arguments
print('Error function of 65 =',find_error_function_value(65))
# method accepts the negative value as well
print('Error function of -3 =',find_error_function_value(-3))
print('Error function of -0.016 =',find_error_function_value(-0.016))
print('Error function of 19 =',find_error_function_value(19))
print('Error function of 1.98 =',find_error_function_value(1.98))
print('Error function of 908 =',find_error_function_value(908))
``````

Output:

``````Error function of 65 = 1.0
# negative value has negative floating value result
Error function of -3 = -0.9999779095030014
Error function of -0.016 = -0.01805252617815065
Error function of 19 = 1.0
Error function of 1.98 = 0.9948920003868136
# maximum value is 1.0
Error function of 908 = 1.0
``````

Example Code: Use the `math.erf()` Method for the Same Number With Different Signs

In the below example, we will provide the same number to the `math.erf()` method with a positive and negative sign. The output shows that if the provided number has a `+` sign, the result is positive and vice versa.

These two examples show that the result of the method `math.erf()` always returns a value ranging between -1 and +1.

``````import math

def test_postive_negative_number(number):
return math.erf(number)

# same parameter value with + and - sign
print('Error function of a number with a positive sign =', test_postive_negative_number(9.28))
print('Error function of a number with a negative sign =', test_postive_negative_number(-9.28))
``````

Output:

``````# returns the same result with the same sign
Error function of a number with a positive sign = 1.0
Error function of a number with a negative sign = -1.0
``````

Example Code: Use the `math.erf()` Method to Return the Gaussian Probability Density Function

In the below example, the `math.erf()` method helps to return the area under the Gaussian probability density function.

The method integrates from -Infinity to the provided number. The argument `number` can be a simple real or complex number.

The `ndtr()` method returns the scalar value of the provided number. The method compares the provided value’s absolute and square root and returns the result.

``````import math

def ndtr(number):
# square root method of the math module
square_root = math.sqrt(2) / 2
initial_result = float(number) * square_root
# absolute method always returns a positive value
absolute = abs(initial_result)
if absolute < square_root:
result = 0.5 + 0.5 * math.erf(initial_result)
else:
result = 0.5 * math.erfc(absolute)
if initial_result > 0:
result = 1 - result
return result

print("The result of the area under the Gaussian probability density function is" , ndtr(5.33))
``````

Output:

``````The result of the area under the Gaussian probability density function is 0.9999999508936167
``````

Example Code: Use the `math.erf()` Method to Solve the Gaussian Integral

In Python version 3.2 and above, the `math.erf()` method can also be used to solve the Gaussian integral. For this purpose, we code a method known as `gaussian_integral()` with four parameters: `x`, `y`, `mean`, and `sigma`.

The method contains two statements for the integral of negative/positive infinity to `x` and for the integral of negative/positive infinity to `y`. The method returns the substitution of the ceiling to floor statement.

``````import math
# import for using the square root function
from math import sqrt
# method returns the integral of the gaussian center
def gaussian_integral(x, y, m, sig):

floor = 0.5 * (1 + math.erf((x - m) / (sig * math.sqrt(2.0))))
ceil = 0.5 * (1 + math.erf((y - m) / (sig * sqrt(2.0))))

return ceil - floor

print("The integral of a gaussian center is",gaussian_integral(-3.111, 6.22, 8, 1))
``````

Output:

``````The integral of a gaussian center is 0.0375379803485168
``````

Example Code: Use the `math.erf()` Method to Find the Integral

The method `math.erf()` is also used to find the integral of two given (float number) parameters and returns the float value of the calculated integral. The below example takes two floating values as a parameter to the `default_integral()` method and returns the float value for the integrals.

``````import math
# method returns the integral of two floating values
def default_integral(a, b):

return 0.5 * (math.erf(b) - math.erf(a))
print("The value of the integral is",default_integral(9.188, 3.56))
``````

Output:

``````The value of the integral is -2.394234760449976e-07
``````