Python Forum
Final Projet - Credit Risk
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Final Projet - Credit Risk
#4
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 :

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 !
Reply


Messages In This Thread
Final Projet - Credit Risk - by Rauchvant - Nov-16-2020, 02:22 PM
RE: Final Projet - Credit Risk - by DPaul - Nov-16-2020, 03:49 PM
RE: Final Projet - Credit Risk - by Rauchvant - Nov-16-2020, 03:58 PM
RE: Final Projet - Credit Risk - by Rauchvant - Nov-18-2020, 03:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help needed, stuck at the final output extricate 0 1,618 Jun-20-2020, 05:35 AM
Last Post: extricate
  Final Project (Databases, GUI, and Classes) poochenthecreator 1 1,783 Apr-27-2020, 09:58 PM
Last Post: deanhystad
  School projet robalexx 1 2,033 May-03-2019, 10:41 AM
Last Post: heiner55
  Final Project Help hyg71886 6 4,145 Feb-07-2019, 01:30 AM
Last Post: micseydel
  Trouble with edX Python Final sarah_mb_sues 11 14,093 Jun-19-2018, 10:36 AM
Last Post: cryomick
  Final problem Truman 4 4,183 Jan-22-2018, 11:42 PM
Last Post: Truman
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,233 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020