Final Projet - Credit Risk - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Final Projet - Credit Risk (/thread-30980.html) |
Final Projet - Credit Risk - Rauchvant - Nov-16-2020 Hello dear community, We are a group of 2 persons (me Antoine and my teammate Julien) and we began the Python coding this month. To explain the context of our problem, we study the finance. Today, we work on our final project about Credit Risk. I will show our topic with the example and our work. It's a beginning for us, but we would like tu understand why our program doesn't work and how we can correct the error. There is a little reading but it's necessary if you want to understand our exercice and what is wrong in our code (and yes I organize my projects like a child with some sticking). TOPIC : 1 : https://zupimages.net/viewer.php?id=20/47/4092.jpg 2 : https://zupimages.net/viewer.php?id=20/47/8ns9.jpg At the end of the second picture, it's our first try that you will find below : """ Created on Thu Nov 12 17:17:27 2020 @author: Antoine CHEVALIER """ 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))Thank you for your help, I'm waiting your remarks RE: Final Projet - Credit Risk - DPaul - Nov-16-2020 At first sight this program runs smootly. What exactly goes wrong ? (I cannot comment on the results.) Paul
RE: Final Projet - Credit Risk - Rauchvant - Nov-16-2020 Dear community, Mea culpa, my code works! I don't now how is it possible because yesterday it was not ! But I have to improve it to have results closer to those requested, I have to find a d2 more closer to 1.408 (not 1.426). RE: Final Projet - Credit Risk - Rauchvant - Nov-18-2020 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 : 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 : 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 : 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 ! |