开发者

Error when using lambda function with Cython

I am trying to use Cython to speed up a piece of code. Cython is giving an error that reads "Expected an identifier or literal" when I use lambda functions. As far as I can tell, lambda functions are meant to be supported in Cython 0.13. Am I incorrect on this point? If they are, indeed, supported, do I need to do something other than what I have here to implement them?

def f(e_1, e_2, rho):
    """Bivariate Normal pdf with mean zero, unit variances, and correlation coefficient rho."""
    return开发者_C百科 (1.0 / (2.0 * pi * sqrt(1 - rho**2))) * exp(-(1.0 / (2*(1 - rho**2))) * (e_1**2 + e_2**2 - 2*rho*e_1*e_2))

def P_zero(b_10, b_11, b_20, b_21, rho, gamma, x):
    """Returns the probability of observing zero entrants in a market by numerically
    integrating out the unobserved firm-specific profit shocks."""
    h_z = lambda e_1: -inf
    g_z = lambda e_1: -b_10 - b_11*x[0] - gamma*x[1]
    I   = lambda e_1, e_2: f(e_1, e_2, rho)
    return dblquad(I, -inf, (-b_20 - b_21*x[0] - gamma*x[2]), h_z, g_z)[0]


In my opinion you should change h_z = lambda e_1: -inf with h_z = lambda e_1: -float('inf') unless you have defined inf somewhere else.


I'm able to compile the below simplified Cython code and run it fine using Cython 0.14.1 on OS X 10.6.6. I don't know the details as to why it doesn't work on 0.13. The easiest solution is to upgrade Cython, if possible.

def f(e_1, e_2, rho):
    return e_1 + e_2 + rho

def dummy(a, b, c, d, e):
    return [a(1,2) + b + c + d(1) + e(3)]

def P_zero(b_10, b_11, b_20, b_21, rho, gamma, x):
    h_z = lambda e_1: -1000
    g_z = lambda e_1: -b_10 - b_11 * x[0] - gamma * x[1]
    I   = lambda e_1, e_2: f(e_1, e_2, rho)
    return dummy(I, -1000, (-b_20 - b_21 * x[0] - gamma * x[2]), h_z, g_z)[0]

print P_zero(1, 2, 3, 4, 5, 6, [6, 7, 8]) 
# outputs "-2122"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