Re: Numerical Optimization of

*To*: mathgroup at smc.vnet.net*Subject*: [mg44479] Re: Numerical Optimization of*From*: Bill Rowe <readnewscix at mail.earthlink.net>*Date*: Wed, 12 Nov 2003 08:01:28 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

On 11/10/03 at 7:55 PM, rogers at tanis.hss.caltech.edu (Brian Rogers) wrote: > Thanks for the reply! > Here are more details of my program. > The function I care to optimize is NegLogLikelihood[lambda] (which I do > believe actually should be convex, but I'm not positive)--see below. > datamat is a matrix that contains my data. > publicbelief = PA = 0.5. > siga[] and sigb[], and postplus[] and postminus[] compute a Baye's Law > application that depends on a CDF with parameter \lambda. > Just to confirm, I'm actually not using a function with multidimensional > output here, so that is not the problem. > Plot[NegLogLikelihood[z],{z,8,12}] works fine, but > FindMinimum[NegLogLikelihood[z],{z,6,7}] and > NMinimize[NegLogLikelihood[z],{z}] do not--they run forever. > If I replace nMatch by the number 2 in NegLogLikelihood[], then the plot > appears almost instantly, and the optimization takes 36 sec of CPU time > (using Timing[]), which seems way too long. Could something be wrong with > the IF statement? <code for NegLogLikelihood function snipped> Even though you've supplied the code for the function you are trying to find the minimum of I still cannot tell you specifically how to fix your problem. However, I can hazard a guess as to what the issue is likely to be. The code consists of two For loops, one nested in the other. While it is possible to code Mathematica with For loops, in general this results in much slower execution times then functional programming constructs. For example consider In[1]:= data=Table[Random[],{100000}]; In[2]:= Timing[sum=0; For[m=1,m<Length@data+1,m++,sum=sum+(data[[m]])^2]; sum] Out[2]= {2.34 Second,33289.1} In[3]:= Timing[Tr[data^2]] Out[3]= {0.03 Second,33289.1} Both get the same answser, but the functional programming construct executes nearly 100 times faster. Getting back to your particular example, by default Plot samples a function 25 times and makes no further samples if the function is sufficiently flat. OTOH, depending on your choice of starting point, FindMinimum or NMinimize likely needs to evaluate your function many more times than Plot. A second issue is your function depends on the values of variables set outside of the function. For example nSub is used as the limit of the inner most For loop but not defined within your function. IMO, this is poor programming practice even though it may work. Finally, you are calling your function with a argument lambda that doesn't appear to be used anywhere within the function. Why? -- To reply via email subtract one hundred and nine