MATLAB Answers

Index exceeds array bounds error

10 views (last 30 days)
Dear all,
I wrote this simple code:
%input data
SP=[exp(-(0.01*0.5)/(1-0)); exp(-(0.01*1)/(1-0))];
m=3;
k=2;
b=0.01;
dt=0.5;
w=[100 200 300; 100 300 400];
a0=[0 0]; % guess per fsolve
fun=@(a) (get_survivalprobability(a,b,w,dt,m,k)-SP);
[a] = fsolve(fun,a0); % find f(a)=0 ;
for i=1:k % find hazard rates with the new parameters a
for j=1:m
h(i,j)=exp(a(i)+b*w(i,j))
end
end
whose main function that I implemented is:
function [SP_Simulation]=get_survivalprobability(a,b,w,dt,m,k)
for i=1:k
for j=1:m
hzrd(i,j)=exp(a(i)+b*w(i,j)) ;
end
end
h1=hzrd(1,:) ;
SP1=(sum(exp(h1*-dt)))/m ; % find a for the first row vector
for i=1:k-1
h=(hzrd(i,:)+hzrd(i+1,:))*-dt;
SP_others=sum(exp(h),2)/m ;
SP_Simulation=[SP1;SP_others];
end
end
For the previous example, where " w " is a matrix 2x3 , everything works. However, when I want to implement this function for a different input matrix data " w " of dimension 253x100 , the following error displays:
" Index exceeds array bounds. Error in get_survivalprobability (line 6)
hzrd(i,j)=exp(a(i)+b*w(i,j)) ; "
Does anyone know how can I fix it, please?
Thank you in advance.
Regards,
Martina

  1 Comment

Adam Danz
Adam Danz on 15 Jul 2019
When I use your code but exchange w with a 253x100 matrix, there is no error.
w = randi(500,253,100)

Sign in to comment.

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 15 Jul 2019
Edited: KALYAN ACHARJYA on 15 Jul 2019
Yes no error with
w=rand(253,100);
Check:
>> whos w
Name Size Bytes Class Attributes
w 253x100 202400 double

  3 Comments

KALYAN ACHARJYA
KALYAN ACHARJYA on 15 Jul 2019
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
h =
0.0100
h =
0.0100 0.0100
h =
0.0100 0.0100 0.0100
h =
0.0100 0.0100 0.0100
0.0100 0 0
h =
0.0100 0.0100 0.0100
0.0100 0.0100 0
h =
0.0100 0.0100 0.0100
0.0100 0.0100 0.0100
KALYAN ACHARJYA
KALYAN ACHARJYA on 15 Jul 2019
Second case, I dont expect any error in this case, as you defind
k=2;
m=3;
for i=1:k
for j=1:m
hzrd(i,j)=exp(a(i)+b*w(i,j)) ;
end
....
i iterate from 1 to 2 and j iterate 1 to 3, so the w elements are there with those indices.
Martina Della Monica
Martina Della Monica on 15 Jul 2019
Thank you for your quick and detailed answer.
I have already check with rand(253,100) before writing there, because I was thought that my code was wrong.
Sorry if I bother you again, but I really don't understand why it shows me this kind of error.
The code where I am extrapolating " w " is the following:
S_0= 1.91;
Maturity=1;
sigma_annual=0.15;
r=0.02;
sigma_daily=sigma_annual/sqrt(252);
mu_daily= (r-sigma_daily^2)/0.5;
npaths= 100 ;
nsteps=252;
dt=Maturity/252 ;
t=(0:nsteps)'*dt;
Z=randn(nsteps,npaths);
Weiner= [zeros(1,npaths); cumsum(Z).*sqrt(dt)] ;
S_t= bsxfun(@plus, (mu_daily-0.5*sigma_daily.^2).*t, sigma_daily*Weiner);
S_t=S_0*exp(S_t) ;
F_0= S_0*exp(r*Maturity) ; %forward price at time
w =(S_t - F_0*exp(-r*(Maturity-t))); % forward contract value
SP=repmat(0.995,1,253)
b=0.01;
dt=0.5;
m=npaths;
k=nsteps+1
a0=[0 0]; % guess per fsolve
fun=@(a) (get_survivalprobability(a,b,w,dt,m,k)-SP);
[a] = fsolve(fun,a0);
for i=1:k
for j=1:m
h(i,j)=exp(a(i)+b*w(i,j))
end
end
Could you try with these new values and check if it shows the same error, please?
Thank you in advance again, I will really appreciate your help.

Sign in to comment.

More Answers (0)