Minimize function by choosing a variable that enters indirectly
9 views (last 30 days)
Show older comments
Hello,
I am running a simulation of non-parametric estimation in which I need to minimize (maximize) a certain log-likelihood function. The function and commands I am using are:
LL=@(Bbin) (-1)*(sum(Yc'*log(proby*(fxnc./fxna))+(ones(M,1)-Yc)'*... log(ones(M,1)-proby*(fxnc./fxna))));
Bbin = fminsearch(LL,[-1;-1;-1])
As you can see, I am trying to find the Bbin that minimizes LL, but Bbin does not directly enter into my function "LL". Instead, it is used to calculate variables "fxnc" and "fxna". Those variables are calculated in such a way that I cannot add their calculation into the LL function (the code for how they are calculated is pasted below, but I don't believe it is relevant and may just add extra confusion). As you might guess, when trying to minimize LL, Matlab treats fxnc and fxna as an exogenous matrix, and therefore changing Bbin does not effect the LL function. Naturally, it spits out my original values for Bbin [-1;-1;-1]. How can I tell Matlab that varying Bbin changes fxnc and fxna?
Thanks!
Code for how fxnc and fxna are calculated:
%Calculate conditional means for Yc M=sum(Y); index=Xc*Bbin; si=std(index); fxnc=zeros(M,1); h=1.06*si*M^(-0.2); d=zeros(M,1); for j=1:M d(:,1)=(index(j,1)-index(:,1))./h; fx1c=normpdf(d); fxnc(j,1)=mean(fx1c/h); end
%Calculate conditional means for Y fxn=zeros(N,1); index=X*Bbin; si=std(index); h=1.06*si*N^(-0.2); d=zeros(N,1); for j=1:N d(:,1)=(index(j,1)-index(:,1))./h; fx1=normpdf(d); fxn(j,1)=mean(fx1/h); end
7 Comments
Star Strider
on 30 Sep 2012
Edited: Star Strider
on 30 Sep 2012
What do you want to do and how do you want Bbin to be optimised? What's your objective?
I'm lost.
Accepted Answer
Babak
on 28 Sep 2012
I wouldn't use an inline function like @(Bbin)... for this case.
I would do it this way: Write a function like this: (untested code - just a template)
function LL_value = LL_function(Bbin)
...
fxnc = ...
fxna = ...
LL_function = ...
end
then in another script .m file, wirte routines that calls the LL_function recursively, like this:
%initializing your parameters
...
Bbin_initial = [-1;-1;-1]
LL_value = LL_function(Bbin)
Bbin_new = fminsearch(LL_function,Bbin_initial)
If this doesn't work fine, you can write your own minimization routine like this:
%initializing your parameters
...
Bbin_initial = [-1;-1;-1]
while abs(Bbin_new - Bbin_old) > 0.001
LL_value = LL_function(Bbin)
Bbin_new = YourMinimizationRoutine(LL_function,Bbin_initial)
end
Where you can use a newton raphson method to write the YourMinimizationRoutine function.
2 Comments
Babak
on 2 Oct 2012
for three dimension, you can also write routines like a 1 dim minimizor. Try doing something like this:
function Fvalue = func(vect)
x = vect(1); y = vect(2); z = vect(3);
Fvalue = x^2+y^2+z^2 % or the function you want to minimize
end
then write your minimization script like MATLAB's minimizors, like,
IC = [1,2,3]; %initial condition
value_i = func(IC)
delx_i=0.1;dely_i=0.1;delz_i=0.1;
while abs(error)>1e-5 && i<10000
% compute the value of the function as you deviate from x by delx_i
x0 = IC+delx_i*[1,0,0];
val_dx = func(x0)
% compute the value of the function as you deviate from y by dely_i
y0 = IC+dely_i*[0,1,0];
val_dy = func(y0)
% compute the value of the function as you deviate from z by delz_i
z0 = IC+delz_i*[0,0,1];
val_dz = func(z0)
% Next, compare the new values val_dx,val_dy,val_dz,Value_i
and choose the direction at which the function value has decreased the most. This direction is in the form of [a,b,c] like [3,-4,-12]/13. Then compute the value for the new input to the func you just found like IC+[a,b,c] and repeat until you either converge to a local/global minimum point or exceed a specific number of iterations,...
end
More Answers (0)
See Also
Categories
Find more on Parallel Computing 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!