1
$\begingroup$

Problem:. Numerical optimization on learned PredictorFunction do not seem to behave the same for constraint and region specifications, and the latter fail to evaluate.

Consider the following example:

(* define a noisy function *)
f[x_] := Sin[x] + RandomReal[NormalDistribution[0, 0.1]]

data = (# -> f[#]) & /@ RandomReal[{-Pi, Pi}, 10];
model = Predict[data, Method -> "GaussianProcess"];

If we attempt to find the maximum of the learned function by specifying constraints, we get some warnings about incompatible variable types, but ultimately get an answer:

NMaximize[{model[x], -Pi <= x <= Pi}, x]

PredictorFunction error

No problem! We know how to fix this by making a wrapper that forces the input to be numeric, and all of our warnings disappear:

m[x_?NumericQ] := model[x]
NMaximize[{m[x], -Pi <= x <= Pi}, x]
(* {1.04459, {x -> 1.25916}} *)

But trying to solve the same problem with a region specification fails. Being careful to use the proper syntax for intervals, one attempts:

NMaximize[model[x], {x} \[Element] Interval[{-Pi, Pi}]]

error with interval

Only a warning and no results output. Furthermore, attempting to use our numericized function, m fails to evaluate:

NMaximize[m[x], {x} \[Element] Interval[{-Pi, Pi}]]
(* NMaximize[m[x], {x} \[Element] Interval[{-\[Pi], \[Pi]}]] *)

(From a practical standpoint, one can obtain the answer using the constraint specification approach demonstrated above, but I would like to understand better the reason why the domain specification fails and how to fix it.)

$\endgroup$
2
  • 1
    $\begingroup$ Not sure why NMaximize doesn't evaluate with a region specified by Interval here, but if you use NMaximize[ m[x], {x} ∈ ImplicitRegion[-π <= x <= π, {x}]] like bbgodfrey did in your linked question it does evaluate $\endgroup$ Commented Aug 2 at 16:00
  • $\begingroup$ @ydd — Indeed, that works. interestingly, use of ImplicitRegion to specify takes 5x longer (according to AbsoluteTiming) than specifying the constraint in the first argument. $\endgroup$ Commented Aug 3 at 22:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.