Nov-18-2020, 03:21 PM
Hi dear community,
I need your tips to improve once again my code.
For my first code, I just translated the exercise 22.1 from the topic to Python considering that we already had V0 and Sigma_V. For this process, we have :
and we get :
BUT we wanted to improve the code without fixed value for V0 and Sigma_V. It means, we considered the two equations (in yellow in the topic of my first publication above).
Here, our code :
Do you know what we have to change to get a value closer to 12.7% ?
(ps: We are french. If it's necessary, I can translate comments in our code to English)
Thanks you !
I need your tips to improve once again my code.
For my first code, I just translated the exercise 22.1 from the topic to Python considering that we already had V0 and Sigma_V. For this process, we have :
import numpy as np r = 0.05 T = 1 D = 10 V_O = 12.4 sigma_val = 0.2123 def d1(r,T,D,V_0,sigma_val): return (np.log(V_O/D)+(r+sigma_val**2/2)*T)/sigma_val*np.sqrt(T) a = d1(0.05,1,10,12.4,0.2123) print("d1 =",a) def d2(r,T,D,V_0,sigma_val): return d1(r,T,D,V_O,sigma_val)-sigma_val*np.sqrt(T) b = d2(0.05,1,10,12.4,0.2123) print("d2 =",b) import scipy.integrate as integrate def phi(x): return np.exp(-x**2/2) def N(x): return 1/np.sqrt(2*np.pi)*integrate.quad(phi,-np.inf,x)[0] print ("The default probability of the company is",1-N(b))
and we get :
Output:runfile('/Users/Chvantoine/Downloads/topic5.py', wdir='/Users/Chvantoine/Downloads')
d1 = 1.3549082647995547
d2 = 1.1426082647995548
The default probability of the company is 0.1266006362637757
So we get a probability of default of 12.7% as required in the exercise.BUT we wanted to improve the code without fixed value for V0 and Sigma_V. It means, we considered the two equations (in yellow in the topic of my first publication above).
Here, our code :
import numpy as np from scipy import integrate as intg # valeurs donnees E0 = 3 sE = 0.8 r = 0.05 T = 1 D = 10 # fonctions intermediaires : def g(u): return np.exp(-u*u/2)/np.sqrt(np.pi*2) def N(d): res = intg.quad(g, -np.inf, d) return res[0] def d1(V0, sV): return (np.log(V0/D)+T*(r + sV*sV/2)) / (sV*np.sqrt(T)) def d2(V0, sV): return d1(V0, sV) - sV*np.sqrt(T) # FONCTION A MINIMISER : on est obligé de mettre 1 seule variable, mais ici X c'est un vecteur à 2 coordonnées def F(X): V0 = X[0] sV = X[1] return np.abs(E0 - V0*N(d1(V0, sV)) + D*np.exp(-r*T)*N(d2(V0, sV))) + np.abs(sE * E0 - N(d1(V0, sV))*sV*V0) import scipy.optimize # module pour optimiser # Valeurs de départ X0: il faut donner des valeurs initiales, l'algo va chercher à minimiser la fonction de proche en proche. X0 = np.array([12.4, 0.21]) resultat = scipy.optimize.least_squares(F, X0, ftol = 1e-20) Xfinal = resultat.x print('Current asset value and volatility are', Xfinal) print('Valeur de F en ce point : ', F(Xfinal)) print('d1=',d1(Xfinal[0],Xfinal[1])) print('d2=',d2(Xfinal[0],Xfinal[1])) print('The default probability is', (1-N(d2(Xfinal[0],Xfinal[1])))*100,'%')AND the result is :
Output:runfile('/Users/Chvantoine/Downloads/Final.py', wdir='/Users/Chvantoine/Downloads')
/Users/Chvantoine/opt/anaconda3/lib/python3.8/site-packages/scipy/optimize/_lsq/least_squares.py:110: UserWarning: Setting `ftol` below the machine epsilon (2.22e-16) effectively disables the corresponding termination condition.
warn("Setting `{}` below the machine epsilon ({:.2e}) effectively "
Current asset value and volatility are [12.40012136 0.2121201 ]
Valeur de F en ce point : 0.0039537810988634625
d1= 1.3559235075207152
d2= 1.143803403426495
The default probability is 12.635258903691348 %
Conclusion, our code works ! BUT we would like to correct a light problem : our probability of default is too far from the desired value. If we round up, we get 12.6% and not 12.7%Do you know what we have to change to get a value closer to 12.7% ?
(ps: We are french. If it's necessary, I can translate comments in our code to English)
Thanks you !