Hi all, I can't find my bug in the following call of fminunc. The relevant section of my main file looks like this:
global W obs log_share price cost_shifter1 cost_shifter2 cost_shifter3 product_attrib1 product_attrib2 product_attrib3opts = optimset('display','iter-detailed','Diagnostics','on','TolFun',1e-10,'TolX',1e-10,'GradObj','on','DerivativeCheck','off','Hessian','user-supplied');start = beta_2SLS;iter=1;maxiter=2;W=eye(6);while iter<=maxiter[betahat, fval] = fminunc(@(beta) moments(beta),start,opts);W = varcov(betahat);iter=iter+1;end
That is, I call fminunc to minimize the function moments specified here:
function distance=moments(beta)global W obs log_share price cost_shifter1 cost_shifter2 cost_shifter3 product_attrib1 product_attrib2 product_attrib3 residual1=zeros(obs,1);for i_obs=1:obs residual1(i_obs) = log_share(i_obs) - beta(1)-beta(2)*price(i_obs)- ... [beta(3), beta(4), beta(5)]*[product_attrib1(i_obs); product_attrib2(i_obs); product_attrib3(i_obs)];endmoment=(1/obs)*residual1'*[cost_shifter1, cost_shifter2, cost_shifter3, product_attrib1, product_attrib2, product_attrib3]; % ( 1 X 6) vector
distance = moment*W*moment'; end
When I run the section main file, I get the error message: Error using moments Too many output arguments.
Error in @(beta)moments(beta)
Error in fminunc (line 285) [f,GRAD,HESS] = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial objective function evaluation. FMINUNC cannot continue
Does anybody see my error or give me a hint, how to debug this? Thanks a lot.
Best Answer