The binomial distribution
Probability and Statistics
1 Simulating coin flips
In these exercises, you’ll practice using the rbinom()
function, which generates random “flips” that are either 1 (“heads”) or 0 (“tails”).
- With one line of code, simulate 10 coin flips, each with a 30% chance of coming up 1 (“heads”).
- What kind of values do you see?
2 Simulating draws from a binomial
In the last exercise, you simulated 10 separate coin flips, each with a 30% chance of heads. Thus, with rbinom(10, 1, .3)
you ended up with 10 outcomes that were either 0 (“tails”) or 1 (“heads”).
But by changing the second argument of rbinom()
(currently 1
), you can flip multiple coins within each draw. Thus, each outcome will end up being a number between 0 and 10, showing the number of flips that were heads in that trial.
- Use the
rbinom()
function to simulate 100 separate occurrences of flipping 10 coins, where each coin has a 30% chance of coming up heads. - What kind of values do you see?
3 Calculating density of a binomial
If you flip 10 coins each with a 30% probability of coming up heads, what is the probability exactly 2 of them are heads?
- Answer the above question using the
dbinom()
function. This function takes almost the same arguments asrbinom()
. The second and third arguments aresize
andprob
, but now the first argument isx
instead ofn
. Usex
to specify where you want to evaluate the binomial density. - Confirm your answer using the
rbinom()
function by creating a simulation of 10,000 trials. Put this all on one line by wrapping themean()
function around therbinom()
function.
4 Calculating cumulative density of a binomial
If you flip ten coins that each have a 30% probability of heads, what is the probability at least five are heads?
- Answer the above question using the pbinom() function. (Note that you can compute the probability that the number of heads is less than or equal to 4, then take 1 - that probability).
- Confirm your answer with a simulation of 10,000 trials by finding the number of trials that result in 5 or more heads.
5 Varying the number of trials
In the last exercise you tried flipping ten coins with a 30% probability of heads to find the probability at least five are heads. You found that the exact answer was 1 - pbinom(4, 10, .3)
= 0.1502683, then confirmed with 10,000 simulated trials.
Did you need all 10,000 trials to get an accurate answer? Would your answer have been more accurate with more trials?
- Try answering this question with simulations of 100, 1,000, 10,000, 100,000 trials.
- Which is the closest to the exact answer?
6 Calculating the expected value
What is the expected value of a binomial distribution where 25 coins are flipped, each having a 30% chance of heads?
- Calculate this using the exact formula you learned in the lecture: the expected value of the binomial is
size * p
. Print this result to the screen. - Confirm with a simulation of 10,000 draws from the binomial.
7 Calculating the variance
What is the variance of a binomial distribution where 25 coins are flipped, each having a 30% chance of heads?
- Calculate this using the exact formula you learned in the lecture: the variance of the binomial is
size * p * (1 - p)
. Print this result to the screen. - Confirm with a simulation of 10,000 trials.