scipy.optimize.minimize:含numpy数组参数时如何指定边界
scipy.optimize.minimize I'm using
scipy.optimize.minimizefor optimization calculations, here's my code:sig_init = 2 b_init = np.array([0.2,0.01,0.5,-0.02]) params_init = np.array([b_init, sig_init]) mle_args = (y,x) results = opt.minimize(crit, params_init, args=(mle_args))The problem is that I need to set bounds for
sig_init, butopt.minimize()requires bounds for each input parameter, and my input parameter includes a numpy array. How do I specify parameter bounds in this case?
First off, let's fix a critical detail in your parameter setup: right now, params_init is a nested structure (it holds a 4-element array and a scalar), but scipy.optimize.minimize expects a flat 1D array of individual parameters. This is a common gotcha—you'll need to flatten those values into a single sequence first.
Once that's sorted, setting bounds is straightforward: you just define bounds for each individual parameter in the flattened array, in order. Here's how to do it step by step:
1. Fix Your Initial Parameter Array
Instead of wrapping b_init and sig_init into a nested array, concatenate them into a flat 1D array:
sig_init = 2 b_init = np.array([0.2, 0.01, 0.5, -0.02]) # Flatten into a single 1D array: [0.2, 0.01, 0.5, -0.02, 2] params_init = np.concatenate([b_init, [sig_init]])
2. Define Bounds for Each Parameter
The bounds argument accepts either a list of tuples or a Bounds object (from scipy.optimize). Let's assume you want no bounds on the b_init parameters, but need sig_init to be positive (a common requirement for standard deviation-like parameters):
Option 1: List of Tuples (Simple)
# First 4 parameters (b_init) have no bounds; 5th (sig_init) must be > 0 bounds = [(-np.inf, np.inf)] * 4 + [(0.0, np.inf)]
Option 2: Bounds Class (Cleaner for Larger Sets)
from scipy.optimize import Bounds # Lower bounds: -infinity for b_init, 0 for sig_init lower_bounds = [-np.inf] * 4 + [0.0] # Upper bounds: infinity for all parameters upper_bounds = [np.inf] * 5 bounds = Bounds(lower_bounds, upper_bounds)
3. Run the Minimization with Bounds
Pass the bounds to the minimize function:
results = minimize(crit, params_init, args=mle_args, bounds=bounds)
Critical Note for Your Criterion Function
Make sure your crit function expects a flat 1D array as input. Inside the function, you can split the parameters back into b and sig like this:
def crit(params, y, x): b = params[:4] # First 4 elements are the b parameters sig = params[4] # 5th element is sig # ... rest of your MLE calculation ...
内容的提问来源于stack exchange,提问作者Stan Shunpike

