-1

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 :=).

  • The question is too long and contains details that are not needed. What do you want to pass to the function, if it is lists you can `loss_price([1, 2, 3], [4, 5, 6])` and `loss_price([1], [2])`... – Doncho Gunchev Sep 01 '22 at 19:46
  • HI and thanks for taking the time to answer ! As I said, both functions loss_price and calibrate take a matrix (2D-array) of size (2, n). – Othmane lakhdar Sep 02 '22 at 06:41
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 03 '22 at 07:43

1 Answers1

-1

what about passing the parameters separately instead of combining them into one (2, n) matrix?

def loss_price(*params):
    A, S = params

A = 1   # matrix 1
B = 2   # matrix 2
loss_price(A, B)
ybbb
  • 30
  • 6
  • @Dmitrv Khamitov, ohh I do not have a new question :), just want to suggest Othmane pass the parameters separately – ybbb Sep 02 '22 at 12:35