Writing likelihood function for fmincon

3 views (last 30 days)
Pova
Pova on 18 Nov 2014
Commented: Pova on 22 Nov 2014
I need to write a likelihood function to be optimized via fmincon. The problem here is that:
1) To simplify things, my likelihood function is dependent on \alpha, \beta where \beta is specified somewhere in the code before the fmincon part. \alpha is the vector to be estimated via fmincon.
2) I plan to use fmincon in a for loop which controls the number of samples I need for the likelihood function. In other words, for n = 1, my likelihood function is simply f(x_1;\alpha,\beta) whereas for n = 3, my likelihood function now becomes f(x_1;\alpha,\beta)*f(x_2;\alpha,\beta)*f(x_3;\alpha,\beta)*f(x_4;\alpha,\beta) and so on.
Because of 1), it appears to me that it is necessary for me to write the likelihood function as fun = @(\alpha) ... because if I write a separate function file for it and call it in fmincon, I have two arguments for my likelihood function, one of which is specified earlier in the code.
Because of 2), things are tricky. Can I have a for loop in the fun = @(\alpha) definition of my likelihood function? If so, how do I do it?
Thanks!

Accepted Answer

Matt J
Matt J on 18 Nov 2014
Edited: Matt J on 18 Nov 2014
it appears to me that it is necessary for me to write the likelihood function as fun = @(\alpha) ... because if I write a separate function file for it and call it in fmincon, I have two arguments for my likelihood
You can still have the bulk of your objective function code in a separate file, e.g.,
function val=myObjective(alpha,beta,x)
%resides in myObjective.m
....
end
and then when you call fmincon
fun=@(alpha) myObjective(alpha,beta,x);
fmincon(fun,alpha0,... )
Or, you can make the objective a Nested Function and have x and beta be externally scoped variables.
2) I plan to use fmincon in a for loop which controls the number of samples I need for the likelihood function.
In your loop, you would re-execute
fun=@(alpha) myObjective(alpha,beta,x);
with different vectors x=[x_1,x_2,x_3,...,x_n] of potentially different lengths in each loop iteration. Then, inside myObjective(), use length(x) to guide the computation of val.

More Answers (0)

Categories

Find more on Get Started with Optimization Toolbox 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!