# Determining sample size with no replacement for a given confidence and reliability when there are x failures observed during sampling?

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 *)

d=Round[(1-r)*pop]
gamma=1-c
Do[{
Label[iterate];
n=n+1;
dist=HypergeometricDistribution[n,d,pop];
cdf[z_]:=CDF[dist,z];
cdfvalue=N[cdf[x]];
If[cdfvalue>gamma,Goto[iterate],Print["Solution:"]];
Print["n = ",n];
Print["CDF[x] = ",cdfvalue];
Break[]},
{pop+2}]

• 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. – ciao Apr 1 '14 at 20:29
• Hmm, @rasher, you are certainly correct regarding the requirements of the distribution parameters. I see I've got some "homework" to do here. . . thanks. – Steve Apr 1 '14 at 21:28

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;

defectsSeen] > 1 - confidence, bads++];

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

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

{N[CDF[HypergeometricDistribution[sampleSize,
Round[(1 - reliability)*populationSize], populationSize],

(*
{0.0468071, 50, 110, 500, 0.78}
{0.0933601,37,0,100,0.95}
*)


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...

• 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. – Steve Apr 2 '14 at 12:39