Python Forum
Final Projet - Credit Risk
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Final Projet - Credit Risk
#1
Smile 
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 Rolleyes
Reply
#2
At first sight this program runs smootly.
What exactly goes wrong ? (I cannot comment on the results.)
Paul
Output:
d1 = 1.3549082647995547 d2 = 1.1426082647995548 The default probability of the company is 0.1266006362637757
Reply
#3
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).
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help needed, stuck at the final output extricate 0 1,534 Jun-20-2020, 05:35 AM
Last Post: extricate
  Final Project (Databases, GUI, and Classes) poochenthecreator 1 1,638 Apr-27-2020, 09:58 PM
Last Post: deanhystad
  School projet robalexx 1 1,934 May-03-2019, 10:41 AM
Last Post: heiner55
  Final Project Help hyg71886 6 3,940 Feb-07-2019, 01:30 AM
Last Post: micseydel
  Trouble with edX Python Final sarah_mb_sues 11 13,731 Jun-19-2018, 10:36 AM
Last Post: cryomick
  Final problem Truman 4 4,014 Jan-22-2018, 11:42 PM
Last Post: Truman
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,091 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