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 allglobal 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];endxlswrite('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));endend
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?
Best Answer