Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the derivative of cumulative density function in Python

Is it the case that the exact derivative of a cumulative density function is the probability density function (PDF)? I am calculating the derivative using the numpy.diff(), is this correct? See below code below:

import scipy.stats as s
import matplotlib.pyplot as plt
import numpy as np

wei = s.weibull_min(2, 0, 2) # shape, loc, scale - creates weibull object
sample = wei.rvs(1000)
shape, loc, scale = s.weibull_min.fit(sample, floc=0) 

x = np.linspace(np.min(sample), np.max(sample))

plt.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:], np.diff(wei.cdf(x)), label="derivative")
plt.legend(loc=1)
plt.show()

Compariosn of CDF, PDF and derivative

If so, how do I scale the derivative to be equivalent to the PDF?

like image 259
kungphil Avatar asked Jul 30 '13 03:07

kungphil


1 Answers

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 s
import matplotlib.pyplot as plt
import numpy as np

wei = s.weibull_min(2, 0, 2) # shape, loc, scale - creates weibull object
sample = 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))/dx
plt.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.

like image 184
unutbu Avatar answered Nov 05 '22 07:11

unutbu