how to optimize maximum like-hood function?

I try to use maximum like-hood approach to calculate some parameters. But I do not know the specific expression of the posibility density function. I write code like this:
i = 2:1000;
p0 = [1, 1, 1];
[x, fval] = fminunc(@(x)(-sum(log(pdf(data(i), t(i), data(i-1), t(i-1), x(1), x(2), x(3))))), p0);
( the function pdf can return a posibility density function when you give a set of input, data(i), t(i), data(i-1), t(i-1), x(1), x(2), x(3). )
And I think I should sum all the natural logarithm of posibility density function and put the result in to an optimizer, so I write these codes. But it can not run well. There is an error says Failure in initial objective function evaluation. FMINUNC cannot continue. However, if I give i a specific number, such as 2 or 3, it can work. But in this way, I can't use other data. I have 1000 thousand pairs of data(i) and t(i).

6 Comments

Before calling fminunc, call your objective function as
z = -sum(log(pdf(data(i),data(i-1),t(i-1),p0(1),p0(2),p0(3))))
and see whether z is a proper value.
Hi Torsten
I try to run as
z = -sum(log(pdf(data(i),t(i),data(i-1),t(i-1),p0(1),p0(2),p0(3))))
and it occurs an error in the function pdf and it can't give a return value. The error says wrong usage of "/". The matrix dimensions must be consistent. So maybe z is not a good value.
Torsten
Torsten on 16 Nov 2018
Edited: Torsten on 16 Nov 2018
Z is only the return parameter from your objective function - the problem lies in the function itself. Somewhere you divide vectors/matices using "/" that can't be divided because their dimensions don't fit.
Yes, I know it. But the function can not have any problems. And I think the problem is in the input data. What I need to do is to calculate the sumation of log(pdf(data(i),t(i),data(i-1),t(i-1),p0(1),p0(2),p0(3), where i equals to 2, 3, 4 , ..., 1000. But I have set i equals to 2:1000. So data(i),t(i),data(i-1),t(i-1) will become an array. But it should be a single value. However, if I use a for loop to do this, I don't know how to fit it into a optimizer like fminunc.
Torsten
Torsten on 16 Nov 2018
Edited: Torsten on 16 Nov 2018
Write a function
function obj = fun(p,t,data)
...
end
include your loop within this function and return the result -sum(log(.... in the variable "obj".
The call to "fminunc" will be
[p, fval] = fminunc(@(p)fun(p,t,data),p0)
Best wishes
Torsten.
Ohhhhh, thanks a lot, Torsten!!!
It works finally!!
Thanks for your help again!
Best,
Zonghao

Sign in to comment.

Answers (0)

Asked:

on 15 Nov 2018

Commented:

on 16 Nov 2018

Community Treasure Hunt

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

Start Hunting!