Sympy:集合的概率度量

Sympy: Probability measure of a set

在尝试计算这样的有界集合的概率时:

from sympy.stats import Normal, P
from sympy import  Interval
n = Normal("n", 0, 1)
P(n in Interval(-1, 1))

我收到以下错误:

TypeError: did not evaluate to a bool: (-1 <= n) & (n <= 1) 怎么处理?

要使用 Interval(),您必须调用它的 contains() 方法:

P(Interval(-1,1).contains(n))

如文档所述,将 inInterval() (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.

或者,您可以直接在概率函数内制定条件:

P((n >-1) & (n<1))