Standard Deviation

  • Division by zero should be ERROR, not null. How come?

  • Right answer, wrong explanation. The stdev function is simply programmed to return null when the sample size is less than 2. It has nothing to do with division by zero. Division by zero is an invalid calculation not an unknown result. If it were allowed to perform the zero division the proper response of the function should have been to throw an error; instead they trapped the invalid condition and returned a NULL. I would also argue that this is an inaccurate result since it ignores the fact that an invalid data set is being used without returning the appropriate error.

  • Thanks for the question, it is good to know that SQL Server decides to return NULL for the standard deviation for a data value, though I don't think it is alone.

  • What's going on here is that the STDEV() function in T-SQL returns the sample standard deviation (which for a single value will be undefined, hence the programmatically dictated NULL result) rather than the population standard deviation (which for a single value will be 0). BOL does not clearly state this, which apparently causes some confusion. Another T-SQL function, STDEVP(), returns the population standard deviation. Developers using statistical functions in T-SQL definitely should be aware of this!

    Jason Wolfkill

  • Thanks for the explanation wolfkillj! That makes total sense now.

  • Well I got this one wrong, mainly because the SQL function naming is a little bizarre:

    STDEV is the estimated Population Standard Deviation (although there's no P in the name)

    STDEVP is the actual Sample Standard Deviation (although there is a P in the name)

    I thought the P in STDEVP stood for "Population", so STDEVP would be the estimated Population Standard Deviation (obtained by applying Bessel's correction to the actual Sample Standard Deviation) and STDEV the actual Sample Standard Deviation (in the SQL 2000 days you could look it up in BoL, so I never bothered to learn it - I wanted those functions rarely enough for it not to be worth making an effort to learn it); when I discovered that which is which isn't documented (at least not in any obvious manner) in BoL for SQL 2008 I just tried to work out which was which based on the name, and got it wrong because the SQL function names are misleading.

    You can't apply Bessel's correction when your sample has only 1 member, so SQL Server quite correctly refuses to attempt it. Actually it would be better to return an appropriate error (one that specifically means that STDEV has been applied to a singleton sample) than to return NULL. Returning NULL is better than returning a zero-divide error which might be caused by some other part of a select statement - eg if SELECT STDEV((a+c)/b) FROM T where a > 37 returned a zero-divide error one couldn't tell whether the error occurred because one of the b attributes was zero or because there was at most one row with a > 37. But returning NULL means you don't know whether the sample was empty or a singleton, which is why a specific error should be returned (ideally a specific error should be returned instead of NULL for the empty sample case too, but that is another story, part of a much more general one).

    Tom

  • I thought the P in STDEVP stood for "Population", so STDEVP would be the estimated Population Standard Deviation (obtained by applying Bessel's correction to the actual Sample Standard Deviation) and STDEV the actual Sample Standard Deviation (in the SQL 2000 days you could look it up in BoL, so I never bothered to learn it - I wanted those functions rarely enough for it not to be worth making an effort to learn it); when I discovered that which is which isn't documented (at least not in any obvious manner) in BoL for SQL 2008 I just tried to work out which was which based on the name, and got it wrong because the SQL function names are misleading.

    I think the "P" in STDEVP does stand for "population", as that function returns the actual standard deviation of the entire population (sometimes called the "n" method); hence, the standard deviation of a population consisting of a single value = 0, as STDEVP will return. STDEV returns the standard deviation with Bessel's correction applied (i.e., the estimated population standard deviation or the sample standard deviation, sometimes call the "n-1" method), which is why applying this function to a population consisting of a single value will not work.

    Can anyone who knows the programmatic bases of STDEV and STDEVP confirm my conclusion?

    Jason Wolfkill

  • wolfkillj (1/28/2011)


    I think the "P" in STDEVP does stand for "population", as that function returns the actual standard deviation of the entire population (sometimes called the "n" method); hence, the standard deviation of a population consisting of a single value = 0, as STDEVP will return. STDEV returns the standard deviation with Bessel's correction applied (i.e., the estimated population standard deviation or the sample standard deviation, sometimes call the "n-1" method), which is why applying this function to a population consisting of a single value will not work.

    Can anyone who knows the programmatic bases of STDEV and STDEVP confirm my conclusion?

    Up to a point you are right (certainly the code bases are the way round that you suggest), but STDEVP returns the standard deviation of the sample provided to it, not that of the population from which the sample is drawn. I guess that what's happened is that someone chose the name STDEVP to mean a version of STDEV to which the whole population was provided, rather than a sample, and that's why the code without Bessel's correction has the P in its name. But many statisticians (and other mathematicians) talk about the standard deviation of the sample and the estimated standard deviation of the population, and (at least for distributions thought to be close to normal) instead of using the standard deviation of the sample as an estimate for the population SD they multiply it by N/(N-1) (Bessel's correction) to get a better estimate (or more usually shortcircuit that by dividing by N-1 instead of by N in the first place), so in the usual terminology the population is ony referred to in the corrected case, not in the uncorrected case, so people may be confused by the naming with P included in the name that doesn't try to provide the population (as opposed to sample) standard deviation (as I was).

    edit: take out superfluous quote tag

    Tom

  • Hi Tom,

    I see that we are on the same page. The only way I can make sense of Microsoft's nomenclature is to remember that I should use STDEVP when I'm applying the function to a data set to calculate the standard deviation of only those data points (i.e., when I'm feeding the function the whole population and not a sample), hence my reliance on the "P for population" mnemonic device, and STDEV when I'm applying the function to a data set that serves as a sample of some larger data set for which I want to estimate the standard deviation. Otherwise, I go crazy trying to remember whether I should be using the "n" or the "n-1" method and which function goes with which!

    Jason

    Jason Wolfkill

  • Simple Question but discussion was so detailed that you learn a loads of thing from the discussion ๐Ÿ˜€

  • Thanks for the question. I learn new thing today. Discussion about the topic is really good...

    Thanks

  • Thanks for the question

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • mdv 9731 (1/28/2011)


    I do not agree that the answer is right.

    Think it is supposed to be 0.

    the standard deviation s (sigma) is the square root of the average value of (X - ยต)2.

    Which means s = sqrt(((1-1)^2)/1) = 0

    Or in other words std. deviation equals sqrt of (population-avarage) sq / number of population values

    Please correct me if im wrong on the formula it has been a while since i used my statistics ๐Ÿ˜€

    What you state is correct for a population standard deviation. Then you devide by the number of samples N, which is one for a single record. You do this with the function STDEVP.

    When using STDEV however, you take the sample standard deviation in which you devide by N - 1 which would result in a division by zero. Hence the NULL.

    SELECT STDEV(100)

    Results in NULL

    SELECT STDEVP(100)

    Results in 0

    See: http://en.wikipedia.org/wiki/Standard_deviation

  • Thanks for clarifying this. As I said it has been a while since I used statistics ๐Ÿ™‚

Viewing 14 posts - 16 through 28 (of 28 total)

You must be logged in to reply to this topic. Login to reply