http://www.mathworks.com/matlabcentral/newsreader/view_thread/286077
MATLAB Central Newsreader  optimization
Feed for thread: optimization
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sat, 03 Jul 2010 16:15:08 +0000
optimization
http://www.mathworks.com/matlabcentral/newsreader/view_thread/286077#759611
Hobst Friedli
Hello<br>
<br>
I am a lerner in Stata and have some major problems replicating results from a paper.<br>
<br>
The Lagrangian was differentated and reformulatet, gives:<br>
<br>
(ni.  nii)/(1pi) +(n.i  nii)/(pi) +lamda = 0 für alle i = 1,...,20<br>
<br>
and Sum (pi) =1<br>
<br>
n.i = sum row<br>
ni. = sum column<br>
nii = diagonal entries<br>
Pii = cellpercentage of rowsum<br>
<br>
I now want to solve this for the Pi's, probably with fmincon. But i just dont get how to specifie the commands.<br>
<br>
For every kind of help i would be very grateful... I need to do this estimation for my bachelor thesis and i cant find help anywhere... Thank you very much in advance<br>
<br>
Here i add the part of the paper i want to replicate :<br>
<br>
<a href="http://img340.imageshack.us/img340/9861/paperu.png">http://img340.imageshack.us/img340/9861/paperu.png</a>

Sat, 03 Jul 2010 17:28:05 +0000
Re: optimization
http://www.mathworks.com/matlabcentral/newsreader/view_thread/286077#759621
us
"Hobst Friedli" <tobias.friedli@access.uzh.ch> wrote in message <i0nnmc$cp2$1@fred.mathworks.com>...<br>
> Hello<br>
> <br>
> I am a lerner in Stata and have some major problems replicating results from a paper.<br>
> <br>
> The Lagrangian was differentated and reformulatet, gives:<br>
> <br>
> (ni.  nii)/(1pi) +(n.i  nii)/(pi) +lamda = 0 für alle i = 1,...,20<br>
> <br>
> and Sum (pi) =1<br>
> <br>
> n.i = sum row<br>
> ni. = sum column<br>
> nii = diagonal entries<br>
> Pii = cellpercentage of rowsum<br>
> <br>
> I now want to solve this for the Pi's, probably with fmincon. But i just dont get how to specifie the commands.<br>
> <br>
> For every kind of help i would be very grateful... I need to do this estimation for my bachelor thesis and i cant find help anywhere... Thank you very much in advance<br>
> <br>
> Here i add the part of the paper i want to replicate :<br>
> <br>
> <a href="http://img340.imageshack.us/img340/9861/paperu.png">http://img340.imageshack.us/img340/9861/paperu.png</a><br>
<br>
what have YOU done so far to solve YOUR particular problem...<br>
(after all, it is YOUR thesis...)<br>
<br>
us

Sun, 04 Jul 2010 15:17:05 +0000
Re: optimization
http://www.mathworks.com/matlabcentral/newsreader/view_thread/286077#759751
Hobst Friedli
"us " <us@neurol.unizh.ch> wrote in message <i0nrv5$fe3$1@fred.mathworks.com>...<br>
> "Hobst Friedli" <tobias.friedli@access.uzh.ch> wrote in message <i0nnmc$cp2$1@fred.mathworks.com>...<br>
> > Hello<br>
> > <br>
> > I am a lerner in Stata and have some major problems replicating results from a paper.<br>
> > <br>
> > The Lagrangian was differentated and reformulatet, gives:<br>
> > <br>
> > (ni.  nii)/(1pi) +(n.i  nii)/(pi) +lamda = 0 für alle i = 1,...,20<br>
> > <br>
> > and Sum (pi) =1<br>
> > <br>
> > n.i = sum row<br>
> > ni. = sum column<br>
> > nii = diagonal entries<br>
> > Pii = cellpercentage of rowsum<br>
> > <br>
> > I now want to solve this for the Pi's, probably with fmincon. But i just dont get how to specifie the commands.<br>
> > <br>
> > For every kind of help i would be very grateful... I need to do this estimation for my bachelor thesis and i cant find help anywhere... Thank you very much in advance<br>
> > <br>
> > Here i add the part of the paper i want to replicate :<br>
> > <br>
> > <a href="http://img340.imageshack.us/img340/9861/paperu.png">http://img340.imageshack.us/img340/9861/paperu.png</a><br>
> <br>
> what have YOU done so far to solve YOUR particular problem...<br>
> (after all, it is YOUR thesis...)<br>
> <br>
> us<br>
<br>
<br>
Sorry, you are right of course, i didn't write what i have so far:<br>
<br>
<br>
clear all<br>
A = [.....]<br>
B = [.....]<br>
C = [.....]<br>
x0 = [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]<br>
<br>
for ind =1:15<br>
nii=A(ind)<br>
n_i=B(ind)<br>
ni_=C(ind)<br>
fun = @(x) [(niini_)/(1x) + (niin_i)/x  L, sum(x)1]<br>
<br>
x(ind,: ) = fsolve(fun,x0)<br>
<br>
I dont know if any of this code is useable. And there are obviously some parts missing.<br>
<br>
Now my problem is still, how can i specifie the L to be a constant for every i ? and it seems that the fsolve does only solve for one variable. But i have 2 Variables x and L. <br>
<br>
Any tipps would be appreciated. Thank you

