I'm interested in mathematics and statistics, but when I look at mathematical formulas, there are too many symbols to tell which one is which, or I forget it immediately, so I decided to write what I learned in the program. Did.
This time, we will create a class with the following functions for the binomial distribution with a total number of trials n times and a success probability p.
--Obtain the probability mass of the number of successes x --Get the lower cumulative probability of x --Get the upper cumulative probability of x --Draw a graph
Performance is not considered as it is intended to remember the contents of the expression.
When Bernoulli trials (binomial trials) with a success probability of p are performed n times in total,
f(x,n,p) = {}_n \mathrm{ C }_xp^x(1-p)^{ n-x } \quad (0 \leqq n;\ 0 \leqq x \leqq n;\ 0 \leqq p\leqq 1)\\
P(n,x,p) = \sum_{i=0}^xf(i,n,p)
P(n,x,p) = \sum_{i=x}^nf(i,n,p)
import math
import numpy as np
import matplotlib.pyplot as plt
class ToolBox:
def combination(total, chosen):
return math.factorial(total) / math.factorial(chosen)/ math.factorial(total-chosen)
class BinomialDistribution:
"""
Generate a binomial distribution object with the total number of trials n and the probability of success p.
Attributes
----------
total : int
Total number of trials n.
p_of_sccess : float
Probability of success in one trial p.
p_of_fail : float
Failure probability in one trial 1-p.
"""
def __init__(self, total, p_of_success):
"""
Parameters
----------
total : int
Total number of trials n.
p_of_success : float
Probability of success in one trial p.
"""
assert total > 0, "Condition error: 0 <= total"
assert p_of_success >= 0 and p_of_success <= 1, "Condition error: 0 <= p_of_success <= 1"
self.total = total
self.p_of_sccess = p_of_success
self.p_of_fail = 1 - p_of_success
def get_probability_mass(self, success):
"""
Find the probability mass that succeeds x times
Parameters
----------
success : int
Number of successes x.
Returns
-------
probability_mass : float
Probability mass of x.
"""
assert success >= 0 and success <= self.total, "Condition error: 0 <= sccuess <= total"
fail = self.total - success
combination = ToolBox.combination(self.total, success)
probability_mass = combination * (self.p_of_sccess ** success) * (self.p_of_fail) ** fail
return probability_mass
def get_lower_culmitive_distribution(self, success):
"""
Lower cumulative probability of x(Sum of probability masses from 0 to x times)Seeking.
Parameters
----------
success : int
Number of successes x.
Returns
-------
result : float
Lower cumulative probability of x.
"""
result = 0
for i in range (0, success + 1):
result += binomial_distribution.get_probability_mass(i)
return result
def get_upper_culmitive_distribution(self, success):
"""
Upper cumulative probability of x(x ~ sum of probability masses of all trials,)Seeking.
Parameters
----------
success : int
Number of successes x.
Returns
-------
result : float
Upper cumulative probability of x.
"""
result = 0
for i in range (success, self.total + 1):
result += binomial_distribution.get_probability_mass(i)
return result
def draw_graph(self):
"""
Draw a graph and save it in png format.
"""
x = np.arange(0, self.total + 1, 1)
y = []
for i in range(0, self.total + 1):
y.append(self.get_probability_mass(i))
plt.plot(x, y)
plt.savefig('graph.png')
#10 trials, 0 success rate.Create a binomial distribution object of 5.
binomial_distribution = BinomialDistribution(10, 0.5)
#Get the probability mass for 2 successes.
print(binomial_distribution.get_probability_mass(2))
#Get the lower cumulative probability when the success probability is 2 times.
print(binomial_distribution.get_lower_culmitive_distribution(2))
#Get the upper cumulative probability when the success probability is 2 times.
print(binomial_distribution.get_upper_culmitive_distribution(2))
#Draw a graph of this binomial distribution.
binomial_distribution.draw_graph()
0.0439453125 #Probability mass when the number of successes is 2
0.0546875 #Lower cumulative probability with 2 success probabilities
0.9892578125 #Upper cumulative probability when the success probability is 2 times
I used this site to match the answers https://keisan.casio.jp/exec/system/1161228843
The graph is saved in the same directory.
--Corrected the part that was called "Probability Density Function (PDF)" to "Probability Mass Function (PMF)". It seems that the latter is used when the random variable x (success
in this case) is a discrete value. However, some sites and books use "probability density" for discrete binomial distributions, and I feel that they are not so strictly distinguished.
--Added graph drawing function using Matplotlib
--Fixed to separate the number of successes x (successs
) from the constructor and specify it when calling the method.
--Fixed a typo in the formula
How to write MathJax https://easy-copy-mathjax.nakaken88.com/
A site that calculates the binomial distribution https://keisan.casio.jp/exec/system/1161228843
Probability density function and probability mass function https://data-science.gr.jp/theory/tbs_pdf_and_pmf.html
Recommended Posts