lsqnonlin problem - Where did I go wrong?
10 views (last 30 days)
Show older comments
Hi, Can someone please point out to me the mistake I did in my program below. It returns the following error:
"Optimization running. Error running optimization. Attempt to reference field of non-structure array."
function lnprice0T=calibration(x)
%define parameters to look for
cb=x(1); ab=x(2); bb=x(3); sigmab=x(4);
cr=x(5); ar=x(6); br=x(7); sigmar=x(8);
alpha=x(9); beta=x(10); rho=x(11); theta=x(12);
T=3; alphar=1; alphab=1; gammar=0; gammab=0; x0=0.05; y0=0.025;
StartAt=[0 -1 0.5 0.02 0 -1 0.5 0.01 100 200 4 -0.999]; %found by trial and error
lb=[-inf -5 0 -5 -inf -5 0 -5 50 100 1 -0.999];%lower bound
ub=[inf 5 1 5 inf 5 1 5 500 1000 12 0.999];%upper bound
%define the bond price
%define the big C first
constantcrate=(2.*cr.*br/sigmar.^2);%constant for part c of bond price (rate)constantcbond=(2.*cb.*bb/sigmab.^2);%constant for part c of bond price (issuer's default intensity)
%define the components
compbN=sqrt((cb.*ab).^2+2.*gammab.*sigmab.^2); %later set gamma = 0
expbN=exp(-T.*compbN);
expb2=exp(-(T.*compbN+cb.*ab)./2); %for ln component
bondnumN=2.*gammab.*(1-expbN)+alphab.*(compbN+cb.*ab+expbN.*(compbN-cb.*ab));
bondenomN=alphab.*sigmab.^2.*(1-expbN)+(compbN-cb.*ab)+(compbN+cb.*ab).*expbN;
bondN=bondnumN./bondenomN;
lnCb=log((2.*compbN.*expb2)./bondenomN);
comprN=sqrt((cr.*ar).^2+2.*gammar.*sigmar.^2);
exprN=exp(-T.*comprN);
expr2=exp(-(T.*comprN+cr.*ar)./2); %for ln component
ratenumN=2.*gammar.*(1-exprN)+alphar.*(comprN+cr.*ar+exprN.*(comprN-cr.*ar));
ratedenomN=alphar.*sigmar.^2.*(1-exprN)+(comprN-cr.*ar)+(comprN+cr.*ar).*exprN;
rateN=ratenumN./ratedenomN;
lnCr=log((2.*compbN.*expr2)./ratedenomN);
%log price of corporate bond, with maturity T years
lnprice0T=constantcrate.*lnCr+constantcbond.*lnCb-bondN.x0-rateN.y0-rho.*T+rho.*triplequad(@Gcop2,9.99734*10^-12/5,25.3284/5,9.99734*10^-12/10,25.3284/10,0,1)-log(100);
Answers (1)
Matt Tearle
on 15 May 2012
Before trying to do the optimization, try testing your function first:
calibration(rand(12,1))
This gives an error (the same one about referencing a field of a non-structure variable) on the last line of the function. Looking there you can see that you have
...-bondN.x0-...
MATLAB is interpreting this as a reference to the x0 field of the structure variable bondN. I'm guessing that you actually want to just multiply these and forgot the *. Same for the next term rateN.y0
I don't have access to Gcop2 so I don't know if the function works once the above problem is fixed.
Also, pay attention to the Code Analyzer messages. alpha, beta, theta, StartAt, lb, and ub are all unused in this function. Are you sure that's what you want?
0 Comments
See Also
Categories
Find more on Problem-Based Optimization Setup in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!