Mon, 05 Jul 2010 00:33:05 +0000
Re: optimization
http://www.mathworks.com/matlabcentral/newsreader/view_thread/286077#759836
Roger Stafford
"Hobst Friedli" <tobias.friedli@access.uzh.ch> wrote in message <i0q8lh$5v8$1@fred.mathworks.com>...<br>
> > "Hobst Friedli" <tobias.friedli@access.uzh.ch> wrote in message <i0nnmc$cp2$1@fred.mathworks.com>...<br>
> > > .......<br>
> > > (ni.  nii)/(1pi) +(n.i  nii)/(pi) +lamda = 0 für alle i = 1,...,20<br>
> > > <br>
> > > and Sum (pi) =1<br>
> > > <br>
> > > n.i = sum row<br>
> > > ni. = sum column<br>
> > > nii = diagonal entries<br>
> > > Pii = cellpercentage of rowsum<br>
> > > <br>
> > > I now want to solve this for the Pi's, probably with fmincon. But i just dont get how to specifie the commands.<br>
> > > <br>
> > > For every kind of help i would be very grateful... I need to do this estimation for my bachelor thesis and i cant find help anywhere... Thank you very much in advance<br>
> > > <br>
> > > Here i add the part of the paper i want to replicate :<br>
> > > <br>
> > > <a href="http://img340.imageshack.us/img340/9861/paperu.png">http://img340.imageshack.us/img340/9861/paperu.png</a><br>
> .......<br>
> clear all<br>
> A = [.....]<br>
> B = [.....]<br>
> C = [.....]<br>
> x0 = [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]<br>
> <br>
> for ind =1:15<br>
> nii=A(ind)<br>
> n_i=B(ind)<br>
> ni_=C(ind)<br>
> fun = @(x) [(niini_)/(1x) + (niin_i)/x  L, sum(x)1]<br>
> <br>
> x(ind,: ) = fsolve(fun,x0)<br>
> <br>
> I dont know if any of this code is useable. And there are obviously some parts missing.<br>
> <br>
> Now my problem is still, how can i specifie the L to be a constant for every i ? and it seems that the fsolve does only solve for one variable. But i have 2 Variables x and L. <br>
> <br>
> Any tipps would be appreciated. Thank you<br>
        <br>
