Error using optimization and likelihood
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, I keep getting this errors:
Error in selectivitykk>@(initial)neg_log_likelihood(initial,c,p2,R2,R3) (line 23)
[params,fval,exitflag,output,grad,hessian] = fminunc
(@(initial)neg_log_likelihood(initial,c,p2,R2,R3),initial,OPTIONS);
Error in fminunc (line 292)
f = feval(funfcn{3},x,varargin{:});
Error in selectivitykk (line 23)
[params,fval,exitflag,output,grad,hessian] = fminunc
(@(initial)neg_log_likelihood(initial,c,p2,R2,R3),initial,OPTIONS);
Caused by:
Failure in initial objective function evaluation. FMINUNC cannot continue.
My code so far:
function [R0,SIG2,K]=selectivitykk
load data.mat % LOAD DATA
c=zeros(31,3);
for k=14:44 % Length from 14 cm to 44 cm
c(k-13,1)=k; % length
c(k-13,2)=sum(LENG2==k); % Number of fish with length k caught in 2 inch
c(k-13,3)=sum(LENG3==k); % Number of fish with length k caught in 3 inch
end
R2=c(:,1)/(2*2.54); % Length divided by mesh size in cm for 2 inch mesh
R3=c(:,1)/(3*2.54); % Length divided by mesh size in cm for 3 inch mesh
p2=EFFORT2/(EFFORT2+EFFORT3);
%%OPTIMIZATION
initial=[1,1,1]; % Initial guess of R0 and Sigma (can be negative)
% Setting options for minimization routine
OPTIONS = optimset(@fminunc);
OPTIONS.MaxFunEvals=1000*8;
OPTIONS.MaxIter=10000;
OPTIONS.TolFun=1.0000e-21;
OPTIONS.TolX=1.0000e-14;
OPTIONS.Algorithm='quasi-newton';
%*********************************************************************************
% minimize likelihood
[params,fval,exitflag,output,grad,hessian] = fminunc (@(initial)neg_log_likelihood(initial,c,p2,R2,R3),initial,OPTIONS);
loglik=-fval; % Log Likelihood
R0=sqrt(params(1)^2); % making sure R0 is positive
SIG2=params(2)^2; %Converting Sigma to Sigma Squared (STD to VARIANCE)
K=params(3);%making sure K is positive
a=(1/sqrt(2*pi)*exp(R2-R0/SIG2)/2);%lowecase phi mesh size 2
b=1/2*(1+erf(3*(R2-R0/SIG2)/sqrt(2)));%capital case phi mesh size 2
c=(1/sqrt(2*pi)*exp(R3-R0/SIG2)/2);%lowecase phi mesh size 3
d=1/2*(1+erf(3*(R3-R0/SIG2)/sqrt(2)));
end
function loglik=neg_log_likelihood(params,~,~,R2,R3)
% Negative log likelihood assuming normal distribution
% Cirresponidng to Equation 15 in Fujimori and Tokai 2001
R0=sqrt(params(1)^2); % making sure R0 is positive
SIG2=params(2); %Converting Sigma to Sigma Squared (STD to VARIANCE)
K=params(3);%making sure K is positive
a=(1/sqrt(2*pi)*exp(R2-R0/SIG2)/2);%lowecase phi mesh size 2
b=1/2*(1+erf(K*(R2-R0/SIG2)/sqrt(2)));%capital case phi mesh size 2
c=(1/sqrt(2*pi)*exp(R3-R0/SIG2)/2);%lowecase phi mesh size 3
d=1/2*(1+erf(K*(R3-R0/SIG2)/sqrt(2)));%capital phi mesh size 3
ps2=(2.*a.*b)./SIG2;
ps3=(2.*c.*d)./SIG2;
loglik=-sum(c(:,2).*log(ps2./(ps2+ps3))+c(:,3).*log(ps3./(ps2+ps3)));%Negative of equation 15
end
Thanks!
2 Kommentare
Matt J
am 7 Dez. 2017
Try executing this
neg_log_likelihood([1,1,1],c,p2,R2,R3)
and tell us the result.
Duncan Lilley
am 13 Dez. 2017
This error can occur when the objective function produces a NaN output. Try executing the command Matt suggested to see what is produced by your "neg_log_likelihood" function.
To resolve this, edit the function so that it does not produce NaN values, or set constraints on the function's inputs to prevent the objective function from producing NaN outputs.
Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!