1

I am trying to find log Maximum likelihood estimation for Gaussian distribution, in order to estimate parameters. I know that Matlab has a built-in function that does this by fitting a Gaussian distribution, but I need to do this with logMLE in order to expand this method later for other distributions. So here is the log-likelihood function for gaussian dist : Gaussian Log MLE

And I used this code to estimate the parameters for a set of variables (r) with fminsearch. but my search does not coverage and I don't fully understand where is the problem:

clear
clc
close all
%make random numbers with gaussian dist
r=[2.39587291079469
1.57478022109723
-0.442284350603745
4.39661178526569
7.94034385633171
7.52208574723178
5.80673144943155
-3.11338531920164
6.64267230284774
-2.02996003947964];
% mu=2 sigma=3

%introduce f
f=@(x,r)-(sum((-0.5.*log(2*3.14.*(x(2))))-(((r-(x(2))).^2)./(2.*(x(1))))))
fun = @(x)f(x,r);

% starting point
x0 = [0,0];
 [y,fval,exitflag,output] = fminsearch(fun,x0)


f = 
    @(x,r)-(sum((-0.5.*log(2*3.14.*(x(2))))-(((r-(x(2))).^2)./(2.*(x(1))))))


Exiting: Maximum number of function evaluations has been exceeded
         - increase MaxFunEvals option.
         Current function value: 477814.233176 
y = 1×2    
1.0e+-3 *

    0.2501   -0.0000

fval = 4.7781e+05 + 1.5708e+01i
exitflag = 0
output = 
    iterations: 183
     funcCount: 400
     algorithm: 'Nelder-Mead simplex direct search'
       message: 'Exiting: Maximum number of function evaluations has been exceeded↵         - increase MaxFunEvals option.↵         Current function value: 477814.233176 ↵' 
Frankova T
  • 33
  • 6

1 Answers1

0

Rewrite f as follows:

enter image description here

function y = g(x, r)

     n = length(r);

     log_part = 0.5.*n.*log(x(2).^2);

     sum_part = ((sum(r-x(1))).^2)./(2.*x(2).^2);

     y = log_part + sum_part;

 end

Use fmincon instead of fminsearch because standard deviation is always a positif number.

Set standard deviation lower bound to zero 0


The entire code is as follows:

%make random numbers with gaussian dist
r=[2.39587291079469
1.57478022109723
-0.442284350603745
4.39661178526569
7.94034385633171
7.52208574723178
5.80673144943155
-3.11338531920164
6.64267230284774
-2.02996003947964];
% mu=2 sigma=3

fun = @(x)g(x, r);
% starting point
x0 = [0,0];

% borns 
lb = [-inf, 0];
ub = [inf, inf];
[y, fval] = fmincon(fun,x0,[],[],[],[],lb,ub, []);
function y = g(x, r)

     n = length(r);

     log_part = 0.5.*n.*log(x(2).^2);

     sum_part = ((sum(r-x(1))).^2)./(2.*x(2).^2);

     y = log_part + sum_part;
end

Solution

y = [3.0693    0.0000]

For better estimation use mle() directly

The code is quiet simple:

y = mle(r,'distribution','normal')

Solution

y = [3.0693    3.8056]
Adam
  • 2,726
  • 1
  • 9
  • 22
  • Dear Adam, I know mle does that, but I needed to do this for another usage and wanted to expand this for the distributions for which, matlab does not have built-in code. Yes, your suggestions made it clear – Frankova T Jul 06 '19 at 14:16
  • Alright kindly accept my answer, let me know if you have any question – Adam Jul 06 '19 at 14:27
  • Read this if you have a custom pdf https://stackoverflow.com/questions/56625339/estimating-the-parameters-of-a-custom-distribution-using-mle and this https://stackoverflow.com/questions/56522903/using-mle-function-to-estimate-the-parameters-of-a-custom-distribution/56526868#56526868 – Adam Jul 06 '19 at 14:59
  • Dear Adam, i have accepted your answer but Stack told me my answer cannot be counted since I dont have high reputation; but I was informed that my voting would be considered. – Frankova T Jul 07 '19 at 15:16
  • @FrankovaT Try again, with low reputation you only can accept answers – Adam Jul 07 '19 at 15:18