Skip to main content

Introduction to Python Binomial Distribution

Python Binomial Distribution

The binomial distribution is a discrete probability distribution that models the number of successes in a fixed number of independent Bernoulli trials.

A Bernoulli trial is an experiment with two possible outcomes: success or failure, usually denoted as 1 or 0.

The probability mass function (PMF) of the binomial distribution is given by the following formula:

P(X = k) = C(n, k) * p^k * (1 - p)^(n - k)

Where:

  • P(X = k) is the probability of getting exactly k successes in n trials.
  • C(n, k) is the binomial coefficient, also known as "n choose k," which represents the number of ways to choose k successes from n trials. It is calculated as C(n, k) = n! / (k! * (n - k)!), where ! denotes the factorial function.
  • p is the probability of success in a single trial.
  • (1 - p) is the probability of failure in a single trial.
  • k is the number of successes.
  • n is the total number of trials.

In Python, you can use the scipy.stats.binom module from the SciPy library to work with the binomial distribution.

  • The binom.pmf() function can be used to calculate the probability mass function for a specific value of k.

As an example:

from scipy.stats import binom

# Define the parameters
n = 10 # Number of trials
p = 0.5 # Probability of success

# Calculate the probability of getting exactly k successes
k = 3
probability = binom.pmf(k, n, p)
print("Probability of getting exactly", k, "successes:", probability)

In this example:

  • We use the binom.pmf(k, n, p) function to calculate the probability of getting exactly 3 successes in 10 trials, where the probability of success in each trial is 0.5. The result is stored in the variable probability and printed.