Solve a Probability Problem with Python and SciPy
2 min readMar 17, 2024
Today while solving the question based on Probability and Stats. I just thought how could I solve the same problem with the help of some programming language like Python (Which is a very good programming language for Data Analytics and Data Analysis). Hence during R&D I just got multiple solution but I found the SciPy provides to many build in functions for solving such kinds of problem.
from scipy.stats import binom
#(a) The probability that exactly five customers will return items purchased.
print(binom.pmf(5, 20, 0.1))
#(b) The probability that a maximum of five customers will return items purchased.
p_at_least_5 = binom.pmf(0, 20, 0.1) + binom.pmf(1, 20, 0.1) +binom.pmf(2, 20, 0.1)+binom.pmf(3, 20, 0.1)+binom.pmf(4, 20, 0.1)+binom.pmf(5, 20, 0.1)
print(p_at_least_5)
#(c) The probability that more than five customers will return items purchased.
p_more_then_5_return_product = 1 - p_at_least_5
print(p_more_then_5_return_product)
#(d) The average number of customers likely to return items.
e_x = 20*0.1
print(e_x)
#(e) The variance and the standard deviation of the number of returns.
#var_X = npq
var_X = 20*0.1*0.9
print(var_X)
StandardDev = np.sqrt(var_X)
print(StandardDev)