Distribution of Defective Item-Infinite

 /****************************************************************/
 /*          S A S   S A M P L E   L I B R A R Y                 */
 /*                                                              */
 /*    NAME: IEDISTZ2                                            */
 /*   TITLE: Distribution of Defective Item-Infinite             */
 /* PRODUCT: QC                                                  */
 /*  SYSTEM: ALL                                                 */
 /*    KEYS: Inspection Sampling,                                */
 /*   PROCS: TABULATE                                            */
 /*    DATA:                                                     */
 /*                                                              */
 /*    MISC:                                                     */
 /*                                                              */
 /*   NOTES: This program tabulates the distribution of Z, the   */
 /*          apparent number of nonconforming (defective) items  */
 /*          in a sample drawn from a single lot under an imper- */
 /*          imperfect inspection model where the lot size       */
 /*          approaches infinity.                                */
 /*                                                              */
 /*          Notation:                                           */
 /*                                                              */
 /*          zprob   = Pr[ Z = z ]                               */
 /*                                                              */
 /*          z       = apparent number of nonconforming items    */
 /*          omega   = proportion of nonconforming items in lot  */
 /*          nsample = sample size                               */
 /*          p       = Pr[ nonconforming item is classified      */
 /*                        as nonconforming ]                    */
 /*          pprime  = Pr[ conforming item is classified as      */
 /*                        nonconforming ]                       */
 /*                                                              */
 /*                                                              */
 /*                                                              */
 /*     REF: Johnson, N. L., Kotz, S., and Rodriguez, R. N.      */
 /*          (1985), Statistical Effects of Imperfect Inspection */
 /*          Sampling:  I. Some Basic Distributions, Journal of  */
 /*          Quality Technology 17, 1-31.  See Table 2.          */
 /*                                                              */
 /*          Johnson, N. L., Kotz, S., and Wu, X. (1991).        */
 /*          Inspection Errors for Attributes in Quality         */
 /*          Control.  London:  Chapman & Hall.  See Chapter 2.  */
 /*                                                              */
 /****************************************************************/


data table;

   keep omega nsample p pprime z zprob;

   label omega   = 'omega'
         nsample = 'n (sample)'
         p       = 'p'
         pprime  = 'p'''
         z       = 'z'
         zprob   = 'Pr[ Z = z ]' ;

   format zprob 6.4 ;

   /*---set main parameters---*/
   omega   = 0.05 ;
   nsample = 5;

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

   /*---loop over range of p values---*/
   do p = 0.75 to 1.00 by 0.05 ;

      /*---loop over range of pprime values---*/
      do pprime = 0.0 to 0.10 by 0.025 ;

         /*---binomial parameter---*/
         p_ = omega * p + ( 1 - omega ) * pprime ;

         /*---determine Pr[ Z = z ]---*/
         do z = 0 to nsample by 1 ;

            /*---compute binomial probability---*/
            n_ = nsample;
            k_ = z;
            link binomial;
            zprob = binprob;

            /*---output Pr[ Z = z ] ---*/
            output;

            end;  /* finish loop over z */

         end;  /* finish loop over pprime */

      end;  /* finish loop over p */

   /*---finish main program---*/
   return;

   /*---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;

run;


proc sort data=table;
 by omega nsample p;

proc tabulate data=table noseps;
   by omega nsample p;
   class z pprime;
   var zprob;
   table z, pprime*zprob=' '*sum=' '*f=8.4 / rts=5;
run;