Sympy - stats.P() in Python

Sympy - stats.P() in Python

sympy.stats.P() is a function in SymPy's statistics module that computes the probability of a given event with respect to a probability space. The module allows symbolic computation with random variables, and the P() function can handle various events related to these random variables.

Here's a basic example using the P() function:

from sympy.stats import P, Die from sympy import Eq # Define a fair six-sided die D = Die('D', 6) # Probability that the die shows 3 p1 = P(Eq(D, 3)) # Probability that the die shows a number greater than 4 p2 = P(D > 4) print(f"P(D=3) = {p1}") print(f"P(D>4) = {p2}") 

This would output:

P(D=3) = 1/6 P(D>4) = 1/3 

In this example, Die is a function that represents a fair dice with a given number of sides. Other random variable types include Normal for normally distributed random variables, Binomial for binomially distributed random variables, and so on.

Another example using a continuous random variable:

from sympy.stats import P, Normal from sympy import symbols mu, sigma = symbols('mu sigma', real=True, positive=True) Z = Normal('Z', mu, sigma) # Probability that Z is less than mu p = P(Z < mu) print(f"P(Z < mu) = {p}") 

This will print:

P(Z < mu) = 1/2 

As seen from the above examples, the P() function is versatile and can be used with both discrete and continuous random variables, enabling a wide range of symbolic probability computations.


More Tags

google-maps-markers android-fileprovider odata react-native-text combinatorics goland ngrx ngx-datatable date avplayerviewcontroller

More Programming Guides

Other Guides

More Programming Examples