Try transcribing the probability mass function of the binomial distribution in Python

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.

The formula to reproduce in Python this time

When Bernoulli trials (binomial trials) with a success probability of p are performed n times in total,

Function to find the probability mass that the number of successes is x

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)\\

Function to find the lower cumulative probability of the number of successes x

P(n,x,p) = \sum_{i=0}^xf(i,n,p)

Function to find the upper cumulative probability of the number of successes x

P(n,x,p) = \sum_{i=x}^nf(i,n,p)

I wrote it in Python


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')

Run


#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()

result

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. スクリーンショット 2020-04-07 2.05.18.png

April 7, 2020 Amendments

--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

Referenced site

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

Try transcribing the probability mass function of the binomial distribution in Python
Check the asymptotic nature of the probability distribution in Python
Defeat the probability density function of the normal distribution
Get the caller of a function in Python
Match the distribution of each group in Python
Try to get the function list of Python> os package
Have the equation graph of the linear function drawn in Python
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
A function that measures the processing time of a method in python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
OR the List in Python (zip function)
[Python3] Rewrite the code object of the function
The result of installing python in Anaconda
[Python] Try pydash of the Python version of lodash
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
Try hitting the YouTube API in Python
[Python] Note: A self-made function that finds the area of the normal distribution
Output the number of CPU cores in Python
Draw a graph of a quadratic function in Python
Try the python version of emacs-org parser orgparse
[Python] Sort the list of pathlib.Path in natural sort
[Cloudian # 7] Try deleting the bucket in Python (boto3)
View the result of geometry processing in Python
Statistical test grade 2 probability distribution learned in Python ②
Try using the BitFlyer Ligntning API in Python
Make a copy of the list in Python
Find the divisor of the value entered in python
Try the free version of Progate [Python I]
Fix the argument of the function used in map
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
I implemented the inverse gamma function in python
Try using the collections module (ChainMap) of python3
Statistical test grade 2 probability distribution learned in Python ①
Try implementing the Monte Carlo method in Python
Try using the DropBox Core API in Python
Set an upper limit on the number of recursive function iterations in Python
Find the cumulative distribution function by sorting (Python version)
Experience the good calculation efficiency of vectorization in Python
Logistic distribution in Python
How to get the number of digits in Python
[python] Get the list of classes defined in the module
Carefully understand the exponential distribution and draw in Python
the zen of Python
Plot and understand the multivariate normal distribution in Python
The story of FileNotFound in Python open () mode ='w'
Note: The meaning of specifying only * (asterisk) as an argument in the Python function definition.
Learn the design pattern "Chain of Responsibility" in Python
Implement the solution of Riccati algebraic equations in Python
Carefully understand the Poisson distribution and draw in Python
Get the size (number of elements) of UnionFind in Python
Not being aware of the contents of the data in python
Try gRPC in Python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Let's use the open data of "Mamebus" in Python