Error using the function fmincon
Mostrar comentarios más antiguos
function [LLF, LL, H ] = garchlike(data)
p=1;
q=1;
if isempty(q)
m=p;
else
m = max(p,q);
end
%this are the parameters
psi=[0.01;0.01];
gamma=[0;0.02;0.02;0];
lambda=[0;0.03;0.03;0];
A=[1;-0.04;-0.03;1];
stdestimate = std(data,1);
data = [stdestimate ; data];
T = size(data,1);
B=(reshape(A,2,2))^-1;
%I am squaring each element of the inverse of A
B2=((reshape(A,2,2))^-1).^2;
%getting the Bl matrix(squared interactions)
Bl=[B2(1,:);B(1,:).*B(2,:);B2(2,:)];
garchc=Bl*psi;
garcha=Bl*(reshape(gamma,2,2))*reshape(A.^2,2,2);
garchb= Bl*(reshape(lambda,2,2))*reshape(A.^2,2,2);
H(1,1).day=[0.001; 0; 0; 0.002; ];
%convert to matrix
H(1,1).day=ivech(H(1,1).day);
for t = (m + 1):T
H(1,t).day= ivech(garchc + garcha*(diag(H(1,t-1).day))+ garchb*(data((t-1),:).^2)');
end
% Getting the loglikelihood
LLF=zeros(1,T);
%the loklikelihood
for t = (m + 2):T
LLF(t)= log(det(H(1,t).day))+ data(t,:)/(H(1,t).day)*data(t,:)';
end
t=(m+2):T;
LLF=-LLF(t);
LL=sum(LLF);
end
I have this function from up and trying to minimize using fmincon:
[parameters,LLF,EXITFLAG, OUTPUT,HESSIAN] = fmincon('garchlike', [psi;gamma;lambda;A],sumA ,sumB ,[] , [] , LB , UB,[],options, data, p , q);
I don't know why is not working and gives me an error : "Error using garchlike Error using garchlike Too many input arguments. Error in fmincon (line 631) initVals.f = feval(funfcn{3},X,varargin{:}); Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue."
I appreciate any help you can give me/tips..
Respuestas (2)
Walter Roberson
el 9 de Oct. de 2013
0 votos
Your call to fmincon() has three variables after your options structure. That is not any documented syntax for fmincon(). In practice, for backwards compatibility, it will attempt to pass those extra variables as additional arguments to the function being called. That would result in 4 inputs, but your function only accepts one input.
It appears to me that you should not be passing data, p, q into fmincon()
3 comentarios
Walter Roberson
el 9 de Oct. de 2013
Try
[parameters,LLF,EXITFLAG, OUTPUT,HESSIAN] = fmincon(@garchlike, [psi;gamma;lambda;A],sumA ,sumB ,[] , [] , LB , UB,[],options);
and for debugging purposes, modify garchlike a little:
function [LLF, LL, H ] = garchlike(data, varargin)
persistent warned
if nargin > 1 && isempty(warned)
warn(sprintf('garchlike called with %d extra arguments', nargin - 1));
warned = true;
end
Note: each time you retest and want the potential for notifications re-armed, execute
clear garchlike
Laura Pop
el 9 de Oct. de 2013
I'm just guessing, but this might be what you're trying to do
x0=[psi;gamma;lambda;A];
fmincon(@(x,data,p,q) garchlike, x0 ,sumA ,sumB ,[] , [] , LB , UB,[],options);
You should test that the modified function below works before you try feeding it to fmincon. Note that it now takes 4 input arguments.
function [LLF, LL, H ] = garchlike(x,data,p,q)
%this are the parameters
psi=x(1)
gamma=x(2);
lambda=x(3);
A=x(4);
if isempty(q)
m=p;
else
m = max(p,q);
end
stdestimate = std(data,1);
data = [stdestimate ; data];
T = size(data,1);
B=(reshape(A,2,2))^-1;
%I am squaring each element of the inverse of A
B2=((reshape(A,2,2))^-1).^2;
%getting the Bl matrix(squared interactions)
Bl=[B2(1,:);B(1,:).*B(2,:);B2(2,:)];
garchc=Bl*psi;
garcha=Bl*(reshape(gamma,2,2))*reshape(A.^2,2,2);
garchb= Bl*(reshape(lambda,2,2))*reshape(A.^2,2,2);
H(1,1).day=[0.001; 0; 0; 0.002; ];
%convert to matrix
H(1,1).day=ivech(H(1,1).day);
for t = (m + 1):T
H(1,t).day= ivech(garchc + garcha*(diag(H(1,t-1).day))+ garchb*(data((t-1),:).^2)');
end
% Getting the loglikelihood
LLF=zeros(1,T);
%the loklikelihood
for t = (m + 2):T
LLF(t)= log(det(H(1,t).day))+ data(t,:)/(H(1,t).day)*data(t,:)';
end
t=(m+2):T;
LLF=-LLF(t);
LL=sum(LLF);
end
37 comentarios
Laura Pop
el 9 de Oct. de 2013
Matt J
el 10 de Oct. de 2013
gives me the same error at line 631 in fmincon.
As I told you, you shouldn't be running fmincon yet. You should be running garchlike and making sure it works first.
Also tells me that the fmincon must have at least 4 inputs,don't I have 4?
We can't know. You haven't show us how you're now running fmincon, now that p and q have been eliminated.
Walter Roberson
el 10 de Oct. de 2013
Change
@(data,X) garchlike
to
@(X) garchlike(data, X)
Walter Roberson
el 10 de Oct. de 2013
This is not necessarily incorrect behavior. If your matrix is close to singular, then your conditions are so sensitive to round-off error that the answer may well be useless.
Laura Pop
el 15 de Oct. de 2013
Walter Roberson
el 15 de Oct. de 2013
As a sub-expression you have
log(det(H(1,t).day))
if the determinant is negative, the log() of it would be complex. Is it certain that none of the determinants are negative ?
Walter Roberson
el 15 de Oct. de 2013
Put in a conditional breakpoint after LLF is assigned to:
dbstop at LINENUMBER if ~isreal(LLF)
you can also click at right-click at the place you would normally left-click to set a breakpoint: that will allow you to enter the appropriate conditional expression.
In addition to Walter's last remark, you should really modify this final for-loop as follows
for t = (m + 2):T
LLF(t)= log(det(H(1,t).day))+ data(t,:)/(H(1,t).day)*data(t,:)';
if ~isreal(LLF(t)) | ~isfinite(LLF(t))
LL=Inf; return
end
end
You should also be using the sqp algorithm
options=optimoptions(options,'Algorithm','sqp');
which can recover from steps into regions where the loglikelihood is not finite.
I don't get why is telling me(If I am understanding well) that when the objective function(multi_garch_likelihood) is evaluated the result is complex?? When I am running is working very well and is givin me a scalar value.
Because you have only been testing "safe" values of the parameters. If all psi, gamma, and lambda are chosen zero, for example, you will not get a finite real value for the loglikelihood. If you aren't using the sqp algorithm, you are not doing anything to prevent fmincon from exploring those parameter choices.
Laura Pop
el 15 de Oct. de 2013
I tryed using also 'sqp' in the format both of you said,but is not able to find me a local minimum,is just telling me that is possible. What should I change in order to actually find?
If it's telling you "Local Minimum Possible", you can't automatically assume that it failed. FMINCON will never return an exit message "Local minimum definitely found". You can find out more information about the exit condition by calling fmincon with 4 output arguments
[x,fval,exitflag,output] = fmincon(...)
and/or by turning the 'Display' option to 'iter'.
Until now I used the TolX and TolFun set to 1e-6. I noticed that if I change the tolerance to 1e-2 to both and using... then is able to find me a minimum after 2 iterations.
I think what you really saw is a message that the TolX or TolFun stopping criterion was met. I don't think you're claiming to see evidence of an actual minimum.
It is easy to make fmincon hit the TolX/TolFun stopping criteria early if you make their values really large, as you have done. If you make them Inf, you should see fmincon stop in 1 iteration and return your initial guess as the solution. Therefore, setting TolX and TolFun to arbitrary large values is usually a bad thing to do.
However since I haven't used before this function I don't know if is the right minimum since Matlab finds it after 2 iterations?
You should always be testing your code on simulated problem data where you know the true solution in advance.
I alreday called the function with all the outputs.And the problem is with the tolerance functions.
What evidence of that did you see in the fmincon output data?
I kind of tryed all sort of things..
You shouldn't be trying all sorts of random things. If there is a good reason to change TolX and TolFun to a particular value then you should do so, but from what I can tell, you do not know what these parameters mean. You are just picking values for them by random experimentation.
Matt J
el 21 de Oct. de 2013
The likelihood LL returns complex values,implies that inverse of H(1,t).day is badly scaled(error which I get),implies that the values I give are not good.
I think we already solved that problem: by setting LL=Inf when the determinant goes negative and using the 'sqp' algorithm. You gave no evidence that that did not work.
Laura Pop
el 21 de Oct. de 2013
Laura Pop
el 21 de Oct. de 2013
Walter Roberson
el 21 de Oct. de 2013
Then your optimization works.
MATLAB cannot know what the "true" minimum value is, so it cannot issue a response such as "Minimum found!". MATLAB can just say that given the conditions you gave, the location it found fit the termination conditions you gave.
the estimated values that I get are not right.(doesn't finds different values from the one that I gave,is estimating the values only where I have zero).
What happens when you run fmincon using the known solution as your initial point? If the algorithm doesn't stop immediately, then you have coded your objective function or your data simulation incorrectly. If your known solution is a minimum of the objective you've provided, the algorithm should recognize this immediately once derivatives are calculated.
I also get big differences in values if either I use LL=sum(LLF) or LL=-sum(LLF). What should I do then?
There is no reason the minimum of sum(LLF) and -sum(LLF) should be the same. They are very different functions.
Is it okay to apply fmincon if I don't have any constraints,no bonds?(just by setting the LB=-inf and UB=inf)
If fmincon gives no errors when you do so, why would you be worried?
Laura Pop
el 21 de Oct. de 2013
If the first order optimality measure at the point where fmincon stops is small and the exitflag is good, then you can probably be confident that fmincon fulfilled its part of the bargain.
The result can still be bad, however, if you have coded something incorrectly in your objective function or constraints. fmincon can't do anything about that. Obviously also, the initial guess is important. If you initialize at a point where the function is locally flat, the algorithm will see it as a local min, and won't move.
Matt J
el 24 de Oct. de 2013
Compare the magnitudes of
ivech(garchc + garchb*(data((t-1),:).^2)');
with the magnitudes of
ivech(garchc);
Laura Pop
el 24 de Oct. de 2013
Matt J
el 24 de Oct. de 2013
Attach "data" so that we can run run your code.
Matt J
el 24 de Oct. de 2013
I actually cannot run this because I don't have ivech, nor do I know what it does.
You recently changed LL=-sum(LLF) to LL=sum(LLF). I'm confused about which form of the code is now the official one.
Either way, the objective function has no upper or lower bound. FMINCON is just pushing it to -Inf by making the H(t).day matrices go to zero or to infinity, depending on the sign of LL. The reason your lambda barely change is that the quickest path to -Inf is to make psi either very large or very small, without modifying XX(5:6).
There is probably some mistake in your model, or your coding of it, because a loglikelihood should always have an upper bound.
Laura Pop
el 25 de Oct. de 2013
Matt J
el 25 de Oct. de 2013
Yes, but we now know why the results are bad. As I explained in my last comment, your objective function is incorrect: it shouldn't be unbounded, but it is.
There is probably some mistake in your model, or your coding of it, because a loglikelihood should always have an upper bound.
I guess that's not true, come to think of it. As a function of the parameters, a loglikelihood can surely be unbounded.
However, it shows that you need constraints...
It's your model and only you know what "okay" means, but since you aren't getting uniformly good results, I would guess there's a problem. I have the vague impression that constraints relating psi to lambda might be needed. Currently, the objective function seems very sensitive to psi, but much less so to lambda. Constraints relating the two could help with that. However, I don't know what would make sense for those additional constraints. That's a modelling question that only you could answer.
Categorías
Más información sobre Numerical Integration and Differentiation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!