The "function value is not a number" type error seems fairly common. Forgive me, but I can't seem to fix my issue using some of the other posts on this site.

I'm working on a problem where I need to minimize the expectation of a bivariate random variable from the following distribution:

\[ScriptCapitalD] = MixtureDistribution[
   {4, 5}, 
   {MultinormalDistribution[{0, 0}, {{2, 1/2}, {1/2, 3}}],
    MultinormalDistribution[{2, 2}, {{10, 8}, {8, 11}}]}

I want to compute

$$ \text{argmin}_{a \in \mathbb{R}^2} \mathbb{E} \lbrack ||X-a||^p \rbrack $$

where $X$ has the distribution above, and say $p \geq 2$ is an integer. I tried doing this for $p=2$ with

obj = (Norm[{x, y} - {a1, a2}])^2
 NExpectation[obj, {x, y} \[Distributed] \[ScriptCapitalD], 
  WorkingPrecision -> 10], {a1, a2}, WorkingPrecision -> 10

which returns the error

NArgMin: The function value NExpectation[obj,{x,y}\[Distributed]\[ScriptCapitalD],WorkingPrecision->10] is not a number at {a1,a2} = {0.9186212149,0.7166887534}.

I don't understand this error message, as I'm relatively new to Mathematica, and I do not know how to solve this issue.

Thanks in advance for any and all help/suggestions.

  • $\begingroup$ This is just an evaluation order / symbolic check problem - you need to NumericQ pattern test everything out so that it does all calculations numerically. Try this - it's very slow but it should be doing what you want: expc[a1_?NumericQ, a2_?NumericQ] := NExpectation[EuclideanDistance[{x, y}, {a1, a2}]^2, {x, y} \[Distributed] \[ScriptCapitalD]] then run NArgMin[expc[a1, a2], {a1, a2}] - I eventually got an answer of {1.11111, 1.11111} after 5 minutes. $\endgroup$
    – flinty
    Jun 29 at 18:31
  • $\begingroup$ Great, thanks a bunch. Any ideas to make this run a bit quicker? $\endgroup$
    – Alex
    Jun 29 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.