Home > Net >  Probability theory in sympy.stats
Probability theory in sympy.stats

Time:01-16

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!

CodePudding user response:

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

CodePudding user response:

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
  • Related