[para,std_dev]=my_mle('gh_log_fun',[-1;25;1;0.0005;0.02],y)
??? Attempt to execute SCRIPT gh_log_fun as a function:
E:\软件包\matlab\bin\gh_log_fun.m
Error in ==> fminsearch at 205
fv(:,1) = funfcn(x,varargin{:});
Error in ==> my_mle at 25
[para,fv]=fminsearch(fun,para0,[],2,varargin{:});
function[para,standard_deviation,fv]=my_mle(fun,para0,varargin)%estimate parameters and standard errors when using maximum likelihood%estimation(MLE)%input%fun:a function defined by users for calculating log probability density%function(pdf) and negative sum of logatithm of pdf%para0:given initial parameters%varargin;other needed inputs required by fun%output%para:estimated parameters%standard_deviation:standard deviations of estimated parameters%fv:maximized likelihood function value%%%%%%%%%%%%%%%%%%%%%%example1:estima mean and standard devition by realizations of a random%variable which is normally distributed%function f=mynormpdfsum(x,num,y)%yy=1/sqrt(2*pi)/x(2)*exp(-(y-x(1)).^2/2/x(2)^2);%if num==1%%(note: it must set to 1)%f=log(yy);%else f=-sum(log(yy));end%%%%%%%%%%%%%%y=2+3*randn(5000,1);%[para,standard_deviation]=my_mle('mynormpdfsum',[0;2],y)para0=para0(:);[para,fv]=fminsearch(fun,para0,[],2,varargin{:});fv=-fv;d=my_numerical_derivative(fun,para,1,varargin{:});standard_deviation=sqrt(diag(pinv(d'*d)));
function f=my_numerical_derivative(fun,parameter,varargin)%input%fun:the name of a function%parameter:given parameter with respect to which first-order derivative%is calculated%varargin:other needed inputs reguired by fun%output%f:numerical first order derivative of fun at parametern=length(parameter);for i =1:n    a=zeros(n,1);    a(i)=max(parameter(i)*le-6,le-6);    y1(;,i)=feval(fun,parameter+a,varargin{:});    y2(:,i)=feval(fun,parameter-a,varargin{:});    f(:,i)=(y1(:,i)-y2(:,i)/2/a(i));end