# What is the correct formula for coefficient of variation for a binomial distribution?

# Create a binomial random variable with 100 observations and a probability of success of 0.7
binom.data<-rbinom(100, 1, 0.7)

# Calculate the coefficiente of variation (CV(%)) of the binomial random variable
cv.binom.1<-sd(binom.data)/mean(binom.data)*100
cv.binom.1
#[1] 64.23212


Ok!! But, I find in some literature that the CV of a binomial random variable is: The mean of the binomial distribution with parameters n and p is np and the standard deviation is sqrt(np(1-p)). Therefore, the CV is sqrt((1-p)/np):

cv.binom.2 <-sqrt((1-0.7)/(100*0.7))*100
cv.binom.2
#[1] 6.546537


I have an expressive difference between the two results. What is the correct way to calculate the CV of a binomial random variable?

You simulated 100 realizations from a binomial distributions with $$n = 1, p=0.7$$, but calculated the CV for a binomial with $$n=100, p =0.7$$. This is a quite common trap to run into: the n in rbinom(n, size, p) stands for the number of simulated values, and size is the parameter $$n$$ of the binomial. Try with binom.data<-rbinom(10000, 100, 0.7) instead, for example.
• Why N = 10000 in the code?
• Just to make the simulation more precise. You can also use 100, or 999, or whatever you like. Try several times with the same number n of simulated values, and you will see how the much the results vary as a consequence of how many simulations you run