There are at least three things wrong with your anonymous function fun. First the expression in brackets must be a vector expression, not something that is doneoneatatime in a forloop. Second, the division operators need a dot so as to avoid matrix division. Finally, you need to include the unknown L (which I presume is your lambda) as one of the elements of vector x to be evaluated, say the last one. That means if k is the k of the paper you referenced, then x should have a length of k+1.<br>
<br>
Let A and B be row vectors of length k and defined for each i as<br>
<br>
A(i) = ni.  nii<br>
B(i) = n.i  nii<br>
<br>
in your earlier notation. Then do:<br>
<br>
fun = @(x) [A./(1x(1:k))+B./x(1:k)x(k+1),sum(x(1:k))1]<br>
<br>
x = fsolve(fun,x0);<br>
<br>
Also be sure to include an initial estimate for L in the k+1 element of x0.<br>
<br>
<br>
However, I would predict that fsolve might have difficulty successfully solving the equations in this particular problem. My reasoning goes something like this. For each particular value of L, the ith equation<br>
<br>
A(i)/(1x(i))+B(i)/x(i) = L<br>
<br>
is equivalent to a quadratic equation in x(i) (where I am assuming A(i) and B(i) are always positive quantities since they derive from "ncounts".) There are two real roots, one root, or none, to this quadratic depending on whether<br>
<br>
sqrt(A(i))+sqrt(B(i))<br>
<br>
is less than, equal to, or greater than L, respectively. If L is greater than the maximum of this sum of two square roots for all i, then all k equations would have two roots, so there would be 2^k = 32,768 possible cases which fsolve would have to "investigate" for such L values to make sure it didn't overlook a possible solution to sum(x(1:k))1=0. It looks like a very ugly problem when looked at from that point of view, and I can conceive of fsolve getting hopelessly stuck in these numerous blind alleys.<br>
<br>
I can envision a process by which a single solution can certainly be found, but I see no guarantee that it would be the only possible one. It can easily be seen that as L approaches infinity, one of the roots of each of the above quadratics must approach zero while the other root must approach one. Start with a very high value of L and choose in each case the smaller of the two possible roots for x(i). If L is sufficiently large the sum of these x(i) would be less than 1. Now imagine L decreasing continuously unless reaching the greatest<br>
<br>
sqrt(A(i))+sqrt(B(i))<br>
<br>
at an i=i0, which would be the lowest value that L can be allowed to have. The corresponding sum(x(1:k)) would continuously increase as the roots increase. If it passes through one somewhere along the line, there is your solution. If not, then let L commence increasing again from this minimum value but choosing the greater of the two possible roots for the i0 case while the other roots remain at their lesser values. This would be a continuous process for sum(x(1:k)) with no discontinuous jumps, since you would have passed through the oneroot point at i0 when making the switch in roots. Since x(i0) would now approach one as L again approaches infinity, there surely must come a point where sum(x(1:k)) crosses one, and there would be your solution.<br>
<br>
In other words, there must always exist a solution in which either all the x(i) roots are chosen as the lesser of a pair, or else there is a solution in which just one of the x(i) is the greater of a pair and all the other roots are still at their lesser values.<br>
<br>
However, making the transition for any other of the root pairs from the lower to the upper one would in general involve a discontinuous jump in sum(x(1:k)), so we are not sure any other solutions can be found in the Lvarying process. If they do exist I foresee fsolve having a difficult time finding them.<br>
<br>
As for the single "surefire" solution above, it should be possible to translate that process to something fzero, rather than fsolve, could solve in terms of the single unknown L. It can be determined in advance whether sum(x(1:k)) is above or below one for L chosen at sqrt(A(i0))+sqrt(B(i0)), and this would allow you to determine which of the above two alternatives to select for the x(i0) root in starting fzero with a pair of surrounding estimates for L.<br>
<br>
My apologies for not going into greater detail in the previous paragraph with respect to fzero. I could not be sure you would be interested in such an approach.<br>
<br>
Roger Stafford

