How to Handle Estimating Parameters With MLE and Fmincon Errors

5 views (last 30 days)
Hi, I am trying to estimate the parameters of a distribution using mle and this is my log likelihood function
written in code as so filename (bgg.m)
function LL = bgg(p,x)
sum1 = 0;
sum2 = 0;
sum3 = 0;
sum4 = 0;
n = length(x);
for i = 1:n
a = exp(p(3) * x(i));
z = exp(-(p(2)/p(3)) * (a-1));
sum1 = sum1 + x(i);
sum2 = sum2 + a;
sum3 = sum3 + log(1 - z);
sum4 = sum4 + log(1 - (1-z)^p(1));
end
LL = n * log(p(1)) + n * log(p(2)) + n * p(2) / p(3) - n * log(beta(p(4),p(5)))...
+ p(3) * sum1 - p(2) / p(3) * sum2 + (p(1) * p(4) - 1) * sum3 + (p(5)-1) * sum4;
LL = double(-LL);
end
and I am trying to minimize the negative loglikelihood using fmincon as follows filename (calculate.m)
p = [];
x = [0.1, 0.2, 1, 1, 1, 1, 1, 2, 3, 6, 7, 11, 12, 18, 18, 18, 18, 18,...
21, 32, 36, 40, 45, 46, 47, 50, 55, 60, 63, 63, 67, 67, 67, 67, ...
72, 75, 79, 82, 82, 83, 84, 84, 84, 85, 85, 85, 85, 85, 86, 86];
lb = [2 0.003 0.05 0.1 0.008];
ub = [3 0.005 0.075 0.15 0.012];
p0 = lb + (ub-lb).*rand(1,length(lb));
i = 1;
options = optimoptions('fmincon','Algorithm',...
'interior-point','Display','iter','MaxFunEvals',1e+5,'MaxIter',500);
Ain = []; bin = []; Aeq = []; beq = [];
p = fmincon(@(p)bgg(p,x), p0,Ain,bin,Aeq,beq,lb,ub,@(p) confun(p), options);
y = bgg(p,x);
function[c,ceq]=confun(p)
c = [];
cin = [];
end
I have gotten multiple errors such as the most recent one is
Any suggestions for the problem I have or alternatives to using the code above? Thank you.

Answers (1)

Matt J
Matt J on 9 Jun 2022
Edited: Matt J on 9 Jun 2022
The error is thrown because your confun tries to return a variable called ceq which you never create. You do create a variable called cin which is never used.
It is moot, however. Since you have no actual nonlinear constraints, there is no need to define a confun() at all:
p = fmincon(@(p)bgg(p,x), p0,Ain,bin,Aeq,beq,lb,ub, [] , options);
  2 Comments
Natasha Davina
Natasha Davina on 10 Jun 2022
@Matt J You're correct thank you very much! If i may ask a followup question, in the example I know the results I wanted and used that as a way of setting the lb, ub, and p0. However if I used random data for X do you know how should I determine the initial values?
Matt J
Matt J on 10 Jun 2022
Are there closed form formulas for the mean and variance of the distribution? Maybe you could use them to minimize a simplified approximation to the LL.

Sign in to comment.

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!