Scipy: speed up integration when doing it for the whole surface?
In the end, this is what I've done:
F is cdf, f is pdf
F(5,5) = F(5,4) + F(4,5) - 2 *F(4,4) + f(5,5)
And loop through the whole surface, you can get the result.
The code would look like this:
def cdf_from_pdf(pdf): if not isinstance(pdf[0], np.ndarray): original_dim = int(np.sqrt(len(pdf))) pdf = pdf.reshape(original_dim,original_dim) cdf = np.copy(pdf) xdim, ydim = cdf.shape for i in xrange(1,xdim): cdf[i,0] = cdf[i-1,0] + cdf[i,0] for i in xrange(1,ydim): cdf[0,i] = cdf[0,i-1] + cdf[0,i] for j in xrange(1,ydim): for i in xrange(1,xdim): cdf[i,j] = cdf[i-1,j] + cdf[i,j-1] - cdf[i-1,j-1] + pdf[i,j] return cdf
This is a very rough approximate, and you can perfect the result by changing the +/- equantion into integration.
As for the original value and the margin, cdf[0,:]
and cdf[:,0]
, you can also use integration as well. In my case, it's very small, so I just use the pdf value.
You can test the function by plotting the cdf
, or check the value at the cdf[n,n]