Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

Standard Deviation Expand / Collapse
Author
Message
Posted Friday, January 28, 2011 2:01 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 5:53 PM
Points: 8,543, Visits: 9,030
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
Post #1055609
Posted Friday, January 28, 2011 2:22 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 3:21 PM
Points: 1,210, Visits: 2,512
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
Blog: SQLSouth
Twitter: @SQLSouth
Post #1055622
Posted Friday, January 28, 2011 2:51 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 5:53 PM
Points: 8,543, Visits: 9,030
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
Post #1055638
Posted Friday, January 28, 2011 3:13 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 3:21 PM
Points: 1,210, Visits: 2,512
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
Blog: SQLSouth
Twitter: @SQLSouth
Post #1055656
Posted Friday, January 28, 2011 7:40 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing 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
Post #1055697
Posted Monday, February 7, 2011 4:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 7:10 AM
Points: 1,013, Visits: 1,286
Thanks for the question. I learn new thing today. Discussion about the topic is really good...

Thanks
Post #1059447
Posted Tuesday, February 8, 2011 12:15 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:44 PM
Points: 21,187, Visits: 14,879
Thanks for the question



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1060548
Posted Monday, September 17, 2012 7:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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(((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
Post #1360201
Posted Monday, September 17, 2012 8:03 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, June 27, 2014 5:36 AM
Points: 20, Visits: 112
Thanks for clarifying this. As I said it has been a while since I used statistics
Post #1360224
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse