SQL Server BINOM_DIST_RANGE Function
Updated 2024-02-13 20:44:08.533000
Description
Use the scalar function BINOM_DIST_RANGE to calculate the probability of a trial result using a binomial distribution.
Syntax
SELECT [westclintech].[wct].[BINOM_DIST_RANGE](
<@Trials, int,>
,<@Probability_s, float,>
,<@Number_s, int,>
,<@Number_s2, int,>)
Arguments
@Trials
The number of independent trials. @Trials must be of a type int or of type that intrinsically converts to int.
@Probability_s
The probability of success in each trial. @Probability_s must be of a type float or of a type that intrinsically converts to float.
@Number_s
The number of successes in @Trials. @Number_s must be of a type int or of a type that intrinsically converts to int.
@Number_s2
The upper bound of the number of successes in @Trials. @Number_s2 must be of a type int or of a type that intrinsically converts to int.
Return Type
float
Remarks
@Trials > 0.
0 ≤ @Probability_s ≤ 1.
0 ≤ @Number_s = @Trials
If NOT NULL then 0 ≤ @Number_s2 ≤ @Trials
Examples
Calculate the probability of having exactly 48 successes in 60 trials when the probability of success is 75%.
SELECT wct.BINOM_DIST_RANGE( 60, --@Trials
0.75, --@Probability_s
48, --@Number_s
NULL --@Number_s2
) as BINOM_DIST_RANGE;
This produces the following result.
| BINOM_DIST_RANGE |
|---|
| 0.0839749674290474 |
This is actually no different than calculating the probability mass function of the binomial distribution.
SELECT wct.BINOMDIST(48, 60, 0.75, 'False') as PMF;
This produces the following result.
| PMF |
|---|
| 0.0839749674290512 |
Let's look at another example where we calculate the probability of having exactly 500 successes in 1,000 trial where the probability of success is 50%.
SELECT wct.BINOM_DIST_RANGE( 1000, --@Trials
0.50, --@Probability_s
500, --@Number_s
NULL --@Number_s2
) as BINOM_DIST_RANGE;
This produces the following result.
| BINOM_DIST_RANGE |
|---|
| 0.0252250181783607 |
It might be far more useful, however, to calculate the probability of having between 450 and 550 successes.
SELECT wct.BINOM_DIST_RANGE( 1000, --@Trials
0.50, --@Probability_s
450, --@Number_s
550 --@Number_s2
) as BINOM_DIST_RANGE;
This produces the following result.
| BINOM_DIST_RANGE |
|---|
| 0.998608258405578 |
In this example we bucket the results into 20 equal groups using the XLeratorDB SeriesInt function.
SELECT seriesvalue - 49 as lb,
seriesvalue as ub,
wct.BINOM_DIST_RANGE(1000, 0.50, seriesvalue - 49, seriesvalue) as p
FROM wct.SeriesInt(50, 1000, 50, NULL, NULL);
This produces the following result.
| lb | ub | p |
|---|---|---|
| 1 | 50 | 9.31846353326976E-217 |
| 51 | 100 | 6.70171779000614E-162 |
| 101 | 150 | 1.52122150476367E-119 |
| 151 | 200 | 8.22499367788738E-86 |
| 201 | 250 | 6.73812825301519E-59 |
| 251 | 300 | 8.83283900397519E-38 |
| 301 | 350 | 8.07815519348284E-22 |
| 351 | 400 | 1.36423207802494E-10 |
| 401 | 450 | 0.000865267906064952 |
| 451 | 500 | 0.511747241046692 |
| 501 | 550 | 0.486691620113609 |
| 551 | 600 | 0.000695870707126844 |
| 601 | 650 | 9.00841623519E-11 |
| 651 | 700 | 0 |
| 701 | 750 | 0 |
| 751 | 800 | 0 |
| 801 | 850 | 0 |
| 851 | 900 | 0 |
| 901 | 950 | 0 |
| 951 | 1000 | 0 |
See Also
BETA_DIST - Calculate pdf or cdf of beta distribution
BINOMDIST - Binomial distribution