Sunday, February 1, 2015

py3. Cumulative Normal Distribution in Python

A for loop is used here to find the fraction of points upto a particular x.


The cdf function from scipy.stats.norm is used to get the actual cumulative normal distribution.


Since the calculated, from the random sample set, is very close to the actual, the resulting plot is mostly magenta, rather than pure red or blue.

# ex3.py
from __future__ import division, print_function
from numpy.random import normal
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
num = 10000
sig = normal(size=num)
x = np.linspace(min(sig),max(sig))
y = np.zeros(len(x))
for i,v in enumerate(x):
    y[i] = sum(sig<=v)/num
plt.plot(x,y,'b',x,norm.cdf(x),'r')
plt.title('Cumulative Normal Distribution')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

Output:

 

No comments:

Post a Comment