Thread Subject: Help on fminsearch

Subject: Help on fminsearch

From: Steven

Date: 8 Nov, 2009 10:32:32

Message: 1 of 6

Hi, all

I am a newbie and need some help on fminsearch. I worte this function
on cox proportional hazard model, and try to maximise the likelihood
on the coefficient of the covariates.

But the code doesn't work, it only sticks to thei nitial values of the
coefficients, ie. beta = ones(p,1) and won't do the optimisation.
Could anybody please help me on this? Many Thanks!!

function [logL] = myfun(beta)

load mydata

d = size(mydata);
n = d(1,1);
p = d(1,2)-2;
t = mydata(1:n,1);
ind = mydata(1:n,2);
z = mydata(1:n,3:p+2);
beta = ones(p,1);

ebz = exp(z*beta);
L = ones(n,1);

for i = 1:n
    if ind(i,1)== 1
        L(i,1) = ebz(i)/sum(ebz(i:n));
    end
end

logL = -sum(log(L));

[beta] = fminsearch(@myfun,ones(p,1));

Subject: Help on fminsearch

From: John D'Errico

Date: 8 Nov, 2009 11:15:04

Message: 2 of 6

Steven <ytsteven@gmail.com> wrote in message <09b40b63-dab0-4d44-ae14-e078505de963@s31g2000yqs.googlegroups.com>...
> Hi, all
>
> I am a newbie and need some help on fminsearch. I worte this function
> on cox proportional hazard model, and try to maximise the likelihood
> on the coefficient of the covariates.
>
> But the code doesn't work, it only sticks to thei nitial values of the
> coefficients, ie. beta = ones(p,1) and won't do the optimisation.
> Could anybody please help me on this? Many Thanks!!
>
> function [logL] = myfun(beta)
>
> load mydata
>
> d = size(mydata);
> n = d(1,1);
> p = d(1,2)-2;
> t = mydata(1:n,1);
> ind = mydata(1:n,2);
> z = mydata(1:n,3:p+2);
> beta = ones(p,1);

You do recognize that regardless of the value of
beta as it is passed in, that this line will overwrite it?

John

Subject: Help on fminsearch

From: Steven

Date: 8 Nov, 2009 11:48:00

Message: 3 of 6

On Nov 8, 10:15 pm, "John D'Errico" <woodch...@rochester.rr.com>
wrote:
> Steven <ytste...@gmail.com> wrote in message <09b40b63-dab0-4d44-ae14-e078505de...@s31g2000yqs.googlegroups.com>...
> > Hi, all
>
> > I am a newbie and need some help on fminsearch. I worte this function
> > on cox proportional hazard model, and try to maximise the likelihood
> > on the coefficient of the covariates.
>
> > But the code doesn't work, it only sticks to thei nitial values of the
> > coefficients, ie. beta = ones(p,1) and won't do the optimisation.
> > Could anybody please help me on this? Many Thanks!!
>
> > function [logL] = myfun(beta)
>
> > load mydata
>
> > d = size(mydata);
> > n = d(1,1);
> > p = d(1,2)-2;
> > t = mydata(1:n,1);
> > ind = mydata(1:n,2);
> > z = mydata(1:n,3:p+2);
> > beta = ones(p,1);
>
> You do recognize that regardless of the value of
> beta as it is passed in, that this line will overwrite it?
>
> John- Hide quoted text -
>
> - Show quoted text -

Hi, John

Do you mean this line ----> beta = ones(p,1); ?

But without this line, I always have the error message like "beta is
undefined" or something...

Subject: Help on fminsearch

From: John D'Errico

Date: 8 Nov, 2009 12:05:19

Message: 4 of 6

Steven <ytsteven@gmail.com> wrote in message <bd6f5cbd-b390-436e-b740-2d13064169ef@l13g2000yqb.googlegroups.com>...
> On Nov 8, 10:15?pm, "John D'Errico" <woodch...@rochester.rr.com>
> wrote:
> > Steven <ytste...@gmail.com> wrote in message <09b40b63-dab0-4d44-ae14-e078505de...@s31g2000yqs.googlegroups.com>...
> > > Hi, all
> >
> > > I am a newbie and need some help on fminsearch. I worte this function
> > > on cox proportional hazard model, and try to maximise the likelihood
> > > on the coefficient of the covariates.
> >
> > > But the code doesn't work, it only sticks to thei nitial values of the
> > > coefficients, ie. beta = ones(p,1) and won't do the optimisation.
> > > Could anybody please help me on this? Many Thanks!!
> >
> > > function [logL] = myfun(beta)
> >
> > > load mydata
> >
> > > d = size(mydata);
> > > n = d(1,1);
> > > p = d(1,2)-2;
> > > t = mydata(1:n,1);
> > > ind = mydata(1:n,2);
> > > z = mydata(1:n,3:p+2);
> > > beta = ones(p,1);
> >
> > You do recognize that regardless of the value of
> > beta as it is passed in, that this line will overwrite it?
> >
> > John- Hide quoted text -
> >
> > - Show quoted text -
>
> Hi, John
>
> Do you mean this line ----> beta = ones(p,1); ?
>
> But without this line, I always have the error message like "beta is
> undefined" or something...