Mon, 05 Jul 2010 09:27:03 +0000
Re: optimization
http://www.mathworks.com/matlabcentral/newsreader/view_thread/286077#759891
Hobst Friedli
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i0r981$5nd$1@fred.mathworks.com>...<br>
> "Hobst Friedli" <tobias.friedli@access.uzh.ch> wrote in message <i0q8lh$5v8$1@fred.mathworks.com>...<br>
> > > "Hobst Friedli" <tobias.friedli@access.uzh.ch> wrote in message <i0nnmc$cp2$1@fred.mathworks.com>...<br>
> > > > .......<br>
> > > > (ni.  nii)/(1pi) +(n.i  nii)/(pi) +lamda = 0 für alle i = 1,...,20<br>
> > > > <br>
> > > > and Sum (pi) =1<br>
> > > > <br>
> > > > n.i = sum row<br>
> > > > ni. = sum column<br>
> > > > nii = diagonal entries<br>
> > > > Pii = cellpercentage of rowsum<br>
> > > > <br>
> > > > I now want to solve this for the Pi's, probably with fmincon. But i just dont get how to specifie the commands.<br>
> > > > <br>
> > > > For every kind of help i would be very grateful... I need to do this estimation for my bachelor thesis and i cant find help anywhere... Thank you very much in advance<br>
> > > > <br>
> > > > Here i add the part of the paper i want to replicate :<br>
> > > > <br>
> > > > <a href="http://img340.imageshack.us/img340/9861/paperu.png">http://img340.imageshack.us/img340/9861/paperu.png</a><br>
> > .......<br>
> > clear all<br>
> > A = [.....]<br>
> > B = [.....]<br>
> > C = [.....]<br>
> > x0 = [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]<br>
> > <br>
> > for ind =1:15<br>
> > nii=A(ind)<br>
> > n_i=B(ind)<br>
> > ni_=C(ind)<br>
> > fun = @(x) [(niini_)/(1x) + (niin_i)/x  L, sum(x)1]<br>
> > <br>
> > x(ind,: ) = fsolve(fun,x0)<br>
> > <br>
> > I dont know if any of this code is useable. And there are obviously some parts missing.<br>
> > <br>
> > Now my problem is still, how can i specifie the L to be a constant for every i ? and it seems that the fsolve does only solve for one variable. But i have 2 Variables x and L. <br>
> > <br>
> > Any tipps would be appreciated. Thank you<br>
>         <br>
> There are at least three things wrong with your anonymous function fun. First the expression in brackets must be a vector expression, not something that is doneoneatatime in a forloop. Second, the division operators need a dot so as to avoid matrix division. Finally, you need to include the unknown L (which I presume is your lambda) as one of the elements of vector x to be evaluated, say the last one. That means if k is the k of the paper you referenced, then x should have a length of k+1.<br>
> <br>
> Let A and B be row vectors of length k and defined for each i as<br>
> <br>
> A(i) = ni.  nii<br>
> B(i) = n.i  nii<br>
> <br>
> in your earlier notation. Then do:<br>
> <br>
> fun = @(x) [A./(1x(1:k))+B./x(1:k)x(k+1),sum(x(1:k))1]<br>
> <br>
> x = fsolve(fun,x0);<br>
> <br>
> Also be sure to include an initial estimate for L in the k+1 element of x0.<br>
> <br>
> <br>
> However, I would predict that fsolve might have difficulty successfully solving the equations in this particular problem. My reasoning goes something like this. For each particular value of L, the ith equation<br>
> <br>
> A(i)/(1x(i))+B(i)/x(i) = L<br>
> <br>
> is equivalent to a quadratic equation in x(i) (where I am assuming A(i) and B(i) are always positive quantities since they derive from "ncounts".) There are two real roots, one root, or none, to this quadratic depending on whether<br>
> <br>
> sqrt(A(i))+sqrt(B(i))<br>
> <br>
> is less than, equal to, or greater than L, respectively. If L is greater than the maximum of this sum of two square roots for all i, then all k equations would have two roots, so there would be 2^k = 32,768 possible cases which fsolve would have to "investigate" for such L values to make sure it didn't overlook a possible solution to sum(x(1:k))1=0. It looks like a very ugly problem when looked at from that point of view, and I can conceive of fsolve getting hopelessly stuck in these numerous blind alleys.<br>
> <br>
> I can envision a process by which a single solution can certainly be found, but I see no guarantee that it would be the only possible one. It can easily be seen that as L approaches infinity, one of the roots of each of the above quadratics must approach zero while the other root must approach one. Start with a very high value of L and choose in each case the smaller of the two possible roots for x(i). If L is sufficiently large the sum of these x(i) would be less than 1. Now imagine L decreasing continuously unless reaching the greatest<br>
> <br>
> sqrt(A(i))+sqrt(B(i))<br>
> <br>
> at an i=i0, which would be the lowest value that L can be allowed to have. The corresponding sum(x(1:k)) would continuously increase as the roots increase. If it passes through one somewhere along the line, there is your solution. If not, then let L commence increasing again from this minimum value but choosing the greater of the two possible roots for the i0 case while the other roots remain at their lesser values. This would be a continuous process for sum(x(1:k)) with no discontinuous jumps, since you would have passed through the oneroot point at i0 when making the switch in roots. Since x(i0) would now approach one as L again approaches infinity, there surely must come a point where sum(x(1:k)) crosses one, and there would be your solution.<br>
> <br>
> In other words, there must always exist a solution in which either all the x(i) roots are chosen as the lesser of a pair, or else there is a solution in which just one of the x(i) is the greater of a pair and all the other roots are still at their lesser values.<br>
> <br>
> However, making the transition for any other of the root pairs from the lower to the upper one would in general involve a discontinuous jump in sum(x(1:k)), so we are not sure any other solutions can be found in the Lvarying process. If they do exist I foresee fsolve having a difficult time finding them.<br>
> <br>
> As for the single "surefire" solution above, it should be possible to translate that process to something fzero, rather than fsolve, could solve in terms of the single unknown L. It can be determined in advance whether sum(x(1:k)) is above or below one for L chosen at sqrt(A(i0))+sqrt(B(i0)), and this would allow you to determine which of the above two alternatives to select for the x(i0) root in starting fzero with a pair of surrounding estimates for L.<br>
> <br>
> My apologies for not going into greater detail in the previous paragraph with respect to fzero. I could not be sure you would be interested in such an approach.<br>
> <br>
> Roger Stafford<br>
<br>
<br>
<br>
Thank you very much for your detailed answer, i really appreciate it and it helps me a lot. I will implement the suggested changes today and see if it gives me useful results. As i have the original data and results from a similar estimation, i can easily verify the results. I see the problem your mentioning.<br>
<br>
an other idea was to not use the lamda and the sum(x)=1 at all. My thoungt is, that as long as i do not have a unknown lamda in the equation i dont need the sum=1 equation, the xi are determines by the k equations. If i set lamda =0 or zero (or whatever) the relatiive values for xi should stay the same. Then i can normalize the xi in a second step, by deviding the result vertor by sum(x).<br>
<br>
Anyways i will try these approaches, and report back later. And again, thank you for your help.

