

SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 4:58 PM
Points: 8,400,
Visits: 10,115


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 zerodivide 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 zerodivide 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




Ten Centuries
Group: General Forum Members
Last Login: Tuesday, October 28, 2014 12:50 PM
Points: 1,061,
Visits: 2,580


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 "n1" 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 Blog: SQLSouth Twitter: @SQLSouth




SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 4:58 PM
Points: 8,400,
Visits: 10,115


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 "n1" 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/(N1) (Bessel's correction) to get a better estimate (or more usually shortcircuit that by dividing by N1 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




Ten Centuries
Group: General Forum Members
Last Login: Tuesday, October 28, 2014 12:50 PM
Points: 1,061,
Visits: 2,580


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 "n1" method and which function goes with which!
Jason
Jason Wolfkill Blog: SQLSouth Twitter: @SQLSouth




SSChasing Mays
Group: General Forum Members
Last Login: Sunday, November 17, 2013 11:53 AM
Points: 623,
Visits: 237


Simple Question but discussion was so detailed that you learn a loads of thing from the discussion




Ten Centuries
Group: General Forum Members
Last Login: Thursday, November 20, 2014 5:13 AM
Points: 1,131,
Visits: 1,394


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




SSCoach
Group: General Forum Members
Last Login: Yesterday @ 9:44 PM
Points: 18,647,
Visits: 16,912





Forum Newbie
Group: General Forum Members
Last Login: Monday, September 17, 2012 7:48 AM
Points: 1,
Visits: 2


mdv 9731 (1/28/2011)
I do not agree that the answer is right. Think it is supposed to be 0. the standard deviation σ (sigma) is the square root of the average value of (X − μ)2. Which means σ = sqrt(((11)^2)/1) = 0 Or in other words std. deviation equals sqrt of (populationavarage) 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




SSC Rookie
Group: General Forum Members
Last Login: Wednesday, February 11, 2015 4:33 AM
Points: 27,
Visits: 122


Thanks for clarifying this. As I said it has been a while since I used statistics



