Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

fminsearch and mle

90 views
Skip to first unread message

Aaron

unread,
Apr 20, 2011, 5:29:04 PM4/20/11
to
I am trying to create a simple mle example by writing a negative log likelihood function, assuming a normal distribution, and use fminsearch to find the estimates. However, I am struggling to get fminsearch to allow a second input. Specifically, if I write the following function (the normal log likelihood function excluding sigma):

function L = likelihoodfunc(B)
load myvars.mat
L = (n/2)*log(2*pi)+0.5*(y-X*B)'*(y-X*B); %negative log-likelihood

Everything runs fine and my fminsearch returns estimates for B. However, I cannot figure out how to include sigma. The normal log likelihood function should read:

L = (n/2)*log(2*pi*sig^2)+0.5*(1/sig^2)*(y-X*B)'*(y-X*B)

Can fminsearch be used here, or should I be using a different feature?

Steve

unread,
Apr 21, 2011, 10:20:05 AM4/21/11
to
"Aaron " <azo...@gmail.com> wrote in message <ionj70$t4s$1...@fred.mathworks.com>...

You want to estimate sigma as well, correct? I am assuming that B is the mean.

You need to code the likelihood function to accept a vector of parameters that is part mean estimates, and part variance estimates:

function L = likelihoodfunc(params)
%where params = [B;
% sig];

B = params(1:numberOfElementsInB);
sig = params(numberOfElementsInB+1:end);

% The rest of the function goes here ...

If I have this wrong, and you are not estimating sig here, then there is a simple way to do that as well. In this case, sig is fixed beforehand. Define likelihoodfunc as follows:

function L = likelihoodfunc(B,sig)
% The rest of the function goes here ...

And when calling fminsearch, redefine an anonymous function as follows:

sigma = ... % Some fixed values
myObj = @(x) likelihoodfunc(x,sigma);
[...] = fminsearch(myObj, ...)

One other tip, try to avoid loading a MAT file in your likelihood function. This just adds overhead everytime it's called. Instead, load the MAT file once before calling fminsearch, and pass in the data using an anonymous function like I showed in the second code snippet.

Regards,
+Steve

Aaron

unread,
Apr 21, 2011, 2:20:20 PM4/21/11
to
"Steve Grikschat" wrote in message <iopeel$np5$1...@fred.mathworks.com>...

Thanks a ton! Worked like a charm.

rand olpha

unread,
May 3, 2013, 5:15:09 AM5/3/13
to
I am trying to create a simple mle example by writing a negative log likelihood function, assuming a normal distribution, and use fminsearch to find the estimates. However, I am struggling to get fminsearch to get correct estimated parameters. why I can't get correct result?
load data.mat;% data is 30 x 3 matrix
params =[2;2;2];
likepdf=@(dd,p1,p2,p3) (1/(sqrt(2*pi)*p1).*exp(-((dd(:,1)-p2*dd(:,2)-p3*dd(:,3)).^2)/2/p1^2));
myfun = @(par) -prod(likepdf(data,par(1),par(2),par(3)));
[unparams,negmaxlike,exitflag,output] = fminsearch(myfun,params)

unparams =

2
2
2


negmaxlike =

0


exitflag =

1


output =

iterations: 11
funcCount: 54
algorithm: 'Nelder-Mead simplex direct search'
message: [1x194 char]

Alan_Weiss

unread,
May 3, 2013, 9:03:55 AM5/3/13
to
I suggest you take the log likelihood (you have taken the likelihood).
It is very likely (no pun intended) that the likelihood is about zero in
the region of the initial point, and small perturbations don't change
that. The log likelihood will be more sensitive to small changes in the
parameters.

Alan Weiss
MATLAB mathematical toolbox documentation
0 new messages