objective function is undefined at initial point. Fmin cannot continue
Mostrar comentarios más antiguos
Hello,
I am trying to run a code and it always gave me the same error message:
"Error using sfminbx (line 27)
Objective function is undefined at initial point. fmunc cannot continue
I really need some help with my initial starting values or gmm objective function.
Any suggestions are welcomed!
Thanks!
global happycount invA ns x1 x2 s_jt IV theti thetj theta1 theta2 rho cdid cdindex nestid nestindex mktnestindex brand
load ps2
load instruments
IV=[x1(:,2:end) instruments];
clear instruments inst
N=size(x1,1);
invA = inv([IV'*IV]/N);
ns = 200;
%starting values:
theta2w= [0.09 0 0 0 0 0 ;
0.04 0 0.0228 0 0 0 ;
0.06 0 0 0 0 0.0336 ];
[theti, thetj, theta2]=find(theta2w);
temp = cumsum(s_jt);
sum1 = temp(cdindex,:);
sum1(2:size(sum1,1),:) = diff(sum1);
outshr = 1.0 - sum1(cdid,:);
y = log(s_jt) - log(outshr);
first=1;
for i=1:size(nestindex,1)
last=nestindex(i);
n = last - first + 1;
s_jgt(first:last,:) = s_jt(first:last,1)./(ones(n,1)*(sum(s_jt(first:last,1))));
first=last+1;
end
lnSjgt = log(s_jgt);
rho=0.2;
delta_NL = y - rho*lnSjgt; .
mvalold = exp(delta_NL/(1-rho));
oldt2 = zeros(size(theta2));
save mvalold mvalold oldt2
options = optimset('GradObj','on','MaxFunEvals',900000);
clc
beta =[rho;theta2];
%Minimization
exitflag=0;
happycount = 0;
while exitflag==0
%[beta, fval,exitflag]=fminsearch(@gmmobj,beta,options) % Simplex search method
[beta, fval,exitflag]=fminunc({@gmmobj,@gradobj},beta,options); % Newton method
end
Respuestas (1)
Walter Roberson
el 9 de Mayo de 2019
if you can compute the gradient of fun and the SpecifyObjectiveGradient option is set to true, as set by
options = optimoptions('fminunc','SpecifyObjectiveGradient',true)
then fun must return the gradient vector g(x) in the second output argument.
However, you syntax
{@gmmobj,@gradobj}
does not return the gradient vector in the second output argument: instead it tries to specify fun as a cell array of function handles.
You need
[beta, fval,exitflag]=fminunc(@(x) deal(gmmobj(x),gradobj(x)), beta, options); % Newton method
9 comentarios
ebdilden
el 9 de Mayo de 2019
ebdilden
el 10 de Mayo de 2019
Walter Roberson
el 10 de Mayo de 2019
I copied and pasted from what I posted to a copy of your code, and it was fine. The only problem I saw is that your line
delta_NL = y - rho*lnSjgt; .
has an unexpected period at the end.
ebdilden
el 10 de Mayo de 2019
Walter Roberson
el 10 de Mayo de 2019
We would need example input files to test.
ebdilden
el 10 de Mayo de 2019
Matt J
el 10 de Mayo de 2019
Have you tested the objective function by itself (separate from fmincon) on the initial input? It should be easy to trap any errors in the debugger and to observe any problem with the output.
ebdilden
el 10 de Mayo de 2019
Walter Roberson
el 10 de Mayo de 2019
Tracking is a bit easier if you parameterize the functions and get rid of the global.
Categorías
Más información sobre MATLAB 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!