# 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
#[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
#[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?


1 Answer 1


Your theoretical formula is correct! So, why do the simulated results so grossly disagree with theoretical results?

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.
Edit: you can change the number of simulated values, here I chose 10000 to get lower variation, that is, a higher chance to have a simulated result close to the theoretical value.

  • 1
    $\begingroup$ Why N = 10000 in the code? $\endgroup$
    – Dave
    Jul 10, 2023 at 20:38
  • 2
    $\begingroup$ 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 $\endgroup$
    – Ute
    Jul 10, 2023 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.