Why does fminunc give me the message "User objective function returned NaN; trying a new point..."?

1 view (last 30 days)
Hi all, I have to solve a minimization problem using fminunc and many starting values but the code stucks somewhere saying that "User objective function returned NaN; trying a new point...". How can I fix this problem? Sometimes it is not even able to solve one optimization.
Here the code:
1) Generation of data
clear all
T=500;
n=2;
alpha=[1 1]; %vector 1xn alpha1 alpha2
beta=[2 2]; %vector 1xn beta1 beta2
rho=0.2; %corr(u1, u2)
thetatrue=[alpha beta rho];
mu=zeros(1,n); %mean vector 1xn
sigma=[1 rho; rho 1]; %variance covariance matrix nxn
optionsunc=optimset('Display','iter','MaxIter',100000000,'TolX',10^-30,'TolFun',10^-30, 'MaxFunEvals', 10000000); %options for fminunc
X=[unidrnd(2,T,1)-1 unidrnd(4,T,1)-2]; %matrix Txn
profit1= repmat(alpha,T,1)+[X(:,1).*beta(1) X(:,2).*beta(2)]; %vector Txn
u = mvnrnd(mu,sigma,T); %vector Txn
profit=profit1+u; %matrix Txn
prof=(profit>0)==zeros(T,2);
sumequil=sum(prof,2);
W1=(sumequil==n*ones(T,1));
W2=1-W1;
data=[X W1 W2];
2) Setting up the minimization problem
A1(:,2)=-X(:,1);
A1(:,1)=-1;
A2(:,2)=-X(:,2);
A2(:,1)=-1;
rhogrid=[(-0.9:0.1:0.1) (0.11:0.01:0.19) (0.21:0.01:0.29) (0.3:0.1:0.9)];
L_nstarho=zeros(size(rhogrid,2),1); %allocate space for the max log-lik value at each rho
for j=1:size(rhogrid,2) %for each value of rho
theta0grid=[]; %initiate theta0grid
while size(theta0grid,1)==0
% Grid of starting values alpha1, alpha2, beta1, beta2 for each rho
alpha1=normrnd(thetatrue(1),1,1,2); %random draws from N(alpha1true,1)
alpha2=normrnd(thetatrue(2),1,1,2); %random draws from N(alpha2true,1)
beta1=normrnd(thetatrue(3),1,1,2); %random draws from N(beta1true,1)
beta2=normrnd(thetatrue(4),1,1,2); %random draws from N(beta2true,1)
CC = {alpha1,alpha2,beta1,beta2};
b = cellfun(@numel,CC);
n1 = cumsum(b);
a = fullfact(b);
idx = bsxfun(@plus,a,[0, n1(1:end-1)]);
v = cat(2,CC{:});
theta0grid = v(idx); % grid of starting values
cdfun=@(x) mvncdf( [A1*[x(1);x(3)], A2*[x(2);x(4)] ]...
,[0 0],[1 rhogrid(j); rhogrid(j) 1]); %F(-alpha1-beta1*x1, -alpha2-beta2*x2; rhogrid(j))
% Check feasibility of starting values
drop=false(size(theta0grid,1),1);
for i=1:size(theta0grid,1)
%P=cdfun(theta0grid(i,:));
fval=log_lik(theta0grid(i,:),cdfun,W1,W2);
drop(i)=((isnan(fval)) | fval==0);
end
theta0grid(drop,:)=[];
end
l_nstarho=zeros(size(theta0grid,1),1); %allocate the space for listing max log-lik at each starting value (given rhogrid(j))
for t=1:size(theta0grid,1) %for each starting value
theta0=theta0grid(t,:);
[theta,fval,exitflag,output]=fminunc(@(x) log_lik(x,cdfun,W1,W2),theta0,optionsunc);
if exitflag>0 %if convergence
l_nstarho(t)=-fval;
else
l_nstarho(t)=nan;
end
end
L_nstarho(j)=max(l_nstarho); %pick the max among the values obtained for each starting value
end

Accepted Answer

Walter Roberson
Walter Roberson on 17 Mar 2014
At the command prompt, command
dbstop if naninf
and run. The program will stop in the debugger when the program generates infinity or nan.
NaN can be 0/0 or 0 * infinity

More Answers (0)

Community Treasure Hunt

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

Start Hunting!