NEGHYPGEOM_DIST
Updated 2024-03-13 12:33:12.017000
Syntax
SELECT [westclintech].[wct].[NEGHYPGEOM_DIST](
<@num_trials, float,>
,<@num_success, float,>
,<@pop_success, float,>
,<@pop_size, float,>
,<@Cumulative, bit,>)
Description
Use the scalar function NEGHYPGEOM_DIST to calculate the probability mass function or the cumulative distributive function of the negative hypergeometric distribution. The negative hypergeometric distribution models the sample size required to achieve a specified number of failures given the number of successes and the size of the population. The probability mass function can be calculated as:
P(x)=\frac{\binom{x-1}{k-1}\binom{N-x}{M-k}}{\binom{N}{M}};k\leq{x}\leq{k+N-M}
Where:
{"columns":[{"field":"column 1",
"maxWidth": 25},{"field":"column 2",
"maxWidth": 25},{"field":"column 3",
"maxWidth": 600,"wrapText":"true","autoHeight": "true"}],"rows":[{"column 1":"x","column 2":"=","column 3":"number of trials"},{"column 1":"M","column 2":"=","column 3":"number of successes in the population"},{"column 1":"N","column 2":"=","column 3":"population size"},{"column 1":"k","column 2":"=","column 3":"number of successes to achieve with the sample"}]}
The cumulative distribution function can be calculated as:
cdf=\sum_{x=k}^{num\_trials}\frac{\binom{x-1}{k-1}\binom{N-x}{M-k}}{\binom{N}{M}};k\leq{x}\leq{k+N-M}
Arguments
@Cumulative
a bit value identifying whether the probability mass function ( 'False') or the cumulative distribution function ( 'True') is to be returned. @Cumulative is of a type bit or a type that implicitly converts to bit.
@num_trials
the number of trials until @num_success have occurred. @num_trials must be of a type float or of type that intrinsically converts to float.
@num_success
the number of successes to achieve with the sample. @num_success must be of a type float or of type that intrinsically converts to float.
@pop_success
the number of successes in the population. @pop_success must be of a type float or of type that intrinsically converts to float.
@pop_size
the size of the population. @pop_size must be of a type float or of a type that intrinsically converts to float.
Return Type
float
Remarks
@num_trials, @num_success, @pop_success, and @pop_size are truncated; only the integer part is used.
If @num_success < 0 then NULL is returned.
If @pop_success < 0 then NULL is returned.
If @num_trials = 0 then NULL is returned.
If @pop_size = 0 then NULL is returned.
If @pop_success > @pop_size then NULL is returned.
If @num_trials > @pop_size - @pop_success + @num_success then NULL is returned.
Examples
In an urn with 1000 balls of which 300 are white we want to calculate the probability of drawing exactly 40 white balls in 150 draws from the urn.
SELECT wct.NEGHYPGEOM_DIST( 150, --@num_trials
40, --@num_success
300, --@pop_success
1000, --@pop_size
'False' --@Cumulative
) as NEGHYPGEOM_DIST;
This produces the following result.
{"columns":[{"field":"NEGHYPGEOM_DIST","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"NEGHYPGEOM_DIST":"0.0131376591508012"}]}
We can contrast this to the value returned using the binomial coefficients.
SELECT wct.NEGHYPGEOM_DIST( 150, --@num_trials
40, --@num_success
300, --@pop_success
1000, --@pop_size
'False' --@Cumulative
) as NEGHYPGEOM_DIST,
wct.BICO(150 - 1, 40 - 1) * wct.BICO(1000 - 150, 300 - 40) / wct.BICO(1000,
300) as pmf;
This produces the following result.
{"columns":[{"field":"NEGHYPGEOM_DIST","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"pmf","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"NEGHYPGEOM_DIST":"0.0131376591508012","pmf":"0.0131376591507985"}]}
Using the same data we calculate the cumulative distribution function.
SELECT wct.NEGHYPGEOM_DIST( 150, --@num_trials
40, --@num_success
300, --@pop_success
1000, --@pop_size
'True' --@Cumulative
) as NEGHYPGEOM_DIST;
This produces the following result.
{"columns":[{"field":"NEGHYPGEOM_DIST","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"NEGHYPGEOM_DIST":"0.856400290288149"}]}
In this example, we demonstrated the relationship between the hypergeometric and the negative hypergeometric distribution .
SELECT wct.NEGHYPGEOM_DIST( 150, --@num_trials
40, --@num_success
300, --@pop_success
1000, --@pop_size
'True' --@Cumulative
) as NEGHYPGEOM_DIST,
wct.HYPGEOM_DIST(150 - 40, 150, 1000 - 300, 1000, 'True') as HYPGEOM_DIST;
This produces the following result.
{"columns":[{"field":"NEGHYPGEOM_DIST","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"HYPGEOM_DIST","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"NEGHYPGEOM_DIST":"0.856400290288149","HYPGEOM_DIST":"0.856400290288149"}]}
In this example we use the XLeratorDB SeriesInt function to show how the cdf is the sum of the pmf values from @num_success to @num_trial.
DECLARE @num_success as float = 40;
DECLARE @num_trials as float = 150;
DECLARE @pop_success as float = 300;
DECLARE @pop_size as float = 1000;
SELECT num_trials,
pmf,
SUM(pmf) OVER (ORDER BY num_trials) as cdf
FROM
(
SELECT SeriesValue as num_trials,
wct.NEGHYPGEOM_DIST(seriesValue, @num_success, @pop_success, @pop_size,
'False') as pmf
FROM wct.SeriesInt(@num_success, @num_trials, NULL, NULL, NULL)
) n;
This produces the following result.
{"columns":[{"field":"num_trials","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"pmf","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"cdf","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"num_trials":"40","pmf":"1.7614981027881E-22","cdf":"1.7614981027881E-22"},{"num_trials":"41","pmf":"5.13770279980309E-21","cdf":"5.3138526100819E-21"},{"num_trials":"42","pmf":"7.67682088318186E-20","cdf":"8.20820614419005E-20"},{"num_trials":"43","pmf":"7.8306778361577E-19","cdf":"8.65149845057671E-19"},{"num_trials":"44","pmf":"6.13096252423383E-18","cdf":"6.9961123692915E-18"},{"num_trials":"45","pmf":"3.92792042556879E-17","cdf":"4.62753166249794E-17"},{"num_trials":"46","pmf":"2.14390421133981E-16","cdf":"2.6066573775896E-16"},{"num_trials":"47","pmf":"1.02488766161703E-15","cdf":"1.28555339937599E-15"},{"num_trials":"48","pmf":"4.37849108427305E-15","cdf":"5.66404448364904E-15"},{"num_trials":"49","pmf":"1.69743183771367E-14","cdf":"2.26383628607857E-14"},{"num_trials":"50","pmf":"6.04346420537374E-14","cdf":"8.30730049145232E-14"},{"num_trials":"51","pmf":"1.99521067067724E-13","cdf":"2.82594071982247E-13"},{"num_trials":"52","pmf":"6.15645484341946E-13","cdf":"8.98239556324193E-13"},{"num_trials":"53","pmf":"1.78719026677994E-12","cdf":"2.68542982310413E-12"},{"num_trials":"54","pmf":"4.90823539023829E-12","cdf":"7.59366521334243E-12"},{"num_trials":"55","pmf":"1.28132961096537E-11","cdf":"2.04069613229961E-11"},{"num_trials":"56","pmf":"3.19273102467746E-11","cdf":"5.23342715697707E-11"},{"num_trials":"57","pmf":"7.62053646367773E-11","cdf":"1.28539636206548E-10"},{"num_trials":"58","pmf":"1.74782081458471E-10","cdf":"3.03321717665019E-10"},{"num_trials":"59","pmf":"3.86282266966779E-10","cdf":"6.89603984631798E-10"},{"num_trials":"60","pmf":"8.24677747314151E-10","cdf":"1.51428173194595E-09"},{"num_trials":"61","pmf":"1.70450111907096E-09","cdf":"3.21878285101691E-09"},{"num_trials":"62","pmf":"3.41750081570169E-09","cdf":"6.6362836667186E-09"},{"num_trials":"63","pmf":"6.65885159399702E-09","cdf":"1.32951352607156E-08"},{"num_trials":"64","pmf":"1.26292546840779E-08","cdf":"2.59243899447935E-08"},{"num_trials":"65","pmf":"2.33500886603117E-08","cdf":"4.92744786051052E-08"},{"num_trials":"66","pmf":"4.21425396944296E-08","cdf":"9.14170182995348E-08"},{"num_trials":"67","pmf":"7.43385175575885E-08","cdf":"1.65755535857123E-07"},{"num_trials":"68","pmf":"1.28311058612437E-07","cdf":"2.94066594469561E-07"},{"num_trials":"69","pmf":"2.16934369333757E-07","cdf":"5.11000963803318E-07"},{"num_trials":"70","pmf":"3.59607746715971E-07","cdf":"8.70608710519289E-07"},{"num_trials":"71","pmf":"5.85001849496499E-07","cdf":"1.45561056001579E-06"},{"num_trials":"72","pmf":"9.34708115218811E-07","cdf":"2.3903186752346E-06"},{"num_trials":"73","pmf":"1.46798986120825E-06","cdf":"3.85830853644285E-06"},{"num_trials":"74","pmf":"2.26784359198026E-06","cdf":"6.12615212842311E-06"},{"num_trials":"75","pmf":"3.44857771018759E-06","cdf":"9.5747298386107E-06"},{"num_trials":"76","pmf":"5.16509949836555E-06","cdf":"1.47398293369762E-05"},{"num_trials":"77","pmf":"7.62406637081614E-06","cdf":"2.23638957077924E-05"},{"num_trials":"78","pmf":"1.10970009778345E-05","cdf":"3.34608966856269E-05"},{"num_trials":"79","pmf":"1.59353896905121E-05","cdf":"4.9396286376139E-05"},{"num_trials":"80","pmf":"2.25876795398639E-05","cdf":"7.1983965916003E-05"},{"num_trials":"81","pmf":"3.16179607557323E-05","cdf":"0.000103601926671735"},{"num_trials":"82","pmf":"4.37259735524565E-05","cdf":"0.000147327900224192"},{"num_trials":"83","pmf":"5.97679137913006E-05","cdf":"0.000207095814015492"},{"num_trials":"84","pmf":"8.07773398174178E-05","cdf":"0.00028787315383291"},{"num_trials":"85","pmf":"0.000107985311336647","cdf":"0.000395858465169557"},{"num_trials":"86","pmf":"0.000142838731496047","cdf":"0.000538697196665604"},{"num_trials":"87","pmf":"0.000187015727321325","cdf":"0.000725712923986929"},{"num_trials":"88","pmf":"0.000242436803688707","cdf":"0.000968149727675636"},{"num_trials":"89","pmf":"0.000311270454001415","cdf":"0.00127942018167705"},{"num_trials":"90","pmf":"0.000395931917330823","cdf":"0.00167535209900787"},{"num_trials":"91","pmf":"0.000499073845374445","cdf":"0.00217442594438232"},{"num_trials":"92","pmf":"0.000623567788652001","cdf":"0.00279799373303432"},{"num_trials":"93","pmf":"0.000772475631458075","cdf":"0.0035704693644924"},{"num_trials":"94","pmf":"0.000949010396921317","cdf":"0.00451947976141371"},{"num_trials":"95","pmf":"0.00115648619993372","cdf":"0.00567596596134744"},{"num_trials":"96","pmf":"0.00139825753553368","cdf":"0.00707422349688112"},{"num_trials":"97","pmf":"0.00167764853820918","cdf":"0.00875187203509029"},{"num_trials":"98","pmf":"0.00199787331455993","cdf":"0.0107497453496502"},{"num_trials":"99","pmf":"0.00236194891688691","cdf":"0.0131116942665371"},{"num_trials":"100","pmf":"0.00277260296553173","cdf":"0.0158842972320689"},{"num_trials":"101","pmf":"0.00323217832047311","cdf":"0.019116475552542"},{"num_trials":"102","pmf":"0.00374253752601197","cdf":"0.0228590130785539"},{"num_trials":"103","pmf":"0.00430496998697509","cdf":"0.027163983065529"},{"num_trials":"104","pmf":"0.00492010496472804","cdf":"0.0320840880302571"},{"num_trials":"105","pmf":"0.00558783349566022","cdf":"0.0376719215259173"},{"num_trials":"106","pmf":"0.00630724222656429","cdf":"0.0439791637524816"},{"num_trials":"107","pmf":"0.00707656193184792","cdf":"0.0510557256843295"},{"num_trials":"108","pmf":"0.00789313313032647","cdf":"0.058948858814656"},{"num_trials":"109","pmf":"0.00875339076726693","cdf":"0.0677022495819229"},{"num_trials":"110","pmf":"0.00965286938563953","cdf":"0.0773551189675624"},{"num_trials":"111","pmf":"0.0105862296000044","cdf":"0.0879413485675669"},{"num_trials":"112","pmf":"0.0115473060311552","cdf":"0.099488654598722"},{"num_trials":"113","pmf":"0.0125291761848761","cdf":"0.112017830783598"},{"num_trials":"114","pmf":"0.0135242490916024","cdf":"0.125542079875201"},{"num_trials":"115","pmf":"0.0145243718912293","cdf":"0.14006645176643"},{"num_trials":"116","pmf":"0.0155209519726824","cdf":"0.155587403739112"},{"num_trials":"117","pmf":"0.0165050917845435","cdf":"0.172092495523656"},{"num_trials":"118","pmf":"0.0174677330381254","cdf":"0.189560228561781"},{"num_trials":"119","pmf":"0.0183998067405309","cdf":"0.207960035302312"},{"num_trials":"120","pmf":"0.019292385333677","cdf":"0.227252420635989"},{"num_trials":"121","pmf":"0.0201368331765685","cdf":"0.247389253812558"},{"num_trials":"122","pmf":"0.0209249516924812","cdf":"0.268314205505039"},{"num_trials":"123","pmf":"0.0216491157039097","cdf":"0.289963321208948"},{"num_trials":"124","pmf":"0.0223023977831076","cdf":"0.312265718992056"},{"num_trials":"125","pmf":"0.0228786778440169","cdf":"0.335144396836073"},{"num_trials":"126","pmf":"0.0233727356712114","cdf":"0.358517132507284"},{"num_trials":"127","pmf":"0.0237803246069954","cdf":"0.38229745711428"},{"num_trials":"128","pmf":"0.0240982251767387","cdf":"0.406395682291019"},{"num_trials":"129","pmf":"0.0243242780054456","cdf":"0.430719960296464"},{"num_trials":"130","pmf":"0.024457395944771","cdf":"0.455177356241235"},{"num_trials":"131","pmf":"0.0244975558724492","cdf":"0.479674912113684"},{"num_trials":"132","pmf":"0.0244457711255874","cdf":"0.504120683239272"},{"num_trials":"133","pmf":"0.0243040459770855","cdf":"0.528424729216357"},{"num_trials":"134","pmf":"0.0240753139435928","cdf":"0.55250004315995"},{"num_trials":"135","pmf":"0.0237633620211266","cdf":"0.576263405181077"},{"num_trials":"136","pmf":"0.0233727431728488","cdf":"0.599636148353925"},{"num_trials":"137","pmf":"0.0229086795436013","cdf":"0.622544827897527"},{"num_trials":"138","pmf":"0.0223769589445473","cdf":"0.644921786842074"},{"num_trials":"139","pmf":"0.021783827149438","cdf":"0.666705613991512"},{"num_trials":"140","pmf":"0.0211358784696466","cdf":"0.687841492461159"},{"num_trials":"141","pmf":"0.0204399469428016","cdf":"0.70828143940396"},{"num_trials":"142","pmf":"0.0197030002835194","cdf":"0.72798443968748"},{"num_trials":"143","pmf":"0.0189320385189765","cdf":"0.746916478206456"},{"num_trials":"144","pmf":"0.0181339989722908","cdf":"0.765050477178747"},{"num_trials":"145","pmf":"0.017315668978619","cdf":"0.782366146157366"},{"num_trials":"146","pmf":"0.0164836074272317","cdf":"0.798849753584598"},{"num_trials":"147","pmf":"0.0156440759320556","cdf":"0.814493829516653"},{"num_trials":"148","pmf":"0.0148029801471166","cdf":"0.82929680966377"},{"num_trials":"149","pmf":"0.0139658214731601","cdf":"0.84326263113693"},{"num_trials":"150","pmf":"0.0131376591508012","cdf":"0.856400290287731"}]}
See Also
BINOMDIST - Binomial distribution
HYPGEOM_DIST - Hypergeometric distribution
LCHOOSE - Natural logarithm of the binomial coefficient
NEGBINOM_DIST - Negative binomial distribution
NEGHYPGEOM_INV - Calculate the quantiles of the negative hypergeometric distribution.