Clear Filters
Clear Filters

Error using the function fmincon

5 views (last 30 days)
Laura Pop
Laura Pop on 9 Oct 2013
Edited: Matt J on 26 Oct 2013
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..

Answers (2)

Walter Roberson
Walter Roberson on 9 Oct 2013
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 Comments
Walter Roberson
Walter Roberson on 9 Oct 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
Laura Pop on 9 Oct 2013
@Walter Roberson I tryed what you told me but still nothing:( Gives me the same error in fmincon at line 631: initVals.f = feval(funfcn{3},X,varargin{:});
I just cannot fiogure it out why is not working,for univariate case is going fine(when I have one variable):(

Sign in to comment.


Matt J
Matt J on 9 Oct 2013
Edited: Matt J on 9 Oct 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 Comments
Laura Pop
Laura Pop on 25 Oct 2013
Edited: Laura Pop on 26 Oct 2013
I tryed to set up the LB to zero and UB to 2 and is giving me some results,is estimating the paramters to lambda. I have general constraints for the parameters so psi>0 and lambda>=0 so if I am setting the LB,UB like this should this be okay? I noticed is going well for Arch(1) but not for Garch..:(
Matt J
Matt J on 26 Oct 2013
Edited: Matt J on 26 Oct 2013
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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!