Last active
August 3, 2021 08:07
-
-
Save fmarthoz/33227575768b63a82840b1617050d467 to your computer and use it in GitHub Desktop.
Implementation of the Contingent Claim Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prob_default(ticker): | |
""" DEFINING THE FUNCTION WE WANT TO OPTIMISE""" | |
def fun(x): | |
va=x[0] | |
siga=x[1] | |
d1=(np.log(va/X)+(r+0.5*siga**2)*T)/(siga*np.sqrt(T)) | |
d2=d1-siga*np.sqrt(T) | |
return [va*norm.cdf(d1)-X*np.exp(-r*T)*norm.cdf(d2)-ve,va*norm.cdf(d1)*siga/ve-sige] | |
"""DATA SELECTION""" | |
start_date='2018-01-01' | |
end_date='2018-12-31' | |
# get data for that ticker | |
daily_=daily[(daily.ticker==ticker)&(daily.date>=start_date)&(daily.date<=end_date)] | |
# if after selecting the period, we get an empty dataframe, we skip that ticker | |
if daily_.shape[0]==0: | |
print(ticker+" is empty") | |
return False | |
daily_=daily_.reset_index() | |
daily_.marketcap=daily_.marketcap*1e6 | |
"""CALCULATING EQUITY RETURNS""" | |
for j in range(1,daily_.shape[0]): | |
daily_.loc[j-1,'returns']=np.log(daily_.loc[j-1,'marketcap']/daily_.loc[j,'marketcap']) | |
"""CALCULATING THE VOLATILITY OF EQUITY: SIGE""" | |
sige=np.std(daily_.returns)*np.sqrt(252) | |
"""SOLVE SIMULTANEOUS EQUATIONS for 2018 and 2017""" | |
#2018 | |
# Initialising values | |
T=1 | |
r=0.017 | |
ve=daily_.loc[0,'marketcap'] | |
X=df[df.ticker==ticker]['debtc'].values[0]+.5*df[df.ticker==ticker]['debtnc'].values[0] | |
sol = optimize.root(fun, [X+ve, sige*ve/(ve+X)]) | |
va=sol.x[0] | |
siga=sol.x[1] | |
#2017 | |
T=1 | |
r=0.015 | |
ve=daily_.loc[daily_.shape[0]-1,'marketcap'] | |
X=df[df.ticker==ticker]['debtc'].values[0]+.5*df[df.ticker==ticker]['debtnc'].values[0] | |
sol = optimize.root(fun, [X+ve, sige*ve/(ve+X)]) | |
va_1=sol.x[0] | |
siga=sol.x[1] | |
#this gives the annual return | |
mu=max((va-va_1)/va_1,r) | |
DDt=(np.log(va/X)+(mu-0.5*siga**2)*T)/siga*np.sqrt(T) | |
return norm.cdf(-DDt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment