Failure in initial objective function evaluation. LSQNONLIN cannot continue.

44 visualizaciones (últimos 30 días)
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 comentarios
Stephan
Stephan el 21 de Oct. de 2019
Where is the function AmericanCallTrinBoyle
Violeta Oikonomou
Violeta Oikonomou el 21 de Oct. de 2019
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

Iniciar sesión para comentar.

Respuesta aceptada

Stephan
Stephan el 21 de Oct. de 2019
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 comentarios
Stephan
Stephan el 21 de Oct. de 2019
Editada: Stephan el 21 de Oct. de 2019
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.
Violeta Oikonomou
Violeta Oikonomou el 21 de Oct. de 2019
I'll tried this.Thanks a lot for all your notifications!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by