%test KMV
%r: risk-free rate
%T: Time to expiration T=1;%输入 月数
%DP:Defaut point;DP=SD+0.5*LD;SD: short debt, LD: long debt
%D:Debt maket value D=DP;%债务的市场价值,可以修改
%theta: volatility
%PriceTheta: volatility of stock price
%EquityTheta: volatility of Theta value
%AssetTheta: volatility of asset
%E:Equit maket value
%Va: Value of asset
%%%%%%%%%%%变量设定
%sheet1 是股权波动率 EquityTheta
%sheet2 股权价值-元 E
%sheet3 银行违约点-元 DP
%sheet4 无风险利率 r
%sheet5 T 将设定债务到期期限
%sheet6 D 债务的市场价值 元
clc;clear;
data1=xlsread('textdatafavar.xls','sheet1');
data2=xlsread('textdatafavar.xls','sheet2');
data3=xlsread('textdatafavar.xls','sheet3');
data4=xlsread('textdatafavar.xls','sheet4');
data5=xlsread('textdatafavar.xls','sheet5');
data6=xlsread('textdatafavar.xls','sheet6');
[a,b]=size(data1);
function E=EstAssetValue(D,r,T,AssetTheta,Va)
d1=( log(Va/D)+(r+0.5*AssetTheta^2)*T ) / (AssetTheta*sqrt(T));
d2=d1-AssetTheta*sqrt(T);
E=Va*normcdf(d1)-exp(-r*T)*normcdf(d2)*D;
function [Va,AssetTheta,fval,exitflag]=KMVOptSearch(E,D,r,T,EquityTheta)
%KMVOptSearch
%code by ariszheng@gmail.com
EtoD=E/D;
x0=[1,1];%搜素初始点
[VaThetaX,fval,exitflag]=fsolve(@(x) KMVfun(EtoD,r,T,EquityTheta,x), x0);
Va=VaThetaX(1)*E;
AssetTheta=VaThetaX(2);
% F=KMVfun(EtoD,r,T,EquityTheta,x)
function F=KMVfun(EtoD,r,T,EquityTheta,x)
d1=( log(x(1)*EtoD)+(r+0.5*x(2)^2)*T ) / ( x(2)*sqrt(T));
d2=d1-x(2)*sqrt(T);
F=[ x(1)*normcdf(d1)-exp(-r*T)*normcdf(d2)/EtoD-1;
normcdf(d1)*x(1)*x(2)-EquityTheta];
for i=1:b
for j=1:a
EquityTheta=data1(j,i);
E=data2(j,i);
D=data6(j,i);
DP=data3(j,i);
r=data4(j);
T=data5(j);
[Va(j,i),AssetTheta(j,i),fval,exitflag(i)]=KMVOptSearch(E,D,r,T,EquityTheta); %to compute the Va and AssetTheta
DD(j,i)=(Va(j,i)-DP)/(Va(j,i)*AssetTheta(j,i)); %计算违约距离
EDF(j,i)=normcdf(-DD(j,i)); %计算预期违约率
end
end
for m=1:14
for n=1:a
DP=data6(n,m);
r=data4(n);
T=data5(n);
d1(n,m)=(log(Va(n,m)/DP)+(r+0.5*AssetTheta(n,m)^2)*T)/(AssetTheta(n,m)*sqrt(T));
d2(n,m)=d1(n,m)-AssetTheta(n,m)*sqrt(T);
creditpremiumDP(n,m)=-1/T*log(normcdf(d2(n,m))+Va(n,m)/(DP*exp(-r*T))*normcdf(-d1(n,m))); %内生的信用溢价
end
end
junzhi=mean(creditpremiumDP);
biaozhuncha=std(creditpremiumDP);