60
$\begingroup$

I have two programs that both behave nearly identically: they both take in any numbers you give them and can tell you the arithmetic mean and how many numbers were given. However, when you don't give them any numbers, one says the arithmetic mean is 0.0, and the other says it's NaN ("Not a Number"). Which of these answers, if any, is more correct, and why?

Note: Although I use "programs" as a metaphor here, this isn't a programming question; I could've just as easily said "computers", "machines", "wise men", etc. and my question would be the same

$\endgroup$
23
  • 10
    $\begingroup$ The Empty sum is normally considered to be zero. Your second program perhaps computed the empty sum and then divided by the number of terms being summed... that is, $0/0$. $\endgroup$
    – user98602
    Aug 26, 2014 at 4:39
  • 25
    $\begingroup$ NaN is better because how can you find the average without numbers. $\endgroup$
    – Freddy
    Aug 26, 2014 at 4:40
  • 13
    $\begingroup$ From a programming standpoint, NaN is better than 0, because 0 is a possible legitimate average. (Sample: { -1, 1 }, or { 0, 0, 0 }). $\endgroup$
    – friedo
    Aug 26, 2014 at 7:09
  • 2
    $\begingroup$ As a programmer, I'd expect to get (and would supply) null rather than either NaN or 0... $\endgroup$ Aug 26, 2014 at 14:42
  • 7
    $\begingroup$ @Cruncher, in Java, I would expect Float.NaN or Double.NaN over null. After all, my method should be returning a float or double, and null can only be returned for Object types. $\endgroup$
    – Brian S
    Aug 26, 2014 at 16:28

10 Answers 10

93
$\begingroup$

From a statistical point-of-view, the average of no sample points should not exist. The reason is simple. The average is an indication of the centre of mass of the distribution. Clearly, for no observations there can be no way to prefer one location vs. another as their centre of mass since the the empty set is translation invariant.

More mathematically, taking the average is a linear operation, which means if you add a constant $c$ to each observation, then the average $a$ becomes $a+c$. Now if you add $c$ to each observation in the empty set, you get the empty set again, and thus the average will have to satisfy $a+c=a$ for all $c$, clearly nonsense.

$\endgroup$
21
  • 2
    $\begingroup$ Couldn't one say, then, that there is zero mass for all values of $x$ between $-\infty$ and $\infty$? In that case, the sum of all zeroes would be, itself, $0$ $\endgroup$
    – Ky -
    Aug 26, 2014 at 14:53
  • 5
    $\begingroup$ Adding 1 to every number should shift the mean by 1 (i.e., make it 1 instead of 0). But $1 + \mathbb{R} = \mathbb{R}$. $\endgroup$
    – asmeurer
    Aug 26, 2014 at 19:03
  • 1
    $\begingroup$ @Mehrdad yes, the empty set does not change upon a translation. But the mean does translate with a translation, and thus one can't associate a translation consistent value for the mean of the empty set. That is what the second paragraph is saying and I'd appreciate it if you say what is wrong with that. $\endgroup$ Aug 27, 2014 at 10:27
  • 4
    $\begingroup$ @Mehrdad you need to understand the difference between: "translate the set first, then take the mean", and "first take the mean, then translate it". The mean has the property that these operations yield the same result. What the second paragraph does is conclude no assignment of the mean to the empty set still has this property and uses it as a reason against assigning any value to the mean of empty mean. This is standard extrapolation from general properties to extreme cases. $\endgroup$ Aug 27, 2014 at 10:35
  • 1
    $\begingroup$ @Mehrdad The arithmetic mean satisfies this identity: $\text{mean}\ (\text{map}\ (c+{})\ X) = c + \text{mean}\ X$. Substituting $\{\}$ for $X$, we get $\text{mean}\ (\text{map}\ (c+{})\ \{\}) = c + \text{mean}\ \{\}$. Mapping over the empty multiset gives the empty multiset, so $\text{mean}\ \{\} = c + \text{mean}\ \{\}$. That's a contradiction ($c$ is arbitrary), so either the identity doesn't hold or $\text{mean}\ \{\}$ doesn't exist. The least objectionable choice is the latter. $\endgroup$
    – mudri
    Aug 27, 2014 at 14:16
36
$\begingroup$

The Fréchet mean generalizes the concept of mean to arbitrary metric spaces. It is the point which minimizes the sum-of-squared distances between elements of the dataset $X$: $$\text{arg}\min_\bar{x} \sum_{x\in X} d(\bar{x},x)^2$$

In the case that $X=\varnothing$, the summation is the empty sum and hence $0$, thus there is no minimizer and the mean is undefined.

However, in general there are multiple points that minimize this sum (consider the dataset consisting of a pair of antipodal points on the sphere), so we should not speak of the mean, rather we should consider the set of such points as mean. Then, the empty set has as mean the entire space (presumably $\mathbb{R}$ in this case).

To directly answer the question, NaN would be better, since being undefined is certainly not-a-number and, likewise, a set is not-a-number.

$\endgroup$
2
  • 1
    $\begingroup$ +1. Dumb question: why is $\text{arg}\min_\bar{x} 0$ undefined? $\min_\bar{x} 0$ is obviously $0$, then $\text{arg}\min_\bar{x} 0$ should be the set of values $\bar{x}$ for which the sum is $0$. That's any (presumably real) number. $\endgroup$
    – nikie
    Aug 26, 2014 at 13:58
  • 1
    $\begingroup$ @nikie This is just a technicality: the Fréchet mean is defined to be the unique minimizer. When there is no uniqueness, it is undefined. You are correct, however, that $\operatorname{arg min}$ is $\mathbb{R}$ when $X=\emptyset$. $\endgroup$
    – Slade
    Aug 26, 2014 at 17:40
20
$\begingroup$

The correct answer is "Error: Cannot compute the average without any numbers. Please enter at least one number."

$0$ is incorrect, because division by $0$ is undefined, not $0$: $\frac00\neq 0$. If you have $0$ elements, you simply cannot compute their average.

NaN is slightly better, but still kind of wrong. It's a special value of the IEEE floating point standard and represents the result of a calculation that is undefined. It's an implementation detail of how numbers work under the hood, not a proper result to show to the user. You should at least translate it into something like "undefined" or "N/A". Or display a message to explain why there is no result (see above).

$\endgroup$
3
  • 9
    $\begingroup$ How is NaN an improper result to show to a user? "undefined" or "N/A" would just be different versions of NaN. Why invent two types of the same object? $\endgroup$
    – asmeurer
    Aug 26, 2014 at 15:57
  • 8
    $\begingroup$ I would agree in the general case that NaN is not a value you should be showing the user (depending on target audience there may be exceptions). However, I disagree that NaN is the wrong result of the computation. $\endgroup$
    – Brian S
    Aug 26, 2014 at 16:32
  • 4
    $\begingroup$ @asmeurer The average user has no idea what NaN means. It's jargon. $\endgroup$ Aug 26, 2014 at 17:22
14
$\begingroup$

The average of an empty collection of numbers is clearly undefined, as is the centroid of the empty set (or a set of measure zero, for that matter). Therefore the value $0$ given by one of your computers is wrong.

What a clever computer should say in such a case depends on the implementation. I'd expect at least some sort of error message, but certainly not an overflow alert.

$\endgroup$
4
  • $\begingroup$ This is my favourite answer, "as is the centroid of a set of measure zero" (+1) $\endgroup$
    – Tony
    Aug 26, 2014 at 11:24
  • 10
    $\begingroup$ NaN is like an embedded error message. Just overflow is expected to be +Inf. $\endgroup$
    – Vi0
    Aug 26, 2014 at 12:15
  • $\begingroup$ I doubt $10^{gogol}$ would overflow into NaN. NaN as a float is $x 2^{11111111_b}$ =) $\endgroup$
    – Brian S
    Aug 26, 2014 at 16:36
  • $\begingroup$ @thisismuchhealthier As an example, the center of mass of Cantor's set (a supspace of $[0,1]$) does not exist, wrt. the Lebesgue measure? $\endgroup$ Aug 28, 2014 at 9:46
7
$\begingroup$

The average of $n$ numbers is their sum divided by $n$.

If $n=0$ then the sum of $0$ numbers is $0$. But dividing by $0$ will result in a computation error.

Either answer can be taken as correct, depending on your needs.

  • If you want the average of $0$ numbers to be defined, make it $0$ (since it's the only sane choice),
  • and if you want it to be indeterminate (e.g. you want to make a claim like "the average of $a_1,\ldots,a_n$ is the unique $a$ such that $n\cdot a=a_1+\ldots+a_n$", in which case for $n=0$ any $a$ would work) then leave it as an indeterminate.
$\endgroup$
9
  • 7
    $\begingroup$ To be philosophical, I'd argue against the first answer, or at least would refine it by saying that the 'sane choice' for the average depends on context. For example, suppose we have $n$ bicycles, and that $a_n$ is the number of wheels on the $n$th bicycle. Then the average $a$ will be $2$ for any $n > 0$; and for $n=0$, wouldn't the sane choice be to again define $a=2$? $\endgroup$
    – Théophile
    Aug 26, 2014 at 5:16
  • 6
    $\begingroup$ Théophile, well, I argue that $0$ is still a sane choice in that case. The case of no bike is clearly related to a psychotic episode, and anything which has a positive number of wheels clearly shows symptoms of insanity. :-) $\endgroup$
    – Asaf Karagila
    Aug 26, 2014 at 5:54
  • 3
    $\begingroup$ What possible reason is there to consider $0$ a sane choice? Averages are supposed to be translation-invariant, so there's nothing to distinguish $0$ from any other real. Maybe we're averaging wheels, or degrees Fahrenheit. What is the average of no temperatures? Zero? Zero what? $\endgroup$
    – Slade
    Aug 26, 2014 at 9:26
  • $\begingroup$ @you-sir-33433: IF you insist that the average of no numbers is to be defined, then my claim is that $0$ is the sane choice. Note that boldface capital letters "if". I never said that one answer is preferable to another. $\endgroup$
    – Asaf Karagila
    Aug 26, 2014 at 9:43
  • 3
    $\begingroup$ I agree that 0 is a sane choice if it needs to be defined - and in practical scenarios, it might need to be. I would also suggest that if you are taking a mean of something with a known or expected population mean, some other value might be a good default to choose, such as said expected mean. Imputation basically works this way - when NaN doesn't suffice, and you need a point for each day, picking a point that makes sense with the other points is a reasonable choice.' $\endgroup$
    – Joe
    Aug 27, 2014 at 19:38
3
$\begingroup$

Others have already given a number of excellent answers, however I'll give one more idea for why an empty average shouldn't be zero, but should instead be undefined. My reasoning is somewhat in line with Ittay's. The point I'd make is that averages of non-empty sets make sense in affine spaces, which look like a vector space where we've forgotten the privileged basepoint.

For example: think about Newtonian Physics or even Special Relativity. In these cases space(time) has a fundamentally affine underlying model. Nevertheless we can take averages of points, for example when looking for centers of mass. But to say the empty average is zero is ridiculous, as zero was arbitrarily chosen.

$\endgroup$
3
$\begingroup$

In support of the Frechet mean argument, the mean of no numbers is defined as 0/0. This is the solution of the equation x * 0 = 0 (from the definition of division) which is solved by any x. So the mean is any number.

Note that 0/0 is different from a/0 where a != 0, because x * 0 = a has no solutions.

However there is no way to represent "any number" as a double, so if you want a function returning double I would say NaN is the best. Or in C++ you could throw different exceptions to distinguish these cases say NoNumberException and AnyNumberException.

The correct behaviour of a program depends upon the use for which it is intended. I would need to see your User Requirements :-)

$\endgroup$
1
$\begingroup$

The average of $n$ real numbers is defined to be their sum divided by $n$. When there are no numbers, i.e. when $n=0$, we have that the average is $\frac{S}{0}$. It doesn't matter what $S$ equals to (even though by convention the empty sum $S$ is usually taken to be $0$ because of some technical and conceptual reasons) - any real number divided by zero is undefined because it breaks math.

$\endgroup$
0
$\begingroup$

Is this a mathematics question, or a programming question?

If you are asking about the proper behavior for the two programs you have, then my proposal is that the correct output would be throwing an exception (exiting with an error message if they are standalone programs).

Returning zero is unacceptable: the input (empty set) is clearly incorrect for the operation performed (averaging), so the return value can not be a valid one. Returning a NaN is roughly equivalent to the proposed solution of raising an exception/exiting with error message.

$\endgroup$
1
  • $\begingroup$ this is math. I use the programs as a metaphor in the same way "computer", "machine", "wise man", etc. has been used in the past. $\endgroup$
    – Ky -
    Aug 27, 2014 at 14:01
-2
$\begingroup$

You can have an average of whatever is input (assuming we are not looking at PI which I do not know as having an ending). If you press Enter or whatever method is used to cause an input into the program, then the programming language or input design of the program will interpret that information, likely perform some calculation, then give the result of the calculation.

What I mean is, you can just press enter (with no other value entered) and (depending on the programming language or design of the program) the program can interpret your value (if reading integers) as Zero. Some languages may interpret the input as a non value or nul and may output something similar to your 'NaN'.

However, in answer to your first question, the average of one thing is always that thing. Unless you can only have an average until you have two or more of something to create an average. So whatever is input, as long as it was only one input, would result in that input as being the average.

To answer your ending question, I would say the NaN is more correct because just pressing enter (assuming that is how the data is input) and is done so with the intent of at least not giving a specific value. That is the answer for the person inputting the data. The answer may also depend on the programmers intent on what type of input they are looking for --- numerical (0 may be correct) or non-numerical (Nan is correct).

$\endgroup$
2
  • 5
    $\begingroup$ -1 you're just restating my question in a confusing way that makes me think you don't know what you're talking about. If you can boil down your answer to one paragraph and clear up your English, I might vote it up $\endgroup$
    – Ky -
    Aug 27, 2014 at 2:21
  • $\begingroup$ You nearly lost me entirely at the $\pi$ thing. I took the time to read through the rest, however, and your last paragraph, and your last sentence in particular, almost seem reasonable. $\endgroup$
    – String
    Aug 28, 2014 at 8:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .