Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional expectation problem #13324

Closed
erelsgl opened this issue Sep 19, 2017 · 2 comments
Closed

Conditional expectation problem #13324

erelsgl opened this issue Sep 19, 2017 · 2 comments
Labels

Comments

@erelsgl
Copy link

erelsgl commented Sep 19, 2017

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.

@jksuom
Copy link
Member

jksuom commented Sep 19, 2017

It looks like the conditional distribution function is not normalized properly on this line. The integral in the denominator should probably be explicitly computed by .doit().

@normalhuman
Copy link
Contributor

normalhuman commented Sep 24, 2017

There are multiple issues, as the output of E(v, v > 1/2, evaluate=False) shows:

Integral(Piecewise((v/Integral(1, (v, 0.5, inf)), (0 <= v) & (v <= 1)), (nan, True)), (v, 0.5, inf))

  1. We see Integral(1, (v, 0.5, inf)) which is infinity. So the result of division of v by this integral is 0. Instead of 1, the integral should have a piecewise function there.

  2. The Piecewise object has (nan, True) which should be (0, True). The density outside of the interval [0, 1] is 0, not NaN. Since the outer integral is (correctly) taken from 1/2 to infinity, it includes those NaN values, and so evaluates to NaN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants