如何用MATLAB实现随机游走算法?我不理解这个算法,因为条件3永远不会成立。我最后发布了我的代码。
选择时间步
分发的独立随机变量h>0
,以便M=9/h
为整数 设置log(L_0) = L_0 = 0.13
和Z(0)=0
。
rho_k
是由法律P(+-1)=0.5
1) log(L_{k+1}) = log(L_k)-0.5*sigma^2*h+sigma*sqrt(h)*rho_{k+1} 2) Z_{k+1} = Z_k - sigma* [(erfc((2*2^(1/2)*(log((25*exp(log L_k))/7) + 9/32))/3)/2 - erfc((2*2^(1/2)*(log(100*exp(log L_k)) + 9/32))/3)/2 + erfc((2*2^(1/2)*(log(7/(25*exp(log L_k))) - 9/32))/3)/56 - erfc((2*2^(1/2)*(log(196/(25*exp(log L_k))) - 9/32))/3)/56 + (exp(log L_k)*((2*2^(1/2)*exp(-(8*(log(7/(25*exp(log L_k))) - 9/32)^2)/9))/(3*exp(log L_k)*pi^(1/2)) - (2*2^(1/2)*exp(-(8*(log(196/(25*exp(log L_k))) - 9/32)^2)/9))/(3*exp(log L_k)*pi^(1/2))))/28 - exp(log L_k)*((2*2^(1/2)*exp(-(8*(log((25*exp(log L_k))/7) + 9/32)^2)/9))/(3*exp(log L_k)*pi^(1/2)) - (2*2^(1/2)*exp(-(8*(log(100*exp(log L_k)) + 9/32)^2)/9))/(3*exp(log L_k)*pi^(1/2))) - (14*2^(1/2)*exp(-(8*(log(7/(25*exp(log L_k))) + 9/32)^2)/9))/(75*exp(log L_k)*pi^(1/2)) + (14*2^(1/2)*exp(-(8*(log(196/(25*exp(log L_k))) + 9/32)^2)/9))/(75*exp(log L_k)*pi^(1/2)) + (2^(1/2)*exp(-(8*(log((25*exp(log L_k))/7) - 9/32)^2)/9))/(150*exp(log L_k)*pi^(1/2)) - (2^(1/2)*exp(-(8*(log(100*exp(log L_k)) - 9/32)^2)/9))/(150*exp(log L_k)*pi^(1/2))))]*sqrt(h)*rho_{k+1}; 3) log L_k < log(H)+1/2*sigma^2*h-sigma*sqrt(h)
H=0.28 sigma=0.25 K=0.01
为了实现算法,我们遵循(1)生成的随机游走,
每次t_k
;我们检查条件3是否成立。如果没有,L_k
已到达边界区域,我们将链接停在log(H)
。如果是的话,
我们执行(1) - (2)查找log(L_k+1)
和Z_{k+1}
。 If k+1=M;
我们停止,否则我们继续算法。
模拟每个轨迹的结果是一个点(t_kappa, log(L_kappa), Z_kappa)
。
使用蒙特卡罗技术评估期望E[(exp(log(L_{kappa}))-K)^+ * Chi(kappa=M) + Z_{kappa}]=price
并执行10^6
蒙特卡洛运行。
这是我的代码。它没有用,因为我没有得到0.0657
附近的东西(结果)。
Y = zeros(1,M+1); %ln L_k = Y(k)
Z = zeros(1,M+1);
sigma = 0.25;
H = 0.28;
K = 0.01;
Y(1) = 0.13;
Z(1) = 0;
M = 90;
h = 0.1;
for k = 1:M+1
vec = [-1 1];
index = random('unid', length(vec),1);
x(1,k) = vec(index);
end %'Rho'
for k = 1:M
if Y(k) < log(H) + 1/2*sigma^2*h - sigma*sqrt(h) %Bedingung
Y(k+1) = Y(k) - (1/2)*(sigma)^2*h + sigma*sqrt(h)*x(k+1);
Z(k+1) = Z(k) + (-sigma*(erfc((2*2^(1/2)*(log((25*exp(Y(k)))/7) + 9/32))/3)/2 - erfc((2*2^(1/2)*(log(100*exp(Y(k))) + 9/32))/3)/2 + erfc((2*2^(1/2)*(log(7/(25*exp(Y(k)))) - 9/32))/3)/56 - erfc((2*2^(1/2)*(log(196/(25*exp(Y(k)))) - 9/32))/3)/56 + (exp(Y(k))*((2*2^(1/2)*exp(-(8*(log(7/(25*exp(Y(k)))) - 9/32)^2)/9))/(3*exp(Y(k))*pi^(1/2)) - (2*2^(1/2)*exp(-(8*(log(196/(25*exp(Y(k)))) - 9/32)^2)/9))/(3*exp(Y(k))*pi^(1/2))))/28 - exp(Y(k))*((2*2^(1/2)*exp(-(8*(log((25*exp(Y(k)))/7) + 9/32)^2)/9))/(3*exp(Y(k))*pi^(1/2)) - (2*2^(1/2)*exp(-(8*(log(100*exp(Y(k))) + 9/32)^2)/9))/(3*exp(Y(k))*pi^(1/2))) - (14*2^(1/2)*exp(-(8*(log(7/(25*exp(Y(k)))) + 9/32)^2)/9))/(75*exp(Y(k))*pi^(1/2)) + (14*2^(1/2)*exp(-(8*(log(196/(25*exp(Y(k)))) + 9/32)^2)/9))/(75*exp(Y(k))*pi^(1/2)) + (2^(1/2)*exp(-(8*(log((25*exp(Y(k)))/7) - 9/32)^2)/9))/(150*exp(Y(k))*pi^(1/2)) - (2^(1/2)*exp(-(8*(log(100*exp(Y(k))) - 9/32)^2)/9))/(150*exp(Y(k))*pi^(1/2))))*sqrt(h)*x(k+1);
else
Y(k)=log(H);
break
end
end
if k == M
end
if (L(M)-K) > 0
(L(M)-K);
else
0;
end
return
exp(Y(M))-K+Z(M)