The binomial formula gives the probability of k successes in n independent trials each with k probability of success:

\[ P(k | n,p) = {{n}\choose{k}} \cdot p^k (1-p)^{n-k} \]

In R there are several functions that you can use work with binomial probabilities.

dbinom

dbinom simply plugs into the binomial formula. If you want to know the probability of exactly 5 successes in 10 trials if each trial has a 70% chance of success you can type:

dbinom(x=5, size=10, prob=0.7)
## [1] 0.1029193

or simply:

dbinom(5, 10, 0.7)
## [1] 0.1029193

Better yet, dbinom can do operations on vectors of numbers. If I wanted to know the probability of any number of successes, 0 through 10, in 10 trials each with a 70% chance of success we could do:

dbinom(0:10, 10, 0.7)
##  [1] 0.0000059049 0.0001377810 0.0014467005 0.0090016920 0.0367569090
##  [6] 0.1029193452 0.2001209490 0.2668279320 0.2334744405 0.1210608210
## [11] 0.0282475249

and we could even plot these probabilities versus the number of successes:

plot(x = 0:10, y=dbinom(0:10, 10, 0.7), type="h", xlab="Successes", ylab="Probability")

  1. In the r chunk below, calculate the probability of exactly 10 successes in 30 independent trials, if there is a 30% chance of success in each trial:

  2. In the r chunk below, calculate the probability of every number of successes, 0 through 30:

  3. Plot these probabilities:

Calculating the probabilities of ranges of outcomes

You can also sum up the probabilities of rangs of successes. Suppose that I want to know the probability of getting between 10 and 15 (inclusive) sucesses in 30 independent trials each with a 30% chance of success. First, I will calculate the probability of each number of successes within my range:

dbinom(10:15, 30, 0.3)
## [1] 0.14156170 0.11030782 0.07485173 0.04441751 0.02311524 0.01056697

Then I can add them up:

sum(dbinom(10:15, 30, 0.3))
## [1] 0.404821

Questions (write code within each R chunk to answer these):

  1. What is the probability of a 55% shooter hitting at least 8 of 12 shots in a basketball game?

  2. What is the probability that a true .300 hitter (in other words, a hitter with a 30% chance of a hit in every at bat) will get 20 or more hits in 50 at bats?

  3. What is the probability that a true .300 hitter will hit .360 or higher in 50 at bats?

qbinom and quantile outcomes

If the median height for a five-year-old is 43 inches, that tells us that 50% of five-year-olds are taller than 43 inches and 50% are shorter. The term median means the same thing as 50th percentile or, equivalently, the 0.50 quantile. We might be interested in other quantiles as well. The 95th percentile (0.95 quantile) in height for five-year-olds is 46 inches. This means that 95% of five-year-olds are shorter than 46" and only 5% are taller. We can talk about any random variable in terms of quantiles and qbniom, calculates quantiles for binomial random variables.

Let’s imagine that we flip a coin 100 times, what is the 90th percentile outcome in the number of heads? To calculate this we can use:

qbinom(p=0.90, size=100, prob=0.50)
## [1] 56

This tells us that 90% of the time, we will flip 56 or fewer heads. We could double-check this outcome using dbinom:

sum(dbinom(0:56, 100, 0.5))
## [1] 0.903326

qbinom can also perform operations on vectors. Let’s make a vector of quantiles that we might be interested in:

quantiles <- c(0.01, 0.05, 0.25, 0.50, 0.75, 0.95, 0.99)

Now, we can use qbinom to tells us even more about the results of 100 coin flips:

qbinom(quantiles, 100, 0.5)
## [1] 38 42 47 50 53 58 62

We see that the 1st, 5th, 25th, 50th, 75th, 95th and 99th percentiles outcomes (in numbers of heads) are: 38, 42, 47, 50, 53, 58, and 62. We could say that 98% of the time (from the 1st percentile to the 99th) we get between 38 and 62 heads.

Questions:

  1. If a true .300 hitter has 500 at bats…
  1. What is her 50th percentile outcome in terms of number of hits?

  2. What is her 50th percentile outcome in batting average?

  3. What are her 10th and 90th percentile outcomes in batting average?

  1. If a basketball player has a 55% chance of hitting every shot he takes what are his 5th, 50th and 95th percentile outcomes?