Add which_vars to minimizeNelderMead
Added option to HARK.estimate.minimizeNelderMead to select which parameters are estimated by passing a vector of booleans of the same size as parameter_guess.  When not passed, it defaults to estimating all of the parameters (previous behavior).  Not a breaking change.
@@ -14,7 +14,7 @@ from scipy.optimize import fmin, fmin_powell # Minimizers
import warnings
-def minimizeNelderMead(objectiveFunction, parameter_guess, verbose=False, **kwargs):
+def minimizeNelderMead(objectiveFunction, parameter_guess, verbose=False, which_vars=None, **kwargs):
'''
Minimizes the objective function using the Nelder-Mead simplex algorithm,
starting from an initial parameter guess.
@@ -27,6 +27,9 @@ def minimizeNelderMead(objectiveFunction, parameter_guess, verbose=False, **kwar
parameter_guess : [float]
A starting point for the Nelder-Mead algorithm, which must be a valid
input for objectiveFunction.
+ which_vars : np.array or None
+ Array of booleans indicating which parameters should be estimated. When
+ not provided, estimation is performed on all parameters.
verbose : boolean
A flag for the amount of output to print.
@@ -35,10 +38,19 @@ def minimizeNelderMead(objectiveFunction, parameter_guess, verbose=False, **kwar
xopt : [float]
The values that minimize objectiveFunction.
'''
+ # Specify a temporary "modified objective function" that restricts parameters to be estimated
+ if which_vars is None:
+ which_vars = np.ones(len(parameter_guess),dtype=bool)
+ def objectiveFunctionMod(params):
+ params_full = np.copy(parameter_guess)
+ params_full[which_vars] = params
+ out = objectiveFunction(params_full)
+ return out
+ parameter_guess_mod = parameter_guess[which_vars]
# Execute the minimization, starting from the given parameter guess
t0 = time() # Time the process
- OUTPUT = fmin(objectiveFunction, parameter_guess, full_output=1, maxiter=1000, disp=verbose, **kwargs)
+ OUTPUT = fmin(objectiveFunctionMod, parameter_guess_mod, full_output=1, disp=verbose, **kwargs)
t1 = time()
# Extract values from optimization output:
@@ -49,17 +61,18 @@ def minimizeNelderMead(objectiveFunction, parameter_guess, verbose=False, **kwar
warnflag = OUTPUT[4] # warnflag : int
# 1 : Maximum number of function evaluations made.
# 2 : Maximum number of iterations reached.
-
# Check that optimization succeeded:
if warnflag != 0:
warnings.warn("Minimization failed! xopt=" + str(xopt) + ', fopt=' + str(fopt) +
', optiter=' + str(optiter) +', funcalls=' + str(funcalls) +
', warnflag=' + str(warnflag))
+ xopt_full = np.copy(parameter_guess)
+ xopt_full[which_vars] = xopt
# Display and return the results:
if verbose:
print("Time to estimate is " + str(t1-t0) + " seconds.")
- return xopt
+ return xopt_full
def minimizePowell(objectiveFunction, parameter_guess, verbose=False):
