how to let a function call another functions and then optimize it

7 views (last 30 days)
I really need your help about my problem. It is a little complicated so I hope I can explain it well. Firstly, I have decfine a function as
function [ pf ] = ambportfchc (r,lb1,lb2,lb3)
then I define another function
function [ sumllh ] = binormal(hatpc,mu,s1,s12,s2)
rn=size(mu,1);
SIGMA =@(s1,s12,s2) [s1 s12; s12 s2];
for i=1:rn
llh(i,:)=log( mvnpdf(hatpc(i,:),mu(i,:),SIGMA));
end
sumllh=0;
for i=1:size(mu,1)
sumllh=sumllh+ llh(i,:)
end
end
which will call ambportfchc . At the end of day I want use fmincon to optimize binormal through the variables r,lb1,lb2,lb3,s1,s12,s2.
But it doesn't work at all! There are so many errors so I think I have done many things totally wrong. There are several points
1 when I define binormal, should I use mu (which will call function ambporfchc) or the orginal variables r,lb1,lb2,lb3 as input?
2 As you see in sumllh=sumllh+ llh(i,:), I actually want to sum up function handles (notice mu is a matrix, because it is the output of ambportfchc). What I am doing is wrong, isn't it? Then how should I do it right?
I would really appreciate your advice. I have been stuck in this program for a while....

Answers (1)

Star Strider
Star Strider on 17 Jul 2012
The problems I see are:
  1. In the code you posted, ‘ambportfchc’ is missing an ‘end’ statement.
  2. You are not calling ‘binormal’ inside ‘ambportfchc’, so you are not passing the arguments to ‘ambportfchc’ as arguments to ‘binormal’.
  3. You are not assigning any value to ‘pf’ inside ‘ambportfchc’.
I refer you to this description of nested functions:
and this demo:
for details.
It is certainly possible to do what you intend. I have done something similar (integrate differential equations as nested functions inside an objective function) to do nonlinear parameter estimation for parametric curve fitting.
  3 Comments
Star Strider
Star Strider on 18 Jul 2012
You seem to have the functions nested correctly as you list them in your original post, although you need to change your code as I suggested in my answer.
I still do not understand what you want to do. To use an optimization function, ‘ambportfchc’ would be your objective function. It will return ‘pf’ to your optimization function. For parameter estimation, you will need (x,y) data that ’pf’ will approximate for each value of ‘y’ as a function of ‘x’.
The ‘mle’ function requires only a vector of data, so you need to generate a vector describing a probability density function (the ‘data’ vector in the documentation for ‘mle’) and then let ‘mle’ fit it.
xueqi
xueqi on 18 Jul 2012
Hi 'ambportfchc’ is for giving the relation between the independetn variables (r,lb1,lb2,lb3) and dependent variables 'pf'(actually there are two vaiables which I assume they are bivariable normal distributed). 'binormal' is for calculating the sum of log likelihood function. So 'ambportfchc' is nested in 'binormal' and 'binormal' is the objective function when doing optimization.It think it would be easier if I can use some built-in function in matlab to estimator parameters, but all of them seems un-applicable. Maybe I don't look them carefully. I will check if 'mle' works. Thanks:)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!