<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/265283</link>
    <title>MATLAB Central Newsreader - Help on fminsearch</title>
    <description>Feed for thread: Help on fminsearch</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 08 Nov 2009 10:32:32 -0500</pubDate>
      <title>Help on fminsearch</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/265283#692980</link>
      <author>Steven</author>
      <description>Hi, all&lt;br&gt;
&lt;br&gt;
I am a newbie and need some help on fminsearch. I worte this function&lt;br&gt;
on cox proportional hazard model, and try to maximise the likelihood&lt;br&gt;
on the coefficient of the covariates.&lt;br&gt;
&lt;br&gt;
But the code doesn't work, it only sticks to thei nitial values of the&lt;br&gt;
coefficients, ie. beta = ones(p,1) and won't do the optimisation.&lt;br&gt;
Could anybody please help me on this? Many Thanks!!&lt;br&gt;
&lt;br&gt;
function [logL] = myfun(beta)&lt;br&gt;
&lt;br&gt;
load mydata&lt;br&gt;
&lt;br&gt;
d = size(mydata);&lt;br&gt;
n = d(1,1);&lt;br&gt;
p = d(1,2)-2;&lt;br&gt;
t = mydata(1:n,1);&lt;br&gt;
ind = mydata(1:n,2);&lt;br&gt;
z = mydata(1:n,3:p+2);&lt;br&gt;
beta = ones(p,1);&lt;br&gt;
&lt;br&gt;
ebz = exp(z*beta);&lt;br&gt;
L = ones(n,1);&lt;br&gt;
&lt;br&gt;
for i = 1:n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if ind(i,1)== 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;L(i,1) = ebz(i)/sum(ebz(i:n));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
logL = -sum(log(L));&lt;br&gt;
&lt;br&gt;
[beta] = fminsearch(@myfun,ones(p,1));</description>
    </item>
    <item>
      <pubDate>Sun, 08 Nov 2009 11:15:04 -0500</pubDate>
      <title>Re: Help on fminsearch</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/265283#692989</link>
      <author>John D'Errico</author>
      <description>Steven &amp;lt;ytsteven@gmail.com&amp;gt; wrote in message &amp;lt;09b40b63-dab0-4d44-ae14-e078505de963@s31g2000yqs.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi, all&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I am a newbie and need some help on fminsearch. I worte this function&lt;br&gt;
&amp;gt; on cox proportional hazard model, and try to maximise the likelihood&lt;br&gt;
&amp;gt; on the coefficient of the covariates.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; But the code doesn't work, it only sticks to thei nitial values of the&lt;br&gt;
&amp;gt; coefficients, ie. beta = ones(p,1) and won't do the optimisation.&lt;br&gt;
&amp;gt; Could anybody please help me on this? Many Thanks!!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function [logL] = myfun(beta)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; load mydata&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; d = size(mydata);&lt;br&gt;
&amp;gt; n = d(1,1);&lt;br&gt;
&amp;gt; p = d(1,2)-2;&lt;br&gt;
&amp;gt; t = mydata(1:n,1);&lt;br&gt;
&amp;gt; ind = mydata(1:n,2);&lt;br&gt;
&amp;gt; z = mydata(1:n,3:p+2);&lt;br&gt;
&amp;gt; beta = ones(p,1);&lt;br&gt;
&lt;br&gt;
You do recognize that regardless of the value of&lt;br&gt;
beta as it is passed in, that this line will overwrite it?&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Sun, 08 Nov 2009 11:48:00 -0500</pubDate>
      <title>Re: Help on fminsearch</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/265283#692995</link>
      <author>Steven</author>
      <description>On Nov 8, 10:15&#160;pm, &quot;John D'Errico&quot; &amp;lt;woodch...@rochester.rr.com&amp;gt;&lt;br&gt;
wrote:&lt;br&gt;
&amp;gt; Steven &amp;lt;ytste...@gmail.com&amp;gt; wrote in message &amp;lt;09b40b63-dab0-4d44-ae14-e078505de...@s31g2000yqs.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi, all&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; I am a newbie and need some help on fminsearch. I worte this function&lt;br&gt;
&amp;gt; &amp;gt; on cox proportional hazard model, and try to maximise the likelihood&lt;br&gt;
&amp;gt; &amp;gt; on the coefficient of the covariates.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; But the code doesn't work, it only sticks to thei nitial values of the&lt;br&gt;
&amp;gt; &amp;gt; coefficients, ie. beta = ones(p,1) and won't do the optimisation.&lt;br&gt;
&amp;gt; &amp;gt; Could anybody please help me on this? Many Thanks!!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; function [logL] = myfun(beta)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; load mydata&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; d = size(mydata);&lt;br&gt;
&amp;gt; &amp;gt; n = d(1,1);&lt;br&gt;
&amp;gt; &amp;gt; p = d(1,2)-2;&lt;br&gt;
&amp;gt; &amp;gt; t = mydata(1:n,1);&lt;br&gt;
&amp;gt; &amp;gt; ind = mydata(1:n,2);&lt;br&gt;
&amp;gt; &amp;gt; z = mydata(1:n,3:p+2);&lt;br&gt;
&amp;gt; &amp;gt; beta = ones(p,1);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; You do recognize that regardless of the value of&lt;br&gt;
&amp;gt; beta as it is passed in, that this line will overwrite it?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; John- Hide quoted text -&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; - Show quoted text -&lt;br&gt;
&lt;br&gt;
Hi, John&lt;br&gt;
&lt;br&gt;
Do you mean this line ----&amp;gt; beta = ones(p,1); ?&lt;br&gt;
&lt;br&gt;
But without this line, I always have the error message like &quot;beta is&lt;br&gt;
undefined&quot; or something...</description>
    </item>
    <item>
      <pubDate>Sun, 08 Nov 2009 12:05:19 -0500</pubDate>
      <title>Re: Help on fminsearch</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/265283#692996</link>
      <author>John D'Errico</author>
      <description>Steven &amp;lt;ytsteven@gmail.com&amp;gt; wrote in message &amp;lt;bd6f5cbd-b390-436e-b740-2d13064169ef@l13g2000yqb.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; On Nov 8, 10:15?pm, &quot;John D'Errico&quot; &amp;lt;woodch...@rochester.rr.com&amp;gt;&lt;br&gt;
&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt; Steven &amp;lt;ytste...@gmail.com&amp;gt; wrote in message &amp;lt;09b40b63-dab0-4d44-ae14-e078505de...@s31g2000yqs.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Hi, all&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; I am a newbie and need some help on fminsearch. I worte this function&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; on cox proportional hazard model, and try to maximise the likelihood&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; on the coefficient of the covariates.&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; But the code doesn't work, it only sticks to thei nitial values of the&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; coefficients, ie. beta = ones(p,1) and won't do the optimisation.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Could anybody please help me on this? Many Thanks!!&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; function [logL] = myfun(beta)&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; load mydata&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; d = size(mydata);&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; n = d(1,1);&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; p = d(1,2)-2;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; t = mydata(1:n,1);&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; ind = mydata(1:n,2);&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; z = mydata(1:n,3:p+2);&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; beta = ones(p,1);&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; You do recognize that regardless of the value of&lt;br&gt;
&amp;gt; &amp;gt; beta as it is passed in, that this line will overwrite it?&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; John- Hide quoted text -&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; - Show quoted text -&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hi, John&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Do you mean this line ----&amp;gt; beta = ones(p,1); ?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; But without this line, I always have the error message like &quot;beta is&lt;br&gt;
&amp;gt; undefined&quot; or something...&lt;br&gt;
&lt;br&gt;
How are you calling this?&lt;br&gt;
&lt;br&gt;
Do you actually have the call to fminsearch&lt;br&gt;
INSIDE the myfun function?&lt;br&gt;
&lt;br&gt;
Telling me &quot;or something&quot; gives no useful&lt;br&gt;
information as to the actual error.&lt;br&gt;
&lt;br&gt;
Are you trying to run myfun as a script? Are&lt;br&gt;
you using &quot;run&quot; on it?&lt;br&gt;
&lt;br&gt;
Do you realize that loading the data file for&lt;br&gt;
EVERY function call here is a terrible way to&lt;br&gt;
waste CPU time? Ok, maybe that was wrong.&lt;br&gt;
It is actually a very GOOD way if your goal is&lt;br&gt;
to waste CPU time. Load the variable ONCE in&lt;br&gt;
your mainline, and then pass it in to myfun&lt;br&gt;
using an anonymous function call.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Sun, 08 Nov 2009 13:39:25 -0500</pubDate>
      <title>Re: Help on fminsearch</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/265283#693003</link>
      <author>Steven</author>
      <description>On Nov 8, 11:05&#160;pm, &quot;John D'Errico&quot; &amp;lt;woodch...@rochester.rr.com&amp;gt;&lt;br&gt;
