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?
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
Thanks a ton! Worked like a charm.