3
$\begingroup$

The problem below fails to converge to default accuracy using default method.

With[{a = 1 - 1/100000}, 
 NExpectation[Log[1 - 2 a x^2 + a^2 x^4]^2, 
  x \[Distributed] NormalDistribution[], WorkingPrecision -> 100]]

The warning I get is that

following: singularity, value of the integration is 0, highly \
oscillatory integrand, or WorkingPrecision too small

I think I can rule out "working precision" and value of integrand is 0....what should I do if it's highly oscillatory?

More generally I'm trying to plot this expectation as a function of $a$ and end up with fractal-like structure in the plot. Trying to figure out if it's really there, or whether it's artifact of default numerical integration method.

obj[a_] := NExpectation[Log[1 - 2 a x^2 + a^2 x^4]^2, 
   x \[Distributed] NormalDistribution[]];
Plot[obj[a], {a, 0.75, 1.2}]

enter image description here

Edit after letting it run overnight, original plot seems to be due to numerical issues

Clear[x];
term[a_] := 
  TransformedDistribution[Log[1 - 2 a x^2 + a^2 x^4], 
   x \[Distributed] NormalDistribution[]];
mean[a_] := 
  NExpectation[q, q \[Distributed] term[a], 
   Method -> {"NIntegrate", {MinRecursion -> 9, 
      MaxRecursion -> 30}}];
mean2[a_] := 
  NExpectation[q^2, q \[Distributed] term[a], 
   Method -> {"NIntegrate", {MinRecursion -> 9, MaxRecursion -> 30}}];

enter image description here

$\endgroup$
1
  • $\begingroup$ If you increase the accuracy of NExpectation it should compute a smoother graph and need to evaluate obj at fewer points $\endgroup$
    – mikado
    Commented Aug 21, 2022 at 8:09

1 Answer 1

4
$\begingroup$

By increasing the number of allowed bisections you can make it work. You can even reduce the WorkingPrecision to the default value:

With[{a = 1 - 1/100000}, 
 NExpectation[Log[1 - 2 a x^2 + a^2 x^4]^2, 
  x \[Distributed] NormalDistribution[], 
  Method -> {"NIntegrate", {MinRecursion -> 12, MaxRecursion -> 30}}]]

(* 4.59843 *)
$\endgroup$
3
  • $\begingroup$ thanks for the tip...indeed the jagginess seem to be numeric artifacts $\endgroup$ Commented Aug 21, 2022 at 9:02
  • $\begingroup$ @DanielHuber, Reduction of workingPrecision to default value seems to less. Difference to With[{a = 1 - 1/100000}, NExpectation[Log[1 - 2 a x^2 + a^2 x^4]^2, x \[Distributed] NormalDistribution[], WorkingPrecision -> 30, AccuracyGoal -> 5, Method -> {"NIntegrate", {MinRecursion -> 12, MaxRecursion -> 30}}]] can not realy be neglected. Yields 4.59996306650049691893344677743 $\endgroup$
    – Akku14
    Commented Aug 22, 2022 at 6:17
  • $\begingroup$ O course, I only pointed at the fact that it evaluates with default precision. Question is if this is good enough for you or not. In any case higher working precision means higher precision of the result. $\endgroup$ Commented Aug 22, 2022 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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