Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 770
Mohammed Itani Avatar asked Oct 22 '25 19:10

Mohammed Itani


1 Answers

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
like image 83
Kraigolas Avatar answered Oct 25 '25 09:10

Kraigolas