I was horrified to find recently that Matlab returns $0$ for the sample variance of a scalar input:

>> var(randn(1),0)   %the '0' here tells var to give sample variance
ans =
>> var(randn(1),1)   %the '1' here tells var to give population variance
ans =

Somehow, the sample variance is not dividing by $0 = n-1$ in this case. R returns a NaN for a scalar:

> var(rnorm(1,1))
[1] NA

What do you think is a sensible way to define the population sample variance for a scalar? What consequences might there be for returning a zero instead of a NaN?

edit: from the help for Matlab's var:

VAR normalizes Y by N-1 if N>1, where N is the sample size.  This is
an unbiased estimator of the variance of the population from which X is
drawn, as long as X consists of independent, identically distributed
samples. For N=1, Y is normalized by N. 

Y = VAR(X,1) normalizes by N and produces the second moment of the
sample about its mean.  VAR(X,0) is the same as VAR(X).

a cryptic comment in the m code for `var states:

if w == 0 && n > 1
    % The unbiased estimator: divide by (n-1).  Can't do this
    % when n == 0 or 1.
    denom = n - 1;
    % The biased estimator: divide by n.
    denom = n; % n==0 => return NaNs, n==1 => return zeros

i.e. they explicitly choose not to return a NaN even when the user requests a sample variance on a scalar. My question is why they should choose to do this, not how.

edit: I see that I had erroneously asked about how one should define the population variance of a scalar (see strike through line above). This probably caused a lot of confusion.

  • $\begingroup$ Did you mean to ask about the population variance definition or about the sample variance definition? There's no issue with the former. $\endgroup$
    – whuber
    Oct 29, 2010 at 18:27
  • $\begingroup$ @whuber I think what Matlab is doing is the following. When it encounters a scalar it reports the population variance by default. When it encounters a vector it reports the sample variance by default unless you ask it to report population variance. In either context they use same function: var(). $\endgroup$
    – user28
    Oct 29, 2010 at 18:39
  • $\begingroup$ @Srikant That may be, but it does not address my request for clarification. I suspect there may be a typographical error in the last line. The var() function appears to be returning the correct values of population variance but not the correct values for sample variance. $\endgroup$
    – whuber
    Oct 29, 2010 at 18:47
  • $\begingroup$ @whuber Actually, var(randn(1),0) weights the variance by 0 and hence the output is 0. The second element is used to do a weighted average if it is different from 1. See: Mathworks help for var $\endgroup$
    – user28
    Oct 29, 2010 at 18:52
  • 2
    $\begingroup$ @shabbychef: The page Srikant has referenced clearly documents the behavior you have noted. In particular, it does not maintain that var() returns the 1/(n-1) version of the variance when n=1. So the issue is not that Matlab is wrong, per se. (From a software design point of view this implementation of var() is terrible because it tries to do too much in a single function and consequently risks misunderstanding and erroneous results by unvigilant users.) $\endgroup$
    – whuber
    Oct 29, 2010 at 19:17

3 Answers 3


Scalars can't 'have' a population variance although they can be single samples from population that has a (population) variance. If you want to estimate that then you need at least: more than one data point in the sample, another sample from the same distribution, or some prior information about the population variance by way of a model.

btw R has returned missing (NA) not NaN

  • $\begingroup$ aha, I have not worked with R enough to spot the NA vs NaN distinction. Nice that it has both... $\endgroup$
    – shabbychef
    Oct 29, 2010 at 18:22
  • $\begingroup$ Of course a single number has a variance! It's the mean square deviation from its mean, namely zero. Perhaps you meant to say a dataset with one number cannot have a sample variance? $\endgroup$
    – whuber
    Oct 29, 2010 at 18:23
  • $\begingroup$ You'll notice that I said it can't have a population variance, not that it cannot have a variance. Clearly it has a sample variance, which is indeed trivially zero. $\endgroup$ Oct 30, 2010 at 9:07
  • 1
    $\begingroup$ I have come to realize that our conversation derives from two different understandings of "population" and "sample" variance. The terms are confused on the Internet, so I think we're both blameless and that we may be in agreement. I was attempting to use the convention adopted by the OP, whose terminology indicates the "sample" variance divides by n-1 while the "population" variance divides by n. Incidentally, your comment seems (correctly) to allow for three variances: that of a random variable, the unbiased estimator thereof, and that of a set of independent realizations of it. $\endgroup$
    – whuber
    Oct 30, 2010 at 20:55
  • $\begingroup$ @whuber that sounds like an awesome discussion to have. I will try to think of a question for it :) $\endgroup$
    – naught101
    Apr 30, 2012 at 1:52

I am sure people in this forum will have better answers, here is what I think:

I think R's answer is logical. The random variable has a population variance, but it turns out that with 1 sample you don't have enough degrees of freedom to estimate sample variance i-e- you are trying to extract information that is NOT there.

Regarding Matlab's answer, I don't know how to justify 0, except that it is from the numerator.

Consequences can be bizarre. But I can think of anything else related to the estimation.

  • 3
    $\begingroup$ Actually, a single observation of a random variable often provides information about that variable's variance. It depends on the possibilities you allow. For instance, a single observation from a Uniform, Poisson, or Exponential distribution lets you estimate its single parameter, which gives you an estimate of the variance. $\endgroup$
    – whuber
    Oct 29, 2010 at 18:34
  • $\begingroup$ @whuber thanks for the pointers. Actually, I was thinking more on the lines of sample variance. What I meant to say was, estimating sample variance may not be appropriate with one sample. It stands corrected now. $\endgroup$
    – suncoolsu
    Oct 30, 2010 at 22:30
  • $\begingroup$ @whuber But this is not the case here; this function is documented to return variance/(n-1) and is returning 0 for 0/0 symbol. $\endgroup$
    – user88
    Oct 31, 2010 at 19:51
  • $\begingroup$ @mbq You missed the disclaimer in the documentation: "For N=1, V is normalized by N." That's an elliptical way of saying "For N=1, a value of 0 is returned." The point I was making with my comment actually is about something else: it addresses @suncoolsu's assertion that "you are trying to extract information that is NOT there" in the case N=1. On the contrary, a single observation from a random variable indeed does provide information about its variance in many practical applications. (However, I am not saying that the variance is best estimated by some kind of variance formula!) $\endgroup$
    – whuber
    Oct 31, 2010 at 20:23
  • $\begingroup$ @whuber Fair enough; yet this does not justify this behavior. $\endgroup$
    – user88
    Oct 31, 2010 at 20:42

I think Matlab is using the following logic for a scalar (analogous to how we define population variance) to avoid having to deal with NA and NAN.

$Var(x) = \frac{(x - \bar{x})^2}{1} = 0$

The above follows as for a scalar: $\bar{x} = x$.

Their definition is probably a programming convention that may perhaps make some aspect of coding easier.

  • $\begingroup$ You have restated the problem without offering a resolution. The concern is that this convention can lead to wrong answers when the variance estimate is used later. It certainly delays the stage at which a failure is recognized. (Think about what happens if you try to use this "variance" in a t-test, for instance.) $\endgroup$
    – whuber
    Oct 29, 2010 at 18:50
  • $\begingroup$ @whuber Not exactly. The OP is thinking that var returns the sample variance for a scalar whereas it returns the 'population variance'. Whether it is a problem or not depends on what type of error handling routines they have internally. However, I agree that their convention is susceptible to errors. $\endgroup$
    – user28
    Oct 29, 2010 at 18:57
  • $\begingroup$ @Srikant In order to determine who is more confused, you or I, I have learned that we both are! Wikipedia terms the formula with 1/(n-1) the "sample variance" (at en.wikipedia.org/wiki/Variance ) whereas Mathworld clearly uses 1/n at mathworld.wolfram.com/SampleVariance.html . However, the OP uses "sample variance" in the first sense when remarking that "the sample variance is not dividing by 0=n−1". In this sense there is no such thing as "sample variance" for a dataset of one value (a "scalar"), so your characterization of what the OP is "thinking" looks incorrect. $\endgroup$
    – whuber
    Oct 29, 2010 at 19:10
  • $\begingroup$ +1 As I know MATLAB, I think your hypothesis is (sadly) true. $\endgroup$
    – user88
    Oct 31, 2010 at 19:53

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.