python-scipyHow do I use the Python Scipy binom function?
The scipy.stats.binom
function is a part of the SciPy library and is used to calculate the probability mass function (PMF) or cumulative density function (CDF) of a binomial distribution. A binomial distribution is a statistical distribution used to model the probability of a given number of successes in a set number of independent trials.
Example code
from scipy.stats import binom
# Calculate the probability of getting exactly 3 heads in 10 coin flips
binom.pmf(3, 10, 0.5)
Output example
0.11718750000000006
The code above calculates the probability mass function (PMF) of a binomial distribution with 10 trials and a probability of success of 0.5. The probability of getting exactly 3 heads in 10 coin flips is 0.11718750000000006.
The scipy.stats.binom
function can also be used to calculate the cumulative density function (CDF) of a binomial distribution.
Example code
from scipy.stats import binom
# Calculate the probability of getting 3 or fewer heads in 10 coin flips
binom.cdf(3, 10, 0.5)
Output example
0.998046875
The code above calculates the cumulative density function (CDF) of a binomial distribution with 10 trials and a probability of success of 0.5. The probability of getting 3 or fewer heads in 10 coin flips is 0.998046875.
Code explanation
from scipy.stats import binom
: this imports thebinom
function from the SciPy library.binom.pmf(3, 10, 0.5)
: this calculates the probability mass function (PMF) of a binomial distribution with 10 trials and a probability of success of 0.5, and returns the probability of getting exactly 3 successes.binom.cdf(3, 10, 0.5)
: this calculates the cumulative density function (CDF) of a binomial distribution with 10 trials and a probability of success of 0.5, and returns the probability of getting 3 or fewer successes.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I create a zero matrix using Python and Numpy?
- How do I use the NumPy transpose function in Python?
- How do I create a numpy array of zeros using Python?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use Python and SciPy to create a tutorial PDF?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use the scipy ttest_ind function in Python?
- How do I use the trapz function in Python SciPy?
- How do I create an array of zeros with the same shape as an existing array using Python and NumPy?
See more codes...