I have set-up a polynomial equation with 4 factors inside, that I want to solve using scipy.optimize.minimize.
However, I might want to solve only a certain set of the 4 factors at any one time. For example, -> given Y(x1, x2, x3, x4) = Function(x1, x2, x3, x4) = 0.0 (to get the roots)
Sometimes, I want to get all [x1, x2, x3, x4], but sometimes, I want to just solve-for/calibrate [x2, x3] etc, or any iteration of the set [x1, x2, x3, x4]. Is there a way to do this elegantly?
def fit_beta(fwd_ = 0.01, shift_ = 0.0, time_ = 1.0, beta = 1.0, k_ = np.array([]), vols_ = np.array([]), params = np.array([1, 1, 1, 1])):
def calib_(x):
curve_ = []
for strike_ in k_:
vol_ = vol_log(strike_, fwd_, time_, x[0], x[1], x[2], x[3])
curve_.append(vol_)
return np.sum((np.array(curve_) - np.array(vols_))**2)
x0 = np.array([0.01, 0.5, 0.00, 0.10]) # initial guess
bounds = [(0.0001, None), (0.0001, 0.9999), (-0.9999, 0.9999), (0.0001, None)]
res = minimize(calib_, x0, method='L-BFGS-B', bounds=bounds)
x1_, x2_, x3_, x4_ = res.x
return [x1_, x2_, x3_, x4_]