# NMaximize/NArgMax complain about nonnumerical value (::nnum) when working with a region [duplicate]

## Finding the mode of a SmoothKernelDistribution

I am using a smooth kernel approximation to a mixture distribution to find the distribution's mode (e.g. by using NArgMax). To demonstrate the issue that I am facing I have reproduced it by simply using a normal distribution:

SeedRandom["NArgMax160404"];
sample = RandomVariate[ NormalDistribution[ 5, 3 ], 10^6 ];
region = Interval[ {0, 10} ]; (* the region of definition/interest *)
dist = TruncatedDistribution[ { 0, 10 }, SmoothKernelDistribution @ sample ];
Plot[ Evaluate @ PDF[ dist, x ], x \[Element] region, Filling-> Axis, Axes -> {True, False} ]


## Using NArgMax without any constraints

This looks rather unspectacular and finding its mode does not give problems:

NArgMax[ PDF[ dist, x ], x ]


4.97893

## Using NArgMax/NMinimize with a region or constraints gives error

Using NArgMax with the given region will cause an error:

NArgMax[ PDF[ dist, x], x \[Element] region ]


Giving the region as a constraint is (contrary to documentation) will give the same error message:

NArgMax[ { PDF[ dist, x ], x \[Element] Interval[ {0, 10} ] }, x ]


What is going on here?

• I should add that I am using Version 10.4 on Windows 10 Pro (64).
– gwr
Commented Apr 4, 2016 at 15:32
• (comment recast as an answer) Commented Apr 4, 2016 at 16:00
• It seems the question has been edited a bit and no longer matches the answer... Commented Apr 4, 2016 at 16:08
• @ilian you were too quick and I have learned from your comment. :) It still does match your answer but the question is now rather precisely driving at the problem of 1D regions.
– gwr
Commented Apr 4, 2016 at 16:11
• This question seems to be a duplicate of 73465.
– gwr
Commented Apr 4, 2016 at 16:28

I think this is just a matter of using the correct syntax. Since Interval[{0, 10}] is a one-dimensional region, its elements have the form {x} instead of just x:

RandomPoint[Interval[{0, 10}]]

(* {3.23781} *)

Element[%, Interval[{0, 10}]]

(* True *)


The documentation for NArgMax does say

NArgMax[... , x ∈ reg] constrains x to be in the region reg

but here x is of course a vector variable, since reg could have any dimension. This fact is also made clear a little further down in the details,

For x ∈ reg, the different coordinates can be referred to using Indexed[x, i]

Vector variables are also illustrated in the last two Scope examples on the same page.

Therefore, the input for the first example should be

NArgMax[PDF[dist, x], {x} ∈ Interval[{0, 10}]]

(* 4.97893 *)


For the second example -- originally given as NArgMax[ { PDF[ dist, x] && 0 <= x <= 10}, x ] and now edited out by the OP -- that's not how constraints are specified. The given objective function has head And and evaluates to False, which is not a number. Instead, try

NArgMax[{PDF[dist, x], 0 <= x <= 10}, x]

(* 4.97893 *)


or

NArgMax[{PDF[dist, x], {x} ∈ Interval[{0, 10}]}, x]

(* 4.97893 *)

• The problem is indeed 1D regions and not a simple RTFM error. For regions and NArgMax the documentation gives: NArgMax[{f,cons},x∈reg] is effectively equivalent to NArgMax[{f,cons∧x∈reg},x]. Maybe I missed a good example somewhere for 1D?
– gwr
Commented Apr 4, 2016 at 16:19