## Acceptance Probabilities for Link Sampling

``` /****************************************************************/
/*          S A S   S A M P L E   L I B R A R Y                 */
/*                                                              */
/*   TITLE: Acceptance Probabilities for Link Sampling          */
/* PRODUCT: QC                                                  */
/*  SYSTEM: ALL                                                 */
/*    KEYS: Inspection Sampling,                                */
/*   PROCS: TABULATE                                            */
/*    DATA:                                                     */
/*                                                              */
/*    MISC:                                                     */
/*                                                              */
/*   NOTES: This program tabulates the acceptance probability   */
/*          for link sampling from a multiple lots under an     */
/*          imperfect inspection model.                         */
/*                                                              */
/*          Notation:                                           */
/*                                                              */
/*          nlot    = size of lot                               */
/*          d1      = number of nonconforming items in lot i-1  */
/*          d2      = number of nonconforming items in lot i    */
/*          d3      = number of nonconforming items in lot i+1  */
/*                                                              */
/*          z1      = number of apparent defectives in sample   */
/*                    from (i-1)st lot                          */
/*          z2      = number of apparent defectives in sample   */
/*                    from ith lot                              */
/*          z3      = number of apparent defectives in sample   */
/*                    from (i+1)st lot                          */
/*                                                              */
/*          nsample = sample size                               */
/*                                                              */
/*          c1      = acceptance number                         */
/*          c2      = rejection  number                         */
/*          c2p     = acceptance number                         */
/*                                                              */
/*          p       = Pr[ nonconforming item is classified      */
/*                        as nonconforming ]                    */
/*          pprime  = Pr[ conforming item is classified as      */
/*                        nonconforming ]                       */
/*                                                              */
/*          accprob = Pr[ acceptance ]                          */
/*                                                              */
/*                                                              */
/*          General Procedure:                                  */
/*                                                              */
/*          Take a random sample of size n from the ith lot.    */
/*          Record the apparent number Zi of defective items.   */
/*                                                              */
/*          (a) If Zi <= c1 then accept.                        */
/*                                                              */
/*          (b) If Zi > c1 then reject.                         */
/*                                                              */
/*          (c) If c1 < Zi <= c2, then if                       */
/*                                                              */
/*              Zi-1 + Zi + Zi+1 <= c2p,                        */
/*                                                              */
/*              then accept.                                    */
/*                                                              */
/*          (d) If c1 < Zi <= c2, then if                       */
/*                                                              */
/*              Zi-1 + Zi + Zi+1 > c2p,                         */
/*                                                              */
/*              then reject.                                    */
/*                                                              */
/*          Note that in the program, Zi-1 is denoted by z1,    */
/*          Zi by z2, and Zi+1 by z3.                           */
/*                                                              */
/*                                                              */
/*     REF: Johnson, N. L., Kotz, S., and Rodriguez, R. N.      */
/*          (1986), Statistical Effects of Imperfect Inspection */
/*          Sampling:  II. Double Sampling and Link Sampling,   */
/*          Journal of Quality Technology 18, 116-138.          */
/*          See Table 4.                                        */
/*                                                              */
/*          Johnson, N. L., Kotz, S., and Wu, X. (1991).        */
/*          Inspection Errors for Attributes in Quality         */
/*          Control.  London:  Chapman & Hall.  See Chapter 4.  */
/*                                                              */
/****************************************************************/

data table;

keep nlot d1 d2 d3 nsample c1 c2 c2p p pprime accprob;

label nlot    = 'N (lot)'
d1      = 'D(i-1)'
d2      = 'D(i)'
d3      = 'D(i+1)'
nsample = 'n'
c1      = 'c1'
c2      = 'c2'
c2p     = 'c2'''
p       = 'p'
pprime  = 'p'''
accprob = 'Pr[ Accept ]';

format zprob   6.4
accprob 6.4 ;

/*---set main parameters---*/
nlot    = 100;
nsample = 20;
d1      = 15;
d2      = 10;
d3      = 15;
c1      = 1;
c2      = 5;
c2p     = 5;

/*---loop over p values---*/
do p = 0.75, 0.90, 0.95, 0.98, 1.00;

/*---loop over pprime values---*/
do pprime = 0.0, 0.01, 0.02, 0.05, 0.10;

/*---compute term1---*/

/*---computer term2---*/

accprob = term1 + term2;

output;

end;  /* finish loop over pprime values */

end;  /* finish loop over p values */

return;  /* finish main program */

/*------------------------------------------------------------*/
/*                                                            */
/* This module computes the probability Pr[ Z2 <= c1 ]        */
/*                                                            */
/* The following serve as input parameters:                   */
/*                                                            */
/*    c1      = acceptance value for first sample             */
/*    nlot    = lot size                                      */
/*    d       = number of defectives in lot                   */
/*    n1      = first sample size                             */
/*    p       = Pr[ correctly classifying a defective item ]  */
/*    pprime  = Pr[ incorrectly classifying a good item ]     */
/*                                                            */
/* The following is returned:                                 */
/*                                                            */
/*    term1   = Pr[ Z2 <= c1 ]                                */
/*                                                            */
/*------------------------------------------------------------*/
first:

term1 = 0.0 ;
do z2 = 0 to c1 by 1;

/* nlot, nsample, p, pprime are globally defined */
z = z2;
d = d2;

term1 = term1 + zprob;

end;

return;  /* finish first */

/*------------------------------------------------------------*/
/*                                                            */
/* This module computes the probability                       */
/*                                                            */
/*    term2   = Pr[ c1 < Z2 <= c2, Z1 + Z2 + Z3 <= c2p ]      */
/*                                                            */
/*                                                            */
/* The following serve as input parameters:                   */
/*                                                            */
/*    c1      = acceptance value                              */
/*    c2      = acceptance value                              */
/*    c2p     = acceptance value                              */
/*    nlot    = lot size                                      */
/*    d1      = number of defectives in lot i-1               */
/*    d2      = number of defectives in lot i                 */
/*    d3      = number of defectives in lot i+1               */
/*    nsample = sample size                                   */
/*    p       = Pr[ correctly classifying a defective item ]  */
/*    pprime  = Pr[ incorrectly classifying a good item ]     */
/*                                                            */
/*                                                            */
/*------------------------------------------------------------*/
second:

term2 = 0.0 ;
zimax = max( c1, c2, c2p );

do z1 = 0 to zimax by 1;

do z2 = 0 to zimax by 1;

do z3 = 0 to zimax by 1;

if ( c1 < z2 ) & ( z2 <= c2 ) & ( z1 + z2 + z3 <= c2p )
then do;

/* nlot, nsample, p, pprime are globally defined */
z = z1;
d = d1;
product = zprob;

z = z2;
d = d2;
product = product * zprob;

z = z3;
d = d3;
product = product * zprob;

term2 = term2 + product;
end;

end;

end;

end;

return;  /* finish second */

/*------------------------------------------------------------*/
/*                                                            */
/* This module computes the probability Pr[ Z = z ], where Z  */
/* is the number of items classified as defective.            */
/*                                                            */
/* The following serve as input parameters:                   */
/*                                                            */
/*    z       = number of items classified as defective       */
/*    nlot    = lot size                                      */
/*    d       = number of defectives in lot                   */
/*    nsample = sample size                                   */
/*    p       = Pr[ correctly classifying a defective item ]  */
/*    pprime  = Pr[ incorrectly classifying a good item ]     */
/*                                                            */
/* The following is returned:                                 */
/*                                                            */
/*    zprob   = Pr[ Z = z ]                                   */
/*                                                            */
/*------------------------------------------------------------*/
uncond:

/*---used for roundoff---*/
fuzz = 0.0001 ;

/*---lower and upper limits for y---*/
miny = max( 0, nsample + d - nlot );
maxy = min( nsample, d );

/*---initialize probability to zero---*/
zprob = 0.0 ;

/*---Case I: p = 0 ---*/
if p = 0 then do;

/*---Ia: pprime = 0 ---*/
if pprime = 0 then do;

if z = 0 then zprob = 1 ;

end;  /* finish Ia */

/*---Ib: pprime = 1 ---*/
else if abs( pprime - 1 ) < fuzz then do;

minz = max( 0, nsample - d );
maxz = min( nsample, nlot - d );

if ( minz <= z ) & ( z <= maxz ) then do;

bign_ = nlot;
litn_ = nsample;
d_    = d;
y_    = nsample - z;

zprob = hypprob;

end;

end;  /* finish Ib */

/*---Ic:  0 < pprime < 1 ---*/
else do;

/* Note: minz =  0 */
maxz = nsample - max( 0, nsample + d - nlot );

if ( z <= maxz ) then
do y = miny to maxy by 1;

/*---obtain Pr[ Y = y ]---*/
bign_ = nlot;
litn_ = nsample;
d_    = d;
y_    = y;

/*---obtain Pr[ Z = z | Y = y ]---*/
n_ = nsample - y;
k_ = z;
p_ = pprime;

zprob = zprob + binprob * hypprob ;

end;

end;  /* finish Ic */

end;  /* finish Case I */

/*---Case II:  p = 1 ---*/
else if ( abs( p - 1 ) < fuzz ) then do;

/*---IIa:  pprime = 0 (perfect inspection) ---*/
if pprime = 0 then do;

minz = max( 0, nsample + d - nlot );
maxz = min( nsample, d );

if ( minz <= z ) & ( z <= maxz ) then do;

bign_ = nlot;
litn_ = nsample;
d_    = d;
y_    = z;

zprob = hypprob;

end;

end;  /* finish IIa */

/*---IIb:  pprime = 1 ---*/
else if ( abs( pprime - 1 ) < fuzz ) then do;

if z = nsample then zprob = 1 ;

end;  /* finish IIb */

/*---IIc:  0 < pprime < 1 ---*/
else do;

minz = max( 0, nsample + d - nlot );
maxz = nsample ;

if ( minz <= z ) & ( z <= maxz ) then
do y = miny to maxy by 1;

/*---compute Pr[ Y = y ] ---*/
bign_ = nlot ;
litn_ = nsample ;
d_    = d;
y_    = y;

/*---obtain Pr[ Z = z | Y = y ]---*/
p_ = pprime;
k_ = z - y;
n_ = nsample - y;

zprob = zprob + hypprob * binprob;

end;

end;  /* finish IIb */

end;  /* finish Case II */

/*---Case III:  0 < p < 1---*/
else do;

/*---IIIa:  pprime = 0 ---*/
if pprime = 0 then do;

/* zmin = 0 */
zmax = min( nsample, d );

if z <= zmax then
do y = miny to maxy by 1;

/*---obtain Pr[ Y = y ]---*/
bign_ = nlot ;
litn_ = nsample ;
d_    = d;
y_    = y;

/*---obtain Pr[ Z = z | Y = y ]---*/
p_ = p;
k_ = z;
n_ = y;

/*---increment unconditional probability---*/
zprob = zprob + binprob * hypprob ;

end;

end;  /* finish IIIa */

/*---IIIb:  pprime = 1 ---*/
else if abs( pprime - 1 ) < fuzz then do;

zmin = nsample - min( nsample, d );
/* zmax = nsample */

if z >= zmin then
do y = miny to maxy by 1;

/*---obtain Pr[ Y = y ]---*/
bign_ = nlot;
litn_ = nsample;
d_    = d;
y_    = y;

/*---obtain Pr[ Z = z | Y = y ]---*/
p_ = p;
k_ = z - ( nsample - y );
n_ = y;

/*---increment unconditional probability---*/
zprob = zprob + binprob * hypprob ;

end;

end;  /* finish Case IIIb */

/*---IIIc:  0 < pprime < 1 ---*/
else
do y = miny to maxy by 1;

/*---obtain Pr[ Y = y ]---*/
bign_ = nlot;
litn_ = nsample;
d_    = d;
y_    = y;

/*---obtain Pr[ Z = z | Y = y ]---*/
condprob = 0.0 ;
minx     = max( 0, y + z - nsample );
maxx     = min( y, z );

do x = minx to maxx by 1;

p_ = p;
k_ = x;
n_ = y;
factor1 = binprob;

p_ = pprime;
k_ = z - x;
n_ = nsample - y;
factor2 = binprob;

condprob = condprob + factor1 * factor2;

end;

/*---increment unconditional probability---*/
zprob = zprob + condprob * hypprob ;

end;  /* finish IIIc */

end;  /* finish Case III */

return;  /* finish uncond */

/*---Compute Binomial Probability---*/
binomial:

binprob=0.0;

if n_ = 0 then do;

if k_ = 0 then binprob = 1.0 ;

end;

else
if n_ > 0 then do;

if ( k_ > 0 ) & ( k_ < n_ ) then
binprob = probbnml( p_, n_, k_ ) -
probbnml( p_, n_, k_-1 );

else
if k_ = n_ then do;
if ( p_> 0.0 ) & ( p_ < 1.0 ) then
binprob = p_**n_;
else if p_ = 1.0 then
binprob = 1.0;
end;

else
if k_ = 0 then do;
if ( p_ > 0.0 ) & ( p_ < 1.0 ) then
binprob = (1.0 - p_)**n_;
else if p_ = 0.0 then
binprob = 1.0;
end;

end;

/*---finish binomial computation---*/
return;

/*---Compute Hypergeometric Probability---*/
hypergmt:

hypprob = 0 ;
minarg  = max( 0, litn_ + d_ - bign_ );
maxarg  = min( litn_, d_ );

if y_ = minarg then

hypprob = probhypr( bign_, d_, litn_, y_ );

else
if ( minarg < y_ ) & ( y_ <= maxarg ) then

hypprob = probhypr( bign_, d_, litn_, y_     ) -
probhypr( bign_, d_, litn_, y_ - 1 );

/*---finish hypergeometric computation---*/
return;

run;

proc sort data=table;
by nlot d1 d2 d3 nsample c1 c2 c2p;

proc tabulate data=table noseps;
by nlot d1 d2 d3 nsample c1 c2 c2p;
class p pprime;
var accprob;
table p, pprime*accprob=' '*sum=' '*f=8.4 / rts=7;
run;

```