Mon, 05 Jul 2010 15:12:07 +0000
Re: optimization
http://www.mathworks.com/matlabcentral/newsreader/view_thread/286077#759951
Roger Stafford
"Hobst Friedli" <tobias.friedli@access.uzh.ch> wrote in message <i0s8h7$8c7$1@fred.mathworks.com>...<br>
> .......<br>
> an other idea was to not use the lamda and the sum(x)=1 at all. My thoungt is, that as long as i do not have a unknown lamda in the equation i dont need the sum=1 equation, the xi are determines by the k equations. If i set lamda =0 or zero (or whatever) the relatiive values for xi should stay the same. Then i can normalize the xi in a second step, by deviding the result vertor by sum(x).<br>
> .......<br>
         <br>
I don't think that would give a valid solution. Multiplying all the xi by a normalizing factor could give you a sum xi of one, but in general it would also cause the quantities<br>
<br>
(ni_nii)/(1xi) +(n_inii)/xi <br>
<br>
to no longer be equal to one another for the various values of i. Consequently your probability values would then fail to represent a maximum likelihood. Having a common value of L along with a sum xi of one would appear to be a crucial ingredient in finding a valid solution.<br>
<br>
After giving your problem more thought it seems likely to me that a solution using all lower quadratic roots is the only one possible. In this case, using fzero with L as the only unknown would surely be the best way of solving your equations. The use of fsolve with many unknowns may end up searching a great many blind alleys.<br>
<br>
For the equation A/(1x) + B/x = L, the lower root is given by<br>
<br>
x = (L+BAsqrt((LAB)^24*A*B))/(2*L)<br>
<br>
so adjusting the single variable L so that the sum of these lower roots is one, is the task that you could set fzero to solving. It should perform very well if given an appropriate initial x0 interval.<br>
<br>
By the way, in my previous article I misstated the value of L which gives just one root in the quadratic. It should have been<br>
<br>
(sqrt(A(i))+sqrt(B(i)))^2<br>
<br>
instead of just<br>
<br>
sqrt(A(i))+sqrt(B(i))<br>
<br>
Roger Stafford