这是我的代码:
import numpy as np
import scipy.integrate as integrate
from scipy.stats import norm
def integrand(xi, thetai, *theta):
sum = 0
for thetaj in theta:
prod = 1
for t in tuple(list(theta).remove(thetaj)):
prod = prod * (1 - norm.cdf(xi - t))
sum = sum + norm.cdf(xi - thetaj) * prod
return sum * norm.pdf(xi - thetai)
def integral(thetai, *theta):
return (integrate.quad(integrand, -np.inf, np.inf, args=(thetai, *theta, )))[0]
print(integral(0.12849237,0.67286398,0.1124954,-0.3242629,0.28836734,0.33057082,-0.0843643,-0.085148,-0.7902458,-0.4907209,-0.5297461,-0.6957624))
但是,当我运行该代码时,出现以下错误:
TypeError: 'NoneType' object is not iterable
对应行 for t in tuple(list(theta).remove(thetaj)):
我在调试它时遇到了麻烦。
list.remove
是一个 in-place 操作,因此它不返回列表。因此,当您执行 tuple(list(theta).remove(thetaj))
时,您最终会得到 tuple(None)
,这会产生错误。相反,您应该简单地检查 t
是否(大约)thetaj
,如果是则跳过它。
def integrand(xi, thetai, *theta):
s = 0
for thetaj in theta:
prod = 1
for t in theta:
if abs(t - thetaj) < 1e-10:
continue
prod = prod * (1 - norm.cdf(xi - t))
s = s + norm.cdf(xi - thetaj) * prod
return s * norm.pdf(xi - thetai)