Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the binom.pmf() method in Python?

Rukhshan Haroon

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The binom.pmf function is a part of Python’s SciPy library and is used to model probabilistic experiments with the help of binomial distribution.

To use the binom.pmf function, you must import scipy at the very start of the program:

from scipy.stats import binom

Syntax

The binom.pmf method has the following syntax:

scipy.stats.binom.pmf(r,n, p)

Parameters

The binom.pmf function takes in three parameters:

  • n: the total number of trials or times the experiment will be carried out.
  • r: a list of integers from 0 to n, inclusive.
  • p: the probability that the outcome of a single experiment will be a success. The value of p must be between 0 and 1, inclusive.

Binomial distribution is used to model experiments that have only one of two outcomes, success or failure.

Return value

The binom.pmf method returns a list with the same number of values and sequence as r. The return value is the probability mass function for the values in r.

Example

The code below shows how to use the binom.pmf function in Python.

First, we set the values of n and p to 10 and 0.35, respectively. This is to denote that the experiment will be carried out 10 times, and the probability that the outcome of a single experiment is a success is 0.35.

Next, we call the binom.pmf function and print its return value with the print function. The return value is a list of 10 values, which corresponds to the probability for 0-10 trials of the experiment being a success. For example, the first value corresponds to the probability for none of the 10 experiments being a success, and the last value corresponds to the probability for all 10 experiments being a success.

Since all of the values in the returned list are probabilities, any value in this list and its numerical difference from 1 must sum to 1. We affirm our observation with the for loop at the end of the snippet.

from scipy.stats import binom
import matplotlib.pyplot as plt
# setting the values
# of n and p
n = 10
p = 0.35
#defining list of r values
r= list(range(n + 1))
#calling the binom.pmf function and printing its return value
return_val=binom.pmf(r, n, p)
print('Return Value:')
print(return_val)
#print the sum of 1-return_value[i] and return_value[i]
print("Loop to attest our observation:")
for i in range(0,n+1):
print(return_val[i]+1-return_val[i])

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring