While trying to calculate a probability of a bounded set like this:
from sympy.stats import Normal, P
from sympy import Interval
n = Normal("n", 0, 1)
P(n in Interval(-1, 1))
I'm getting the following error:
TypeError: did not evaluate to a bool: (-1 <= n) & (n <= 1)
How to deal with that?
To use Interval()
you have to call its contains()
method:
P(Interval(-1,1).contains(n))
as the documentation states on using in
with Interval()
(here):
As a shortcut it is possible to use the ‘in’ operator, but that will raise an error unless an affirmative true or false is not obtained. [..] The result of ‘in’ is a bool, not a SymPy value.
Alternatively, you can directly formulate the conditions inside the probability function:
P((n >-1) & (n<1))