Mohammed Itani
Mohammed Itani

Reputation: 61

Probability theory in sympy.stats

This code should output 2/6 but I just get 0

from sympy.stats import Die, P, Normal

n = 6

X = Die('X', n) # Six sided Die

P(X == 1) + P(X == 5)

Even when I just try to calculate the probability of the die landing on a specific number like P(X==1), it outputs 0!

Upvotes: 4

Views: 190

Answers (2)

Marijn
Marijn

Reputation: 1915

You can use Eq(x,y) from sympy itself:

from sympy.stats import Die, P, Normal
from sympy import Eq

n = 6
X = Die('X', n) # Six sided Die

print(P(Eq(X, 1)) + P(Eq(X, 5))) # prints 1/3

Upvotes: 6

Kraigolas
Kraigolas

Reputation: 5560

Die doesn't implement equals in the way you are expecting it to. If you just write X == 1 the result is False. However, if you write something like X > 3 you do not get True or False, you get an expression (meaning P can hope to return something from it).

When you write

P(X == 1)

That is identical to writing

P(False)

which is meaningless for a die. If you want the probabilities of each face, you can use density:

from sympy.stats import Die, P, Normal, density
n = 6
X = Die('X', n) # Six sided Die
density(X)[1] + density(X)[5] # 1/3

Upvotes: 6

Related Questions