In [2]:

```
%matplotlib inline
from matplotlib.pyplot import plot, subplot, title, show, xlabel, ylabel
n = 10 # number of samples
A2 = 0.577
D3 = 0
D4 = 2.115
# number of defectives
x1 = 11.274
x2 = 11.246
x3 = 11.204
x4 = 11.294
x5 = 11.252
x6 = 11.238
x7 = 11.230
x8 = 11.276
x9 = 11.208
x10 = 11.266
r1 = 0.15
r2 = 0.20
r3 = 0.33
r4 = 0.46
r5 = 0.10
r6 = 0.15
r7 = 0.20
r8 = 0.23
r9 = 0.50
r10 = 0.30
x = x1+x2+x3+x4+x5+x6+x7+x8+x9+x10
r = r1+r2+r3+r4+r5+r6+r7+r8+r9+r10
Xavg = x/n
Ravg = r/n
# for X chart
ucl1 = Xavg + A2*Ravg
lcl1 = Xavg - A2*Ravg
# for R chart
ucl2 = D4*Ravg
lcl2 = D3*Ravg
print "control limits \nFor X charts \n UCL = %0.2f cm \n LCL = %0.2f cm\n For R charts \n UCl = %0.3f \n LCL = %0.3f"%(ucl1,lcl1,ucl2,lcl2)
# X chart
x=[1,2,3,4,5,6,7,8,9,10]
y=[11.274,11.246,11.204,11.294,11.252,11.238,11.230,11.276,11.208,11.266]
subplot(211)
plot(x,y)
title("X chart")
xlabel("Sample No.")
ylabel("X")
# R chart
z = [0.15,0.20,0.33,0.46,0.10,0.15,0.20,0.23,0.50,0.30]
subplot(212)
plot(x,z)
title("R chart")
xlabel("Sample no.")
ylabel( "R")
show()
```

In [3]:

```
from __future__ import division
from math import sqrt
%matplotlib inline
from matplotlib.pyplot import plot, subplot, xlabel, ylabel, show, title
from numpy import arange
n = 100 # total number of sub groups
s = 10 # number of samples
# number of defectives
d = [3,2,3,5,3,3,2,4,3,2]
p = sum(d) # total number of defectives
pbar = p/(n*s) # average fraction of defectives
sigmapbar = sqrt(pbar*(1-pbar)/n)
ucl1 = pbar + 3*sigmapbar
lcl1 = pbar - 3*sigmapbar
# percent defective (mean)
pbar = pbar*100
sigmap2 = sqrt(pbar*(100-pbar)/n)
ucl2 = pbar + 3*sigmap2
lcl2 = pbar - 3*sigmap2
print " Control limits \n Fraction defectives \n UCL = %0.3f\n LCL = %0.4f = %d (-ve fraction defective is meaningless)\n Percent defectives \n UCL = %0.1f \n LCL = %0.1f = %d (-ve fraction defective is meaningless)"%( ucl1,lcl1,lcl1,ucl2,lcl2,0)
# control chart for fraction defectives
x = arange(0,10.2,1.111)
y = arange(0,0.081,0.009)
subplot(211)
plot(x, y)
title("Control chart for fraction defectives")
xlabel("Samples")
ylabel("Fraction defectives")
show()
# control chart for percent defect
#z = linspace(0,8.1,10)
z = arange(0.8,9,0.9)
subplot(212)
plot(x,z)
title("Control chart for percent defects")
xlabel("Sample No.")
ylabel("Percent defects")
show()
```

In [4]:

```
from math import exp, factorial
n = 1000 # number of units
s = 4 # random sample
d = 50 # defectives
z = d*s/n
pp0 = exp(-0.2)*1 # poisson probabilities for 0 defectives
pp1 = exp(-0.2)*(z) # poisson probabilities for 1 defectives
pp2 = exp(-0.2)*(z**2/factorial(2)) # poisson probabilities for 2 defectives
pp3 = exp(-0.2)*(z**3/factorial(3))# poisson probabilities for 3 defectives
print "Proabilities for 0,1,2 and 3 defectives are : %0.3f ,%0.4f, %0.4f, %0.5f"%(pp0,pp1,pp2,pp3)
```

In [5]:

```
from __future__ import division
d = 50 # defectives
l = 1000 # lot of pieces
p = d/l # proability of an event happening
q = 1-p # proability of an event not happening
n = 4 # sample size
p0 = q**n #probabilities for 0 defectives
p1 = 4*(q)**3*p # probabilities for 1 defectives
p2 = 6*(q)**2*p**2 # probabilities for 2 defectives
p3 = 4*q*(p)**3 # probabilities for 3 defectives
print " Proabilities for 0,1,2 and 3 defectives are : %0.4f %0.4f %0.4f %0.6f"%(p0,p1,p2,p3)
```

In [6]:

```
from math import ceil, exp
# producer's risk
n = 71 # sample size
AQL = 0.005
LTPD = 0.05
l_s = 500 # lot size
z1 = n*AQL # mean number of defects
pp1 = exp(-z1)+z1*exp(-z1) # poisson proability for 1 or less defective
alpha = (1-pp1)*100 # producer's risk
alpha = ceil(alpha)
# consumer's risk
z2 = n*LTPD # mean number of defects
pp2 = exp(-z2)+z2*exp(-z2) # poisson proability for 1 or less defective
bita = pp2*100 # consumer's risk
print " Producers risk = %d percent\n Consumers risk = %0.2f percent"%( alpha,bita)
```

In [7]:

```
from math import sqrt
td1= 20 # total number of days
n1 = 200 # sample size
# number of defectives
d1 = 10
d2 = 15
d3 = 10
d4 = 12
d5 = 11
d6 = 9
d7 = 22
d8 = 4
d9 = 12
d10 = 24
d11 = 21
d12 = 15
d13 = 8
d14 = 14
d15 = 4
d16 = 10
d17 = 11
d18 = 11
d19 = 26
d20 = 13
d = d1+d2+d3+d4+d5+d6+d7+d8+d9+d10+d11+d12+d13+d14+d15+d16+d17+d18+d19+d20 # total number of defectives
p1 = d/(n1*td1) # average fraction of defectives
sigmap1 = sqrt(p1*(1-p1)/n1)
ucl1 = p1 + 3*sigmap1
lcl1 = p1 - 3*sigmap1
# revised control limits
td2 = 18 # total number of days
D = d - (d10+d19) # number of defects
p2 = D/(n1*td2)
sigmap2 = sqrt(p2*(1-p2)/n1)
ucl2 = p2 + 3*sigmap2
lcl2 = p2 - 3*sigmap2
print " Preliminary control limits \n UCL = %0.3f \n LCL = %0.3f \n Revised control limits \n UCL = %0.3f \n LCL = %0.3f"%(ucl1,lcl1,ucl2,lcl2)
```

In [8]:

```
from math import sqrt, exp
n1 = 15 # total number of sub groups
# number of defectives
d1 = 77
d2 = 64
d3 = 75
d4 = 93
d5 = 45
d6 = 61
d7 = 49
d8 = 65
d9 = 45
d10 = 77
d11 = 59
d12 = 54
d13 = 84
d14 = 40
d15 = 92
d = d1+d2+d3+d4+d5+d6+d7+d8+d9+d10+d11+d12+d13+d14+d15 # total number of defectives
c1 = d/n1
ucl1 = c1 + 3*sqrt(c1)
lcl1 = c1 - 3*sqrt(c1)
# revised control limits
n2 = 12 # total number of sub groups
D = d - (d4+d14+d15) # number of defects
c2 = D/n2
ucl2 = c2 + 3*sqrt(c2)
lcl2 = c2 - 3*sqrt(c2)
print " Preliminary control limits \n UCL = %0.2f \n LCL = %0.2f \n Revised control limits \n UCL = %0.3f \n LCL = %0.3f"%(ucl1,lcl1,ucl2,lcl2)
```

In [9]:

