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 ;)
fminunc(@(param) two_var(param, data), param0 )
? – Jommysumto
- on each step of the loop, you are addingc
to it, and then computing again the variableml
(whose value at the final iteration is what your function will return). You should probably get rid of the loop altogether and just apply vectorized operations todata
. Then usesum
to compute the value of the likelihood function. – mikkola