I want to use the fsolve one by one for each iteration. Supposed I have DataFrame look like below:
| PD Downturn | PD TTC | 
|---|---|
| 0.12 | 0.008 | 
| 0.15 | 0.016 | 
| 0.24 | 0.056 | 
| 0.56 | 0.160 | 
| 1.00 | 1.000 | 
Here is the code I try:
result = []
for i in range(len(df) - 1):
  def R(x):
    ZDownturn = norm.ppf(df['PD Downturn'])[i] #Exclude 'Default' class
    ZShift = np.sqrt(
        x / (1 - x)
    ) * norm.ppf(0.999)
    ZPortion = np.sqrt(
        1 / (1 - x)
    ) * norm.ppf(df['PD TTC'])[i] #Exclude 'Default' class
    target = ZShift + ZPortion
    error =  np.abs(ZDownturn - target)
    return error
  # Initial guess
  x0 = [0.01]
  
  # Solver
  Rho = fsolve(R, x0)
  result.append(Rho[0])
I want to find x variable based on some calculation logic but I need to do it one-by-one. So, I create the new function in every iteration. The results are below:
[0.19153452995548875,
 0.15906256238706026,
 0.08759684851688349,
 0.1348702069117432]
It's work but I am looking for another way maybe more pythonic way to write the code.
Thank you.