forked from nryoung/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapprox_cdf.py
More file actions
36 lines (27 loc) · 1.11 KB
/
approx_cdf.py
File metadata and controls
36 lines (27 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Approximate Cumulative Distribution Function
--------------------------------------------
Calculates the cumulative distribution function (CDF)
of the normal distribution based on an approximation by George Marsaglia:
Marsaglia, George (2004). "Evaluating the Normal Distribution".
Journal of Statistical Software 11 (4).
16 digit precision for 300 iterations when x = 10.
Equation:
f(x) = 1/2 + pdf(x) * (x + (x^3/3) + (x^5/3*5) + (x^7/3*7) + ...)
"""
from algorithms.math import std_normal_pdf
def cdf(x, iterations=300):
"""
Calculates the cumulative distribution function of the normal distribution.
Uses a taylor exponent to calculate this.
:param x: An integer that represents the taylor exponent.
:param iterations: An integer representing the number of iterations.
:rtype: The normal distribution
"""
product = 1.0
taylor_exp = [x]
for i in range(3, iterations, 2):
product *= i
taylor_exp.append(float(x**i)/product)
taylor_fact = sum(taylor_exp)
return (0.5 + (taylor_fact * std_normal_pdf.pdf(x, mean=0, std_dev=1)))