Not sure what this line of code is doing
6 次查看(过去 30 天)
显示 更早的评论
I'm trying to translate the following Matlab code into R, and I'm not sure what line 45 is doing. Can someone explain to me what's going on here? How are variable values being assigned? What is fminserach doing?
[R,fval,outf,op]=fminsearch(@(params) eglike(params,data),pinit,optionsfmin);
It's a part of this larger script:
function R=egfit(data, params, options)
% version 2.2 27/02/07
% uses fminsearch instead of fmins
% (c) Yves Lacouture, Universite Laval
[n,m]=size(data);
if min(n,m) > 1
error('First argument must be a vector');
end
if n == 1 %case of a row vector of data
data = data';
n = m;
end
if min(data)<=0 % get rid of zeros and negative numbers
warning('data include zero(s) and/or negative number(s)');
nc=length(find(data<=0));
fprintf('%d values out of %d are truncated\n', nc, n);
data=data(find(data>0));
end
if (nargin > 1 & ~isempty(params)) % explicit starting parameter values set by user
mu=params(1);
sig=params(2);
tau=params(3);
else
tau=std(data).*0.8; % set defaut starting parameter values if not explicit
mu=mean(data)-tau; % uses heuristic values
sig=sqrt(var(data)-(tau^2));
end
if (nargin > 2 & ~isempty(options)) % explicit options values set by user and pass to fmins
opts(1:3)=options(1:3); % termination, function tolerances and maximum number of iterations
else
opts=[ 1.e-4,1.e-4]; % default values for termination and function tolerances
opts(3)=200*length(data); % default max number of iterations
end
%optionsfmin=optimset('TolX',opts(1),'TolFun',opts(2),'MaxIter',opts(3));
% was in version 2.2; should be MaxFunEvals
optionsfmin=optimset('TolX',opts(1),'TolFun',opts(2),'MaxFunEvals',opts(3));
pinit = [mu sig tau]; % put initial parameter values in an array
% [R,opt] = fmins('eglike',pinit,opts,[],data); % based on old function fmins
[R,fval,outf,op]=fminsearch(@(params) eglike(params,data),pinit,optionsfmin);
if (outf<1)
disp(op)
end
0 个评论
回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!