Failure in initial objective function evaluation. LSQNONLIN cannot continue.

I'm building a script to generate a non-linear least squares estimation, after enterring my data. I build the follwing code:
function [x,resnorm,residual,exitflag,output] = TrCalibrationBoyle(~)
clear all
global strike; global S0; global irate; global TTM; global mktprice; global N; global k;
strike=zeros(80,8);
S0=zeros(80,8);
irate=zeros(80,8);
TTM=zeros(80,8);
mktprice=zeros(80,8);
parameter=zeros(80,2);
res=zeros(80,1);
exit=zeros(80,1);
strike=xlsread('DATA1.xls','strike','A1:A80');
S0=xlsread('DATA1.xls','S0','A1:A80');
irate=xlsread('DATA1.xls','irate','A1:A80');
TTM=xlsread('DATA1.xls','TTM','A1:A80');
mktprice=xlsread('DATA1.xls','mktprice','A1:A80');
N=xlsread('DATA1.xls','N','A1:A80');
for i=80:-1:1
x0=[0.5,1.099]; lb=[0.001,1]; ub=[2,5]; k=i;
[x,resnorm,residual,exitflag]=lsqnonlin(@TrBoyleLSQD,x0,lb,ub);
parameter(i)=x; res(i)=resnorm; exit(i)=exitflag; tr_matrix=zeros(80,8);
for j=1:8
tr_matrix(i,j)=AmericanCallTrinBoyle(strike(i,j),S0(i,j),irate(i,j),TTM(i,j),x(1),x(2),N(i,j));
end
pricedata=[tr_matrix];
end
xlswrite('apotelesmata.xls',pricedata,'results','A1:A80');
xlswrite('apotelesmata.xls',res,'results','B1:A80');
xlswrite('apotelesmata.xls',parameter,'results','C1:C80');
end
The function whitc is included is:
function [trBoyle_lsqd] = TrBoyleLSQD(x)
global strike; global S0; global irate; global TTM; global mktprice; global N; global k;
trBoyle_lsqd=zeros(1,8);
for j=1:8
trBoyle_lsqd(j)=mktprice(k,j)-AmericanCallTrinBoyle(strike(k,j),S0(k,j),irate(k,j),TTM(k,j),x(1),x(2),N(k,j));
end
end
After i run TrCalibrationBoyle in the command window, it shows me the following:
Index in position 1 exceeds array bounds.
Error in TrBoyleLSQD (line 11)
trBoyle_lsqd(j)=mktprice(k,j)-AmericanCallTrinBoyle(strike(k,j),S0(k,j),irate(k,j),TTM(k,j),x(1),x(2),N(k,j));
Error in lsqnonlin (line 206)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Error in TrCalibrationBoyle (line 35)
[x,resnorm,residual,exitflag]=lsqnonlin(@TrBoyleLSQD,x0,lb,ub);
Caused by:
Failure in initial objective function evaluation. LSQNONLIN cannot continue.
Do you have any ideas for what is the error?

9 Commenti

Do not use global variables to pass fixed parameters and data around. Use these techniques instead,
@Stephan ,"DATA1" is the excel that data are imported and "apotelesmata" the excel that take the result.
Also the following is a code that is also called if you need it:
function [price] = AmericanCallTrinBoyle(strike,S0,irate,TTM,sigma,lamda,N)
deltaT=TTM/N; u=exp(sigma*lamda*sqrt(deltaT)); d=1/u; M=exp(irate*deltaT);
V=(M^2)*(exp((sigma^2)*deltaT)-1); p1=(((V+M^2-M)*u)-(M-1))/((u-1)*((u^2)-1));
p3=(((V+M^2-M)*(u^2))-(u^3)*(M-1))/((u-1)*((u^2)-1)); p2=1-p1-p3;
lattice=zeros(2*N+1,N+1);
for i=0:2*N
lattice(i+1,N+1)=max(0,S0*(u^(i-N))-strike);
end
for j=N-1:-1:0
for i=0:2*j
lattice(i+1,j+1)=exp(-irate*deltaT)*(p1*lattice(i+3,j+2)+p2*lattice(i+2,j+2)+p3*lattice(i+1,j+2));
end
end
price=lattice(1,1);
end
@Matt J, i wouldn't have any problem not to use global variables, but the form of codes as i wrote them, is exactly how it is suggested for my thesis.
Well since it is "suggested", it means you are free to ignore the suggestion! I encourage you to do so.
You're welcome. Also, this will be helpful reading as well,
Where is the function AmericanCallTrinBoyle
I typed this above,maybe it hasn't been uploaded:
function [price] = AmericanCallTrinBoyle(strike,S0,irate,TTM,sigma,lamda,N)
deltaT=TTM/N; u=exp(sigma*lamda*sqrt(deltaT)); d=1/u; M=exp(irate*deltaT);
V=(M^2)*(exp((sigma^2)*deltaT)-1); p1=(((V+M^2-M)*u)-(M-1))/((u-1)*((u^2)-1));
p3=(((V+M^2-M)*(u^2))-(u^3)*(M-1))/((u-1)*((u^2)-1)); p2=1-p1-p3;
lattice=zeros(2*N+1,N+1);
for i=0:2*N
lattice(i+1,N+1)=max(0,S0*(u^(i-N))-strike);
end
for j=N-1:-1:0
for i=0:2*j
lattice(i+1,j+1)=exp(-irate*deltaT)*(p1*lattice(i+3,j+2)+p2*lattice(i+2,j+2)+p3*lattice(i+1,j+2));
end
end
price=lattice(1,1);
end

Accedi per commentare.

 Risposta accettata

The problem is that your DATA file is not complete - the sheet that should contain the data for N ist empty - therefore N is also empty in your code. I suspect there could be more problems, but this is the first thing, which leads to this error message.

4 Commenti

Ok! i will run it again with the right N and i ll inform you
After the error you find and the correct arrays bounds, gaves me many alerts in the command window like that:
Solver stopped prematurely.
lsqnonlin stopped because it exceeded the function evaluation limit,
options.MaxFunctionEvaluations = 200 (the default value)
But finnaly it gave me:
ans =
0.523614160300994 1.160412559556117
which is values aprroximately what i want.
Do u think there is something wrong with that messages?
You can use optimoptions to set this value higher - for more complex problems this could be useful - for example set it to 500:
options = optimoptions(@lsqnonlin,'MaxFunctionEvaluations',500);
[x,resnorm,residual,exitflag]=lsqnonlin(@TrBoyleLSQD,x0,lb,ub,options);
This may lead to further warnings - you have to read them and set the options accordingly. Maybe this will improve your results. Also may be you will not get better results with this. You should try it out.
Note that this will increase the calculation time.
I'll tried this.Thanks a lot for all your notifications!!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by