# Why are there two ways to calculate the standard deviation of a binary variable?

I've always been told that the standard deviation of a binary variable is sqrt(npq). However, there also appears to be a different way to calculate it:

. sysuse auto, clear
(1978 Automobile Data)

. sum foreign

Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
foreign |         74    .2972973    .4601885          0          1

. di sqrt(74*.2972973*(1-.2972973))
3.9318519


The terminology is confusing. How can the standard deviation be both 3.9 and .46?

Same thing for the mean. The mean formula I've seen in textbooks is np, but isn't p the mean? Like for this variable, the mean is .2972973, which is also p. np is more like the "number of successes" rather than the mean.

• 1) where did you tell the software that the variable is binary in contrast to e.g. numeric? 2) I think you are mixing binary variables and the the sum of binary variables (= binomial). Commented May 24, 2020 at 18:17
• I don't know? I'm not really sure what's happening. I know that reporting .4601885 is a fairly common way to report standard deviations for binaries in reports. Can you clarify (2)? How can the SD be greater than 1? It's not possible for the value to be greater than 1. Ultimately, I'm trying to determine what sd to use when I convert a regression coefficient to standardized values. Should I be using 3 or .46 for s_x? Commented May 25, 2020 at 2:53
• It's starting to sound like I'm calculating the SD for the bernoulli random variable, not binomial. Is that correct? Commented May 25, 2020 at 5:19

• For a Bernoulli variable $$Ber(p)$$, $$p$$ is the mean and sd $$\sqrt{p(1-p)}$$
• If you have i.i.d. Bernoulli variable $$X_1, \cdots, X_n \sim Ber(p)$$, you can sum them up: the random variable $$X_1 + \cdots + X_n$$ has mean $$np$$ and sd $$\sqrt{np(1-p)}$$ (which I suppose is what you heard) - this is the binomial distribution $$B(n, p)$$.
• But your data your showed very likely refers to the empirical statistics of your $$n$$ samples $$X_1, \cdots, X_n$$ of Bernoulli variable instead instead. So the "mean" in your formula refers to the sample mean $$\frac{X_1 + \cdots + X_n}{n}$$, which is an estimator of $$p$$; and the standard deviation refers to the sample s.d., which is an estimator of s.d. of Bernoulli variable, which is $$\sqrt{p(1-p)}$$. This is also why others said you may be confusing binary variable and binomial distribution in the comments.

Now let's do the comparison you intended to do:

• the sample mean .2972973 estimates $$p$$ like you said.
• the sample s.d. .4601885 estimates $$\sqrt{p(1-p)}$$. If we pretend $$p = .2972973$$, then $$\sqrt{.2972973 (1 - .2972973)} \approx 0.457$$ that is pretty close.
• I'm not sure what you are saying tbh. (1) why do you have two different SD formulas? (2) how do I tell if my random variable is Bernoulli? (3) How should I report this binary variable? Commented May 25, 2020 at 2:55
• actually isn't that second SD the SE? And for the first, why isn't it sqrt(npq)? Why'd you remove the n? Commented May 25, 2020 at 4:33
• "Wrong" is maybe not the right word in item three. Commented May 25, 2020 at 5:06
• I updated the answer since I noticed I had something wrong yesterday.
– Pig
Commented May 25, 2020 at 19:57
• For posterity, Stata computes the sample variance and not the mean variance, so the formula used there is sqrt(.2972973*(1-.2972973)*74/73) = .4570685 which is exactly what's shown in the question Commented Feb 19, 2022 at 4:42

You could use Wolfram website to study the 'binomial distribution' and the Bernoulli random variable.

The previous answer says that if you are adding up numerous random variables, each of which is Bernoulli distributed, then the resulting mean follows a Binomial Distribution. This binomial distribution has 2, not 1 parameters. One parameter is p but the other one is n. Therefore, the n appears in the formula for its mean. See also: https://mathworld.wolfram.com/BernoulliDistribution.html compared that image and algebra with Binomial at: https://mathworld.wolfram.com/BinomialDistribution.html

Probably one should ideally know that the distribution of a mean (or sum) is not the same as the distribution of the individual components. This truth underlies the problem you had in grasping the comment.