```
n = 20 # number of samples
A = 1.342
A1 = 1.596
A2 = 0.577
d2 = 2.326
d3 = 0.864
D1 = 0
D2 = 4.918
D3 = 0
D4 = 2.115
# number of defectives
x1 = 3290
x2 = 3180
x3 = 3350
x4 = 3470
x5 = 3080
x6 = 3240
x7 = 3260
x8 = 3310
x9 = 3640
x10 = 4110
x11 = 3220
x12 = 3590
x13 = 4270
x14 = 4040
x15 = 3580
x16 = 3500
x17 = 3570
x18 = 3560
x19 = 2740
x20 = 3200
r1 = 560
r2 = 410
r3 = 200
r4 = 300
r5 = 90
r6 = 650
r7 = 890
r8 = 410
r9 = 1120
r10 = 520
r11 = 580
r12 = 670
r13 = 480
r14 = 250
r15 = 170
r16 = 670
r17 = 440
r18 = 660
r19 = 560
r20 = 590
x = x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12+x13+x14+x15+x16+x17+x18+x19+x20
r = r1+r2+r3+r4+r5+r6+r7+r8+r9+r10+r11+r12+r13+r14+r15+r16+r17+r18+r19+r20
Xavg = x/n
Ravg = r/n
# for X chart
ucl1 = Xavg + A2*Ravg
lcl1 = Xavg - A2*Ravg
# for R chart
ucl2 = D4*Ravg
lcl2 = D3*Ravg
# Revised control limits
n1 = 15
n2 = 19
X = (x - (x5+x10+x13+x14+x19))/n1
R = (r - (r9))/n2
# for X chart
ucl3 = X + A2*R
lcl3 = X - A2*R
# for R chart
ucl4 = D4*R
lcl4 = D3*R
print " Preliminary control limits \n For X charts \n UCL = %0.2f \n LCL = %0.2f\n For R charts \n UCl = %0.3f \n LCL = %0.3f \n Revised control limits \n For X chart \n UCL = %0.3f \n LCL = %0.3f\n For R charts \n UCl = %0.3f \n LCL = %0.3f"%(ucl1,lcl1,ucl2,lcl2,ucl3,ucl3,ucl4,lcl4)
# 'Answers vary due to round off error'
```

In [10]:

```
from math import exp
%matplotlib inline
from matplotlib import pyplot as plt
n = 50 # sample size
rn = 2 # rejection number
AQL = 0.02
LTPD = 0.08
# Producer's risk
z1 = n*AQL # mean number of defectives
pp1 = exp(-z1)+z1*exp(-z1) # poisson proability for 1 or less defective
alpha = (1-pp1)*100 # producer's risk
# consumer's risk
z2 = n*LTPD # mean number of defectives
bita = (exp(-z2)+z2*exp(-z2))*100 # consumer's risk
d1 = 1 # incoming defective in percent
z3 = n*d1/100 # average number of defective
ppa1 = exp(-z3)+z3*exp(-z3) # proability of acceptance
ppa1 = ppa1*100
ppr1 = 100-ppa1 # proability of rejection
AOQ1 = ppr1*0 + ppa1*d1/100
d2 = 2 # incoming defective in percent
z4 = n*d2/100 # average number of defective
ppa2 = exp(-z4)+z4*exp(-z4) # proability of acceptance
ppa2 = ppa2*100
ppr2 = 100-ppa2 # proability of rejection
AOQ2 = ppr2*0 + ppa2*d2/100
d3 = 4 # incoming defective in percent
z5 = n*d3/100 # average number of defective
ppa3 = exp(-z5)+z5*exp(-z5) # proability of acceptance
ppa3 = ppa3*100
ppr3 = 100-ppa3 # proability of rejection
AOQ3 = ppr3*0 + ppa3*d3/100
d4 = 6 # incoming defective in percent
z6 = n*d4/100 # average number of defective
ppa4 = exp(-z6)+z6*exp(-z6) # proability of acceptance
ppa4 = ppa4*100
ppr4 = 100-ppa4 # proability of rejection
AOQ4 = ppr4*0 + ppa4*d4/100
d5 = 8 # incoming defective in percent
z7 = n*d5/100 # average number of defective
ppa5 = exp(-z7)+z7*exp(-z7) # proability of acceptance
ppa5 = ppa5*100
ppr5 = 100-ppa5 # proability of rejection
AOQ5 = ppr5*0 + ppa5*d5/100
print " Producers risk = %0.2f percent\n Consumers risk = %0.3f percent"%( alpha,bita)
x = [1,2,4,6,8]
y = [0.91,1.4716,1.624,1.194,0.733]
plt.plot(x,y)
plt.title("AOQ curve")
plt.xlabel("Percent dectives")
plt.ylabel("AOQ of lot")
plt.show()
```