Calculating the derivative of cumulative density function in Python Calculating the derivative of cumulative density function in Python numpy numpy

Calculating the derivative of cumulative density function in Python


The derivative of the CDF is the PDF.

Here is an approximation of the derivative of the CDF:

dx = x[1]-x[0]deriv = np.diff(wei.cdf(x))/dx

import scipy.stats as simport matplotlib.pyplot as pltimport numpy as npwei = s.weibull_min(2, 0, 2) # shape, loc, scale - creates weibull objectsample = wei.rvs(1000)shape, loc, scale = s.weibull_min.fit(sample, floc=0) x = np.linspace(np.min(sample), np.max(sample))dx = x[1]-x[0]deriv = np.diff(wei.cdf(x))/dxplt.hist(sample, normed=True, fc="none", ec="grey", label="frequency")plt.plot(x, wei.cdf(x), label="cdf")plt.plot(x, wei.pdf(x), label="pdf")plt.plot(x[1:]-dx/2, deriv, label="derivative")plt.legend(loc=1)plt.show()

yields

enter image description here

Note that the x-locations associated with deriv have been shifted by dx/2 so the approximation is centered between the values used to compute it.