Wednesday, July 3, 2024
HomeLanguagesPythonsympy.stats.Binomial() function in Python

sympy.stats.Binomial() function in Python

With the help of sympy.stats.Binomial() method, we can create a Finite Random Variable representing a binomial distribution. 

A binomial distribution is the probability of a SUCCESS or FAILURE outcome in an experiment or survey that is repeated multiple times.

Syntax: sympy.stats.Binomial(name, n, p, succ=1, fail=0)

Parameters:
 name: distribution name
 n: Positive Integer, represents number of trials
 p: Rational Number between 0 and 1, represents probability of success
 succ: Represents event of success, by default is 1
 fail: Represents event of failure, by default is 0

Example #1 :

Python3




# Import sympy, Binomial, density
from sympy.stats import Binomial, density
  
# Using sympy.stats.Binomial() method
X = Binomial('X', 4, 1 / 3)
binDist = density(X).dict
  
print(binDist)


Output :

{0: 16/81, 1: 32/81, 2: 8/27, 3: 8/81, 4: 1/81}



Example #2 :

Python3




# Import sympy, Binomial, density
from sympy.stats import Binomial, density
  
# Using sympy.stats.Binomial() method
X = Binomial('X', 4, 1 / 3, 1 / 2)
binDist = density(X).dict
  
print(binDist)


Output :

{0: 16/81, 1/2: 32/81, 2: 1/81, 3/2: 8/81, 1: 8/27}



Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments