MATLAB: How to eliminate the error in the following code

file 1:
function [a]=main_MCNIG()
r=[0.05 0.015];
params=[50 40;-50 -40;35 35];
theta1=[0.77663 0.17482];theta2=[0.1585 1.3583];
m=10;
A=[-0.5 0.5;.5 -0.5];
S0=100;
K=60;
X0=1;
T=1;
L=10;
J=10;
[a]=MonteCarlo_callNIG(r,theta1,theta2,params,m,A,K,S0,X0,T,L);
end
file 2:
function [x]=MonteCarlo_callNIG(r,theta1,theta2,params,m,A,K,S0,X0,T,L)
alpha=params(1,:);beta=params(2,:);mu=params(3,:);delta=params(4,:);% NIG parameters.
%L=Number of trajectories
J=2^m; %Number of time steps
Ch=zeros(L,J);
alpha_s=zeros(L,J);
beta_s=zeros(L,J);
delta_s=zeros(L,J);
Mu=zeros(L,J);
R=zeros(L,J);
Theta1=zeros(L,J);% Esscher parameters for case of regime-switching is priced.
Theta2=zeros(L,J);% Esscher parameters for case of regime switching is not priced
h=T/J;
Nume1=ones(L,1); X=zeros(L,J+1);
Nume2=ones(L,1); Z=zeros(L,J+1);
c1=[];
c2=[];
Deno=ones(L,1);Y=zeros(L,J+1);
% simulating the trajectories of the markov chain
for l=1:L
Ch(l,:)=Markov_chain(T,J,A,X0);
c1=find(Ch(l,:)==1);
c2=find(Ch(l,:)~=1);
%simulation of the trajectories of other parameters
for j=1:J
Mu(l,j)=mu(Ch(l,j));
alpha_s(l,j)=alpha(Ch(l,j));
beta_s(l,j)=beta(Ch(l,j));
delta_s(l,j)=delta(Ch(l,j));
R(l,j)=r(Ch(l,j));
Theta1(l,j)=theta1(Ch(l,j));
Theta2(l,j)=theta2(Ch(l,j));
end
if Ch(l,:)==1
Z(l,:)=NIGsampling(alpha(1),beta(1),delta(1),m,T);
for i=1:length(c1)
j=c1(i);
X(l,j)=Z(l,j);
end
else
Z(l,:)=NIGsampling(alpha(2),beta(2),delta(2),m,T);
for i=1:length(c2)
j=c2(i);
X(l,j)=Z(l,j);
end
end
%
end
%
%simulation of trajectories according to log-return process
%NIG(alpha,beta,delta).
for l=1:L
for j=1:J
Y(l,j+1)= Y(l,j) + (Mu(l,j)+exp(delta(l,j)*(sqrt(alpha^2-beta^2))-sqrt(alpha^2-(beta+1)^2)+mu(l,j)))*h+...
X(l,j+1)-X(l,j);
end
end
for l=1:L
for j=1:J
Nume1(l,1)=Nume1(l,1)*exp(-Theta1(l,j)*(Y(l,j+1)-Y(l,j)))*exp(-(R(l,j))*h);
Nume2(l,1)=Nume2(l,1)*exp(-Theta2(l,j)*(Y(l,j+1)-Y(l,j)))*exp(-(R(l,j))*h)*...
(exp(Theta2(l,j)*(Mu(l,j)+exp(delta(l,j)*(sqrt(alpha^2-beta^2))-sqrt(alpha^2-(beta+u)^2)+mu(l,j)*u))*h+...
(M_s(l,j)+Theta2(l,j)*exp(delta(l,j)*(sqrt(alpha^2-beta^2))-sqrt(alpha^2-(beta+Theta2(l,j))^2)+mu(l,j)*Theta2(l,j)))))
+exp(delta(l,j)*(sqrt(alpha^2-beta^2))-sqrt(alpha^2-(beta-Theta2(l,j))^2)-mu(l,j)*Theta2(l,j)*h);
Deno(l,1)=Deno(l,1)*exp(-Theta1(l,j)*(Y(l,j+1)-Y(l,j)));
end
%lookback option
Zl=S0*exp(Y(l,J+1));
Nume1(l,1)=Nume1(l,1)*max(max(Zl)-K,0);
Nume2(l,1)=Nume2(l,1)*max(max(Zl)-K,0);
%european option
% Nume1(l,1)=Nume1(l,1)*max(S0*exp(Y(l,J+1))-K,0);
% Nume2(l,1)=Nume2(l,1)*max(S0*exp(Y(l,J+1))-K,0);
end
x1=sum(Nume1)/sum(Deno);
x2=sum(Nume2)/length(Nume2);
x=[x1 x2];
file 3:
function X=NIGsampling(alpha,beta,delta,m,T)
.
N=2^m;
h=0:T/N:T;
Z=zeros(N+1,1)
X=zeros(N+1,1)
W=zeros(N+1,1)
Y=zeros(N+1,1)
a=1;
b=sqrt(alpha^2-beta^2);
invg1(N+1)=invgrnd(a*h,b)
Z(N+1)=normrnd(0,1)
X(1)=0
for k=1:m
n=2^(m-k)
for j=1:2^(k-1)
i=(2*j-1)*n
X(i+1)=beta*delta*delta*(invg1(N+1))+delta*sqrt(invg1(N+1))
Y(i+1)=X(i+1)-X(i)
W(0)=W(X(1))==0
W(i+1)=W(i)+sqrt(Y(i+1)*Z(N+1))
Z(i+1)=(beta*delta*delta*X(i+1))+delta*W(i+1)
end
end

Best Answer

Wow, that's quite a long tag. If you have a lot of code, you can attach the m-file with the paper clip icon. Put a line in there that says something like
% TODO fix the code here. It produces an error message.
so we can find the problematic part. Here's another link you need to look at : http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Now, look at how you called it, and how it was defined:
[a]=MonteCarlo_callNIG(r,theta1,theta2,params,m,A,K,S0,X0,T,L,J);[x]=MonteCarlo_callNIG(r,theta1,theta2,params,m,A,K,S0,X0,T,L)
Do you see how you're calling it with an extra J input argument? Why are you doing that? Either include it in the input argument list of the function definition, or don't pass it in.
Related Question