How are you calling this?

Do you actually have the call to fminsearch
INSIDE the myfun function?

Telling me "or something" gives no useful
information as to the actual error.

Are you trying to run myfun as a script? Are
you using "run" on it?

Do you realize that loading the data file for
EVERY function call here is a terrible way to
waste CPU time? Ok, maybe that was wrong.
It is actually a very GOOD way if your goal is
to waste CPU time. Load the variable ONCE in
your mainline, and then pass it in to myfun
using an anonymous function call.

John

Subject: Help on fminsearch

From: Steven

Date: 8 Nov, 2009 13:39:25

Message: 5 of 6

On Nov 8, 11:05 pm, "John D'Errico" <woodch...@rochester.rr.com>
wrote:
> Steven <ytste...@gmail.com> wrote in message <bd6f5cbd-b390-436e-b740-2d1306416...@l13g2000yqb.googlegroups.com>...
> > On Nov 8, 10:15?pm, "John D'Errico" <woodch...@rochester.rr.com>
> > wrote:
> > > Steven <ytste...@gmail.com> wrote in message <09b40b63-dab0-4d44-ae14-e078505de...@s31g2000yqs.googlegroups.com>...
> > > > Hi, all
>
> > > > I am a newbie and need some help on fminsearch. I worte this function
> > > > on cox proportional hazard model, and try to maximise the likelihood
> > > > on the coefficient of the covariates.
>
> > > > But the code doesn't work, it only sticks to thei nitial values of the
> > > > coefficients, ie. beta = ones(p,1) and won't do the optimisation.
> > > > Could anybody please help me on this? Many Thanks!!
>
> > > > function [logL] = myfun(beta)
>
> > > > load mydata
>
> > > > d = size(mydata);
> > > > n = d(1,1);
> > > > p = d(1,2)-2;
> > > > t = mydata(1:n,1);
> > > > ind = mydata(1:n,2);
> > > > z = mydata(1:n,3:p+2);
> > > > beta = ones(p,1);
>
> > > You do recognize that regardless of the value of
> > > beta as it is passed in, that this line will overwrite it?
>
> > > John- Hide quoted text -
>
> > > - Show quoted text -
>
> > Hi, John
>
> > Do you mean this line ----> beta = ones(p,1); ?
>
> > But without this line, I always have the error message like "beta is
> > undefined" or something...
>
> How are you calling this?
>
> Do you actually have the call to fminsearch
> INSIDE the myfun function?
>
> Telling me "or something" gives no useful
> information as to the actual error.
>
> Are you trying to run myfun as a script? Are
> you using "run" on it?
>
> Do you realize that loading the data file for
> EVERY function call here is a terrible way to
> waste CPU time? Ok, maybe that was wrong.
> It is actually a very GOOD way if your goal is
> to waste CPU time. Load the variable ONCE in
> your mainline, and then pass it in to myfun
> using an anonymous function call.
>
> John- Hide quoted text -
>
> - Show quoted text -

I wasn't with a machine with Matlab installed just now. The error is
"Input argument "beta" is undefined."

Is there a way to minimise a function without writing a function?

I don't want to load the data in the function, but the likelihood is
calculated based on the event indicator in the data. I don't know how
to do it without actualy loading the data into the function. (I don't
know hoe to declare variable in a function whithout assigning value)

I checked the help file which gives an example of x = fminsearch
(fun,x0) based on banana function, and it doesn't need to declare
variable.

Subject: Help on fminsearch

From: Steven Lord

Date: 8 Nov, 2009 23:05:14

Message: 6 of 6


"Steven" <ytsteven@gmail.com> wrote in message
news:09b40b63-dab0-4d44-ae14-e078505de963@s31g2000yqs.googlegroups.com...
> Hi, all
>
> I am a newbie and need some help on fminsearch. I worte this function
> on cox proportional hazard model, and try to maximise the likelihood
> on the coefficient of the covariates.
>
> But the code doesn't work, it only sticks to thei nitial values of the
> coefficients, ie. beta = ones(p,1) and won't do the optimisation.
> Could anybody please help me on this? Many Thanks!!
>
> function [logL] = myfun(beta)
>
> load mydata
>
> d = size(mydata);
> n = d(1,1);
> p = d(1,2)-2;
> t = mydata(1:n,1);
> ind = mydata(1:n,2);
> z = mydata(1:n,3:p+2);
> beta = ones(p,1);
>
> ebz = exp(z*beta);
> L = ones(n,1);
>
> for i = 1:n
> if ind(i,1)== 1
> L(i,1) = ebz(i)/sum(ebz(i:n));
> end
> end
>
> logL = -sum(log(L));
>
> [beta] = fminsearch(@myfun,ones(p,1));

This line indicates to me that you're calling FMINSEARCH from inside myfun
itself. This is a BAD IDEA -- you WILL receive an error about the Recursion
Limit when you call this function. See Q4.15 in the newsgroup FAQ for an
explanation of the problem and how to fix it.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com