Binomial probability

Author

Arkadiusz Oliwa

Published

July 23, 2023

Binomial Distribution

In binomial probability questions, we're often asked to figure out the probability that we get an exact number of "successes," assuming we perform a specific number of independent trials.

In a binomial experiment, we have the following key characteristics:

  1. Fixed number of trials (n): The experiment consists of a fixed number of independent trials, denoted by “n.”

  2. Independent trials: The outcome of each trial is independent of the outcomes of other trials. In other words, the result of one trial does not influence the results of subsequent trials.

  3. Two outcomes: Each trial can have only two possible outcomes, typically labeled as “success” and “failure.”

  4. Constant probability of success (p): The probability of success in each individual trial remains constant from trial to trial.

The binomial probability formula is used to calculate the probability of getting a specific number of successes (k) in “n” trials with a fixed probability of success (p). The formula is given by:

\[P(x=k)=\binom{n}{k} \cdot p^k \cdot (1 - p)^{n - k} \]

Where:

  • P(X = k) is the probability of getting exactly “k” successes in “n” trials.

  • (n choose k) represents the binomial coefficient and is calculated as n! / (k! * (n - k)!).

  • p is the probability of success in an individual trial.

  • (1 - p) is the probability of failure in an individual trial.

Example

Let’s say there are three marbles in a bag: 2 are green and 1 is red. We’re going to do 5 trials where we pull a marble, note the color, and then replace the marble. What is the probability that we get the red marble exactly 3 times?

To solve this problem, we need to first figure out how many possible combinations we can do this in.

\[\binom{n}{k} = \frac{n!}{k!(n-k)!} \]

  • “n” represents the total number of items in the set.

  • “k” represents the number of items to be chosen from the set.

\[\binom{5}{3} = \frac{5!}{3!(5-3)!} \]

\[\binom{5}{3} = 10 \]

library(combinat)
combn(5,3)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    1    1    1    1    1    2    2    2     3
[2,]    2    2    2    3    3    4    3    3    4     4
[3,]    3    4    5    4    5    5    4    5    5     5

As we can see there are 10 possible combinations

Then to find the probability that we get exactly 3 reds on 5 pulls, we say that f is the probability of pulling a red marble, and therefore that the probability of pulling 3 red marbles in 5 pulls is.

\[P(k\ success\ in\ n\ attempts)=\binom{n}{k} \cdot p^k \cdot (1 - p)^{n - k} \]

\[P(3\ red\ in\ 5\ pulls)=10 \cdot {(\frac{1}{3})}^3 \cdot {(\frac{2}{3})}^2 \]

\[P(3\ red\ in\ 5\ pulls)\approx 16,5 \% \]

dbinom(x = 3, size = 5, prob = 1/3)
[1] 0.1646091

We can also create a probability distribution for binomial random variables. Using our example of pulling a marble 5 times, where we have a 1/3 probability of pulling a red marble and 2/3 probability of pulling a green marble on each pull, we could calculate the following probabilities.

dbinom(x  = 0:5, size = 5, prob = 1/3)
[1] 0.131687243 0.329218107 0.329218107 0.164609053 0.041152263 0.004115226

We could then plot this probability distribution to get a picture of the probability

library(ggplot2) 
library(dplyr)
probability <- dbinom(x  = 0:5, size = 5, prob = 1/3)
success <- 0:5
df <- data.frame(success = success, probability = probability)
df %>% 
  ggplot(mapping = aes(x = success, y = probability, fill = success)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = round(probability, 3)), vjust = -0.5) +
  theme_minimal() +
  theme(legend.position = "none")