62

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

Ky -
  • 1,300
  • 10
    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$. –  Aug 26 '14 at 04:39
  • 25
    NaN is better because how can you find the average without numbers. – Freddy Aug 26 '14 at 04:40
  • 13
    From a programming standpoint, NaN is better than 0, because 0 is a possible legitimate average. (Sample: { -1, 1 }, or { 0, 0, 0 }). – friedo Aug 26 '14 at 07:09
  • 2
    As a programmer, I'd expect to get (and would supply) null rather than either NaN or 0... – Brian Knoblauch Aug 26 '14 at 14:42
  • 1
    @BrianKnoblauch that depends on the language. Never 0, but Java you give null, javascript NaN is better – Cruncher Aug 26 '14 at 14:59
  • 1
    @BrianKnoblauch @Cruncher You've both got great points! I'm writing this in as many languages as I can, so I am trying to only return an IEEE floating-point number. Imagine writing (or even calling) a Java method that returns a double, but then trying to return null! Certainly an InvalidStateException could be thrown, but this comment has already gotten too programmy for Math.SE :P – Ky - Aug 26 '14 at 15:03
  • 1
    In Java, you'd use a "Double" instead of "double" to be able to return null. :-) – Brian Knoblauch Aug 26 '14 at 15:08
  • 7
    @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. – Brian S Aug 26 '14 at 16:28
  • The best practice is to catch this exception or forbid others from entering empty value. You cant compute average from nothing. – sotondolphin Aug 27 '14 at 04:16
  • 1
    If you define the empty sum $\sum_{n \in \emptyset} a_n$ to be zero, then the empty average $\frac 1 0 \sum_{n \in \emptyset} a_n$ is undefined, but the empty average $\sum_{n \in \emptyset} \frac {a_n} 0$ is zero. :) – Yoni Rozenshein Aug 27 '14 at 13:50
  • @BrianKnoblauch, BrianS, sotondolphin: this isn't StackExchange or some other coding site. Please don't discuss coding here. Although I use "programs" as a metaphor here, this isn't a programming question; I could've easily said "computers", "machines", "wise men", etc. – Ky - Aug 27 '14 at 14:05
  • 1
    Consider that the average of { 17539126 } is 17539126. And the average of { 17539126 , 2 } is 8769564. If the average of { } is considered to be zero, wouldn't you expect that to "pull down" the average of the lists of 1 and 2 elements? – Daniel R Hicks Aug 27 '14 at 17:34
  • One data point: SQL returns NULL, meaning no result, and this is what I would expect from other programs. – Thomas Padron-McCarthy Aug 28 '14 at 07:54
  • @friedo +1 for your comment, if it was an answer I would have definitely suggested it as a possible accepted solution. – CompuChip Aug 28 '14 at 08:06
  • As an engineer and not a mathematician my answer would be that it depends. Is this a program used by a bank to calculate an amount of money, or is it part of a control program in an aircraft? In many (I would say almost all) situations it is much better for the program to print an error message or even crash than to silently give an incorrect or meaningless answer. In other situations it might be better to get any reasonable answer than none. – Thomas Padron-McCarthy Aug 28 '14 at 08:07
  • This question is just unclear. Do you want to know "what pure mathematicians would consider about this issue" or do you want to know "What should programming language P provide as an answer according to it's spec when you try to compute zero divided by zero" {Answer: Read The Spec}, or do you mean "What's the best text, to print on the screen, for an end-user, when they try to do this calculation in a statistics package?" or do you mean "what's the best way to handle this situation in code as a software engineer?". It's a shame so many Great Minds spent so long on this! :O – Fattie Aug 28 '14 at 15:19
  • @JoeBlow If I were asking about a program, it wouldn't be on Math.SE - clarification added – Ky - Aug 29 '14 at 03:23
  • @ThomasPadron-McCarthy I find it funny that you think it's a good idea for a bank's or aircraft's systems to crash ;) – Ky - Aug 29 '14 at 03:26
  • @BrianS Except that Float and Double extend from Object (in C# and Java), so a function returning either could easily return null. – Cole Tobin Aug 29 '14 at 06:14
  • Hi @Supuhstar , no problem but the first sentence is about using an app, and the second sentence mentions NaN. anyways you've cleared it up so great! – Fattie Aug 29 '14 at 06:18
  • @Cole, Float and Double do not extend from Object in C# (they are aliases of float and double). And while you could use those classes to return null in Java (you would have to use Nullable<Float> or float? in C#), my previous comment is an assertion that you don't want to return null. – Brian S Aug 29 '14 at 06:45
  • 1
    @Supuhstar: Well, it's not particularly good for a bank system to crash, but I would definitely prefer that to it quietly deleting all my money! – Thomas Padron-McCarthy Aug 31 '14 at 16:18
  • 1
    I would prefer NaN for random input or where input is taken from a population with a uniform distribution (or any distribution with no mean). Otherwise, I would prefer the mean of the population from which the numbers had been drawn. Writing a program that "knows" about the probability distribution of the input values would be impossible to write, which is why I'm glad that this question was posed in Math.StackExchange. :) – John Joy Jan 02 '15 at 14:38

10 Answers10

95

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.

Ittay Weiss
  • 79,840
  • 7
  • 141
  • 236
  • 2
    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$ – Ky - Aug 26 '14 at 14:53
  • But think about the second paragraph. What if you add 1 to every number between $-\infty$ and $\infty$? – asmeurer Aug 26 '14 at 15:54
  • @asmeurer then you get $1+\Bbb R$: http://po.st/x2kQgP – Ky - Aug 26 '14 at 17:34
  • @Supuhstar Even if every individual point has zero weight, the total weight must still be $1$. So adding $c$ to every observation should change the average by $\int_{-\infty}^\infty c w(x) dx = c$. – Andrew Dudzik Aug 26 '14 at 18:10
  • 5
    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}$. – asmeurer Aug 26 '14 at 19:03
  • -1 because the second paragraph of this answer is blatantly wrong. "Adding c" to the members of an empty set does not change the set, and therefore does not change its mean. This is completely self-consistent and also consistent with linearity of expectation -- it would be nonsense to expect the mean of a set to change when the set itself has not changed! – user541686 Aug 27 '14 at 10:05
  • 1
    @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. – Ittay Weiss Aug 27 '14 at 10:27
  • @IttayWeiss: "the mean does translate with a translation"... with a translation of what, exactly? What are you translating that you expect to translate the mean? – user541686 Aug 27 '14 at 10:30
  • @Mehrdad every member of the set of which I take the mean. – Ittay Weiss Aug 27 '14 at 10:31
  • @IttayWeiss: Which members? They don't exist. – user541686 Aug 27 '14 at 10:31
  • @Mehrdad I know. You can still translate them. Just like you can take the empty sum, empty product, empty infimum, empty supremum. You can translate the empty set, you can scale it. It's just a set. – Ittay Weiss Aug 27 '14 at 10:32
  • @IttayWeiss: Yeah, that's exactly what's wrong with the answer. You're translating nothing, so the mean has no reason to change either. Seems rather simple to me. – user541686 Aug 27 '14 at 10:32
  • 4
    @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. – Ittay Weiss Aug 27 '14 at 10:35
  • @IttayWeiss: Your comment is unrigorous and doesn't make sense. Translating the set first and taking the mean gives a number -- the mean. Taking the mean and then translating "it" ("it" is the set here?) gives back, well, God knows what -- a set? You're saying the "results" of what are equal exactly? A number equal a set? The explanation is very handwavy and seems circular to be honest. Can you say what you mean rigorously so we can analyze it? – user541686 Aug 27 '14 at 10:38
  • no, "it" is the mean, not the set. It was about one-trillion percent rigorous. I suggest taking it to a chat btw. – Ittay Weiss Aug 27 '14 at 10:39
  • How do you "translate" a mean though? – user541686 Aug 27 '14 at 10:42
  • 1
    @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. – mudri Aug 27 '14 at 14:16
  • @JamesWood: The problem is the assumptions in that reasoning. What it comes down to is that I don't think it is meaningful to "translate" an empty set in the first place. If you assume an empty set can be translated, then the reasoning is correct. But I don't think that makes sense, because translating a set by $c$ results in a different set unless the set is empty. To me, that means translating an empty set is not possible. – user541686 Aug 28 '14 at 04:26
  • Your objection is a weak one @Mehrdad. It is perfectly ok to translate things and get the same thing back. For instance, translating any set of reals by $0$ results in the same set. Translating $\mathbb R$ by any number results in the same set again. Translating $\mathbb Z$ by any integer yields $\mathbb Z$ again. Similarly for $\mathbb Q$. Your objection seems to stem from an expectation of yours (and I stress, it is yours) that translation should always produce something different than what you started with. This is simply not the case. – Ittay Weiss Aug 28 '14 at 06:32
  • @IttayWeiss: I've been talking about nonzero translations of finite sets here. Zero translations never result in a different set regardless of the set size, so there is no inconsistency between empty and non-empty sets there. And infinite sets are entirely different beasts operating under different definitions; e.g. the "average" of an infinite set under our current definition doesn't even exist in the first place, so you already have to change some definitions to accommodate infinite sets... I don't see why doing the same for the definition of translation would be any different. – user541686 Aug 31 '14 at 11:50
