Cumulative Distribution Function (CDF)
The CDF for a random variable $X$ is defined as: $$ F_X(x) = P(X \leq x) $$representing the probability that $X$ will take a value less than or equal to $x$. It is crucial for describing the distribution of probabilities in statistics.
Implementation:
We implement the CDF for the standard normal distribution using:
PDF Calculation: Compute the Gaussian PDF as $$ \text{pdf}(z) = \frac{1}{\sqrt{2\pi}}e^{-\frac{z^2}{2}} $$
#### Polynomial Approximation
To compute the CDF from the PDF, we first calculate a scaling factor $t$ based on the $z$-score:
$$
t = \frac{1}{1 + 0.2316419 \times |z|}
$$
We then use a polynomial that approximates the integral of the PDF:
$$
\text{poly} = (1.330274429 \times t - 1.821255978) \times t + 1.781477937 \times t - 0.356563782 \times t + 0.319381530 \times t
$$
CDF Calculation
Combining these, the CDF is approximated as follows:
$$
F(z) =
\begin{cases}
1.0 - \text{pdf}(z) \times \text{poly} & \text{if } z > 0 \\
\text{pdf}(z) \times \text{poly} & \text{if } z \leq 0
\end{cases}
$$
Accuracy
The polynomial coefficients are chosen to maximize the precision of the approximation up to $7.5 \times 10^{-8}$. This level of accuracy is suitable for statistical applications requiring high precision.
Note: As of this writing, I am unsure what the specific polynomials are or why they are used in the code below. The code was a direct translation Timothy Masters' code used with reference one.
Further, note that in our CDF implementation, the PDF calculation slightly differs as it uses the absolute value of z
(`zz`), ensuring non-negative inputs to maintain accuracy and stability in the exponential function, which is critical when combined with the polynomial approximation used for the CDF.
Python Code
Here's the Python implementation using Numba for enhanced performance:
from numba import jit
import math
@jit(nopython=True)
def normal_cdf(z):
zz = math.fabs(z)
pdf = math.exp(-0.5 zz zz) / math.sqrt(2.0 * math.pi)
t = 1.0 / (1.0 + zz * 0.2316419)
poly = ((((1.330274429 t - 1.821255978) t + 1.781477937) * t -
0.356563782) t + 0.319381530) t
return 1.0 - pdf poly if z > 0.0 else pdf poly
---
References
1. Masters, T. (2020). Statistically sound indicators for financial market prediction.