Repository: sympy/sympy

Issue:
Conditional expectation problem
I tried to calculate the conditional expectation of a random variable:

    from sympy.stats import *
    v = Uniform("v",0,1)
    E(v)

this returns correctly 1/2, but then:

    E(v, v>1/2)

returns NaN. I also tried:

    E(v, where(v > 1/2))

it returned 1/2, which is incorrect (it should be 3/4).

I asked in Stack Overflow, and was told that:

    E(v, v>1/2, evaluate=False) 

shows that SymPy understands the input and sets up an integral for it; but it fails to evaluate it if .doit() is invoked on the integral.

Discussion:

Patch:
diff --git a/sympy/stats/crv.py b/sympy/stats/crv.py
index c9b6edf53d..e8ef2bbfb3 100644
--- a/sympy/stats/crv.py
+++ b/sympy/stats/crv.py
@@ -336,12 +336,17 @@ def where(self, condition):
         return SingleContinuousDomain(rv.symbol, interval)
 
     def conditional_space(self, condition, normalize=True, **kwargs):
-
         condition = condition.xreplace(dict((rv, rv.symbol) for rv in self.values))
-
         domain = ConditionalContinuousDomain(self.domain, condition)
         if normalize:
-            pdf = self.pdf / domain.integrate(self.pdf, **kwargs)
+            # create a clone of the variable to
+            # make sure that variables in nested integrals are different
+            # from the variables outside the integral
+            # this makes sure that they are evaluated separately
+            # and in the correct order
+            replacement  = {rv: Dummy(str(rv)) for rv in self.symbols}
+            norm = domain.integrate(self.pdf, **kwargs)
+            pdf = self.pdf / norm.xreplace(replacement)
             density = Lambda(domain.symbols, pdf)
 
         return ContinuousPSpace(domain, density)


Test patch:
diff --git a/sympy/stats/tests/test_continuous_rv.py b/sympy/stats/tests/test_continuous_rv.py
index 039e365224..62114e980d 100644
--- a/sympy/stats/tests/test_continuous_rv.py
+++ b/sympy/stats/tests/test_continuous_rv.py
@@ -15,7 +15,7 @@
 from sympy import (Symbol, Abs, exp, S, N, pi, simplify, Interval, erf, erfc,
                    Eq, log, lowergamma, Sum, symbols, sqrt, And, gamma, beta,
                    Piecewise, Integral, sin, cos, besseli, factorial, binomial,
-                   floor, expand_func)
+                   floor, expand_func, Rational)
 
 
 from sympy.stats.crv_types import NormalDistribution
@@ -661,7 +661,6 @@ def test_issue_10003():
     assert P(X < -1) == S.Zero
     assert P(G < -1) == S.Zero
 
-
 def test_precomputed_cdf():
     x = symbols("x", real=True, finite=True)
     mu = symbols("mu", real=True, finite=True)
@@ -678,3 +677,8 @@ def test_precomputed_cdf():
         compdiff = cdf(X)(x) - simplify(X.pspace.density.compute_cdf()(x))
         compdiff = simplify(compdiff.rewrite(erfc))
         assert compdiff == 0
+
+def test_issue_13324():
+    X = Uniform('X', 0, 1)
+    assert E(X, X > Rational(1,2)) == Rational(3,4)
+    assert E(X, X > 0) == Rational(1,2)