Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Problem with NMaximize
Replies: 11   Last Post: Jun 28, 2008 5:58 AM

 Messages: [ Previous | Next ]
 Daniel Lichtblau Posts: 1,761 Registered: 12/7/04
Re: Problem with NMaximize
Posted: Jun 28, 2008 5:58 AM

ramiro wrote:
> Thank you very much for your responses, Bobby and Jean-Marc. I did try
> changing the parametrization (thanks Bobby!) and I got an answer.
>
> However, let me try again with a hopefully more clear example, as I
> don't understand why this doesn't work, in particular, I don't
> understand why is it evaluating negative numbers if I am putting as a
> constraint that it shouldn't?
>
>
> Sample likelihood,
>
> In[196]:=
> logLikelihood1[t1_?NumberQ, t2_?NumberQ, t3_?NumberQ, t4_?NumberQ,
> t5_?NumberQ, t6_?NumberQ, t7_?NumberQ, t8_?NumberQ, t9_?NumberQ,
> t10_?NumberQ, t11_?NumberQ, t12_?NumberQ] :=
> Log[0.00001712341312000713` t1 + 5.648647024829866`*^-6 t10 +
> 8.90597537441381`*^-6 t11 + 1.9727284288726167`*^-6 t12 +
> 4.102725237468317`*^-6 t2 + 3.7864902508468615`*^-6 t3 +
> 9.215772325326653`*^-7 t4 + 0.000057161484895917856` t5 +
> 0.000012892953334779516` t6 + 8.646320198720343`*^-6 t7 +
> 5.877910295858781`*^-6 t8 + 1.6837835562631724`*^-6 t9];
> [...]
> In[198]:= NMaximize[{logLikelihood1[t1, t2, t3, t4, t5, t6, t7, t8,
> t9, t10, t11, t12], t1 > 0, t2 > 0, t3 > 0, t4 > 0, t5 > 0, t6 > 0,
> t7 > 0, t8 > 0, t9 > 0, t10 > 0, t11 > 0, t12 > 0,
> t1 + t10 + t11 + t12 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 ==
> 1}, {t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12}]
>
> During evaluation of In[198]:= NMaximize::nrnum: The function value \
> 15.7729-3.14159 I is not a real number at \
> {t1,t10,t11,t12,t2,t3,t4,t5,t6,t7,<<2>>} = {-0.490249,<<9>>,<<2>>}. \

> >>
>
> Out[198]= {-13.2945, {t1 -> 2.10942*10^-15, t10 -> -2.22045*10^-16,
> t11 -> -2.22045*10^-16, t12 -> -2.22045*10^-16,
> t2 -> -1.11022*10^-16, t3 -> -2.22045*10^-16, t4 -> -2.22045*10^-16,
> t5 -> -6.93889*10^-18, t6 -> -2.22045*10^-16,
> t7 -> -1.11022*10^-16, t8 -> -4.44089*10^-16, t9 -> 1.}}
> [...]

Handling of constraints can be slightly tricky; sometimes the code will
allow a small amount of slop so as not to chase it's metaphorical tail
trying to satisfy them. Some tactics you might try include forcing a log
of a negative to evaluate to something that is clearly undesirable, and
maybe further constraining so that all variables are lessequal to 1.
Other things you could try include adding penalties based on UnitStep of
negative variables, and maybe play with Precision/AccuracyGoal (I found
that these hurt more than they helped).

Here is code to do some of this.

myLog[t_?NumberQ /; t<=0] := -10^6
myLog[t_] := Log[t]

vec = {0.00001712341312000713,4.102725237468317*^-6,3.7864902508468615*^-6,
9.215772325326653*^-7,0.000057161484895917856,0.000012892953334779516,
8.646320198720343*^-6,5.877910295858781*^-6,1.6837835562631724*^-6,
5.648647024829866*^-6,8.90597537441381*^-6,1.9727284288726167*^-6};

vars = Array[t,Length[vec]];

logLikelihood1[vars:{_?NumberQ..}] := myLog[vec.vars]

In[49]:= InputForm[NMaximize[{logLikelihood1[vars],
Flatten[{Map[0<=#<=1&,vars], Total[vars]==1}]}, vars]]

Out[49]//InputForm=
{-9.76963022735483, {t[1] -> 0., t[2] -> 0., t[3] -> 0., t[4] -> 0.,
t[5] -> 1., t[6] -> 0., t[7] -> 0., t[8] -> 0., t[9] -> 0.,
t[10] -> 0., t[11] -> 0., t[12] -> 0.}}

Daniel Lichtblau
Wolfram Research

Date Subject Author
6/26/08 Ramiro Barrantes-Reynolds
6/27/08 Jean-Marc Gulliet
6/27/08 ramiro
6/27/08 DrMajorBob
6/27/08 DrMajorBob
6/27/08 Jean-Marc Gulliet
6/28/08 mante
6/28/08 Bill Rowe
6/28/08 Daniel Lichtblau