我使用numba @jit(nopython = True)定义一个函数,它将通过scipy.special调用普通的cdf函数,我收到此错误消息:
TypingError: Untyped global name 'norm'
以下是我的代码:
import numpy as np
from numba import *
from scipy.stats import *
timelst = np.random.randn(2083)
noncovlst = np.random.randn(2083)
lst = np.random.randint(30,size=127)
lst_num = 0
lst_num = np.append(lst_num,lst)
lst_num = np.cumsum(lst_num)
n = 127
agelst = np.random.randint(16,80,127)
edulst = np.random.randint(1,28,127)
incomelst = np.random.randn(127)
@jit(nopython=True)
def lamb(now,params):
b = params[0]
mu = params[1]
s = params[2]
beta1 = params[3]
beta3 = params[4]
beta4 = params[5]
beta5 = params[6]
beta6 = params[7]
L = 0.0
for i in range(n):
timez = timelst[lst_num[i]:lst_num[i+1]]
noncov = noncovlst[lst_num[i]:lst_num[i+1]]
timeq = timez[timez < now]
noncovq = noncov[timez < now]
L += (b*now + np.exp(beta3*agelst[i]+beta5*edulst[i]
+beta6*incomelst[i])*(norm.cdf((now-timeq-mu)/s)*np.exp(beta1*noncovq)).sum())/n
return L
res = [2.422855,16.320364,28.984707,-9.942004,-3.118748,-0.248391,1.903728,4.126649]
输入
lamb(0.2,res)
错误消息:
TypingError: Failed at nopython (nopython frontend)
Untyped global name 'norm'
我知道drop nopython关键字可能会解决这个问题,然而,它会转换为对象模式,这会对性能造成巨大损失。
有没有更好的方法将普通的cdf纳入numba nopython模式?