我已经构建了这个脚本,但我在其中找不到问题矩阵。这是我的脚本,用于回复呼叫标准选项:
N=50;
T=90/252;
dt=T/N;
K=102;
S0=100;
B0=1;
r=0.02;
sigma=0.25;
for i=1:N
ttomat(i)=(N-i+1)*dt; %+1 serve per aggiustare il tempo
d1(i)=(log(S(i,:)./K)+(r+0.5*sigma^2)*ttomat(i))./(sigma*sqrt(ttomat(i)));
d2(i)=d1(i)-sigma*sqrt(ttomat(i));
Call(i,:)=S(i,:).*normcdf(d1(i))-K*exp(-r*ttomat(i)*normcdf(d2(i)));
alpha(i,:)=normcdf(d1(i)); %delta della Call
beta(i,:)=(Call(i,:)-alpha(i,:).*S(i,:))./(B0*exp(r*(i-1)*dt));
end
您必须初始化/预分配结果,结果在循环中计算。在代码中,尚未预先分配结果。预分配可帮助您快速实现结果。预先分配所需变量始终是最佳做法。检查下面的代码,它对你有用吗?
N=50;
T=90/252;
dt=T/N;
K=102;
S0=100;
B0=1;
r=0.02;
sigma=0.25;
S = rand(N,1) ;
ttomat = zeros(1,N) ;
d1 = zeros(1,N) ;
d2 = zeros(1,N) ;
Call = zeros(1,N) ;
alpha = zeros(1,N) ;
beta = zeros(1,N) ;
for i=1:N
ttomat(i)=(N-i+1)*dt; %+1 serve per aggiustare il tempo
d1(i)=(log(S(i,:)./K)+(r+0.5*sigma^2)*ttomat(i))./(sigma*sqrt(ttomat(i)));
d2(i)=d1(i)-sigma*sqrt(ttomat(i));
Call(i,:)=S(i,:).*normcdf(d1(i))-K*exp(-r*ttomat(i)*normcdf(d2(i)));
alpha(i,:)=normcdf(d1(i)); %delta della Call
beta(i,:)=(Call(i,:)-alpha(i,:).*S(i,:))./(B0*exp(r*(i-1)*dt));
end