Index exceeds matrix dimensions. - In running lsqnonlin optimization.

1 view (last 30 days)
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

Luuk van Oosten
Luuk van Oosten on 28 Feb 2015
What you are trying to do is taking a value (which you try to index) but it does not exist, because it exceeds the dimensions of the matrix. Maybe sound a bit vague (or like bullshit as you call it...) but here an easy example of what is happening:
Imagine you have the 3x3 matrix called 'a'.
a=[1 2 3;6 1 8; 9 1 5]
find a(3,3) (or any other value; a(1,2))
a(3,3) = 5
now try to find a(4,3).
Index exceeds matrix dimensions.
for the obvious reason that you try to to look in the 4th row (while the matrix 'a' only has 3 rows...)... ergo: your index (4,2) is exceeding the matrix dimensions.
  1 Comment
Matt J
Matt J on 28 Feb 2015
Pietro Lindia commented:
Thanks a lot Luuk. You have been very helpful. You gave me the hint to solve it. Indeed I have only one vector for initial parameter Params0 , while in the input of lsqnonlin there was written Params0(i,:).

Sign in to comment.

More Answers (0)

Categories

Find more on Optimization 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!