Ask Your Question
0

convert mpmath function to cython

asked 2011-03-04 15:39:58 +0200

mhampton gravatar image

updated 2011-03-04 19:16:14 +0200

DSM gravatar image

I think I could figure this out eventually, but I'm hoping it will be a very easy question for someone out there. I would like to convert the following function to cython to be as fast as possible. I am not sure exactly what I need to import from mpmath to do that.

Here is the function:

from mpmath import *
mp.dps = 25; mp.pretty = True
def hyp_mp(a1,a2,b1,b2):
    num = gamma(a1+a2+1)*gamma(b1+b2+1)*gamma(a1+b1+1)*gamma(a2+b2+1)
    denom = gamma(a1+1)*gamma(a2+1)*gamma(b1+1)*gamma(b2+1)*gamma(a1+a2+b1+b2+1)
    return num/denom

def myfisher_mp(a1,a2,b1,b2):
    p = hyp_mp(a1,a2,b1,b2)
    outp = p
    for i in range(0,a1):
        temp = hyp_mp(i,a2+a1-i,b1-i+a1,b2-a1+i)
        if temp > p:
            break
        else:
            outp += temp
    for i in range(0,a2):
        temp = hyp_mp(a1+a2-i,i,b1-a2+i,b2+a2-i)
        if temp > p:
            break
        else:
            outp += temp
    return outp

A good test case would be myfisher_mp(1286, 9548, 133437, 148905), which takes about 1 second on my desktop. The inputs a1,a2,b1,b2 can be assumed to be positive ints.

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
1

answered 2011-03-05 19:02:51 +0200

fredrik gravatar image

updated 2011-03-05 19:04:52 +0200

Actually, there is a simple method to speed this up quite significantly. Since the only needed gamma values are at integers (up to a few several thousand), you can just store all of them in a list or a dict.

In fact mpmath provides a decorator "memoize" that effectively does this. So you could just add

gamma = memoize(gamma)

to the program. With this change, the test case myfisher_mp(1286, 9548, 133437, 148905) runs about 2x faster the first time, and 7x faster the second time. But it should be even faster if you precompute the needed gamma values and replace the function calls with list lookups. Note that to precompute a list up to say n = 100000, you could just use a loop instead of repeatedly calling gamma, and this would be very fast.

edit flag offensive delete link more

Comments

Thanks, that's very helpful.

mhampton gravatar imagemhampton ( 2011-03-14 00:15:47 +0200 )edit
0

answered 2011-03-04 20:17:16 +0200

fredrik gravatar image

Use a recurrence instead of computing each term from scratch. The quotient between successive values of hyp_mp should be a rational function of i.

To avoid manual labor, I would usually use Mathematica to rewrite a hypergeometric quotient to rational form. I'm not sure if Sage currently has direct support for this.

edit flag offensive delete link more

Comments

Thanks. Yes, using a recurrence would be much better, but I'm trying to get away with not thinking too much. If I had more time for this project it would be cool to implement Fisher's exact test for the general case (larger contingency tables, not just 2 by 2) but that is much harder.

mhampton gravatar imagemhampton ( 2011-03-05 10:56:24 +0200 )edit
0

answered 2011-03-04 19:04:47 +0200

DSM gravatar image

updated 2011-03-04 19:22:00 +0200

Assuming you mean convert this function to Cython [ah, you do, it's only the title that's wrong-- edited], I'm not sure whether it will help.

First, I get

sage: time z = myfisher_mp(1286, 9548, 133437, 148905)
CPU times: user 1.56 s, sys: 0.00 s, total: 1.56 s
Wall time: 1.60 s
sage: z
4.191535038879969055586166e-1316

Is that right? Almost all of the computations in the loops over i in the function don't matter to the final value because they're too small.

Second, you only get real benefits from Cython when the work can be pushed into C. If the values are so small, though, C floats won't work, as they'll underflow. And it's not that most of the time is being spent in the Python loops, which are linear anyway: the vast majority of the time is being spent in the gamma function itself, which is already pretty fast.

sage: timeit('z=mpmath.gamma(100)')
625 loops, best of 3: 5.67 µs per loop
sage: timeit('z=mpmath.gamma(100000)')
625 loops, best of 3: 50.9 µs per loop

Do you have a link to the definition of this function? I'm sure we can find a way to compute it more efficiently, especially given how many terms are currently noncontributing.

edit flag offensive delete link more

Comments

Its Fisher's exact test. I was using the R implementation, but the smallest value it will give is 10^(-16), and I'd like more precision.

mhampton gravatar imagemhampton ( 2011-03-05 10:55:07 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-03-04 15:39:58 +0200

Seen: 1,138 times

Last updated: Mar 05 '11