# Why getting complex number solution to expectation?

**URL:** <https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388>\
**Category:** Questions\
**Tags:** statistics-and-probability\
**Created:** [August 4, 2024, 1:01pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388 "2024-08-04T13:01:08Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![pastudtmann](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@pastudtmann](https://community.wolfram.com/u/pastudtmann)\
**Post date:** [August 4, 2024, 1:01pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/1 "2024-08-04T13:01:08Z")

</div>

Can someone explain why I am getting a complex number for this computation of an expected value?

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/09884b46-b5f6-4cf6-890a-294eb6e5697c)

---

<div class="post-metadata">

**Author:** ![ivanovdenis710616](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ivanovdenis710616/32/34915_2.png) [@ivanovdenis710616](https://community.wolfram.com/u/ivanovdenis710616)\
**Post date:** [August 5, 2024, 5:49am UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/2 "2024-08-05T05:49:50Z")

</div>

After some modifications:

```
NExpectation[
 R \[Conditioned] (P < 1/(1/S + 1/T) < R), {T \[Distributed] 
   UniformDistribution[{-1, 1}], 
  R \[Distributed] UniformDistribution[{-1, 1}], 
  P \[Distributed] UniformDistribution[{-1, 1}], 
  S \[Distributed] UniformDistribution[{-1, 1}]}]

Out: 0.5

```

---

<div class="post-metadata">

**Author:** ![pastudtmann](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@pastudtmann](https://community.wolfram.com/u/pastudtmann)\
**Post date:** [August 5, 2024, 11:08am UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/3 "2024-08-05T11:08:14Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [August 5, 2024, 11:15am UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/4 "2024-08-05T11:15:48Z")

</div>

What are the modifications? I get the same complex number also in the modified version. I tried with version 12, and I get the complex number but without error messages.

---

<div class="post-metadata">

**Author:** ![pastudtmann](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@pastudtmann](https://community.wolfram.com/u/pastudtmann)\
**Post date:** [August 5, 2024, 2:06pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/5 "2024-08-05T14:06:09Z")

</div>

Same here. I am not sure what the modifications are supposed to be. When I cut and paste the code, I get the same complex number.

---

<div class="post-metadata">

**Author:** ![ivanovdenis710616](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ivanovdenis710616/32/34915_2.png) [@ivanovdenis710616](https://community.wolfram.com/u/ivanovdenis710616)\
**Post date:** [August 5, 2024, 3:48pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/6 "2024-08-05T15:48:46Z")

</div>

Modifications are

```
R \[Conditioned] (P < 1/(1/S + 1/T) < R)

```

instead of

```
R\[Conditioned]R>1/(1/S+1/T)>P

```

I double checked this on version 13.2 and it works

---

<div class="post-metadata">

**Author:** ![mroge02](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mroge02/32/28775_2.png) [@mroge02](https://community.wolfram.com/u/mroge02)\
**Post date:** [August 5, 2024, 3:58pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/7 "2024-08-05T15:58:36Z")

</div>

@Denis’s modification was to put parentheses around the condition. (I think.)

However, I, too, still get the “invalid input” error, which seems more significant than the complex number. Basic GIGO: You can’t expect the output to make sense if the input does not.

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [August 5, 2024, 3:59pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/8 "2024-08-05T15:59:44Z")

</div>

Modifications are

```
R \[Conditioned] (P < 1/(1/S + 1/T) < R)

```

instead of

```
R\[Conditioned]R>1/(1/S+1/T)>P

```

I double checked this on version 13.2 and it works

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [August 5, 2024, 4:01pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/9 "2024-08-05T16:01:16Z")

</div>

I tried your modification in versions 12.0, 14.0, 14.1 and they all give the same complex number. Any idea why `(a<b<c)` and `c>b>a` should give different answers?

---

<div class="post-metadata">

**Author:** ![ivanovdenis710616](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ivanovdenis710616/32/34915_2.png) [@ivanovdenis710616](https://community.wolfram.com/u/ivanovdenis710616)\
**Post date:** [August 5, 2024, 4:05pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/10 "2024-08-05T16:05:19Z")

</div>

Not only parentheses, but more correct order of inequality

---

<div class="post-metadata">

**Author:** ![ivanovdenis710616](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ivanovdenis710616/32/34915_2.png) [@ivanovdenis710616](https://community.wolfram.com/u/ivanovdenis710616)\
**Post date:** [August 5, 2024, 4:06pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/11 "2024-08-05T16:06:17Z")

</div>

What about 13.2?  
Screenshot:

 ![8045333.png](https://global.discourse-cdn.com/wolfram/original/2X/4/49624a430f8624aa055aa81afe176abe04daf61f.png)

IMHO version 13.2 is the most stable, tested and balanced of the most recent.

---

<div class="post-metadata">

**Author:** ![ivanovdenis710616](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ivanovdenis710616/32/34915_2.png) [@ivanovdenis710616](https://community.wolfram.com/u/ivanovdenis710616)\
**Post date:** [August 5, 2024, 4:11pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/12 "2024-08-05T16:11:18Z")

</div>

Screenshot:

 ![8045333.png](https://global.discourse-cdn.com/wolfram/original/2X/4/49624a430f8624aa055aa81afe176abe04daf61f.png)

---

<div class="post-metadata">

**Author:** ![mroge02](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mroge02/32/28775_2.png) [@mroge02](https://community.wolfram.com/u/mroge02)\
**Post date:** [August 5, 2024, 4:34pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/13 "2024-08-05T16:34:27Z")

</div>

I don’t know if the following is any help:

This syntax seems valid (removing `/T` from first argument):

```
NExpectation[
 R \[Conditioned] R > 1/(1 + 1/S) > P,
 {T \[Distributed] UniformDistribution[{-1, 1}],
   R \[Distributed] UniformDistribution[{-1, 1}], 
  P \[Distributed] UniformDistribution[{-1, 1}], 
  S \[Distributed] UniformDistribution[{-1, 1}]}]

```

But the original throws an error. Maybe it’s a bug?

Similarly this is accepted:

```
NExpectation[
 R \[Conditioned] R > T > S > P,
 {T \[Distributed] UniformDistribution[{-1, 1}], 
  R \[Distributed] UniformDistribution[{-1, 1}], 
  P \[Distributed] UniformDistribution[{-1, 1}], 
  S \[Distributed] UniformDistribution[{-1, 1}]}]

```

But this is not:

```
NExpectation[
 R \[Conditioned] R > S > P,
 {T \[Distributed] UniformDistribution[{-1, 1}], 
  R \[Distributed] UniformDistribution[{-1, 1}], 
  P \[Distributed] UniformDistribution[{-1, 1}], 
  S \[Distributed] UniformDistribution[{-1, 1}]}]

```

Perhaps there is a limitation on the number of variables that can be used in the first argument. Maybe the limitation is due to a bug?

I suggest reporting it to Wolfram. They may be able to help. If it is a bug, it needs to be fixed.

---

<div class="post-metadata">

**Author:** ![ivanovdenis710616](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ivanovdenis710616/32/34915_2.png) [@ivanovdenis710616](https://community.wolfram.com/u/ivanovdenis710616)\
**Post date:** [August 5, 2024, 5:25pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/14 "2024-08-05T17:25:58Z")

</div>

All your examples works very well with the right order on Mathematica 13.2:

 ![444.png](https://global.discourse-cdn.com/wolfram/original/2X/1/1af105ca6bc8b542238967e7dba17c227d48dd4c.png)

---

<div class="post-metadata">

**Author:** ![pastudtmann](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@pastudtmann](https://community.wolfram.com/u/pastudtmann)\
**Post date:** [August 5, 2024, 5:36pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/15 "2024-08-05T17:36:21Z")

</div>

I am using 14.0 and am still getting weird results. I tried with Expectation rather than NExpectation. Using Expectation eliminates the ‘invalid input’ but still gives a complex number. When I compute:

```
Expectation[
 R \[Conditioned] (P < 1/(1 + 1/T) && 
    1/(1 + 1/T) < R), {T \[Distributed] UniformDistribution[{-1, 1}], 
  R \[Distributed] UniformDistribution[{-1, 1}], 
  P \[Distributed] UniformDistribution[{-1, 1}], 
  S \[Distributed] UniformDistribution[{-1, 1}]}]

```

I get a real number. But when I compute:

```
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 get a complex number. The only difference is that the first condition has 1/(1+1/T) where the second condition has 1/(1/S+1/T). So something seems amiss.

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [August 5, 2024, 8:03pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/16 "2024-08-05T20:03:57Z")

</div>

My speculation about the appearance of complex numbers is that _Mathematica_ uses the pdf of `1/(1/s+1/t)` which contains logs:

```
dist = TransformedDistribution[1/(1/s + 1/t), {t \[Distributed] UniformDistribution[{-1, 1}], 
    s \[Distributed] UniformDistribution[{-1, 1}]}];
pdf = PDF[dist, st] // FullSimplify

```

 ![pdfofst.png](https://global.discourse-cdn.com/wolfram/original/3X/b/b/bb4878e6a96d9ff97e7ff5cea841042b7edacd5f.png)

Because that random variable can take negative values and there are logs in the pdf, the internal code finds complex numbers even though those complex numbers will eventually “cancel out” outside of that internal code.

So a brute force approach to get the correct result is to do the numerical integeration:

```
numerator = NIntegrate[r (1/2) (1/2) pdf, {r, -1, 1}, {st, -1, r}, {p, -1, st}]
(* 0.0901967 *)
denominator = NIntegrate[(1/2) (1/2) pdf, {r, -1, 1}, {st, -1, r}, {p, -1, st}]
(* 0.180393 *)
conditionalMean = numerator/denominator
(* 0.5 *)

```

If one has a few minutes of patience, then `Integrate` also works:

```
numerator = Integrate[r (1/2) (1/2) pdf, {r, -1, 1}, {st, -1, r}, {p, -1, st}] // FullSimplify
(* 1/64 (3 + Log[16]) *)
denominator = Integrate[(1/2) (1/2) pdf, {r, -1, 1}, {st, -1, r}, {p, -1, st}] // FullSimplify
(* 1/32 (3 + Log[16]) *)
conditionalMean = numerator/denominator
(* 1/2 *)

```

**Note:** If `Log[(-1 + st)/st]` isn’t split into two terms `Log[-1 + st] - Log[st]`, then the imaginary numbers are not an issue. This condition happens when `st < -1/2`.

---

<div class="post-metadata">

**Author:** ![mroge02](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mroge02/32/28775_2.png) [@mroge02](https://community.wolfram.com/u/mroge02)\
**Post date:** [August 5, 2024, 8:05pm UTC](https://community.wolfram.com/t/why-getting-complex-number-solution-to-expectation/24388/17 "2024-08-05T20:05:31Z")

</div>

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.
