Re: Simple Optimization Problem: Using BinCounts within

*To*: mathgroup at smc.vnet.net*Subject*: [mg103792] Re: [mg103785] Simple Optimization Problem: Using BinCounts within*From*: Leonid Shifrin <lshifr at gmail.com>*Date*: Tue, 6 Oct 2009 08:01:18 -0400 (EDT)*References*: <200910051758.NAA11912@smc.vnet.net>

Hi Chris, It is not difficult to get rid of error messages: use the NumericQ predicate to test the arguments in your functions: f[a_?NumericQ, b_?NumericQ, c_?NumericQ] := BinCounts[data, {{0, a, b, c, 1}}]; g[a_?NumericQ, b_?NumericQ, c_?NumericQ] := Max[f[a, b, c]] - Min[f[a, b, c]] ; The real problem seems to maximize the right functional. With your current choice it will always be advantageous to pick the bin where all points are, as one bin, so that there is at least one bin with zero bin count, then your <g> is naturally maximal, and this is what you get in your solution {a -> 0., b -> 0., c -> 1.} You can also try a different setting for a Method option in NMaximize. Hope this helps. Regards, Leonid On Mon, Oct 5, 2009 at 9:58 PM, Chris <uk.org.microserf at googlemail.com>wrote: > Hi > > I need to optimize a function that is defined in terms of BinCounts; > the arguments that I want to maximize over define the bin cutpoints. > > I think the problem is that Mathematica expands the objective function > in terms of the arguments before they have been given numerical > values, so BinCounts complains that the bin specification is not "a > list containing real values, Infinity, and -Infinity". > > I think the following is a minimal example of the kind of thing I'm > trying to do and what's happening. I'd be very grateful for advice on > how to address this problem. > > Thanks > > Chris > > > data = RandomReal[1, 30]; (* Create 30 real-valued random numbers. *) > > In[574]:= f[a_, b_, c_] := BinCounts[data, {{0, a, b, c, 1}}] (* > Define a function to bin the values according to a spec. *) > > In[576]:= g[a_, b_, c_] := Max[f[a, b, c]] - Min[f[a, b, c]] (* Define > the objective function. *) > > In[584]:= NMaximize[{g[a, b, c], 0 < a < b < c < 1}, {a, b, c}] (* > Attempt to maximize g, with sensible constraints. *) > > During evaluation of In[584]:= BinCounts::cvals: The bin specification > {{0,a,b,c,1}} is not a list containing real values, Infinity, and - > Infinity. >> > During evaluation of In[584]:= BinCounts::cvals: The bin specification > {{0,a,b,c,1}} is not a list containing real values, Infinity, and - > Infinity. >> > During evaluation of In[584]:= BinCounts::cvals: The bin specification > {{0,a,b,c,1}} is not a list containing real values, Infinity, and - > Infinity. >> > During evaluation of In[584]:= General::stop: Further output of > BinCounts::cvals will be suppressed during this calculation. >> > > Out[584]= {0., {a -> 0., b -> 0., c -> 1.}} > >

**References**:**Simple Optimization Problem: Using BinCounts within objective***From:*Chris <uk.org.microserf@googlemail.com>