Probability: Binomial Distribution

 Binomial Distribution

The binomial distribution is a discrete probability distribution that describes the number of successes in a fixed number of independent trials, where each trial has a constant probability of success.

The binomial distribution has two parameters: n, the number of trials, and p, the probability of success in each trial. The probability of getting exactly k successes in n trials is given by the formula:

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

where (n choose k) is the binomial coefficient, which is equal to n! / (k! * (n-k)!) and represents the number of ways to choose k successes out of n trials.

The mean or expected value of a binomial distribution is given by E(X) = n * p, and the variance is given by Var(X) = n * p * (1-p).

The binomial distribution is widely used in statistics and probability theory, particularly in fields such as genetics, engineering, and finance, where it is used to model the probability of success or failure in a series of trials.

Code

dbinom(3, size = 13, prob = 1 / 6)
probabilities <- dbinom(x = c(0:10), size = 10, prob = 1 / 6)
data.frame(x, probs)
plot(0:10, probabilities, type = "l")


In R, you can use the dbinom, pbinom, qbinom, and rbinom functions to work with the binomial distribution.

Here's a brief description of each function:

  • dbinom: returns the probability density function (PDF) of the binomial distribution for a given set of parameters.
  • pbinom: returns the cumulative distribution function (CDF) of the binomial distribution for a given set of parameters.
  • qbinom: returns the quantile function of the binomial distribution for a given set of parameters.
  • rbinom: generates random values from the binomial distribution for a given set of parameters.

The parameters you need to specify for these functions are:

  • size: the number of trials.
  • prob: the probability of success in each trial.
  • x: the number of successes.

Here are some examples of how to use these functions in R:

  1. Finding the probability of getting exactly 3 heads in 5 coin tosses, where the probability of getting heads is 0.5:

dbinom(x = 3, size = 5, prob = 0.5)
  1. Finding the probability of getting 3 or fewer heads in 5 coin tosses, where the probability of getting heads is 0.5:

pbinom(q = 3, size = 5, prob = 0.5)
  1. Finding the number of coin tosses required to get 4 heads with a probability of at least 0.8, where the probability of getting heads is 0.5:

qbinom(p = 0.8, size = 10, prob = 0.5)
  1. Generating 10 random values from the binomial distribution with 10 trials and a probability of success of 0.3:

rbinom(n = 10, size = 10, prob = 0.3)

To apply the binomial distribution to a dataset in R, you can use the dbinom() function to calculate the probability mass function (PMF) of the binomial distribution for each value in the dataset. Here is an example: Let's say you have a dataset of 20 coin flips, with 15 of them resulting in heads. You want to calculate the probability of getting exactly 15 heads out of 20 coin flips, assuming a fair coin.


# Define the number of trials (n) and probability of success (p) 
n <- 20 p <- 0.5 
# Calculate the PMF for each value in the dataset using dbinom() 
dataset <- c(rep(1, 15), rep(0, 5)) 
# 15 heads, 5 tails 
pmf <- dbinom(dataset, size = n, prob = p) 
# Print the PMF pmf

This will give you the probability of getting each value in the dataset under the binomial distribution. You can plot the PMF using the plot() function to visualize the distribution:


# Plot the PMF 
plot(dataset, pmf, type = "h", ylim = c(0, max(pmf)), xlab = "Number of Heads", ylab = "Probability")


Comments

Popular posts from this blog

Probability: Normal Distribution

Hypothesis testing