Scipy: speed up integration when you do it for the entire surface?

I have a density density function (pdf) f(x,y) . And in order to get the cumulative distribution function (cdf) f(x,y) at the point (x, y), you need to integrate f(x,y) , for example: enter image description here

In Scipy I can do this with integrate.nquad :

 x, y=5, 4 F_at_x_y = integrate.nquad(f, [[-inf, x],[-inf, y]]) 

Now I need only f(x,y) in the xy panel, something similar to this:

enter image description here

How can i do this?


The main problem is that for every point from (-30,-30) to (30,30) I need to do integrate.nquad from scratch to get f(x,y) . It is too slow.

I am interested, since the results are consistent (for example, you get F(5,6) value of F(4,4) and integrate from the regions between these two points), if it is possible to speed up the process? Therefore, we do not need to do integrate from scratch at every point and, therefore, make the process faster.


Possible useful links:

Multidimensional normal CDF in Python using scipy

http://cn.mathworks.com/help/stats/mvncdf.html

And I'm thinking of borrowing something from a Fibonacci sequence

How to write a Fibonacci sequence in Python

+5
source share
1 answer

In the end, this is what I did:

F - cdf, f - pdf

F (5.5) = F (5.4) + F (4.5) - 2 * F (4.4) + f (5.5)

And swipe across the surface, you can get the result.

The code will 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 example, and you can improve the result by changing +/- equantion to integration.

As for the initial value and the field, cdf[0,:] and cdf[:,0] , you can also use integration. In my case, this is very small, so I just use the pdf value.

You can check the function by building cdf or check the value on cdf[n,n]

0
source

Source: https://habr.com/ru/post/1241919/


All Articles