Search code examples
pythonscipy-optimize

Finding root of a function with two outputs specified in return statement


I am currently writing a code in Python where the objective is to find the root of the output of a function with respect to input variable x. The code looks like this:

def Compound_Correlation_Function(x):
        
    # Here comes a long part of the code...
        
    Equity_Solve = Tranches.loc[0, 'Par_Spread_bps'] - Market_Data.iloc[0,0]
    Mezzanine_Solve = Tranches.loc[1, 'Par_Spread_bps'] - Market_Data.iloc[1,0]
    
    return Equity_Solve, Mezzanine_Solve
    
Correlation_Value = optimize.root(Compound_Correlation_Function, x0 = 0.3)

As can be seen in the code block above, there are two outputs specified:

  1. Equity_Solve
  2. Mezzanine_Solve

I now want to find the root for both outputs separately. If I comment out the Mezzanine_Solve part in the return statement, then the the optimize procedure gives me the solution I want. Obviously, I want to automate my code as much as possible. Is it possible to specify the output for which I want to find the root in the optimize statement?

I tried the following, without success:

Correlation_Value = optimize.root(Compound_Correlation_Function[0], x0 = 0.3) 
Correlation_Value = optimize.root(Compound_Correlation_Function(x)[0], x0 = 0.3) 
Correlation_Value = optimize.root(Compound_Correlation_Function()[], x0 = 0.3) 

Any help is appreciated. Thank you in advance!


Solution

  • I think the problem is that your function returns a tuple of numbers, but root is expecting a single number.

    Assuming you want to solve each equation separately, then you could include an argument in Compound_Correlation_Function to switch between the functions:

    def Compound_Correlation_Function(x, return_equity=True):
            
        # Here comes a long part of the code...
            
        if return_equity:
            Equity_Solve = Tranches.loc[0, 'Par_Spread_bps'] - Market_Data.iloc[0,0]
            return Equity_Solve
        else:
            Mezzanine_Solve = Tranches.loc[1, 'Par_Spread_bps'] - Market_Data.iloc[1,0]
            return Mezzanine_Solve
    

    Then pass the return_equity argument in as an extra argument via args, i.e. call

    root(Compound_Correlation_Function, x0=0.3, args=(True,))
    

    to solve Equity_Solve, and set args=(False,) to solve Mezzanine_Solve.

    You could also define a function wrapper that calls Compound_Correlation_Function and returns only one of the values.