I have two interpolated curves created with scipy.interpolate.interp1d
- how can I calculate the x value that maximizes the difference between these two curves?
Here is the relevant code:
from scipy import interpolate
units = [0.0, 1.0, 2.0 ,..., 49.0, 50.0]
revenue = [0.0, 1.2, 2.4 ,..., 79.1, 80.0]
cost = [0.3, 0.6, 0.9 ,..., 14.7, 15.0]
revenue_func = interpolate.interp1d(units,revenue,kind='cubic')
cost_func = interpolate.interp1d(units,cost,kind='linear')
I need to find the value of units that maximizes revenue_func - cost_func
. This would be easy if I could limit the result to integer values for units, but I need to treat units as continuous between 0 and 50.
I'm stumped on this one, and would really appreciate any suggestions - even those that use another package besides scipy
On possibility is the scipy.optimize package, which includes both minimize and minimize_scalar functions. Since these functions search for a minimum instead of a maximum, you have to negate the function you want to maximize. To maximize revenue - cost
import scipy.optimize as opt
negated_profit_func = lambda x : cost_func(x) - revenue_func(x)
result = opt.minimize_scalar(negated_profit_func, bounds = (0, 50), method = 'bounded')
print(result)