Sub topic 9
p charts: proportion of defective items
This chapter assumes you can compute a mean and a standard deviation, and that you know the binomial idea: each inspected item either passes or fails. It also assumes you have read the earlier chapters on common-cause and special-cause variation and on how to read a control chart. The code uses Python and NumPy.
What a p chart does
A p chart tracks the proportion of items that are defective in each sample, plotted in time order. It is the standard Shewhart chart for attribute data when each inspected item gets a pass/fail verdict.
Typical uses:
- The share of invoices with an error each day.
- The share of solder joints that fail inspection each shift.
- The share of orders shipped late each week.
Two terms need to be kept apart:
- A defective item is one that fails as a whole. It is counted once, however many things are wrong with it.
- A defect is a single flaw. One item can have several defects.
A p chart counts defective items. If you count flaws and a unit can have several, you need the u chart, which is the next chapter.
The model behind the chart
The p chart assumes that, while the process is stable, each item in a sample is defective with the same probability, and items are independent of each other. Under those assumptions the number of defectives d in a sample of size n follows a binomial distribution.
The proportion p = d / n then has this mean and standard deviation:
- Mean:
p̄, the long-run proportion defective. - Standard deviation:
sqrt(p̄ (1 − p̄) / n).
The standard deviation comes from the binomial model, so you do not estimate it from a moving range or a subgroup range. The chart therefore has one panel, not two. Spread is determined by p̄ and n.
Computing the limits
- Add up all defectives and all items inspected across the baseline period.
- Compute the center line:
p̄ = Σd / Σn. - For each period
i, computeσᵢ = sqrt(p̄ (1 − p̄) / nᵢ). - Set
UCLᵢ = p̄ + 3σᵢandLCLᵢ = max(0, p̄ − 3σᵢ).
Note that p̄ is a weighted average, Σd / Σn. It is not the simple average of the per-period proportions. The two agree only when every sample has the same size.
Worked example with varying sample sizes
Eight periods of inspection data:
| Period | Inspected (n) | Defective (d) | p = d / n |
|---|---|---|---|
| 1 | 100 | 4 | 0.040 |
| 2 | 120 | 6 | 0.050 |
| 3 | 90 | 3 | 0.033 |
| 4 | 150 | 8 | 0.053 |
| 5 | 110 | 5 | 0.045 |
| 6 | 130 | 7 | 0.054 |
| 7 | 100 | 4 | 0.040 |
| 8 | 125 | 6 | 0.048 |
The totals are 43 defectives out of 925 items, so p̄ = 43 / 925 ≈ 0.0465.
For a period with n = 100:
σ = sqrt(0.0465 × 0.9535 / 100) ≈ 0.0211UCL ≈ 0.0465 + 0.0632 ≈ 0.110p̄ − 3σ ≈ −0.017, which is below zero, soLCL = 0
For n = 150, σ ≈ 0.0172 and UCL ≈ 0.098. A larger sample gives a smaller σ, so the limits are tighter.
The code computes everything at once:
import numpy as np
d = np.array([4, 6, 3, 8, 5, 7, 4, 6])
n = np.array([100, 120, 90, 150, 110, 130, 100, 125])
p = d / n
p_bar = d.sum() / n.sum() # pooled center line, not p.mean()
sigma_p = np.sqrt(p_bar * (1 - p_bar) / n)
p_ucl = p_bar + 3 * sigma_p
p_lcl = np.maximum(0.0, p_bar - 3 * sigma_p)
print("center line:", round(p_bar, 4))
for i in range(len(d)):
status = "OUT" if (p[i] > p_ucl[i] or p[i] < p_lcl[i]) else "ok"
print(f"period {i + 1}: p={p[i]:.3f} "
f"LCL={p_lcl[i]:.3f} UCL={p_ucl[i]:.3f} {status}")All eight points fall inside their limits, so there is no evidence of special-cause variation in this baseline period.
The limits are a different value for every sample size, so the plotted limit lines form a step pattern. This is correct and expected. Do not replace them with one average limit just to make the chart look tidier, unless the sample sizes are close to each other (see the next section).
Constant sample size and the np chart
When n is the same in every period, the limits become two flat lines, and the formulas simplify to p̄ ± 3 sqrt(p̄(1 − p̄)/n). In that case you can also plot the raw count d instead of the proportion. That variant is called an np chart:
- Center line:
n p̄ - Limits:
n p̄ ± 3 sqrt(n p̄ (1 − p̄))
The np chart carries the same information. Some teams prefer it because operators find counts easier to read than proportions.
If the sample sizes vary only a little, some practitioners use one set of limits based on the average n. A common guideline is to do this only when every nᵢ is within about 25% of the average, and to compute individual limits for any point that sits close to a limit. Computing per-period limits always works and costs nothing in code, so it is the safer default.
Sample size and the zero lower limit
The 3-sigma formula relies on a normal approximation to the binomial. The approximation is poor when n p̄ is small. A usual rule of thumb is to make n large enough that n p̄ is at least about 5. With p̄ ≈ 0.0465, that means n of about 108 or more. In the example above, periods 1, 3, 5 and 7 are slightly under that guideline (for instance, 90 × 0.0465 ≈ 4.2).
When n p̄ is small, two problems appear:
- The lower limit is zero, so the chart cannot signal an improvement. A drop in the defect rate looks like ordinary noise because there is no limit below it.
- The distribution of
pis skewed, so false alarms above the upper limit are not as rare as the 3-sigma label suggests.
If the lower limit is zero and you want the chart to detect improvement, increase the sample size. You can also look at runs below the center line (see the signal rules chapter), because a run does not need a lower limit.
Interpreting signals
A point above the UCL means the proportion defective is higher than common-cause variation explains. Look for what changed at that time: a new supplier, a different shift, a setting change, a new inspector. Runs of eight points on one side of the center line also signal a shift, as they do on other charts. This code applies the rule to the p chart's center line:
import numpy as np
def run_of_eight(x, center):
"""Indices where 8 consecutive points have all been on the same side of the center line."""
side = np.sign(np.asarray(x) - center)
flagged = []
for i in range(len(side) - 7):
window = side[i:i + 8]
if np.all(window == 1) or np.all(window == -1):
flagged.append(i + 7)
return flagged
p = np.array([0.04, 0.05, 0.033, 0.053, 0.045, 0.054, 0.04, 0.048,
0.06, 0.058, 0.061, 0.055, 0.057, 0.062, 0.059, 0.060])
print(run_of_eight(p, 0.0465))The first eight values are mixed around the center line. The last eight are all above 0.0465, so the function flags index 15, the last point. Only the end of a full run is flagged, not each point inside it.
One caution about run rules with varying n: the center line is flat, so run rules apply cleanly. Rules that compare points to 1-sigma or 2-sigma zones are harder, because sigma changes from point to point. If you use them, compute each point's z-score as (pᵢ − p̄) / σᵢ and apply the rule to z.
Overdispersion: when the limits are too tight
The binomial model says the variance of p is p̄(1 − p̄)/n. Real data often vary more than that, because the true defect probability drifts slowly or differs between batches, machines or operators. This extra variation is called overdispersion.
It is most common when n is large (thousands per period). Then the binomial σ becomes very small, and ordinary batch-to-batch variation looks like a signal. The chart shows many points outside the limits, even though the process is not obviously changing.
Signs of overdispersion:
- Many points beyond the limits in a baseline period that you otherwise believe is stable.
- Out-of-limit points that appear on both sides of the center line, with no matching cause when you investigate.
- The problem gets worse as
ngrows.
Common remedies:
- A Laney p′ chart, which scales the limits by an estimate of the between-period variation taken from a moving range of the z-scores.
- Treating the proportions as individual measurements on an I-MR chart. This works well when
nis large and fairly constant.
Do not widen the limits by hand or ignore the signals. Decide whether the extra variation is a normal part of the process (then use a method that models it) or a set of real causes you should remove.
Common errors
- Counting flaws, not defective items. If one item can have three defects, the data are defects per unit, so use a u chart.
- Using a different definition of "defective" across periods. If inspectors change the criterion, the chart will show a step that reflects the definition, not the process.
- Treating a high-volume p chart as exact. See overdispersion above.
- Plotting specification-based pass rates as limits. The control limits come from the data. They are not a target or a tolerance.
- Mixing lots with different histories in one sample. A sample should be drawn from one stable stream, in the sense of the rational subgroups chapter.
- Recalculating limits after every signal. Fix baseline limits from a stable period, then monitor against them.
Exercise
A process has a baseline pooled proportion defective of p̄ = 0.03. This week the inspectors checked n = 200 items and found 16 defective. Compute the 3-sigma limits for this week and decide whether this point is a signal.
Show answer
σ = sqrt(0.03 × 0.97 / 200) = sqrt(0.0001455) ≈ 0.0121. UCL = 0.03 + 3 × 0.0121 ≈ 0.066. p̄ − 3σ ≈ −0.006, so LCL = 0. The week's proportion is 16 / 200 = 0.08, which is above the UCL of 0.066. This is a signal of special-cause variation, so look for what changed that week.
Exercise
A team inspects 5,000 items per day and counts defectives. Their p chart shows 9 of 25 baseline days outside the limits, on both sides of the center line, and a search finds no assignable cause for any of them. What is the most likely explanation, and what are two reasonable next steps?
Show answer
The most likely explanation is overdispersion. With n = 5,000 the binomial standard deviation is very small, so ordinary day-to-day variation in the defect rate exceeds the limits. Two reasonable steps are to switch to a Laney p′ chart, which scales the limits by the observed between-day variation, or to plot the daily proportions on an I-MR chart. Widening the limits by hand is not a reasonable step.
Exercise
Why is the lower control limit often zero on a p chart, and what is the practical consequence?
Show answer
When n × p̄ is small, p̄ − 3σ falls below zero, and a proportion cannot be negative, so the limit is set to zero. The consequence is that the chart cannot signal an improvement with a single point. To detect improvement, increase the sample size or use run rules below the center line.