Again, this might not be helpful.
A similar bug is found here:
Integrate[Boole[R > 1/(1/S + 1/T) > P],
{T, -1, 1}, {R, -1, 1}, {P, -1, 1}, {S, -1, 1}]
It returns an expression numerically equal to 2.88629 - 6.28319 I
. Since Boole[]
has the value either 0
or 1
, the integral cannot be complex. And substituting NIntegrate[]
for Integrate[]
yields the real part 2.88629
. The complex value returned by Integrate[]
is the same as the value used in constructing the PDF of the transformed distribution for NExpectation[]
/Expectation[]
in the OP's problem. Interestingly it's not the same expression, and FullSimplify[]
cannot transform their difference into zero. Nonetheless, they agree to 100 digits. (Probably, if I looked up the right PolyLog
identity, I could do the simplification by hand. But I don't see the point.)
Tools for workarounds
If this is important, here are some ways to figure out the integral being used and how to fix it. The fixes are ad hoc and require some preliminary work (such as verifying the correct PDF).
The following return the integrals used for NExpectation[]
and Expectation[]
respectively, wrapped in Hold[]
so they do no evaluate. If they look good, you could use ReleaseHold[]
to evaluate them. Otherwise you would want to fix them before evaluating them.
integralNExp = Trace[
NExpectation[R \[Conditioned] R > 1/(1/S + 1/T) > P,
{T \[Distributed] UniformDistribution[{-1, 1}],
R \[Distributed] UniformDistribution[{-1, 1}],
P \[Distributed] UniformDistribution[{-1, 1}],
S \[Distributed] UniformDistribution[{-1, 1}]}],
i_NIntegrate :> Return[Hold[i], Trace],
TraceInternal -> True]
integralExp = Trace[
Expectation[R \[Conditioned] R > 1/(1/S + 1/T) > P,
{T \[Distributed] UniformDistribution[{-1, 1}],
R \[Distributed] UniformDistribution[{-1, 1}],
P \[Distributed] UniformDistribution[{-1, 1}],
S \[Distributed] UniformDistribution[{-1, 1}]}],
i_Integrate :> Return[Hold[i], Trace],
TraceInternal -> True]
In the OP's case there is a term -8 I Pi
that should be removed, which I do by replacing the complex number -8 I
by 0
. That this is valid was verified by the Integrate[]
/NIntegrate[]
computations discussed at the beginning of this reply.
Trace[
NExpectation[R \[Conditioned] R > 1/(1/S + 1/T) > P,
{T \[Distributed] UniformDistribution[{-1, 1}],
R \[Distributed] UniformDistribution[{-1, 1}],
P \[Distributed] UniformDistribution[{-1, 1}],
S \[Distributed] UniformDistribution[{-1, 1}]}],
i_NIntegrate :>
Return[Hold[i] /. -8 I -> 0 // Echo // ReleaseHold, Trace],
TraceInternal -> True]
(* 0.5000000106328868` *)
This yields a convenient (?), 2.6MB (!!!) expression. Even though Echo[]
reveals we zeroed out all the complex terms, Integrate[]
gives us another complex result, just like with the first example above.
Trace[
Expectation[R \[Conditioned] R > 1/(1/S + 1/T) > P,
{T \[Distributed] UniformDistribution[{-1, 1}],
R \[Distributed] UniformDistribution[{-1, 1}],
P \[Distributed] UniformDistribution[{-1, 1}],
S \[Distributed] UniformDistribution[{-1, 1}]}],
i_Integrate :>
Return[Hold[i] /. -8 I -> 0 // Echo // ReleaseHold, Trace],
TraceInternal -> True]
N[%, 16]
(* 0.500000000000000 + 2.176903850077749 I *)
The real part agrees with the NExpectation[]
/NIntegrate[]
result, so maybe it's correct.