1
$\begingroup$

If I evaluate the following, Mathematica does not give any error and returns a 0.

SeedRandom[123];
pdf=SmoothKernelDistribution[RandomReal[{0,1},100],MaxExtraBandwidths->0];
g[x_]:=undefined[x];
NIntegrate[PDF[pdf,z] g[z],{z,0,1},Method->{Automatic,"SymbolicProcessing"->0},AccuracyGoal->15]

If instead I remove either the method option to the accuracy goal option, Mathematica returns, as it should, an error and repeats the input expression. Does anybody know if this is a bug or an expected behavior? It was quite difficult to discover this silent bug.

$\endgroup$
  • $\begingroup$ Please note the tag wiki for the bugs tag: "This tag is reserved for questions where the problem has been vetted by this community and the observed behavior is confirmed to be a bug. Please do not use this tag for new questions. Please use the standard bugs header instead of version tags in conjunction with this tag." $\endgroup$ – Michael E2 Jun 30 '17 at 1:32
5
$\begingroup$

The NIntegrate::inumr error "is generated when none of the values of the integrand for the sampling points in a region...is a number". Your integrand evaluates to 0. for (at least) one point in the initial integration region {0., 1.}. Since you set "SymbolicProcessing"->0, NIntegrate seems to assume your function has only a measure-zero set of singular points and bases its estimate ignoring the nonnumeric results. The integral and and error estimate are both 0., which would fail the precision goal if AccuracyGoal is set to Infinity but passes if AccuracyGoal is set to any finite positive number. If you allow "SymbolicProcessing", then NIntegrate[] would divide the interval at the boundaries of the Piecewise function pdf. This creates a region in which "none of the values of the integrand for the sampling points in a region...is a number", and you get the error.

NIntegrate[PDF[pdf, z] g[z], {z, 0, 1}, 
 Method -> {Automatic, "SymbolicProcessing" -> 0}, AccuracyGoal -> 15,
  IntegrationMonitor :> (Print[Map[{#1@"Boundaries", #1@"Integral", #1@"Error"} &, #1]] &), 
 EvaluationMonitor :> Print["z: ", z]]
(*
  z: 0.00795732

  {{{{0,1}},0.,0.}}

  0.
*)

Only one point is used apparently to estimate the integral and the error. Yikes.

$\endgroup$
  • $\begingroup$ thanks! quite bad luck to have these three conditions at the same time (a function that gives 0, no symbolic processing and finite accuracy goal) $\endgroup$ – Valerio Jun 30 '17 at 13:26
  • $\begingroup$ @Valerio You're welcome. I think you should file a bug report with WRI. I came short of calling it a bug, but I think the behavior is undesirable and can probably be avoided. NIntegrate[] actually evaluates your function at 11 points, only one of which is a valid numeric result (the one reported by EvaluationMonitor). You'd think that would at least deserve a warning message. $\endgroup$ – Michael E2 Jun 30 '17 at 14:57
  • $\begingroup$ I just reported it. $\endgroup$ – Valerio Jun 30 '17 at 16:24
1
$\begingroup$
SeedRandom[123];

It would be more informative to call the distribution something like dist rather than pdf

dist = SmoothKernelDistribution[RandomReal[{0, 1}, 100], 
   MaxExtraBandwidths -> 0];

Verifying that the total probability is one

Integrate[PDF[dist, x], {x, -Infinity, Infinity}]

(*  1.  *)

The Mean is

mu = Mean[dist]

(*  0.49436  *)

Alternatively,

mu == Expectation[x, x \[Distributed] dist]

(*  True  *)

Similarly,

var = Variance[dist]

(*  0.0678187  *)

var == Expectation[(x - mu)^2, x \[Distributed] dist]

(*  True  *)

You cannot Integrate or NIntegrate an undefined function such as g or undefined

Expectation[g[x], x \[Distributed] dist]

enter image description here

Integrate[g[z] PDF[dist, z], {z, 0, 1}]

enter image description here

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.