SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Is there an equivalent of Excel's NORMDIST function in SQL Server 2005?


Is there an equivalent of Excel's NORMDIST function in SQL Server 2005?

Author
Message
L Cerniglia
L Cerniglia
SSC Journeyman
SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)

Group: General Forum Members
Points: 84 Visits: 314
Is there an equivalent of Excel's NORMDIST function in SQL Server 2005?
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65086 Visits: 17979
Nothing right out of the box that i know of. You will probably have to write your own. (or borrow somebody else's)

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
L Cerniglia
L Cerniglia
SSC Journeyman
SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)

Group: General Forum Members
Points: 84 Visits: 314
If it helps anyone, this is how my function looks like. Thanks again to all who helped me.

CREATE FUNCTION [dbo].[udf_NORMDIST](@value FLOAT,
@mean FLOAT,
@sigma FLOAT,
@cummulative BIT)
RETURNS NUMERIC(28,8)
AS
/****************************************************************************************
NAME: udf_NORMDIST
WRITTEN BY: Tim Pickering
http://www.eggheadcafe.com/software/aspnet/31021839/normdistx-mean-standarddevtrue-in-sql-2005.aspx
DATE: 2010/07/13
PURPOSE: Mimics Excel's Function NORMDIST

Usage: SELECT dbo.udf_NORMDIST(.48321740,0,1,0)
OUTPUT: 0.35498205

REVISION HISTORY

Date Developer Details
2010/08/11 LC Posted Function


*****************************************************************************************/


BEGIN

DECLARE @x FLOAT
DECLARE @z FLOAT
DECLARE @t FLOAT
DECLARE @ans FLOAT
DECLARE @returnvalue FLOAT

SELECT @x = (@value-@mean)/@sigma

IF (@cummulative = 1)
BEGIN

SELECT @z = abs(@x)/sqrt(2.0)
SELECT @t = 1.0/(1.0+0.5*@z)
SELECT @ans = @t*exp(-@z*@z-1.26551223+@t*(1.00002368+@t*(0.37409196+@t*(0.09678418+@t*(-0.18628806+@t*(0.27886807+@t*(-1.13520398+@t*(1.48851587+@t*(-0.82215223+@t*0.17087277)))))))))/2.0

IF (@x <= 0)
SELECT @returnvalue = @ans
ELSE
SELECT @returnvalue = 1-@ans
END
ELSE
BEGIN
SELECT @returnvalue = exp(-@x*@x/2.0)/sqrt(2.0*3.14159265358979)
END


RETURN CAST(@returnvalue AS NUMERIC(28,8))


END
idea
idea
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 4
Out of the box there is no equivalent in SQL Server (up to 2012) for NORMDIST.

NORMDIST computes the Normal Distribution PDF (Probability Density Function) when the cumulative parameter is false and the CDF (Cumulative Distribution Function) otherwise.

The PDF can be computed from the definition and is pretty straight forward, the CDF however has no close form and can only be approximated.
I've developed a function for the PDF and a set of functions using different approximations for the CDF.
You can read my analysis in:
http://formaldev.blogspot.com.au/2012/09/T-SQL-NORMDIST-1.html
and either copy paste the ones you want from the posts or get them from the project page for the blog posts at:
https://tsqlnormdist.codeplex.com/
chris.palmer.nz
chris.palmer.nz
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 91
I have been able to use the udf_NORMDIST code to stand in for Excel NORM.DIST (NORMDIST). Thank-you very much for posting it!

The other reference from idea/Forum Newbie points to code for NORM.S.DIST (NORMSDIST in older Excel), it seems to be incorrectly labelled as an equivalent for NORMDIST.

However the value returned when @cummulative is set to false (0) seems to be incorrect with some larger numbers, at least it doesn't line up with Excel.

e.g. I used SELECT dbo.udf_NORMDIST(358, 352.791176470588, 234.354144866830, 0)
Excel gives me 0.001701885, but this function gave me 0.398843752

So I created a replacement for the line that returns a value when not using cumulative

The formula for this change comes from http://stattrek.com/online-calculator/normal.aspx

Normal equation:

The value of the random variable Y is:
Y = { 1/[ σ * sqrt(2π) ] } * e-(x - μ)2/2σ2

where X is a normal random variable,
μ is the mean,
σ is the standard deviation,
π is approximately 3.14159,
and e is approximately 2.71828.

Code change:
-- Incorrect for larger numbers?
--SELECT @returnvalue = exp(-@x*@x/2.0)/sqrt(2.0*3.14159265358979)
-- Correct for any numbers...
SELECT @returnvalue = (1/(@sigma * sqrt(2.0*3.14159265358979))) * exp(-((@value - @mean)*(@value - @mean))/(2*(@sigma*@sigma)))

Also, the precision is not quite right, it needs to be 29,9, not 28,8

Here is a replacement for the function:


CREATE FUNCTION [dbo].[udf_NORMDIST](
@value FLOAT,
@mean FLOAT,
@sigma FLOAT,
@cummulative BIT )

RETURNS NUMERIC( 29,9 )
AS

/****************************************************************************************
NAME: udf_NORMDIST
WRITTEN BY: Tim Pickering
http://www.eggheadcafe.com/software/aspnet/31021839/normdistx-mean-standarddevtrue-in-sql-2005.aspx
DATE: 2010/07/13
PURPOSE: Mimics Excel's Function NORMDIST

Usage: SELECT dbo.udf_NORMDIST(.48321740,0,1,0)
OUTPUT: 0.35498205

REVISION HISTORY

Date Developer Details
2010/08/11 LC Posted Function
*****************************************************************************************/
BEGIN
DECLARE @x FLOAT;
DECLARE @z FLOAT;
DECLARE @t FLOAT;
DECLARE @ans FLOAT;
DECLARE @returnvalue FLOAT;
SELECT @x = (@value - @mean) / @sigma;
IF(@cummulative = 1)
BEGIN
SELECT @z = ABS( @x ) / SQRT( 2.0 );
SELECT @t = 1.0 / (1.0 + 0.5 * @z);
SELECT @ans = @t*exp(-@z*@z-1.26551223+@t*(1.00002368+@t*(0.37409196+@t*(0.09678418+@t*(-0.18628806+@t*(0.27886807+@t*(-1.13520398+@t*(1.48851587+@t*(-0.82215223+@t*0.17087277)))))))))/2.0;
IF(@x <= 0)
SELECT @returnvalue = @ans;
ELSE
SELECT @returnvalue = 1 - @ans;
END;
ELSE
BEGIN
SELECT @returnvalue = (1/(@sigma * SQRT( 2.0 * 3.14159265358979 ))) * EXP(-((@value-@mean)*(@value-@mean)) / (2*(@sigma*@sigma)));
END;
RETURN CAST( @returnvalue AS NUMERIC( 29,9 ));
END;
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18463 Visits: 6431
Nothing out of the box as stated, but there is this:

Excel in T-SQL Part 2 – The Normal Distribution (NORM.DIST) Density Functions

There are also some links at the end to approximation formulas for the cumulative distribution function. The probability density function is pretty straightforward.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search