Hướng dẫn fitting binomial distribution python
The binomial distribution is the basis for popular binomial test in statistics. The binomial distribution probability distribution that summarize about probability of getting success in given number of experiments. Show
Nội dung chính
Nội dung chính
Binomial distribution is used to find probability of binomial random variable with given number of repeated trials (n). The probability distribution of binomial random variable is called binomial distribution. In this article, we will discuss about how to calculate binomial distribution in python. If binomial random variable X follows a binomial distribution with parameters number of trials (n) and probability of correct guess (P) and results in x successes then binomial probability is given by : P(X = x) = nCx * px * (1-p)n-x Where, n = number of trials in the binomial experiment x = number of successes in binomial experiment p = probability of success on given trial nCx = number of combinations to n trials ,taken x at a time Scipy for Binomial DistributionWe will be using scipy library to calculate binomial distribution in python. If you don’t have scipy library installed then use below command on windows command prompt for scipy library installation pip install scipy How to Calculate Probabilities using Binomial Distribution?scipy library provide binom function to calculate binomial probabilities. binom function takes inputs as k, n and p and given as for example, given k = 15, n = 25, p = 0.6, binomial probability can be calculated as below using python code from scipy.stats import binom #calculate binomial probability result = binom.pmf(k=15, n=25, p=0.6) #Print the result print("Binomial Probability: ",result) //output Binomial Probability: 0.1611579 In the above code, first we import binom function. As in our above examples, we have below data n = number of trials = 25 k = number of successes = 15 p = probability of success in given trial is 0.6 using binom.pmf() function, it calculate binomial probability which is 0.1611579 Lets understand calculation of binomial distribution in python using some real world examples as given below Example #1 Find Binomial ProbabilitySuppose that a short quiz consists of 6 multiple choice questions. Each question has four possible answers of which any one in correct. A student guesses on every question. Find the probability that a student will answer a. all questions correctly SolutionLet X be the number of questions guessed correctly out of 6 questions. Let p be the probability of correct guess. The random variable X follows a Binomial distribution with parameter n=8 and p=0.25. a. The probability that student will answers all questions correctly is n = number of questions = 6 k = number of successes = 6 p = probability of success in given question = 1 out of 4 = 1/4 = 0.25 Based on above data, using binom function, probability mass function calculated as from scipy.stats import binom #calculate binomial probability mass function result = binom.pmf(k=6, n=6, p=0.25) #Print the result print("Binomial Probability: ",result) //output Binomial Probability: 0.0002 The probability that student will answer all questions is 0.0002 Example #2 Calculate Binomial DistributionA candidate for public office has claimed that 60% of voters will vote for her. If 5 registered voters were sampled, What is the probability that exactly 3 would say they favor this candidate? SolutionHere in above example, we have n = number of register voters = 5 p = probability of successes = 60% = 0.6 k = probability of success that exactly 3 would favor this candidate is from scipy.stats import binom #calculate binomial probability mass function result = binom.pmf(k=3, n=6, p=0.6) #Print the result print("Binomial Probability: ",result) //output Binomial Probability: 0.34559 The probability of 3 would say they favor this candidate is 0.34559 ConclusionI hope you find above article on how to calculate binomial distribution in python code useful and educational. The binomial distribution is one of the most commonly used distributions in statistics. It describes the probability of obtaining k successes in n binomial experiments. If a random variable X follows a binomial distribution, then the probability that X = k successes can be found by the following formula: P(X=k) = nCk * pk * (1-p)n-k where:
This tutorial explains how to use the binomial distribution in Python. How to Generate a Binomial DistributionYou can generate an array of values that follow a binomial distribution by using the random.binomial function from the numpy library: from numpy import random #generate an array of 10 values that follow a binomial distribution random.binomial(n=10, p=.25, size=10) array([5, 2, 1, 3, 3, 3, 2, 2, 1, 4]) Each number in the resulting array represents the number of “successes” experienced during 10 trials where the probability of success in a given trial was .25. How to Calculate Probabilities Using a Binomial DistributionYou can also answer questions about binomial probabilities by using the binom function from the scipy library. Question 1: Nathan makes 60% of his free-throw attempts. If he shoots 12 free throws, what is the probability that he makes exactly 10? from scipy.stats import binom #calculate binomial probability binom.pmf(k=10, n=12, p=0.6) 0.0639 The probability that Nathan makes exactly 10 free throws is 0.0639. Question 2: Marty flips a fair coin 5 times. What is the probability that the coin lands on heads 2 times or fewer? from scipy.stats import binom #calculate binomial probability binom.cdf(k=2, n=5, p=0.5) 0.5 The probability that the coin lands on heads 2 times or fewer is 0.5. Question 3: It is known that 70% of individuals support a certain law. If 10 individuals are randomly selected, what is the probability that between 4 and 6 of them support the law? from scipy.stats import binom #calculate binomial probability binom.cdf(k=6, n=10, p=0.7) - binom.cdf(k=3, n=10, p=0.7) 0.3398 The probability that between 4 and 6 of the randomly selected individuals support the law is 0.3398. How to Visualize a Binomial DistributionYou can visualize a binomial distribution in Python by using the seaborn and matplotlib libraries: from numpy import random import matplotlib.pyplot as plt import seaborn as sns x = random.binomial(n=10, p=0.5, size=1000) sns.distplot(x, hist=True, kde=False) plt.show() The x-axis describes the number of successes during 10 trials and the y-axis displays the number of times each number of successes occurred during 1,000 experiments. |