【发布时间】:2019-07-30 15:13:18
【问题描述】:
如果这只是一个快速回答错误修复问题,我深表歉意;希望相反,我有一个可以从中学习的概念错误。
我正在尝试对积分进行优化,因此我的代码中有很多函数。当我运行它时,我得到一个错误
TypeError: integrand() 接受 3 个位置参数,但给出了 4 个
对应我的代码第53行:
return 积分.nquad(integrand, [bounds_a(), bounds_x()], args = (t_est))[0]
但是根据我对 nquad 上的the documentation 的理解,我在这一行中写的内容完全对应于 3 个参数:a 和 x,隐含在给出它们的限制,以及 t_est,一个向量。不喜欢 t_est 是向量这一事实吗?
代码:
#!/usr/bin/env python3
from scipy.optimize import minimize
import numpy as np
from scipy import integrate
#0)Number of covariates:
p=5
#2)Obtain data y (use a mdified algorithm that takes as input the covariate distributions))
def g(z):
g = np.power((1+np.exp(-z)),-1)
return g
#betatrue for the values of y and how they depend on the coavriates x
betatrue = [1.3,3.5,2.6,0.9,2.1,1.1]
#)Z distribution
means=0
sigmas=1
#)Function as integral
def integrand(a, x, t_est):
t0 = t_est[0]
t1 = t_est[1]
Z = np.power(a,2)/np.sqrt(2*np.pi*np.power(sigmas,2)) * np.exp(-np.power((a-t0-t1*np.sum(x)),2)/(2*sigmas*sigmas))
xint = np.append([1.0],x)
h = g(xint.dot(betatrue))
px = np.divide(np.exp(-np.divide(np.power((x-means),2),(2*np.power(sigmas,2)))), np.sqrt(2*np.pi*np.power(sigmas)))
Px = np.multiply(px)
YV = Px*h
return YV * Z
#Integration limits
def bounds_x():
return [-np.inf,np.inf]
def bounds_a():
return [0,np.inf]
#integration function. If the control is set to 0, gets the integrand. If control is set to 1
#gets the jacobian. Control set to 2, gets the Hessian.
def integration(t_est):
return integrate.nquad(integrand, [bounds_a(), bounds_x()], args = (t_est))[0]
def integration_jac(t_est):
def jacobian_integrand(a,x,t_est):
t0=t_est[0]
t1=t_est[1]
j0 = (t0 + t1 * np.sum(x))/np.power(sigmas,2)
j1 = np.sum(x)*(t0 + t1 * np.sum(x))/np.power(sigmas,2)
jacintegrands = [integrand(a,x,t_est) * j0, integrand(a,x,t_est) * j1]
return jacintegrands
jac0 = integrate.nquad(jacobian_integrand[0], [bounds_a(), bounds_x()], args = (t_est))[0]
jac1 = integrate.nquad(jacobian_integrand[1], [bounds_a(), bounds_x()], args = (t_est))[0]
output = [jac0,jac1]
return output
def integration_hessian(t_est):
t0=t_est[0]
t1=t_est[1]
def hessian_integrand(a,x,t_est):
h00 = (1 + np.power((t0+t1*np.sum(x)),2))/sigmas*sigmas
h01 = np.sum(x)*( + np.power((t0+t1*np.sum(x)),2))/sigmas*sigmas
h11 = np.power(np.sum(x),2)*( + np.power((t0+t1*np.sum(x)),2))/sigmas*sigmas
Hint = [[integrand(a,x,t_est)*h00,integrand(a,x,t_est)*h01],[integrand(a,x,t_est)*h01,integrand(a,x,t_est)*h11]]
return Hint
entry00 = integrand.nquad(hessian_integrand(0,0), [bounds_a(), bounds_x()], args = (t_est))[0]
entry01 = integrand.nquad(hessian_integrand(0,1), [bounds_a(), bounds_x()], args = (t_est))[0]
entry11 = integrand.nquad(hessian_integrand(1,1), [bounds_a(), bounds_x()], args = (t_est))[0]
output = [[entry00,entry01],[entry01,entry11]]
return output
##And then a maximisation here!
t_est = [0,0]
res = minimize(integration, t_est, method='Newton-CG', jac=integration_jac, hess=integration_hessian, options={'xtol': 1e-8, 'disp': True})
【问题讨论】:
-
@MarkMeyer 但是文档中的这个呢:“func = f(x0, x1, x2, t0, t1)”。我可以在这里看到这个例子:integrate.nquad(func2, [lim0, lim1, lim2, lim3], args=(0,0), opts=[opts0, opts1, opts2, opts3]) 我认为其中的限制参考 xs 和 args 到 t 值。这似乎是有道理的,并且与我的代码完全相同(除了我的 x 和 t_est 是数组)
标签: python function numpy scipy