在这里插入代码片
from scipy import stats
from scipy.optimize import fsolve
import numpy as np
def KMVOptSearch(E, D, r, T, EquityTheta):
EtoD = float(E) / float(D)
# print(EtoD)
# print()
# global i
# i = 1
def KMVfun(x, EtoD, r, T, EquityTheta):
EtoD, r, T, EquityTheta = float(EtoD), float®, float(T), float(EquityTheta)
d1 = (np.log(x[0] * EtoD) + (r + 0.5 * x[1] ** 2) * T) / (x[1] * np.sqrt(T))
d2 = d1 - x[1] * np.sqrt(T)
# global i
# print(“第{}次计算:”.format(i))
# i += 1
# print(x[0],x[1])
# print(x[0] * stats.norm.cdf(d1, 0.0, 1.0) - np.exp(-r * T) * stats.norm.cdf(d2, 0.0, 1.0) / EtoD - 1,
# stats.norm.cdf(d1, 0.0, 1.0) * x[0] * x[1] - EquityTheta
# )
# print()
return [
x[0] * stats.norm.cdf(d1, 0.0, 1.0) - np.exp(-r * T) * stats.norm.cdf(d2, 0.0, 1.0) / EtoD - 1,
stats.norm.cdf(d1, 0.0, 1.0) * x[0] * x[1] - EquityTheta
]
# print()
VaThetaX = fsolve(KMVfun, [1, 0.1], args=(EtoD, r, T, EquityTheta))
Va = VaThetaX[0] * E
AssetTheta = VaThetaX[1]
return Va, AssetTheta
# 无风险列表
r_list = [0.0251, 0.0173, 0.0150, 0.0150, 0.0150, 0.0150]
# 违约触发点列表
DP_list = [2140334854, 2406595530, 1680334683, 2363501441, 2470386057, 2947048737]
# 债券面值列表
D_list = [30983551225, 29893388116, 23062745183, 22235856439]
# 股权收益率波动率列表
PriceTheta_list = [0.4155, 0.6321, 0.3676, 0.2174, 0.1781, 0.1448]
# 股权市场价值列表
E_list = [7684982705, 6719038694, 5102104575, 5327292242, 4788084118, 3801898642]
for i in range(6):
# 利用KMV模型计算
r = r_list[i] # 无风险利率
T = 1 # 期权到期时间,以年为单位
SD = 1e8 # LD为长期负债 (单位:元)
LD = 50000000 # TD短期负债 (单位:元)
DP = DP_list[i] # 违约触发点DPT # DP=(TD-LD) +0.5LD (7) 其中:TD为总负债;LD为长期负债。 (单位:元)
D = DP # D=TD-LD+0.5∗LD 债券的面值是D (单位:元)
PriceTheta = PriceTheta_list[i] # 交易波动率,可以是日月年的数据 (单位:%)
EquityTheta = PriceTheta # 股权收益率的波动率σE
E = E_list[i] # 公司股票的市场价值 (单位:元)
Va, AssetTheta = KMVOptSearch(E, D, r, T, EquityTheta) # 资产价值VA(单位:元)资产收益率波动率σA
AssetTheta = AssetTheta # 资产收益率波动率σA (单位:%)
DD = (Va - DP) / (Va * AssetTheta) # 违约距离DD
EDF = stats.norm.cdf(-DD) # 预期违约概率EDF(单位:%)
print("第{}季度的情况:".format(i + 1))
print('Va=', Va)
print('AssetTheta=', AssetTheta)
print('DD=', DD)
print('EDF={}%'.format(EDF * 100))
print()