I am trying to follow the procedure of a Sandia Labs report found here to determine the required sample size, for various population sizes, with no replacement for a given confidence and reliability. The authors provide Mathematica code but I chose to write my own based on their procedure.

The procedure described on page 13 is “The second SNL technique can be extended for any number of defective units in the sample by using the CDF for the hypergeometric distribution. This extension is referred to as the third technique. To find the minimum n given a specific x required for given N, γ, and R, the following technique can be used. Note that D is (1 - R) N rounded to the nearest integer. For the hypergeometric distribution HyperDist(n, D, N), find the minimum n for which CDF(HyperDist(n, D, N), x) does not exceed γ”.

I believe I’m able to reproduce the corrected results in Tables 1 & 2 of the above report with my code below. There are columns labeled “INCORRECT” which I don’t plan to confirm. These tables however are based on observing zero failures in the sample taken while I would like to be able to specify an arbitrary number of x failures. But when I use an x value (number of failures) other than zero I get an error message. Can someone shed any insight into this ? Thanks.

My code is :

c=0.9;         (* Confidence *)
r=0.9;       (* Reliability *)
x=0;           (* Number of failures in the sample *)
pop=10;   (* Population size *)
n=0;            (* Sample size, iterated *)

   Print["n = ",n];
   Print["CDF[x] = ",cdfvalue];
  • $\begingroup$ You're trying to use a sample larger than the population. For HypergeometricDistribution, the first parameter must be positive & <= the last, the second parameter must be between 0 and the last, inclusive. That's not happening when you get the error. $\endgroup$
    – ciao
    Apr 1, 2014 at 20:29
  • $\begingroup$ Hmm, @rasher, you are certainly correct regarding the requirements of the distribution parameters. I see I've got some "homework" to do here. . . thanks. $\endgroup$
    – Steve
    Apr 1, 2014 at 21:28

1 Answer 1


This might help you get going. Note - I shaped the code into a bit more Mathematica style, but in reality, there are more compact ways of doing this (e.g. directly invoking InverseCDF, etc.) but that will be a good learning experience.

From your code, I'm guessing an imperative language background. In general, not good habits for Mathematica code: check the list of questions under "votes", several good starting tutorial-ish questions (like "common pitfalls") that offer great guidance. Caveat: this is just an example, I've done little sanity-checking, so shape as required.

That said:

(* for given population, sample size, defects seen, confidence *)
populationSize = 500;
sampleSize = 50;
confidence = .95;
defectsSeen = 6;
bads = 0;

While[bads <= populationSize && 
   CDF[HypergeometricDistribution[sampleSize, bads, populationSize], 
     defectsSeen] > 1 - confidence, bads++];

{N[CDF[HypergeometricDistribution[sampleSize, bads, populationSize], 
   defectsSeen]], sampleSize, bads, populationSize, 
 N[1 - bads/populationSize]}

(* for given population, required confidence, required reliability *)
populationSize = 100;
confidence = .90;
reliability = .95;
sampleSize = 1;
defectsSeen = 0;
bads = 0;

While[sampleSize <= populationSize && 
      Round[(1 - reliability)*populationSize], populationSize], 
     defectsSeen] > 1 - confidence, sampleSize++];

    Round[(1 - reliability)*populationSize], populationSize], 
   defectsSeen]], sampleSize, bads, populationSize, reliability}

{0.0468071, 50, 110, 500, 0.78}

So the simple output is a list with the result CDF, sample size (actual or required), defects (seen or assumed), population size, and reliability (calculated or given).

Note that in the paper referenced, similar code is used, but the calculations are different depending on what's given.

Results above match that of the two last (relevant) examples in the paper.

Hope this is useful...

  • $\begingroup$ Thanks again for your help. As you pointed out I needed an error trap to prevent erroneous distribution parameters from being used. My Mathematica language programming skills could also be improved. $\endgroup$
    – Steve
    Apr 2, 2014 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.