MATLAB: Index exceeds the number of array

MATLAB

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)

Best Answer

Ah, ok. You flip between treating S1 as a vector containing the different stock prices (which it is in your new code) and as a scalar containing just the stock price at time t = 0 (which it is in your original code).
for i=1:length(S1) % <-- first use
theta1(i)=max(S1(k)-K, ((1/b)*exp(-r*(dt))*(sum(theta2((i-1)*b+1:i*b)))));
% ^ second use
end
For example, in this loop containing the faulty line, you first treat S1 as a scalar. In your original code, length(S1) would be 1, and so the loop would run only once and you wouldn't get an error. However, here S1 contains all of the stock prices from S, so length(S1) is not 1. You acknowledge this in your second use of S1, by indexing with k to pull out the single stock price of interest, but you need to be consistent and use length(S1(k)) in your first use as well.
I tried to take this into consideration below. I also moved the bits where you calculate lowest, cil, cih, and pe to be within the loop, assuming you want to calculate those values for each initial stock price.
% 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];
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];
N=numel(S);
highest=zeros(N,1);
lowest=zeros(N,1);
cil=zeros(N,1);
cih=zeros(N,1);
pe=zeros(N,1);
for k=1:N
S1=S(k); % stock price at time t = 0
for i=1:b
z=normrnd(0,1);
S2(i)=S1*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, ((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)
teta3(i)=(1/b)*(sum(theta4((i-1)*b+1:i*b)));
end
for i=1:length(S2)
teta2(i)=(1/b)*(sum(teta3((i-1)*b+1:i*b)));
end
for i=1:length(S1)
teta1(i)=(1/b)*(sum(teta2((i-1)*b+1:i*b)));
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(k)=max((S1-K),lowest(k)-z*0.124);
cih(k)=highest(k)+z*0.124;
% point estimate
pe(k)=0.5*max((S1-K),lowest(k))+0.5*highest(k);
end
% relative error
% construct the table
T=table(S,highest,lowest,cil,cih,pe,stderrl,stderrh)
This does run without error, and the table it creates has a row for each initial stock price, but you will want to verify that I didn't accidentally mess up some of the logic.
As a side note, wherever you have
NaN=0;
I am not sure this is doing what you might think it is doing. Are you hoping to set NaN values within your matrices to 0? Because this instead creates a variable named NaN with a value of 0, so that in the loop's next iteration, lines such as
stockprice=NaN*ones(size(S4,1),4);
are equivalent to
stockprice=0*ones(size(S4,1),4);
or essentially
stockprice=zeros(size(S4,1),4);
Same result, eventually, but misleading. To set all NaN values within stockprice to 0, you could use
stockprice(isnan(stockprice)) = 0;
However, I'll admit that I'm not 100% sure what you're trying to do there.