I know that MATLAB has a package for maximum likelihood estimation, but for educational purposes I'm writing by myself an algorithm that gives me back estimates. Now, I have written a function that I'm trying to minimize (since I'm using the negative log likelihood). Here it is:
function ml = two_var(param, data)
mu = param(1);
sigma = param(2);
n=numel(data);
sumto = 0;
for i=1:n
c = data(i)- mu;
sumto = sumto + c;
ml = n/2*log(2*pi)+n/2*log(sigma^2)+1/(2*sigma^2)*sumto^2;
end
This code concerns the estimation of a gaussian distibution. Now, the problem that I have is that this function does not seem to be a valid fminunc
input... How can I circumvert the problem? What am I doing wrong? Thanks to anybody who wants to help ;)
Thanks to everybody that helped in commenting the question. Well, I have used all your useful comments in order to improve my code. Here is my final result.
function ml = t_var(param)
rng default;
data = random('norm',0,1,[400,1]);
z = (data - param(1)) ./ param(2);
L = -.5.*z.*z - log(sqrt(2.*pi).*param(2));
ml = -sum(numel(data).*L);
This code is undoubtedly easier to read; moreover, it makes use of the fast vector operations that are allowed in MATLAB. After defining this function, is then easy to call it with
x0 =[0 0]
[x,fval] = fminsearch(@t_var,x0,optimset('TolX',1e-19,'MaxFunEvals',1000,'MaxIter',1000))
And obtain ML estimates that are consistent with the ones that you can get otherwise.
Thanks a lot to everybody who gave me some hints! ;)