刚开始使用matlab编写代码,因为我正在研究Higham的书金融期权评估简介。他给出的示例代码块之一(this is the source)是:
V = zeros(M,1);
Vanti = zeros(M,1);
for i = 1:M
samples = randn(N,1);
% standard Monte Carlo
Svals = S*cumprod(exp((r-0.5*sigma^2)*Dt+sigma*sqrt(Dt)*samples));
Smax = max(Svals);
if Smax < B
V(i) = exp(-r*T)*max(Svals(end)-E,0);
end
% antithetic path
Svals2 = S*cumprod(exp((r-0.5*sigma^2)*Dt-sigma*sqrt(Dt)*samples));
Smax2 = max(Svals2);
V2 = 0;
if Smax2 < B
V2 = exp(-r*T)*max(Svals2(end)-E,0);
end
Vanti(i) = 0.5*(V(i) + V2);
end
我试图让这个循环更有效率,所以我试图删除for循环。这是我到目前为止写的:
V = zeros(M,1);
Vanti = zeros(M,1);
samples = randn(N,M);
Svals = S*cumprod(exp((r-0.5*sigma^2)*Dt+sigma*sqrt(Dt)*samples));
Svals2 = S*cumprod(exp((r-0.5*sigma^2)*Dt-sigma*sqrt(Dt)*samples));
Send = Svals(end,:);
Send2 = Svals2(end,:);
Smax = max(Svals);
Smax2 = max(Svals2);
V2 = zeros(M,1);
for i = 1:M
if Smax(i) < B
V(i) = exp(-r*T)*max(Send(i)-E,0);
end
if Smax2(i) < B
V2(i) = exp(-r*T)*max(Send2(i)-E,0);
end
end
Vanti = 0.5*(V + V2);
aM = mean(V); bM = std(V);
conf = [aM - 1.96*bM/sqrt(M), aM + 1.96*bM/sqrt(M)]
aManti = mean(Vanti); bManti = std(Vanti);
confanti = [aManti - 1.96*bManti/sqrt(M), aManti + 1.96*bManti/sqrt(M)]
toc
这已经使代码显着更快,因为在循环内部没有生成任何randn变量。但是,我不知道如何删除循环的其他部分。它甚至可能吗?