Python: how to build a cdf function given an array of numbers

I am working on windows. I just want to enter an array and get the cdf of the array.

+3
source share
2 answers

First, you can implement CDF as follows:

from bisect import bisect_left

class discrete_cdf:
    def __init__(data):
        self._data = data # must be sorted
        self._data_len = float(len(data))

    def __call__(point):
        return (len(self._data[:bisect_left(self._data, point)]) / 
                self._data_len)

Using the class above, you can build it like this:

from scipy.stats import norm
import matplotlib.pyplot as plt

cdf = discrete_cdf(your_data)
xvalues = range(0, max(your_data))
yvalues = [cdf(point) for point in xvalues]
plt.plot(xvalues, yvalues)

Edit: arange there does not make sense, cdf will always be the same for all points between x and x + 1.

+3
source

Is that what you need? I provided a function for approximating cdf and built it. (Assuming you want to introduce a pdf array with y-values)

import matplotlib.pyplot as plt
from math import exp

xmin=0
xmax=5
steps=1000
stepsize=float(xmax-xmin)/float(steps)
xpoints=[i*stepsize for i in range(int(xmin/stepsize),int(xmax/stepsize))]
print xpoints,int(xmin/stepsize),int(xmax/stepsize)

ypoints=map(lambda x: exp(-x),xpoints)

def get_cdf(pdf_array):
    ans=[0]
    for i in range(0,len(pdf_array)-1):
        ans.append(ans[i]+(pdf_array[i]+pdf_array[i+1])/2.0*stepsize)
    return ans

cdfypoints=get_cdf(ypoints)

plt.plot(xpoints,ypoints)
plt.plot(xpoints,cdfypoints)
plt.show()

enter image description here

+2
source

All Articles