index exceeds the number of array
Show older comments
Hi. I need help with my code. If you could provide help/suggestions, it would be greatly appreciated. Thank you
% generate the tree
K=100; r=0.05; delta=0.1; sigma=0.2; n=100; b=2; dt=1/3;
S=[70;80;90;100;110;120;130];
S1=S*ones(1,1); % stock price at time t = 0
S2=zeros(b,1); % stock price at time t = 1/3
S3=zeros(b*b,1); % stock price at time t = 2/3
S4=zeros(b*b*b,1); % stock price at time t = 1
stderrl=[0.004;0.016;0.039;0.076;0.156;0.139;0.124];
stderrh=[0.004;0.016;0.040;0.078;0.113;0.069;0.049];
truevalue=[0.121;0.670;2.303;5.731;11.341;20;30];
o=length(S);
for k=1:o
for i=1:b
z=normrnd(0,1);
S2(i)=S1(k)*exp((r-delta-(sigma)^2)*(dt)+sigma*sqrt(dt)*z);
end
for i=1:b
for j=1:b
z=normrnd(0,1);
S3((i-1)*b+j)=S2(i)*exp((r-delta-(sigma)^2)*(dt)+sigma*sqrt(dt)*z);
end
end
for i=1:b*b
for j=1:b
z=normrnd(0,1);
S4((i-1)*b+j)=S3(i)*exp((r-delta-(sigma)^2)*(dt)+sigma*sqrt(dt)*z);
end
end
stockprice=NaN*ones(size(S4,1),4);
stockprice(1:size(S1,1),1)=S1;
stockprice(1:size(S2,1),2)=S2;
stockprice(1:size(S3,1),3)=S3;
stockprice(1:size(S4,1),4)=S4;
NaN=0;
% work backward to price the option at each node for the high estimator
theta4=max(S4-K,0);
for i=1:length(S3)
theta3(i)=max(S3(i)-K,((1/b)*exp(-r*(dt))*(sum(theta4((i-1)*b+1:i*b)))));
end
for i=1:length(S2)
theta2(i)=max(S2(i)-K,((1/b)*exp(-r*(dt))*(sum(theta3((i-1)*b+1:i*b)))));
end
for i=1:length(S1)
theta1(i)=max(S1(k)-K, ((1/b)*exp(-r*(dt))*(sum(theta2((i-1)*b+1:i*b)))));
end
% high estimator
bigtheta=NaN*ones(size(theta4,1),4);
NaN=0;
bigtheta(1:size(theta1,1),1)=theta1;
bigtheta(1:size(theta2',1),2)=theta2;
bigtheta(1:size(theta3',1),3)=theta3;
bigtheta(1:size(theta4,1),4)=theta4;
highest(k)=theta1;
% work backward to price the option at each node for the low estimator
for i=1:length(S3)
if max(S3(i)-K) >= (1/(b-1))*(exp(-r*dt)*(sum(theta4((i-1)*b+1:i*b))));
teta3(i)=max(S3(i)-K);
else
teta3(i)=exp(-r*dt)*(sum(theta4((i-1)*b+1:i*b)));
end
teta3(i)=(1/b)*(sum(theta4((i-1)*b+1:i*b)));
end
for i=1:length(S2)
if max(S2(i)-K) >= (1/(b-1))*(exp(-r*dt)*(sum(teta3((i-1)*b+1:i*b))));
teta2(i)=max(S2(i)-K);
else
teta2(i)=exp(-r*dt)*(sum(teta3((i-1)*b+1:i*b)));
end
teta2(i)=(1/b)*(sum(teta3((i-1)*b+1:i*b)));
end
for i=1:length(S1)
if max(S1(i)-K) >= (1/(b-1))*(exp(-r*dt)*(sum(teta2((i-1)*b+1:i*b))));
teta1(i)=max(S1(i)-K);
else
teta1(i)=exp(-r*dt)*(sum(teta2((i-1)*b+1:i*b)));
end
teta1(i)=(1/b)*(sum(teta2((i-1)*b+1:i*b)));
end
end
% low estimator
smalltheta=NaN*ones(size(theta4,1),4);
NaN=0;
smalltheta(1:size(teta1,1),1)=teta1;
smalltheta(1:size(teta2',1),2)=teta2;
smalltheta(1:size(teta3',1),3)=teta3;
smalltheta(1:size(theta4,1),4)=theta4;
lowest(k)=teta1
% 90% confidence interval
z=1.645;
cil=max((S(i)-K),lowest(k)-z*0.124);
cih=highest(k)+z*0.124;
% point estimate
pe=0.5*max((S(i)-K),lowest(k))+0.5*highest(k);
% relative error
% construct the table
T=table(S1,highest,lowest,cil,cih,pe,stderrl,stderrh)
3 Comments
Tommy
on 15 Apr 2020
theta1(i)=max(S1(k)-K, ((1/b)*exp(-r*(dt))*(sum(theta2((i-1)*b+1:i*b)))));
In this line, how do you know the indices given by
(i-1)*b+1:i*b
will not exceed the dimensions of theta2?
AB
on 15 Apr 2020
Tommy
on 15 Apr 2020
In these lines:
for i=1:length(S3)
if max(S3(i)-K) >= (1/(b-1))*(exp(-r*dt)*(sum(theta4((i-1)*b+1:i*b))));
teta3(i)=max(S3(i)-K);
else
teta3(i)=exp(-r*dt)*(sum(theta4((i-1)*b+1:i*b)));
end
teta3(i)=(1/b)*(sum(theta4((i-1)*b+1:i*b)));
end
You set teta3(i) within the if-else statement and then immediately overwrite that value. Is that intentional?
Accepted Answer
More Answers (0)
Categories
Find more on Financial Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!