Scipy nquad报错求助:integrand()需3个参数却传入4个
Let's break down the root cause of your error first, then walk through fixing it and other potential bugs in your code:
The Immediate Error: How nquad Handles args
Your error comes down to how you're passing the args parameter to nquad. When you write args=(t_est), Python doesn't treat this as a tuple—it just evaluates to the list t_est itself. Since t_est has two elements, nquad will split them into separate arguments and pass them to integrand alongside your integration variables a and x. That means your function gets called with a, x, t0, t1 (four arguments) instead of the a, x, t_est (three arguments) it expects.
Fix 1: Correct the args Tuple
Change args=(t_est) to args=(t_est,)—the trailing comma tells Python this is a single-element tuple, so nquad passes the entire t_est vector as one argument to integrand:
def integration(t_est): return integrate.nquad(integrand, [bounds_a(), bounds_x()], args=(t_est,))[0]
Other Critical Fixes for Your Code
Beyond the TypeError, there are a few other issues that will prevent your code from running properly:
1. Define Missing Variables
You reference means and sigmas but never initialize them. Based on your comments (p=5 covariates, Z distribution mean=0, sigma=1), add these at the top of your code:
p = 5 means = np.zeros(p) # Mean of 0 for all 5 covariates sigmas = np.ones(p) # Standard deviation of 1 for all 5 covariates z_sigma = 1.0 # Separate sigma for the Z distribution
2. Fix the Joint Probability Calculation
np.multiply(px) throws an error because it needs at least two arguments. To compute the joint probability of your covariates, use np.prod() to multiply all elements of px:
Px = np.prod(px) # Replace np.multiply(px) with this
3. Fix Jacobian Integration Logic
You can't index jacobian_integrand[0] directly—instead, pass the entire function to nquad, which will handle integrating each element of the returned list:
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) return [integrand(a,x,t_est) * j0, integrand(a,x,t_est) * j1] # nquad returns the integral of each element in the list jac0, jac1 = integrate.nquad(jacobian_integrand, [bounds_a(), bounds_x()], args=(t_est,)) return [jac0,jac1]
4. Fix Hessian Integration Logic
You have two issues here: integrand.nquad is a typo (it should be integrate.nquad), and you can't index hessian_integrand directly. Adjust it to handle the 2D return value correctly:
def integration_hessian(t_est): t0=t_est[0] t1=t_est[1] def hessian_integrand(a,x,t_est): # Fix extra plus signs and sigma squaring h00 = (1 + np.power((t0+t1*np.sum(x)),2))/np.power(sigmas,2) h01 = np.sum(x)*(1 + np.power((t0+t1*np.sum(x)),2))/np.power(sigmas,2) h11 = np.power(np.sum(x),2)*(1 + np.power((t0+t1*np.sum(x)),2))/np.power(sigmas,2) return [ [integrand(a,x,t_est)*h00, integrand(a,x,t_est)*h01], [integrand(a,x,t_est)*h01, integrand(a,x,t_est)*h11] ] # Extract the 2D integral results hess_results = integrate.nquad(hessian_integrand, [bounds_a(), bounds_x()], args=(t_est,))[0] entry00 = hess_results[0][0] entry01 = hess_results[0][1] entry11 = hess_results[1][1] return [[entry00,entry01],[entry01,entry11]]
5. Fix Z Distribution Calculation
Your original Z calculation uses sigmas (a length-5 array) which will cause dimension mismatches. Use the z_sigma we defined earlier for the Z distribution:
# Inside integrand() Z = np.power(a,2)/np.sqrt(2*np.pi*np.power(z_sigma,2)) * np.exp(-np.power((a-t0-t1*np.sum(x)),2)/(2*np.power(z_sigma,2)))
With all these fixes, your code should resolve the TypeError and run correctly for your integration and optimization workflow.
内容的提问来源于stack exchange,提问作者Meep

