ContinuousRV bug?

45 views
Skip to first unread message

F. B.

unread,
May 13, 2014, 8:14:16 AM5/13/14
to sy...@googlegroups.com
I tried this:

In [1]: from sympy.stats import ContinuousRV, P

In [2]: var('x')
Out[2]: x

In [3]: X = ContinuousRV(x, exp(-x), (0, oo))

In [4]: P(X < Rational(1,2))

TypeError: Input must be Sets or iterables of Sets

I get the right answer when avoiding the stats module:

In [6]: var('k')
Out[6]: k

In [7]: cdf = integrate(exp(-x), (x, 0, k))

In [8]: solve(cdf - Rational(1, 2), k)
Out[8]: [log(2)]



Abhishek K Das

unread,
May 13, 2014, 2:02:22 PM5/13/14
to sy...@googlegroups.com
It requires an Interval as the third input parameter , what you are passing is basically a tuple of two numbers . If you write it as Interval(0,oo) instead of (0.oo) . 
It works fine ! 
Message has been deleted

F. B.

unread,
May 14, 2014, 4:17:38 AM5/14/14
to sy...@googlegroups.com
Sorry for the logic error in my previous post. In any case, what about this:

In [15]: X = ContinuousRV(x, exp(-x), Interval(0, oo))

In [16]: P(X < 1/2)
Out[16]:
   
-1/2    
-     + 1

In [17]: P(X < log(2))
AttributeError: 'bool' object has no attribute 'is_Piecewise'

This one still looks like a bug.

F. B.

unread,
May 14, 2014, 4:24:06 AM5/14/14
to sy...@googlegroups.com
Sorry again, I wasn't using the latest version from master, in any case:

In [1]: from sympy.stats import *

In [2]: from sympy import *

In [3]: ;var x
Out[3]: x

In [4]: X = ContinuousRV(x, exp(-x), Interval(0, oo))

In [5]: P(X < log(2).n())
Out[5]: 0.500000000000000

In [6]: P(X < log(2))
Out[6]: oo


I would expect 1/2 as Out[6]

Aaron Meurer

unread,
May 14, 2014, 2:21:16 PM5/14/14
to sy...@googlegroups.com
That looks like an issue in the integration module. There should be
some way to get the integral that it computes, so that we can debug
from there.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/5024a59c-5ef1-480e-a1ce-e30b4c25d1f7%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Matthew Rocklin

unread,
May 14, 2014, 5:28:54 PM5/14/14
to sy...@googlegroups.com
Generally when debugging sympy.stats I recommend inspecting the integrals that it produces using the evaluate=False keyword.  Sympy.stats almost always shoves off most of the hard work onto another SymPy module like integrals.

In [1]: from sympy.stats import *

In [2]: X = ContinuousRV(x, exp(-x), Interval(0, oo))

In [3]: P(X < log(2), evaluate=False)
Out[3]: 
0        
⌠        
⎮   -z   
⎮  ℯ     
⎮  ─── dz
⎮   2    
⌡        
-∞       



F. B.

unread,
May 14, 2014, 6:18:19 PM5/14/14
to sy...@googlegroups.com


On Wednesday, May 14, 2014 11:28:54 PM UTC+2, Matthew wrote:
Generally when debugging sympy.stats I recommend inspecting the integrals that it produces using the evaluate=False keyword.  Sympy.stats almost always shoves off most of the hard work onto another SymPy module like integrals.

In [1]: from sympy.stats import *

In [2]: X = ContinuousRV(x, exp(-x), Interval(0, oo))

In [3]: P(X < log(2), evaluate=False)
Out[3]: 
0        
⌠        
⎮   -z   
⎮  ℯ     
⎮  ─── dz
⎮   2    
⌡        
-∞       

This function should be integrated from -log(2) to 0, not from -oo to 0.

I followed the debugger
 
> /home/.../sympy/sympy/stats/crv.py(392)compute_density()
   
391         fy = sum(fx(g) * abs(g.diff(y)) for g in gs)
--> 392         return Lambda(y, fy)
   
393


Line 391 of crv.py, it looks like the density function gets shifted by -log(2) without properly shifting the definition interval.

Previously, crv.where( ) failed to compute the proper interval with the log(2) condition, raising a NonImplementedError. Maybe it's just a bug in the interval transformation?
Reply all
Reply to author
Forward
0 new messages