36

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.

Joel Bosveld
  • 728
  • 5
  • 10
  • 1
    +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. – nikie Aug 26 '14 at 13:58
  • 1
    @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$. – Andrew Dudzik Aug 26 '14 at 17:40
20

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).

  • 9
    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? – asmeurer Aug 26 '14 at 15:57
  • 8
    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. – Brian S Aug 26 '14 at 16:32
  • 4
    @asmeurer The average user has no idea what NaN means. It's jargon. – Sebastian Negraszus Aug 26 '14 at 17:22
  • That "NaN" means "undefined" is not quite right, it is better thought of a "not a specific number", For example hypot(INF, NAN) is not NAN, but INF (if you have one cathetus infinite, then it doesn't matter what the other one is, the hypotenuse is infinite). – J.J. Green Jan 08 '24 at 23:12
14

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.

7

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.
Asaf Karagila
  • 393,674
  • 7
    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$? – Théophile Aug 26 '14 at 05:16
  • 6
    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. :-) – Asaf Karagila Aug 26 '14 at 05:54
  • 3
    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? – Andrew Dudzik Aug 26 '14 at 09:26
  • @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. – Asaf Karagila Aug 26 '14 at 09:43
  • @AsafKaragila And on what grounds do you make this claim? $0$ is not special in any way in the context of computing averages. I might as well declare that $e$ or $-\sqrt{2}$ is the only sane choice. Again, consider temperatures. The average should be invariant under unit conversion, yet $0$ is not the same in all temperature systems. – Andrew Dudzik Aug 26 '14 at 17:35
  • @you-sir-33433: Your argument is silly. We are talking about unitless numbers in math. I can start making claims that all sort of things should be invariant under all sort of other things that they are not invariant under. To average $0$ numbers we sum $0$ numbers, so this sum is $0$. And no, I don't have a better argument because this is my opinion, and I am not going to answer any more of your comments on the topic. I think that $0$ is the only sane choice for the average of no numbers *IF AND WHEN THE CONTEXT DEMANDS THAT THE AVERAGE OF NO NUMBERS IS TO BE DEFINED*. Have a nice day. – Asaf Karagila Aug 26 '14 at 17:49
  • @AsafKaragila "I can start making claims that all sort of things should be invariant under all sort of other things that they are not invariant under." And my argument is silly? Look, units here are merely a tool to show you where your reasoning is breaking down. If I have a list of temperatures, their average must be a temperature, because arithmetic averages are always linear and units distribute. But just saying "the answer is always zero" means that you're not returning a temperature at all. I really doubt that there is a single situation in which zero is a useful answer here. – Andrew Dudzik Aug 26 '14 at 18:03
  • 3
    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.' – Joe Aug 27 '14 at 19:38
  • Like Joe above, I do agree that, if you have to pick a real number to represent the average of no numbers, 0 is (at least) as good a choice as any. What I do genuinely wonder is what argument you have for declaring it the "only sane choice." Is there some kind of a limit argument (of weighted means?), does it make some useful lemma regarding averages hold also for the average of no numbers, or does it at least simplify some practical formulas if one defines it that way? – Ilmari Karonen Aug 28 '14 at 15:04
3

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.

jxnh
  • 5,228
3

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 :-)

gareth
  • 51
1

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.

user132181
  • 2,726
0

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.

isilanes
  • 111
  • 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. – Ky - Aug 27 '14 at 14:01
-2

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).

  • 5
    -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 – Ky - Aug 27 '14 at 02:21
  • 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. – String Aug 28 '14 at 08:39