0

Let's say I have inputs 'A' and 'B' for my function, which outputs 'C'. For each value of A, I would like to find what value of B results in the maximum value of C; I would then like to record values B and C. Is there a function that can perform this action? Perhaps something which depends on convergence mechanisms?

*in case you found this through one of the non-python related tags I applied, please make note that I am using python 3.x

Deemo
  • 131
  • 1
  • 1
  • 10

1 Answers1

1

Let's define function 1 to take parameters (A,B)2 and return a value C3. We can optimize this with Python by doing

from scipy import optimize

f = lambda a,b: ... # your_code_which_returns_C
optimal_vals = np.zeros((2, len(list_of_all_A_values)))
for i, a in enumerate(list_of_all_A_values) # assuming some list is defined above
    b_opt, c_opt, *rest = optimize.fmin(lambda b: -f(a,b), 0)
    optimal_vals[:,i] = np.array([b_opt, c_opt])

This takes advantage of scipy's fmin function, which relies on the convergence of the downhill simplex algorithm. For this reason, it's crucial to not forget the minus sign on 1.

v0rtex20k
  • 1,041
  • 10
  • 20