Index exceeds matrix dimensions. - In running lsqnonlin optimization.
Show older comments
Hi everybody, I am trying to running the following optimization problem under lsqnonlin function. But I got stuck at line ' [Params, Fval, Residuals, Exitflag] = lsqnonlin(@(Params) NSobjP(Params, BondsCF, Bonds.DirtyPrices), Params0(i,:), lb, ub, options); ' where the optimization is supposed to start. Here is the code:
Nbonds = length(Bonds.Prices);
BondsCF = cell(Nbonds, 1);
ObsYTM = zeros(Nbonds, 1);
Time2Mat = zeros(Nbonds, 1);
for i = 1:Nbonds
% Bonds future cashflows
BondsCF{i} = czbondfuturecf(Bonds.Coupon(i), Bonds.Issue(i), Bonds.Maturity(i), Bonds.Settle);
BondsFig = czbondkeyfigures(Bonds.Coupon(i), Bonds.Issue(i), Bonds.Maturity(i), Bonds.Settle, Bonds.Prices(i));
ObsYTM(i) = BondsFig.YTM;
Time2Mat(i) = BondsFig.TimeToMaturity;
Bonds.DirtyPrices(i, 1) = Bonds.Prices(i);
end
MaxTime2Mat = max(Time2Mat);
MinTime2Mat = min(Time2Mat);
% Set initial parameters
LongYTM = ObsYTM(Time2Mat == max(Time2Mat)); % YTM for the longest maturity
ShortYTM = ObsYTM(Time2Mat == min(Time2Mat)); % YTM for the shortest maturity
LongYTM = log(1+LongYTM); % Convert the LongYTM to the continuous compounding
beta0 = LongYTM; % beta0 = YTM with the maximal time to maturity
beta1 = ShortYTM - beta0; %YTM with the minimal time 2 maturity - beta0
% some bounds for lambda and gamma
MaxMat4lambda = 0.5*MaxTime2Mat;
MaxMat4lambda = min(MaxMat4lambda, 10);
options = optimset('LargeScale', 'off', 'MaxIter', 3e5, 'MaxFunEvals', 3e5, 'TolFun', 1e-5, 'TolX', 1e-5, 'Display', 'off');
lambda0 = 0.6;
OptMinLambda = fminsearch(@(lambda) LambdaLoading(lambda, MaxMat4lambda), lambda0, options);
% ==============================================
lambdaMin = OptMinLambda;
lambdaMax = 30;
lb = [-inf, -inf, -inf, lambdaMin]; %lowerbound
ub = [ inf, inf, inf, lambdaMax]; %upperbound
fprintf('\n\n======================== RUNNING ESTIMATION ============================\n');
beta2 = 0;
lambda = 0.62;
if lambda < lambdaMin
lambda = lambdaMin+0.05;
end
Params0 = [beta0 beta1 beta2 lambda];
%'lsqnonlin'
options = optimset('MaxIter', 3e5, 'MaxFunEvals', 3e5, 'TolFun', 1e-12, 'TolX', 1e-12, 'Display', 'off', 'Algorithm', 'trust-region-reflective');
% Fitting Price
[Params, Fval, Residuals, Exitflag] = lsqnonlin(@(Params) NSobjP(Params, BondsCF, Bonds.DirtyPrices), Params0(i,:), lb, ub, options);
It seems that ' Index exceeds matrix dimensions ' is a bullshit error but still I can't figure it out. At the beginning I thought it was a matter of Params dimension, but since I specified in NSobjP function that is a vector and since lsqnonlin gives vectors as output, I cannot see the solution.
Million thanks in advance :)
Accepted Answer
More Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!