Sympy - stats.DiscreteUniform() in Python

Sympy - stats.DiscreteUniform() in Python

The DiscreteUniform distribution in the sympy.stats module of the SymPy library represents a discrete uniform distribution. It is the simplest type of discrete distribution and is used to model scenarios where each outcome in a finite set of possible outcomes has an equal probability of occurring.

Here's how you can work with the DiscreteUniform distribution using sympy.stats:

  1. Defining a Discrete Uniform Random Variable:

    You can create a Discrete Uniform-distributed random variable using the DiscreteUniform function.

    from sympy.stats import DiscreteUniform, density from sympy import symbols X = DiscreteUniform("X", [1, 2, 3, 4, 5]) # Here, X can take on values from 1 to 5 with equal probability. 
  2. Obtaining the Probability Mass Function (PMF):

    To get the PMF of the Discrete Uniform distribution, you can use the density function.

    pmf = density(X) 

    And to see the probability of a specific outcome, say 3:

    print(pmf(3)) 
  3. Computing Various Measures:

    You can compute measures like expectation, variance, etc., using their respective functions from sympy.stats.

    from sympy.stats import E, variance mean = E(X) var = variance(X) 

Here's a complete example:

from sympy.stats import DiscreteUniform, density, E, variance from sympy import symbols X = DiscreteUniform("X", [1, 2, 3, 4, 5]) pmf = density(X) for outcome in [1, 2, 3, 4, 5]: print(f"P(X = {outcome}) = {pmf(outcome)}") mean = E(X) var = variance(X) print("Mean:", mean) print("Variance:", var) 

This will showcase the probabilities for each outcome, as well as the mean and variance for the random variable X.


More Tags

asp.net-mvc-partialview microsoft-graph-api email-processing nl2br django-guardian maya coalesce pre-commit avplayerviewcontroller delete-row

More Programming Guides

Other Guides

More Programming Examples