Ref Math Erf
## Python math.erf() Method
The `math.erf()` method is a built-in function in Python's standard `math` module. It is used to calculate the **Error Function** (also known as the Gauss error function) of a given number.
The error function is a special function of sigmoid shape that occurs frequently in probability, statistics, and materials science (particularly diffusion).
### Introduction to the Error Function
Mathematically, the error function of $x$ is defined as:
$$\operatorname{erf}(x) = \frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^2} \, dt$$
* **Domain:** The method accepts any real number from negative infinity to positive infinity ($-\infty$ to $+\infty$).
* **Range:** The returned value always falls within the range of $[-1, 1]$.
* **Symmetry:** The error function is an odd function, meaning $\operatorname{erf}(-x) = -\operatorname{erf}(x)$.
---
### Syntax
To use the `math.erf()` method, you must first import the `math` module:
```python
import math
math.erf(x)
```
#### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| **x** | `float` or `int` | **Required.** The numeric value to evaluate. |
#### Return Value
* Returns a **`float`** representing the error function of the input number $x$.
#### Exceptions
* If the argument `x` is not a number (e.g., a string or list), the method raises a **`TypeError`**.
---
### Version History
* **Added in:** Python 3.2
---
### Code Examples
#### Example 1: Basic Usage with Positive and Negative Numbers
This example demonstrates how to calculate the error function for positive and negative values of the same magnitude, illustrating its odd-function symmetry.
```python
import math
# Calculate the error function for positive and negative 1.28
val1 = math.erf(1.28)
val2 = math.erf(-1.28)
print(f"math.erf(1.28) : {val1}")
print(f"math.erf(-1.28) : {val2}")
```
**Output:**
```text
math.erf(1.28) : 0.9297341930135782
math.erf(-1.28) : -0.9297341930135782
```
---
#### Example 2: Evaluating Boundary and Special Values
The error function approaches $1$ as $x \to \infty$ and approaches $-1$ as $x \to -\infty$. Let's test these limits along with $0$.
```python
import math
# erf(0) is exactly 0
print("erf(0):", math.erf(0))
# erf of a large positive number approaches 1.0
print("erf(10):", math.erf(10))
# erf of a large negative number approaches -1.0
print("erf(-10):", math.erf(-10))
# Using infinity constants
print("erf(inf):", math.erf(float('inf')))
print("erf(-inf):", math.erf(float('-inf')))
```
**Output:**
```text
erf(0): 0.0
erf(10): 1.0
erf(-10): -1.0
erf(inf): 1.0
erf(-inf): -1.0
```
---
### Practical Application: Cumulative Distribution Function (CDF)
In statistics, the cumulative distribution function (CDF) of a standard normal distribution $\Phi(x)$ can be calculated using the error function:
$$\Phi(x) = \frac{1}{2} \left[1 + \operatorname{erf}\left(\frac{x}{\sqrt{2}}\right)\right]$$
Here is how you can implement this in Python:
```python
import math
def normal_cdf(x):
"""Calculates the CDF of a standard normal distribution."""
return 0.5 * (1.0 + math.erf(x / math.sqrt(2.0)))
# Probability of a value falling within 1 standard deviation of the mean
prob_1_std = normal_cdf(1) - normal_cdf(-1)
print(f"Probability within 1 standard deviation: {prob_1_std:.4f}")
```
**Output:**
```text
Probability within 1 standard deviation: 0.6827
```
YouTip