Index error for the "ELS-Price choice code"

1 view (last 30 days)
형현
형현 on 26 Sep 2023
Edited: Voss on 26 Sep 2023
ns=10000;
face=10000;
val_date='2023-09-26';
mid_date= ['2024-03-21';'2024-09-23';'2025-03-21';'2025-09-23';'2026-03-23';'2026-09-18'];
strike=[1 0.95 0.9 0.85 0.8 0.75];
c_rate=[0.05 0.1 0.15 0.2 0.25 0.3];
dummy=0.1;
ki=0.5;
ki_YesNO='NO';
ref_S= [9179.5];
S=[6130];
r=0.05;
vol=[0.09];
rho=0.37;
temp_ch=datenum(mid_date)'-datenum(val_date);
mid_ch=temp_ch(find(temp_ch>=0));
c_rate=c_rate(find(temp_ch>=0));
strike=strike(find(temp_ch>=0));
N=mid_ch(end);
dt=1/365;
mid_size=length(mid_ch);
payment=zeros(ns,mid_size);
for i =1:ns
for j=1:mid_size
payment(i,j)=face*(1+c_rate(j));
end
end
corr=[1 rho;rho 1];
M=chol(corr);
SP1=zeros(ns,N+1);
SP1(:,1)=S(1);
for i=1:ns
w0=randn(N,2);
w=w0*M;
for j=2:N+1
SP1(i,j)=SP1(i,j-1)*exp((r-vol(1)^2/2)*dt+...
vol(1)*sqrt(dt)*w(j-1,1));
end
end
R1=SP1/ref_S(1);
WP=min(R1,[],2);
strike_ch=WP(:,mid_ch+1);
payoff=zeros(ns,mid_size);
for i=1:ns
for j=1:mid_size
if strike_ch(i,j)>=strike(j)
payoff(i,j)=payment(i,j);
break
end
end
continue
end
for i=1:ns
if payoff(i,:)==0
switch ki_YesNo
case 'NO'
ki_event=any(WP(i,:)<ki);
if ki_event==1
payoff(i,end)=face*WP(i,end);
else
payoff(i,end)=face*(1+dummy);
end
case 'Yes'
payoff(i,end)=face*WP(i,end);
otherwise
error('ki_YesNO = Yes or No');
end
end
end
exp_payoff=mean(payoff);
disc_payoff=zeros(1,mid_size);
for j=1:mid_size
disc_payoff(j)=exp_payoff(j)*exp(-r*mid_ch(j)/365);
end
ELS_Price=sum(disc_payoff)
--------------------------------------------------
If i run this code, there are "Index of location 2 exceeds array boundary. Index must not exceed 1." error occured, What should i do to solve this error code...

Answers (1)

Angelo Yeo
Angelo Yeo on 26 Sep 2023
In line 43, see that WP is an array of 10000 x 1. However, you are trying to extract elements by WP(:, mid_ch+1) where mid_ch is [177, 363, 542, 728, 909, 1088].
  2 Comments
형현
형현 on 26 Sep 2023
I changed array of 10000x1 to 1x1089 vector. ___ Code change(WP=min(R1,[],2); → WP=min(R1);)
But i still have error code "Index of location 1 exceeds array boundary. Index must not exceed 1
Angelo Yeo
Angelo Yeo on 26 Sep 2023
I would like to recommend debugging functionalities of MATLAB, and try to run the script line by line.
In your new code, see in line 47 that strike_ch is a 1x6 sized array, and you are accessing it by strike_ch(i, j). However, your iterator i changes its value from 1 to 10000.

Sign in to comment.

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!