TLDR: I need a minimzation implementation for functions with matricial variable
I have the following cost function:
def loss_price(params):
A_grid, S_grid = params
print(A_grid, S_grid)
res = 0.
i = 0
print("-------------------------")
print("[BEGINING]: Values for case a, sigma = ", params)
i = 0
for j in range(tenor_size):
maturity = cube[i,j,2]["maturity"]
tenor_swap = cube[i,j,2]["tenor swap"]
strike = cube[i,j,1]
swaption_model = self.swaption(maturity, tenor_swap, A_grid, S_grid, strike)
res += (swaption_model / Mkt_price[i,j] - 1.0)**2
res = res / (tenor_size)
print("-------------------------")
print("[ENDED]: loss for case a, sigma = ", res)
return res
'params' contains the parameters A_grid and S_grid of my model. They are both vectors of size n, n is fixed and corresponds to the number of time-discretisation. 'params' is then a matrix of size (2,n).
'swaption' is a function dependent of the vectors parameters A_grid and S_grid. It also rely on other functions of A_grid & S_grid, there are too many to them describe each of them and i think this depth of explanation is sufficient for this function (ask me if you think otherwise :=)). There is no error compiling loss_function for any given param, for n=1 or higher (important for later).
I aim at minimizing this cost function and obtaining its corresponding minimum which is a matrix. For that purpose i used the following code :
def calibrate(initialization, function, method="Nelder-Mead"):
"""
methods available in minimize:
- trust-constr
- Nelder-Mead
- SLSQP
"""
bnds = (
(-1., 1.), (1e-6, 1.)
)
return minimize(function, initialization, bounds=bnds, method=method)
self.calibrate = calibrate
However, when n=1 the problem here is that in order to do so it evaluates every underlying function with a scalar-component of 'params' rather than taking params[0] as A_grid and params[1] as S_grid.
In other words it evaluates loss_price (and its underlying functions) with 'params[0][0]' rather than A_grid and 'params[1][1]' for S_grid (when tested for n=1).
and when n>1 i get this error value:
Input In [119], in HWCalibration.__init__.<locals>.loss_price(params)
257 def loss_price(params):
--> 258 A_grid, S_grid = params
259 print(A_grid, S_grid)
260 res = 0.
ValueError: too many values to unpack (expected 2)
Is there a way to counter such an issue within this implementation ? Or is there an existing implementation that fulfills my requirement ?
I'm available for any details you might find relevant to answer my question :=).