一、BS公式
1、MATLAB函数:
function BS=BS(S0,K,T,r,sigma,IsCall)
d1=(log(S0/K)+r*T)/(sigma*sqrt(T))+0.5*sigma*sqrt(T);
if IsCall==1
price=S0*normcdf(d1)-K*exp(-T*r)*normcdf(d1-sigma*sqrt(T));
else
price=-S0*normcdf(-d1)+K*exp(-T*r)*normcdf(-d1+sigma*sqrt(T));
end
BS=price;
end
2、VBA函数:
Public Function BSprice(S, k, T, R, q, Sigma As Double, IsCall As Boolean)
'===========================================================
'BS定价计算模块
'输入参数:
'——→ S:标的价
'——→ K:执行价
'——→ T:到期期限
'——→ r:无风险利率
'——→ q:红利率
'——→ Sigma:波动率
'——→ IsCall:是否看涨期权,输入True为看涨,输入False为看跌
'输出参数
'——→ BSprice:欧式期权价格
'===========================================================
Dim D1, d2, Price As Double
D1 = (Log(S / k) + (R - q + Sigma ^ 2 / 2) * T) / (Sigma * Sqr(T))
d2 = D1 - Sigma * Sqr(T)
If IsCall Then
Price = S * Exp(-q * (T)) * Application.WorksheetFunction.Norm_S_Dist(D1, True) - k * Exp(-R * (T)) * Application.WorksheetFunction.Norm_S_Dist(d2, True)
Else
Price = k * Exp(-R * (T)) * Application.WorksheetFunction.Norm_S_Dist(-d2, True) - S * Exp(-q * (T)) * Application.WorksheetFunction.Norm_S_Dist(-D1, True)
End If
BSprice = Price
End Function
二、蒙特卡洛模拟
MATLAB函数:
function BS_MC=BS_MC(S0,K,T,r,sigma,IsCall)
N=1000;
dt=T/N;
Number=10000;
S=zeros(N,Number);
S(1,:)=S0*ones(1,Number);
NormRand = randn(N,Number);
for n=2:N+1
S(n,:)=S(n-1,:)+r*S(n-1,:)*dt+sigma*S(n-1,:).*NormRand(n-1,:)*sqrt(dt);
end
if IsCall==1
price=exp(-r*T)*sum(max(S(N+1,:)-K,0))/Number;
else
price=exp(-r*T)*sum(max(K-S(N+1,:),0))/Number;
end
BS_MC=price;
end
|
|