wrote:&lt;br&gt;
&amp;gt; Steven &amp;lt;ytste...@gmail.com&amp;gt; wrote in message &amp;lt;bd6f5cbd-b390-436e-b740-2d1306416...@l13g2000yqb.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; On Nov 8, 10:15?pm, &quot;John D'Errico&quot; &amp;lt;woodch...@rochester.rr.com&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Steven &amp;lt;ytste...@gmail.com&amp;gt; wrote in message &amp;lt;09b40b63-dab0-4d44-ae14-e078505de...@s31g2000yqs.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; Hi, all&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; I am a newbie and need some help on fminsearch. I worte this function&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; on cox proportional hazard model, and try to maximise the likelihood&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; on the coefficient of the covariates.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; But the code doesn't work, it only sticks to thei nitial values of the&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; coefficients, ie. beta = ones(p,1) and won't do the optimisation.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; Could anybody please help me on this? Many Thanks!!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; function [logL] = myfun(beta)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; load mydata&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; d = size(mydata);&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; n = d(1,1);&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; p = d(1,2)-2;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; t = mydata(1:n,1);&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; ind = mydata(1:n,2);&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; z = mydata(1:n,3:p+2);&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &amp;gt; beta = ones(p,1);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; You do recognize that regardless of the value of&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; beta as it is passed in, that this line will overwrite it?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; John- Hide quoted text -&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; - Show quoted text -&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Hi, John&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Do you mean this line ----&amp;gt; beta = ones(p,1); ?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; But without this line, I always have the error message like &quot;beta is&lt;br&gt;
&amp;gt; &amp;gt; undefined&quot; or something...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; How are you calling this?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Do you actually have the call to fminsearch&lt;br&gt;
&amp;gt; INSIDE the myfun function?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Telling me &quot;or something&quot; gives no useful&lt;br&gt;
&amp;gt; information as to the actual error.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Are you trying to run myfun as a script? Are&lt;br&gt;
&amp;gt; you using &quot;run&quot; on it?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Do you realize that loading the data file for&lt;br&gt;
&amp;gt; EVERY function call here is a terrible way to&lt;br&gt;
&amp;gt; waste CPU time? Ok, maybe that was wrong.&lt;br&gt;
&amp;gt; It is actually a very GOOD way if your goal is&lt;br&gt;
&amp;gt; to waste CPU time. Load the variable ONCE in&lt;br&gt;
&amp;gt; your mainline, and then pass it in to myfun&lt;br&gt;
&amp;gt; using an anonymous function call.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; John- Hide quoted text -&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; - Show quoted text -&lt;br&gt;
&lt;br&gt;
I wasn't with a machine with Matlab installed just now. The error is&lt;br&gt;
&quot;Input argument &quot;beta&quot; is undefined.&quot;&lt;br&gt;
&lt;br&gt;
Is there a way to minimise a function without writing a function?&lt;br&gt;
&lt;br&gt;
I don't want to load the data in the function, but the likelihood is&lt;br&gt;
calculated based on the event indicator in the data. I don't know how&lt;br&gt;
to do it without actualy loading the data into the function. (I don't&lt;br&gt;
know hoe to declare variable in a function whithout assigning value)&lt;br&gt;
&lt;br&gt;
I checked the help file which gives an example of  x = fminsearch&lt;br&gt;
(fun,x0) based on banana function, and it doesn't need to declare&lt;br&gt;
variable.</description>
    </item>
    <item>
      <pubDate>Sun, 08 Nov 2009 23:05:14 -0500</pubDate>
      <title>Re: Help on fminsearch</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/265283#693086</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;Steven&quot; &amp;lt;ytsteven@gmail.com&amp;gt; wrote in message &lt;br&gt;
news:09b40b63-dab0-4d44-ae14-e078505de963@s31g2000yqs.googlegroups.com...&lt;br&gt;
&amp;gt; Hi, all&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I am a newbie and need some help on fminsearch. I worte this function&lt;br&gt;
&amp;gt; on cox proportional hazard model, and try to maximise the likelihood&lt;br&gt;
&amp;gt; on the coefficient of the covariates.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; But the code doesn't work, it only sticks to thei nitial values of the&lt;br&gt;
&amp;gt; coefficients, ie. beta = ones(p,1) and won't do the optimisation.&lt;br&gt;
&amp;gt; Could anybody please help me on this? Many Thanks!!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; function [logL] = myfun(beta)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; load mydata&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; d = size(mydata);&lt;br&gt;
&amp;gt; n = d(1,1);&lt;br&gt;
&amp;gt; p = d(1,2)-2;&lt;br&gt;
&amp;gt; t = mydata(1:n,1);&lt;br&gt;
&amp;gt; ind = mydata(1:n,2);&lt;br&gt;
&amp;gt; z = mydata(1:n,3:p+2);&lt;br&gt;
&amp;gt; beta = ones(p,1);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; ebz = exp(z*beta);&lt;br&gt;
&amp;gt; L = ones(n,1);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; for i = 1:n&lt;br&gt;
&amp;gt;    if ind(i,1)== 1&lt;br&gt;
&amp;gt;        L(i,1) = ebz(i)/sum(ebz(i:n));&lt;br&gt;
&amp;gt;    end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; logL = -sum(log(L));&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; [beta] = fminsearch(@myfun,ones(p,1));&lt;br&gt;
&lt;br&gt;
This line indicates to me that you're calling FMINSEARCH from inside myfun &lt;br&gt;
itself.  This is a BAD IDEA -- you WILL receive an error about the Recursion &lt;br&gt;
Limit when you call this function.  See Q4.15 in the newsgroup FAQ for an &lt;br&gt;
explanation of the problem and how to fix it.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com&lt;br&gt;
comp.soft-sys.matlab (CSSM) FAQ: &lt;a href=&quot;http://matlabwiki.mathworks.com/MATLAB_FAQ&quot;&gt;http://matlabwiki.mathworks.com/MATLAB_FAQ&lt;/a&gt; </description>
    </item>
  </channel>
</rss>

