objective function is undefined at initial point. Fmin cannot continue

6 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2019-5-9
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 个评论
ebdilden
ebdilden 2019-5-10
my obj function is a function of two other function files- it is hard to check it out. any other recommendations?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by