Charts for counts and defects
Choose p, np, c and u charts by what you count, read an improvement as a signal, and widen limits with Laney p′ when samples are large.
What runs in CodeSplash
- Control chart (X̄–R, X̄–S, I–MR, p, np, c, u, Laney p′, CUSUM, EWMA) Verified in Python and R
You will be able to
- Choose the right attribute chart for what you count
- Compute limits for p and c charts
- Explain when a p chart raises too many alarms, and what Laney p′ does
Read first
What are you counting?
ReadPass or fail, or a count of flaws, needs an attribute chartAttribute chartA control chart for counts or pass and fail results, instead of a measurement.:
| You count | Sample size | Chart |
|---|---|---|
| Defective items | Varies | p (proportion) |
| Defective items | Fixed | np (number) |
| Defects on each item | Same area each time | c |
| Defects on each item | Area varies | u (per unit) |
An item with three scratches is one defective, and three defects.
Pick the chart
CheckpointCheck yourself
Each day you inspect every invoice and record how many have errors. The daily number of invoices varies. Which chart fits?
Show the answer
Answer: a
Each invoice is good or bad, and the sample size changes, so plot the proportion. An np chart needs equal samples.
Check yourself
You count paint flaws on car panels of different sizes. Which chart fits?
Show the answer
Answer: c
These are defects, not defective items, and the area varies, so plot flaws per unit of area.
An improvement looks like a signal
PredictPredict first
A machine makes juice cans, and 54 samples of 50 are inspected. Limits come from the first 30, centered on 23.1% defective. Then the machine is adjusted. If the fix worked, what will the later samples show?
Reveal
A long run below the center line. Of 23 signals, most come after the adjustment. They're good news, and signals all the same: the process changed. Now you'd set new limits on the improved process.
See it, and compute it
In the appp chart of Nonconforming: 54 subgroups; 23 signals (Western Electric rules).
- Center
- 0.2313
- σ
- 0.4217
- Subgroups
- 54
- Signals
- 23
Plots
View data
| Center | Chart | Sample | LCL | Rules | UCL | Statistic | Signal |
|---|---|---|---|---|---|---|---|
| 0.2313 | p | 1 | 0.0524 | 0.4102 | 0.2400 | false | |
| 0.2313 | p | 2 | 0.0524 | 0.4102 | 0.3000 | false | |
| 0.2313 | p | 3 | 0.0524 | 0.4102 | 0.1600 | false | |
| 0.2313 | p | 4 | 0.0524 | 0.4102 | 0.2000 | false | |
| 0.2313 | p | 5 | 0.0524 | 0.4102 | 0.0800 | false | |
| 0.2313 | p | 6 | 0.0524 | 0.4102 | 0.1400 | false | |
| 0.2313 | p | 7 | 0.0524 | 0.4102 | 0.3200 | false | |
| 0.2313 | p | 8 | 0.0524 | 0.4102 | 0.1800 | false | |
| 0.2313 | p | 9 | 0.0524 | 0.4102 | 0.2800 | false | |
| 0.2313 | p | 10 | 0.0524 | 0.4102 | 0.2000 | false | |
| 0.2313 | p | 11 | 0.0524 | 0.4102 | 0.1000 | false | |
| 0.2313 | p | 12 | 0.0524 | 0.4102 | 0.1200 | false | |
| 0.2313 | p | 13 | 0.0524 | 0.4102 | 0.3400 | false | |
| 0.2313 | p | 14 | 0.0524 | 0.4102 | 0.2400 | false | |
| 0.2313 | p | 15 | 0.0524 | 1 | 0.4102 | 0.4400 | true |
| 0.2313 | p | 16 | 0.0524 | 0.4102 | 0.1600 | false | |
| 0.2313 | p | 17 | 0.0524 | 0.4102 | 0.2000 | false | |
| 0.2313 | p | 18 | 0.0524 | 0.4102 | 0.1000 | false | |
| 0.2313 | p | 19 | 0.0524 | 0.4102 | 0.2600 | false | |
| 0.2313 | p | 20 | 0.0524 | 0.4102 | 0.2200 | false | |
| 0.2313 | p | 21 | 0.0524 | 0.4102 | 0.4000 | false | |
| 0.2313 | p | 22 | 0.0524 | 2 | 0.4102 | 0.3600 | true |
| 0.2313 | p | 23 | 0.0524 | 1,2 | 0.4102 | 0.4800 | true |
| 0.2313 | p | 24 | 0.0524 | 3 | 0.4102 | 0.3000 | true |
| 0.2313 | p | 25 | 0.0524 | 0.4102 | 0.1800 | false | |
| 0.2313 | p | 26 | 0.0524 | 0.4102 | 0.2400 | false | |
| 0.2313 | p | 27 | 0.0524 | 0.4102 | 0.1400 | false | |
| 0.2313 | p | 28 | 0.0524 | 0.4102 | 0.2600 | false | |
| 0.2313 | p | 29 | 0.0524 | 0.4102 | 0.1800 | false | |
| 0.2313 | p | 30 | 0.0524 | 0.4102 | 0.1200 | false | |
| 0.2313 | p | 31 | 0.0524 | 0.4102 | 0.1800 | false | |
| 0.2313 | p | 32 | 0.0524 | 0.4102 | 0.1200 | false | |
| 0.2313 | p | 33 | 0.0524 | 0.4102 | 0.2400 | false | |
| 0.2313 | p | 34 | 0.0524 | 0.4102 | 0.1000 | false | |
| 0.2313 | p | 35 | 0.0524 | 0.4102 | 0.1200 | false | |
| 0.2313 | p | 36 | 0.0524 | 2,3 | 0.4102 | 0.0800 | true |
| 0.2313 | p | 37 | 0.0524 | 3 | 0.4102 | 0.1200 | true |
| 0.2313 | p | 38 | 0.0524 | 2,3 | 0.4102 | 0.0600 | true |
| 0.2313 | p | 39 | 0.0524 | 3 | 0.4102 | 0.1400 | true |
| 0.2313 | p | 40 | 0.0524 | 3 | 0.4102 | 0.1200 | true |
| 0.2313 | p | 41 | 0.0524 | 1,3,4 | 0.4102 | 0.0400 | true |
| 0.2313 | p | 42 | 0.0524 | 2,3,4 | 0.4102 | 0.0800 | true |
| 0.2313 | p | 43 | 0.0524 | 2,3,4 | 0.4102 | 0.0600 | true |
| 0.2313 | p | 44 | 0.0524 | 3,4 | 0.4102 | 0.1200 | true |
| 0.2313 | p | 45 | 0.0524 | 2,3,4 | 0.4102 | 0.1000 | true |
| 0.2313 | p | 46 | 0.0524 | 2,3,4 | 0.4102 | 0.0800 | true |
| 0.2313 | p | 47 | 0.0524 | 3,4 | 0.4102 | 0.1600 | true |
| 0.2313 | p | 48 | 0.0524 | 2,3,4 | 0.4102 | 0.1000 | true |
| 0.2313 | p | 49 | 0.0524 | 3,4 | 0.4102 | 0.1200 | true |
| 0.2313 | p | 50 | 0.0524 | 3,4 | 0.4102 | 0.1400 | true |
| 0.2313 | p | 51 | 0.0524 | 3,4 | 0.4102 | 0.1000 | true |
| 0.2313 | p | 52 | 0.0524 | 3,4 | 0.4102 | 0.1200 | true |
| 0.2313 | p | 53 | 0.0524 | 2,3,4 | 0.4102 | 0.0600 | true |
| 0.2313 | p | 54 | 0.0524 | 2,3,4 | 0.4102 | 0.1000 | true |
Points and limits
| Subgroup | n | Statistic | Center | LCL | UCL | Rules broken |
|---|---|---|---|---|---|---|
| 1 | 50 | 0.2400 | 0.2313 | 0.0524 | 0.4102 | |
| 2 | 50 | 0.3000 | 0.2313 | 0.0524 | 0.4102 | |
| 3 | 50 | 0.1600 | 0.2313 | 0.0524 | 0.4102 | |
| 4 | 50 | 0.2000 | 0.2313 | 0.0524 | 0.4102 | |
| 5 | 50 | 0.0800 | 0.2313 | 0.0524 | 0.4102 | |
| 6 | 50 | 0.1400 | 0.2313 | 0.0524 | 0.4102 | |
| 7 | 50 | 0.3200 | 0.2313 | 0.0524 | 0.4102 | |
| 8 | 50 | 0.1800 | 0.2313 | 0.0524 | 0.4102 | |
| 9 | 50 | 0.2800 | 0.2313 | 0.0524 | 0.4102 | |
| 10 | 50 | 0.2000 | 0.2313 | 0.0524 | 0.4102 | |
| 11 | 50 | 0.1000 | 0.2313 | 0.0524 | 0.4102 | |
| 12 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | |
| 13 | 50 | 0.3400 | 0.2313 | 0.0524 | 0.4102 | |
| 14 | 50 | 0.2400 | 0.2313 | 0.0524 | 0.4102 | |
| 15 | 50 | 0.4400 | 0.2313 | 0.0524 | 0.4102 | 1 |
| 16 | 50 | 0.1600 | 0.2313 | 0.0524 | 0.4102 | |
| 17 | 50 | 0.2000 | 0.2313 | 0.0524 | 0.4102 | |
| 18 | 50 | 0.1000 | 0.2313 | 0.0524 | 0.4102 | |
| 19 | 50 | 0.2600 | 0.2313 | 0.0524 | 0.4102 | |
| 20 | 50 | 0.2200 | 0.2313 | 0.0524 | 0.4102 | |
| 21 | 50 | 0.4000 | 0.2313 | 0.0524 | 0.4102 | |
| 22 | 50 | 0.3600 | 0.2313 | 0.0524 | 0.4102 | 2 |
| 23 | 50 | 0.4800 | 0.2313 | 0.0524 | 0.4102 | 1, 2 |
| 24 | 50 | 0.3000 | 0.2313 | 0.0524 | 0.4102 | 3 |
| 25 | 50 | 0.1800 | 0.2313 | 0.0524 | 0.4102 | |
| 26 | 50 | 0.2400 | 0.2313 | 0.0524 | 0.4102 | |
| 27 | 50 | 0.1400 | 0.2313 | 0.0524 | 0.4102 | |
| 28 | 50 | 0.2600 | 0.2313 | 0.0524 | 0.4102 | |
| 29 | 50 | 0.1800 | 0.2313 | 0.0524 | 0.4102 | |
| 30 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | |
| 31 | 50 | 0.1800 | 0.2313 | 0.0524 | 0.4102 | |
| 32 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | |
| 33 | 50 | 0.2400 | 0.2313 | 0.0524 | 0.4102 | |
| 34 | 50 | 0.1000 | 0.2313 | 0.0524 | 0.4102 | |
| 35 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | |
| 36 | 50 | 0.0800 | 0.2313 | 0.0524 | 0.4102 | 2, 3 |
| 37 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | 3 |
| 38 | 50 | 0.0600 | 0.2313 | 0.0524 | 0.4102 | 2, 3 |
| 39 | 50 | 0.1400 | 0.2313 | 0.0524 | 0.4102 | 3 |
| 40 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | 3 |
First 40 of 54 rows.
- σ estimated from the first 30 subgroups (phase I).
The exact R script · R R version 4.5.1 (2025-06-13)
# Control chart (X̄–R, X̄–S, I–MR, p, np, c, u, Laney p′, CUSUM, EWMA)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method spc.control_chart 1.0.1 · spec b9febfd9ce75de3f
# Dataset: q1_orangejuice (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_spc_*(): twin of codesplash.quality.spc (qcc 2.7 conventions)
# Classic equivalent: qcc::qcc(qcc.groups(x, sample), type = "xbar"), qcc(type = "R" | "S" | "xbar.one" |
# "p" | "np" | "c" | "u"), qcc::cusum(), qcc::ewma() — same constants and sigma estimators.
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/q1_orangejuice.parquet", col_select = c("Nonconforming", "Size")))
n_input <- nrow(df)
df <- df[stats::complete.cases(df[c("Nonconforming", "Size")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("Nonconforming", "Size")) # infinite values stop the run (registry/errors.yaml)
# %% Subgroups
chart <- "p"
value_col <- "Nonconforming"
subgroup_col <- NULL
size_col <- "Size"
phase1 <- as.integer(30)
nsigmas <- 3
rules <- "western_electric"
x <- as.numeric(df[[value_col]])
labels_raw <- if (!is.null(subgroup_col)) as.character(df[[subgroup_col]]) else NULL
attribute <- chart %in% c("p", "np", "c", "u", "laney_p")
if (attribute) {
sizes <- if (!is.null(size_col)) as.numeric(df[[size_col]]) else NULL
if (!is.null(labels_raw)) {
lv <- unique(labels_raw)
d <- vapply(lv, function(l) sum(x[labels_raw == l]), 0)
n_ <- if (!is.null(sizes)) vapply(lv, function(l) sum(sizes[labels_raw == l]), 0) else NULL
labels <- lv
} else {
d <- x; n_ <- sizes; labels <- as.character(seq_along(x))
}
res <- cs_spc_attributes(unname(d), if (is.null(n_)) NULL else unname(n_), chart, phase1, nsigmas)
} else {
sg <- cs_spc_subgroups(x, labels_raw, NULL)
groups <- sg$groups; labels <- sg$labels
if (length(groups) < 2) { # control limits need two subgroups (registry/errors.yaml)
if (!is.null(subgroup_col)) cs_error("CS_E_TOO_FEW_GROUPS", column = subgroup_col, min = 2, k = length(groups))
cs_check_rows(length(x), 2, "A control chart")
}
if (chart == "cusum") {
res <- cs_spc_cusum(groups, phase1, NULL, 5, 1)
} else if (chart == "ewma") {
res <- cs_spc_ewma(groups, phase1, NULL, 0.2, nsigmas)
} else {
if (chart == "imr" && any(lengths(groups) != 1)) stop("I–MR charts use one value per row; choose X̄–R or X̄–S for subgroups.", call. = FALSE)
res <- cs_spc_variables(groups, chart, phase1, nsigmas)
}
}
m <- length(labels)
# %% Signals
if (chart == "cusum") {
stat <- res$stat
center <- rep(res$center, m)
viol_rules <- lapply(seq_len(m), function(i) if (res$pos[i] > res$h || res$neg[i] < -res$h) 1L else integer())
lcl <- rep(-res$h, m); ucl <- rep(res$h, m)
} else if (chart == "ewma") {
stat <- res$ewma
center <- res$center; lcl <- res$lcl; ucl <- res$ucl
viol_rules <- lapply(seq_len(m), function(i) if (stat[i] > ucl[i] || stat[i] < lcl[i]) 1L else integer())
} else {
stat <- res$stat; center <- res$center; lcl <- res$lcl; ucl <- res$ucl
viol_rules <- cs_spc_rules(stat, center, lcl, ucl, res$sd, rules)
}
violations <- which(lengths(viol_rules) > 0)
sec <- res$secondary
sec_viol <- integer()
if (!is.null(sec)) sec_viol <- which(is.finite(sec$stat) & (sec$stat > sec$ucl | sec$stat < sec$lcl))
# %% Tables and chart
chart_labels <- c(xbar_r = "X̄–R", xbar_s = "X̄–S", imr = "I–MR", p = "p", np = "np", c = "c", u = "u",
laney_p = "Laney p′", cusum = "CUSUM", ewma = "EWMA")
label <- chart_labels[[chart]]
panel <- c(xbar_r = "X̄", xbar_s = "X̄", imr = "Individuals", p = "p", np = "np", c = "c", u = "u", laney_p = "p′",
cusum = "CUSUM", ewma = "EWMA")[[chart]]
rule_text <- vapply(viol_rules, function(r) paste(r, collapse = ","), "")
idx <- seq_len(m)
if (chart == "cusum") {
ch <- data.frame(index = c(idx, idx), value = c(res$pos, res$neg), center = 0, lcl = c(lcl, lcl), ucl = c(ucl, ucl),
violation = c(lengths(viol_rules) > 0, lengths(viol_rules) > 0), rule = c(rule_text, rule_text),
chart = rep(c("C⁺", "C⁻"), each = m), stringsAsFactors = FALSE)
} else {
ch <- data.frame(index = idx, value = stat, center = center, lcl = lcl, ucl = ucl, violation = lengths(viol_rules) > 0,
rule = rule_text, chart = panel, stringsAsFactors = FALSE)
if (!is.null(sec)) ch <- rbind(ch, data.frame(index = idx, value = sec$stat, center = sec$center, lcl = sec$lcl, ucl = sec$ucl,
violation = idx %in% sec_viol, rule = ifelse(idx %in% sec_viol, "1", ""),
chart = sec$label, stringsAsFactors = FALSE))
}
charts <- list(cs_chart(
"control", "control-chart", sprintf("%s chart of %s", label, value_col),
list(index = cs_col(if (!attribute || !is.null(labels_raw)) "Subgroup" else "Sample", ch$index),
value = cs_col(if (chart == "imr") value_col else "Statistic", ch$value),
center = cs_col("Center", ch$center), lcl = cs_col("LCL", ch$lcl), ucl = cs_col("UCL", ch$ucl),
violation = cs_col("Signal", ch$violation, "boolean"), rule = cs_col("Rules", ch$rule, "categorical"),
chart = cs_col("Chart", ch$chart, "categorical"))))
tables <- list(list(
key = "points", title = "Points and limits",
columns = list(list(key = "label", label = "Subgroup", type = "string"), list(key = "n", label = "n", type = "number"),
list(key = "stat", label = "Statistic", type = "number"), list(key = "center", label = "Center", type = "number"),
list(key = "lcl", label = "LCL", type = "number"), list(key = "ucl", label = "UCL", type = "number"),
list(key = "rules", label = "Rules broken", type = "string")),
rows = data.frame(label = labels, n = as.numeric(res$sizes), stat = stat, center = center, lcl = lcl, ucl = ucl,
rules = vapply(viol_rules, function(r) paste(r, collapse = ", "), ""), stringsAsFactors = FALSE)
))
# %% Result
sizes_arr <- as.numeric(res$sizes)
values <- list(
chart = chart, chart_label = label, n_subgroups = m, mean_size = mean(sizes_arr),
center = center[1], sigma = res$sigma,
stat = I(stat), lcl = I(lcl), ucl = I(ucl),
violations = I(violations), violation_rules = I(rule_text[lengths(viol_rules) > 0]), n_signals = length(violations),
sec_stat = if (!is.null(sec)) I(sec$stat) else NULL, sec_center = if (!is.null(sec)) sec$center[1] else NULL,
sec_lcl = if (!is.null(sec)) I(sec$lcl) else NULL, sec_ucl = if (!is.null(sec)) I(sec$ucl) else NULL,
sec_violations = I(sec_viol),
cusum_pos = if (chart == "cusum") I(res$pos) else NULL,
cusum_neg = if (chart == "cusum") I(res$neg) else NULL
)
rule_names <- c(basic = "points beyond the limits", western_electric = "Western Electric rules", nelson = "Nelson rules")
summary <- sprintf("%s chart of %s: %d %s; ", label, value_col, m, if (mean(sizes_arr) != 1) "subgroups" else "points")
summary <- paste0(summary, if (length(violations)) sprintf("%d signal%s (%s).", length(violations), if (length(violations) != 1) "s" else "",
if (chart %in% c("cusum", "ewma")) "beyond the limits" else rule_names[[rules]]) else "no signals.")
notes <- sprintf("σ estimated from %s.", if (phase1 > 0 && phase1 < m) sprintf("the first %d subgroups (phase I)", phase1) else "all subgroups")
if (chart %in% c("xbar_r", "xbar_s", "imr"))
notes <- c(notes, c(xbar_r = "σ = mean(R / d2) (qcc UWAVE-R).", xbar_s = "σ = mean(S / c4) (qcc UWAVE-SD).", imr = "σ = mean moving range / 1.128.")[[chart]])
cs_publish(list(
method = "spc.control_chart",
methodVersion = "1.0.1",
title = sprintf("%s control chart", label),
engine = cs_engine_info("codesplashr", "nanoparquet"),
n = length(x),
summary = summary,
statistics = list(list(key = "center", label = "Center", value = center[1]), list(key = "sigma", label = "σ", value = res$sigma),
list(key = "n_subgroups", label = "Subgroups", value = m, format = "integer"),
list(key = "n_signals", label = "Signals", value = length(violations), format = "integer")),
tables = tables,
charts = charts,
warnings = warnings,
notes = as.list(notes),
values = values
))
p chart of Nonconforming: 54 subgroups; 23 signals (Western Electric rules).
- Center
- 0.2313
- σ
- 0.4217
- Subgroups
- 54
- Signals
- 23
To keep this page light, the plots are shown once, under the R tab.
Points and limits
| Subgroup | n | Statistic | Center | LCL | UCL | Rules broken |
|---|---|---|---|---|---|---|
| 1 | 50 | 0.2400 | 0.2313 | 0.0524 | 0.4102 | |
| 2 | 50 | 0.3000 | 0.2313 | 0.0524 | 0.4102 | |
| 3 | 50 | 0.1600 | 0.2313 | 0.0524 | 0.4102 | |
| 4 | 50 | 0.2000 | 0.2313 | 0.0524 | 0.4102 | |
| 5 | 50 | 0.0800 | 0.2313 | 0.0524 | 0.4102 | |
| 6 | 50 | 0.1400 | 0.2313 | 0.0524 | 0.4102 | |
| 7 | 50 | 0.3200 | 0.2313 | 0.0524 | 0.4102 | |
| 8 | 50 | 0.1800 | 0.2313 | 0.0524 | 0.4102 | |
| 9 | 50 | 0.2800 | 0.2313 | 0.0524 | 0.4102 | |
| 10 | 50 | 0.2000 | 0.2313 | 0.0524 | 0.4102 | |
| 11 | 50 | 0.1000 | 0.2313 | 0.0524 | 0.4102 | |
| 12 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | |
| 13 | 50 | 0.3400 | 0.2313 | 0.0524 | 0.4102 | |
| 14 | 50 | 0.2400 | 0.2313 | 0.0524 | 0.4102 | |
| 15 | 50 | 0.4400 | 0.2313 | 0.0524 | 0.4102 | 1 |
| 16 | 50 | 0.1600 | 0.2313 | 0.0524 | 0.4102 | |
| 17 | 50 | 0.2000 | 0.2313 | 0.0524 | 0.4102 | |
| 18 | 50 | 0.1000 | 0.2313 | 0.0524 | 0.4102 | |
| 19 | 50 | 0.2600 | 0.2313 | 0.0524 | 0.4102 | |
| 20 | 50 | 0.2200 | 0.2313 | 0.0524 | 0.4102 | |
| 21 | 50 | 0.4000 | 0.2313 | 0.0524 | 0.4102 | |
| 22 | 50 | 0.3600 | 0.2313 | 0.0524 | 0.4102 | 2 |
| 23 | 50 | 0.4800 | 0.2313 | 0.0524 | 0.4102 | 1, 2 |
| 24 | 50 | 0.3000 | 0.2313 | 0.0524 | 0.4102 | 3 |
| 25 | 50 | 0.1800 | 0.2313 | 0.0524 | 0.4102 | |
| 26 | 50 | 0.2400 | 0.2313 | 0.0524 | 0.4102 | |
| 27 | 50 | 0.1400 | 0.2313 | 0.0524 | 0.4102 | |
| 28 | 50 | 0.2600 | 0.2313 | 0.0524 | 0.4102 | |
| 29 | 50 | 0.1800 | 0.2313 | 0.0524 | 0.4102 | |
| 30 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | |
| 31 | 50 | 0.1800 | 0.2313 | 0.0524 | 0.4102 | |
| 32 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | |
| 33 | 50 | 0.2400 | 0.2313 | 0.0524 | 0.4102 | |
| 34 | 50 | 0.1000 | 0.2313 | 0.0524 | 0.4102 | |
| 35 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | |
| 36 | 50 | 0.0800 | 0.2313 | 0.0524 | 0.4102 | 2, 3 |
| 37 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | 3 |
| 38 | 50 | 0.0600 | 0.2313 | 0.0524 | 0.4102 | 2, 3 |
| 39 | 50 | 0.1400 | 0.2313 | 0.0524 | 0.4102 | 3 |
| 40 | 50 | 0.1200 | 0.2313 | 0.0524 | 0.4102 | 3 |
First 40 of 54 rows.
- σ estimated from the first 30 subgroups (phase I).
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Control chart (X̄–R, X̄–S, I–MR, p, np, c, u, Laney p′, CUSUM, EWMA)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method spc.control_chart 1.0.1 · spec b9febfd9ce75de3f
# Dataset: q1_orangejuice (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.quality import spc # twin of codesplashr::cs_spc_* (qcc 2.7 conventions)
# Classic equivalent in R: qcc::qcc(data, type = "xbar" | "R" | "S" | "xbar.one" | "p" | "np" | "c" | "u"),
# qcc::cusum(), qcc::ewma(). Python has no maintained SPC library with the same constants.
# %% Data
df = pd.read_parquet("tests/parity/fixtures/q1_orangejuice.parquet", columns=["Nonconforming", "Size"])
n_input = len(df)
df = df.dropna(subset=["Nonconforming", "Size"]) # listwise deletion over the analysis columns
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["Nonconforming", "Size"]) # infinite values stop the run (registry/errors.yaml)
# %% Subgroups
chart = "p"
value_col = "Nonconforming"
subgroup_col = None
size_col = "Size"
phase1 = int(30)
nsigmas = 3
rules = "western_electric"
x = df[value_col].to_numpy(dtype=float)
def _label(v):
return str(int(v)) if isinstance(v, float) and v.is_integer() else str(v)
labels_raw = [_label(v) for v in df[subgroup_col]] if subgroup_col else None
attribute = chart in ("p", "np", "c", "u", "laney_p")
if attribute:
sizes = df[size_col].to_numpy(dtype=float) if size_col else None
if labels_raw is not None:
order = list(dict.fromkeys(labels_raw))
d = np.asarray([x[[i for i, l in enumerate(labels_raw) if l == lv]].sum() for lv in order])
n_ = np.asarray([sizes[[i for i, l in enumerate(labels_raw) if l == lv]].sum() for lv in order]) if sizes is not None else None
labels = order
else:
d, n_, labels = x, sizes, [str(i + 1) for i in range(len(x))]
res = spc.attributes_chart(d, n_, chart, phase1, nsigmas)
else:
groups, labels = spc.subgroups(x, labels_raw, None)
if len(groups) < 2: # control limits need two subgroups (registry/errors.yaml)
if subgroup_col:
cs.cs_error("CS_E_TOO_FEW_GROUPS", column=subgroup_col, min=2, k=len(groups))
cs.check_rows(len(x), 2, "A control chart")
if chart == "cusum":
res = spc.cusum(groups, phase1, None, 5, 1)
elif chart == "ewma":
res = spc.ewma(groups, phase1, None, 0.2, nsigmas)
else:
if chart == "imr" and any(len(g) != 1 for g in groups):
raise ValueError("I–MR charts use one value per row; choose X̄–R or X̄–S for subgroups.")
res = spc.variables_chart(groups, chart, phase1, nsigmas)
m = len(labels)
# %% Signals
if chart == "cusum":
stat = res["stat"]
center = np.full(m, res["center"])
viol_rules = [[1] if (res["pos"][i] > res["h"] or res["neg"][i] < -res["h"]) else [] for i in range(m)]
lcl = np.full(m, -res["h"])
ucl = np.full(m, res["h"])
elif chart == "ewma":
stat = res["ewma"]
center, lcl, ucl = res["center"], res["lcl"], res["ucl"]
viol_rules = [[1] if (stat[i] > ucl[i] or stat[i] < lcl[i]) else [] for i in range(m)]
else:
stat, center, lcl, ucl = res["stat"], res["center"], res["lcl"], res["ucl"]
viol_rules = spc.run_rules(stat, center, lcl, ucl, res["sd"], rules)
violations = [i + 1 for i in range(m) if viol_rules[i]]
sec = res.get("secondary")
sec_viol = []
if sec:
s_ok = np.isfinite(sec["stat"])
sec_viol = [i + 1 for i in range(m) if s_ok[i] and (sec["stat"][i] > sec["ucl"][i] or sec["stat"][i] < sec["lcl"][i])]
# %% Tables and chart
label = spc.CHART_LABEL[chart]
idx = list(range(1, m + 1))
panel = {"xbar_r": "X̄", "xbar_s": "X̄", "imr": "Individuals", "p": "p", "np": "np", "c": "c", "u": "u", "laney_p": "p′", "cusum": "CUSUM", "ewma": "EWMA"}[chart]
rows_idx, rows_val, rows_c, rows_l, rows_u, rows_v, rows_r, rows_f = [], [], [], [], [], [], [], []
if chart == "cusum":
for name, series in (("C⁺", res["pos"]), ("C⁻", res["neg"])):
rows_idx += idx; rows_val += list(map(float, series)); rows_c += [0.0] * m; rows_l += list(map(float, lcl)); rows_u += list(map(float, ucl))
rows_v += [bool(viol_rules[i]) for i in range(m)]; rows_r += [",".join(map(str, viol_rules[i])) for i in range(m)]; rows_f += [name] * m
else:
rows_idx += idx; rows_val += list(map(float, stat)); rows_c += list(map(float, center)); rows_l += list(map(float, lcl)); rows_u += list(map(float, ucl))
rows_v += [bool(viol_rules[i]) for i in range(m)]; rows_r += [",".join(map(str, viol_rules[i])) for i in range(m)]; rows_f += [panel] * m
if sec:
rows_idx += idx; rows_val += list(map(float, sec["stat"])); rows_c += list(map(float, sec["center"])); rows_l += list(map(float, sec["lcl"]))
rows_u += list(map(float, sec["ucl"])); rows_v += [i + 1 in sec_viol for i in range(m)]; rows_r += ["1" if i + 1 in sec_viol else "" for i in range(m)]
rows_f += [sec["label"]] * m
charts = [cs.chart(
"control", "control-chart", f"{label} chart of {value_col}",
{"index": cs.col("Subgroup" if not attribute or labels_raw else "Sample", rows_idx),
"value": cs.col(value_col if chart in ("imr",) else "Statistic", rows_val),
"center": cs.col("Center", rows_c), "lcl": cs.col("LCL", rows_l), "ucl": cs.col("UCL", rows_u),
"violation": cs.col("Signal", rows_v, "boolean"), "rule": cs.col("Rules", rows_r, "categorical"),
"chart": cs.col("Chart", rows_f, "categorical")},
)]
table_rows = []
for i in range(m):
table_rows.append({"label": labels[i], "n": float(res["sizes"][i]) if res.get("sizes") is not None else None, "stat": float(stat[i]),
"center": float(center[i]), "lcl": float(lcl[i]), "ucl": float(ucl[i]), "rules": ", ".join(map(str, viol_rules[i]))})
tables = [{
"key": "points", "title": "Points and limits",
"columns": [{"key": "label", "label": "Subgroup", "type": "string"}, {"key": "n", "label": "n", "type": "number"},
{"key": "stat", "label": "Statistic", "type": "number"}, {"key": "center", "label": "Center", "type": "number"},
{"key": "lcl", "label": "LCL", "type": "number"}, {"key": "ucl", "label": "UCL", "type": "number"},
{"key": "rules", "label": "Rules broken", "type": "string"}],
"rows": table_rows,
}]
# %% Result
sizes_arr = np.asarray(res["sizes"], dtype=float)
values = {
"chart": chart, "chart_label": label, "n_subgroups": m, "mean_size": float(np.mean(sizes_arr)),
"center": float(center[0]), "sigma": float(res["sigma"]),
"stat": list(map(float, stat)), "lcl": list(map(float, lcl)), "ucl": list(map(float, ucl)),
"violations": violations, "violation_rules": [",".join(map(str, r)) for r in viol_rules if r], "n_signals": len(violations),
"sec_stat": list(map(float, sec["stat"])) if sec else None, "sec_center": float(sec["center"][0]) if sec else None,
"sec_lcl": list(map(float, sec["lcl"])) if sec else None, "sec_ucl": list(map(float, sec["ucl"])) if sec else None,
"sec_violations": sec_viol,
"cusum_pos": list(map(float, res["pos"])) if chart == "cusum" else None,
"cusum_neg": list(map(float, res["neg"])) if chart == "cusum" else None,
}
rule_names = {"basic": "points beyond the limits", "western_electric": "Western Electric rules", "nelson": "Nelson rules"}
summary = f"{label} chart of {value_col}: {m} {'subgroups' if np.mean(sizes_arr) != 1 else 'points'}; "
summary += f"{len(violations)} signal{'s' if len(violations) != 1 else ''} ({rule_names[rules] if chart not in ('cusum', 'ewma') else 'beyond the limits'})." if violations else "no signals."
notes = [f"σ estimated from {'the first ' + str(phase1) + ' subgroups (phase I)' if 0 < phase1 < m else 'all subgroups'}."]
if chart in ("xbar_r", "xbar_s", "imr"):
notes.append({"xbar_r": "σ = mean(R / d2) (qcc UWAVE-R).", "xbar_s": "σ = mean(S / c4) (qcc UWAVE-SD).", "imr": "σ = mean moving range / 1.128."}[chart])
cs.publish({
"title": f"{label} control chart",
"engine": cs.engine_info("numpy", "pandas"),
"n": len(x),
"summary": summary,
"statistics": [
{"key": "center", "label": "Center", "value": float(center[0])},
{"key": "sigma", "label": "σ", "value": float(res["sigma"])},
{"key": "n_subgroups", "label": "Subgroups", "value": m, "format": "integer"},
{"key": "n_signals", "label": "Signals", "value": len(violations), "format": "integer"},
],
"tables": tables,
"charts": charts,
"warnings": warnings,
"notes": notes,
"values": values,
})
The limits come from the binomial distribution:
Check yourself
With p̄ = 0.2313 and n = 50, what is the upper control limit? Give three decimals.
Show the answer
Answer: 0.41
0.2313 + 3 × √(0.2313 × 0.7687 / 50) = 0.2313 + 0.1789 = 0.410.
Too many alarms
ReadBinomial limits assume each sample shares one true rate. Real processes vary between samples too. When samples are large, the limits become very narrow, and ordinary variation between samples looks like a signal. That's overdispersionOverdispersionCounts that vary more than a Poisson model allows; standard errors come out too small..
A Laney p′ chartLaney p′ chartA p chart whose limits are widened to match the variation between samples, for when binomial limits are too tight. measures the extra variation from the z-scores of neighboring samples, then widens the limits to match.
Try the Laney chart
In the appLaney p′ chart of Nonconforming: 54 subgroups; 19 signals (Western Electric rules).
- Center
- 0.1778
- σ
- 0.5275
- Subgroups
- 54
- Signals
- 19
Plots
View data
| Center | Chart | Sample | LCL | Rules | UCL | Statistic | Signal |
|---|---|---|---|---|---|---|---|
| 0.1778 | p′ | 1 | 0 | 0.4016 | 0.2400 | false | |
| 0.1778 | p′ | 2 | 0 | 0.4016 | 0.3000 | false | |
| 0.1778 | p′ | 3 | 0 | 0.4016 | 0.1600 | false | |
| 0.1778 | p′ | 4 | 0 | 0.4016 | 0.2000 | false | |
| 0.1778 | p′ | 5 | 0 | 0.4016 | 0.0800 | false | |
| 0.1778 | p′ | 6 | 0 | 0.4016 | 0.1400 | false | |
| 0.1778 | p′ | 7 | 0 | 0.4016 | 0.3200 | false | |
| 0.1778 | p′ | 8 | 0 | 0.4016 | 0.1800 | false | |
| 0.1778 | p′ | 9 | 0 | 0.4016 | 0.2800 | false | |
| 0.1778 | p′ | 10 | 0 | 0.4016 | 0.2000 | false | |
| 0.1778 | p′ | 11 | 0 | 0.4016 | 0.1000 | false | |
| 0.1778 | p′ | 12 | 0 | 0.4016 | 0.1200 | false | |
| 0.1778 | p′ | 13 | 0 | 0.4016 | 0.3400 | false | |
| 0.1778 | p′ | 14 | 0 | 0.4016 | 0.2400 | false | |
| 0.1778 | p′ | 15 | 0 | 1,2 | 0.4016 | 0.4400 | true |
| 0.1778 | p′ | 16 | 0 | 0.4016 | 0.1600 | false | |
| 0.1778 | p′ | 17 | 0 | 0.4016 | 0.2000 | false | |
| 0.1778 | p′ | 18 | 0 | 0.4016 | 0.1000 | false | |
| 0.1778 | p′ | 19 | 0 | 0.4016 | 0.2600 | false | |
| 0.1778 | p′ | 20 | 0 | 0.4016 | 0.2200 | false | |
| 0.1778 | p′ | 21 | 0 | 0.4016 | 0.4000 | false | |
| 0.1778 | p′ | 22 | 0 | 2 | 0.4016 | 0.3600 | true |
| 0.1778 | p′ | 23 | 0 | 1,2,3 | 0.4016 | 0.4800 | true |
| 0.1778 | p′ | 24 | 0 | 3 | 0.4016 | 0.3000 | true |
| 0.1778 | p′ | 25 | 0 | 0.4016 | 0.1800 | false | |
| 0.1778 | p′ | 26 | 0 | 4 | 0.4016 | 0.2400 | true |
| 0.1778 | p′ | 27 | 0 | 0.4016 | 0.1400 | false | |
| 0.1778 | p′ | 28 | 0 | 0.4016 | 0.2600 | false | |
| 0.1778 | p′ | 29 | 0 | 0.4016 | 0.1800 | false | |
| 0.1778 | p′ | 30 | 0 | 0.4016 | 0.1200 | false | |
| 0.1778 | p′ | 31 | 0 | 0.4016 | 0.1800 | false | |
| 0.1778 | p′ | 32 | 0 | 0.4016 | 0.1200 | false | |
| 0.1778 | p′ | 33 | 0 | 0.4016 | 0.2400 | false | |
| 0.1778 | p′ | 34 | 0 | 0.4016 | 0.1000 | false | |
| 0.1778 | p′ | 35 | 0 | 0.4016 | 0.1200 | false | |
| 0.1778 | p′ | 36 | 0 | 0.4016 | 0.0800 | false | |
| 0.1778 | p′ | 37 | 0 | 0.4016 | 0.1200 | false | |
| 0.1778 | p′ | 38 | 0 | 0.4016 | 0.0600 | false | |
| 0.1778 | p′ | 39 | 0 | 0.4016 | 0.1400 | false | |
| 0.1778 | p′ | 40 | 0 | 0.4016 | 0.1200 | false | |
| 0.1778 | p′ | 41 | 0 | 4 | 0.4016 | 0.0400 | true |
| 0.1778 | p′ | 42 | 0 | 4 | 0.4016 | 0.0800 | true |
| 0.1778 | p′ | 43 | 0 | 4 | 0.4016 | 0.0600 | true |
| 0.1778 | p′ | 44 | 0 | 4 | 0.4016 | 0.1200 | true |
| 0.1778 | p′ | 45 | 0 | 3,4 | 0.4016 | 0.1000 | true |
| 0.1778 | p′ | 46 | 0 | 3,4 | 0.4016 | 0.0800 | true |
| 0.1778 | p′ | 47 | 0 | 4 | 0.4016 | 0.1600 | true |
| 0.1778 | p′ | 48 | 0 | 4 | 0.4016 | 0.1000 | true |
| 0.1778 | p′ | 49 | 0 | 4 | 0.4016 | 0.1200 | true |
| 0.1778 | p′ | 50 | 0 | 4 | 0.4016 | 0.1400 | true |
| 0.1778 | p′ | 51 | 0 | 4 | 0.4016 | 0.1000 | true |
| 0.1778 | p′ | 52 | 0 | 4 | 0.4016 | 0.1200 | true |
| 0.1778 | p′ | 53 | 0 | 4 | 0.4016 | 0.0600 | true |
| 0.1778 | p′ | 54 | 0 | 4 | 0.4016 | 0.1000 | true |
Points and limits
| Subgroup | n | Statistic | Center | LCL | UCL | Rules broken |
|---|---|---|---|---|---|---|
| 1 | 50 | 0.2400 | 0.1778 | 0 | 0.4016 | |
| 2 | 50 | 0.3000 | 0.1778 | 0 | 0.4016 | |
| 3 | 50 | 0.1600 | 0.1778 | 0 | 0.4016 | |
| 4 | 50 | 0.2000 | 0.1778 | 0 | 0.4016 | |
| 5 | 50 | 0.0800 | 0.1778 | 0 | 0.4016 | |
| 6 | 50 | 0.1400 | 0.1778 | 0 | 0.4016 | |
| 7 | 50 | 0.3200 | 0.1778 | 0 | 0.4016 | |
| 8 | 50 | 0.1800 | 0.1778 | 0 | 0.4016 | |
| 9 | 50 | 0.2800 | 0.1778 | 0 | 0.4016 | |
| 10 | 50 | 0.2000 | 0.1778 | 0 | 0.4016 | |
| 11 | 50 | 0.1000 | 0.1778 | 0 | 0.4016 | |
| 12 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 | |
| 13 | 50 | 0.3400 | 0.1778 | 0 | 0.4016 | |
| 14 | 50 | 0.2400 | 0.1778 | 0 | 0.4016 | |
| 15 | 50 | 0.4400 | 0.1778 | 0 | 0.4016 | 1, 2 |
| 16 | 50 | 0.1600 | 0.1778 | 0 | 0.4016 | |
| 17 | 50 | 0.2000 | 0.1778 | 0 | 0.4016 | |
| 18 | 50 | 0.1000 | 0.1778 | 0 | 0.4016 | |
| 19 | 50 | 0.2600 | 0.1778 | 0 | 0.4016 | |
| 20 | 50 | 0.2200 | 0.1778 | 0 | 0.4016 | |
| 21 | 50 | 0.4000 | 0.1778 | 0 | 0.4016 | |
| 22 | 50 | 0.3600 | 0.1778 | 0 | 0.4016 | 2 |
| 23 | 50 | 0.4800 | 0.1778 | 0 | 0.4016 | 1, 2, 3 |
| 24 | 50 | 0.3000 | 0.1778 | 0 | 0.4016 | 3 |
| 25 | 50 | 0.1800 | 0.1778 | 0 | 0.4016 | |
| 26 | 50 | 0.2400 | 0.1778 | 0 | 0.4016 | 4 |
| 27 | 50 | 0.1400 | 0.1778 | 0 | 0.4016 | |
| 28 | 50 | 0.2600 | 0.1778 | 0 | 0.4016 | |
| 29 | 50 | 0.1800 | 0.1778 | 0 | 0.4016 | |
| 30 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 | |
| 31 | 50 | 0.1800 | 0.1778 | 0 | 0.4016 | |
| 32 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 | |
| 33 | 50 | 0.2400 | 0.1778 | 0 | 0.4016 | |
| 34 | 50 | 0.1000 | 0.1778 | 0 | 0.4016 | |
| 35 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 | |
| 36 | 50 | 0.0800 | 0.1778 | 0 | 0.4016 | |
| 37 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 | |
| 38 | 50 | 0.0600 | 0.1778 | 0 | 0.4016 | |
| 39 | 50 | 0.1400 | 0.1778 | 0 | 0.4016 | |
| 40 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 |
First 40 of 54 rows.
- σ estimated from all subgroups.
The exact R script · R R version 4.5.1 (2025-06-13)
# Control chart (X̄–R, X̄–S, I–MR, p, np, c, u, Laney p′, CUSUM, EWMA)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method spc.control_chart 1.0.1 · spec cb7f22d1e83eb02a
# Dataset: q1_orangejuice (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_spc_*(): twin of codesplash.quality.spc (qcc 2.7 conventions)
# Classic equivalent: qcc::qcc(qcc.groups(x, sample), type = "xbar"), qcc(type = "R" | "S" | "xbar.one" |
# "p" | "np" | "c" | "u"), qcc::cusum(), qcc::ewma() — same constants and sigma estimators.
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/q1_orangejuice.parquet", col_select = c("Nonconforming", "Size")))
n_input <- nrow(df)
df <- df[stats::complete.cases(df[c("Nonconforming", "Size")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("Nonconforming", "Size")) # infinite values stop the run (registry/errors.yaml)
# %% Subgroups
chart <- "laney_p"
value_col <- "Nonconforming"
subgroup_col <- NULL
size_col <- "Size"
phase1 <- as.integer(0)
nsigmas <- 3
rules <- "western_electric"
x <- as.numeric(df[[value_col]])
labels_raw <- if (!is.null(subgroup_col)) as.character(df[[subgroup_col]]) else NULL
attribute <- chart %in% c("p", "np", "c", "u", "laney_p")
if (attribute) {
sizes <- if (!is.null(size_col)) as.numeric(df[[size_col]]) else NULL
if (!is.null(labels_raw)) {
lv <- unique(labels_raw)
d <- vapply(lv, function(l) sum(x[labels_raw == l]), 0)
n_ <- if (!is.null(sizes)) vapply(lv, function(l) sum(sizes[labels_raw == l]), 0) else NULL
labels <- lv
} else {
d <- x; n_ <- sizes; labels <- as.character(seq_along(x))
}
res <- cs_spc_attributes(unname(d), if (is.null(n_)) NULL else unname(n_), chart, phase1, nsigmas)
} else {
sg <- cs_spc_subgroups(x, labels_raw, NULL)
groups <- sg$groups; labels <- sg$labels
if (length(groups) < 2) { # control limits need two subgroups (registry/errors.yaml)
if (!is.null(subgroup_col)) cs_error("CS_E_TOO_FEW_GROUPS", column = subgroup_col, min = 2, k = length(groups))
cs_check_rows(length(x), 2, "A control chart")
}
if (chart == "cusum") {
res <- cs_spc_cusum(groups, phase1, NULL, 5, 1)
} else if (chart == "ewma") {
res <- cs_spc_ewma(groups, phase1, NULL, 0.2, nsigmas)
} else {
if (chart == "imr" && any(lengths(groups) != 1)) stop("I–MR charts use one value per row; choose X̄–R or X̄–S for subgroups.", call. = FALSE)
res <- cs_spc_variables(groups, chart, phase1, nsigmas)
}
}
m <- length(labels)
# %% Signals
if (chart == "cusum") {
stat <- res$stat
center <- rep(res$center, m)
viol_rules <- lapply(seq_len(m), function(i) if (res$pos[i] > res$h || res$neg[i] < -res$h) 1L else integer())
lcl <- rep(-res$h, m); ucl <- rep(res$h, m)
} else if (chart == "ewma") {
stat <- res$ewma
center <- res$center; lcl <- res$lcl; ucl <- res$ucl
viol_rules <- lapply(seq_len(m), function(i) if (stat[i] > ucl[i] || stat[i] < lcl[i]) 1L else integer())
} else {
stat <- res$stat; center <- res$center; lcl <- res$lcl; ucl <- res$ucl
viol_rules <- cs_spc_rules(stat, center, lcl, ucl, res$sd, rules)
}
violations <- which(lengths(viol_rules) > 0)
sec <- res$secondary
sec_viol <- integer()
if (!is.null(sec)) sec_viol <- which(is.finite(sec$stat) & (sec$stat > sec$ucl | sec$stat < sec$lcl))
# %% Tables and chart
chart_labels <- c(xbar_r = "X̄–R", xbar_s = "X̄–S", imr = "I–MR", p = "p", np = "np", c = "c", u = "u",
laney_p = "Laney p′", cusum = "CUSUM", ewma = "EWMA")
label <- chart_labels[[chart]]
panel <- c(xbar_r = "X̄", xbar_s = "X̄", imr = "Individuals", p = "p", np = "np", c = "c", u = "u", laney_p = "p′",
cusum = "CUSUM", ewma = "EWMA")[[chart]]
rule_text <- vapply(viol_rules, function(r) paste(r, collapse = ","), "")
idx <- seq_len(m)
if (chart == "cusum") {
ch <- data.frame(index = c(idx, idx), value = c(res$pos, res$neg), center = 0, lcl = c(lcl, lcl), ucl = c(ucl, ucl),
violation = c(lengths(viol_rules) > 0, lengths(viol_rules) > 0), rule = c(rule_text, rule_text),
chart = rep(c("C⁺", "C⁻"), each = m), stringsAsFactors = FALSE)
} else {
ch <- data.frame(index = idx, value = stat, center = center, lcl = lcl, ucl = ucl, violation = lengths(viol_rules) > 0,
rule = rule_text, chart = panel, stringsAsFactors = FALSE)
if (!is.null(sec)) ch <- rbind(ch, data.frame(index = idx, value = sec$stat, center = sec$center, lcl = sec$lcl, ucl = sec$ucl,
violation = idx %in% sec_viol, rule = ifelse(idx %in% sec_viol, "1", ""),
chart = sec$label, stringsAsFactors = FALSE))
}
charts <- list(cs_chart(
"control", "control-chart", sprintf("%s chart of %s", label, value_col),
list(index = cs_col(if (!attribute || !is.null(labels_raw)) "Subgroup" else "Sample", ch$index),
value = cs_col(if (chart == "imr") value_col else "Statistic", ch$value),
center = cs_col("Center", ch$center), lcl = cs_col("LCL", ch$lcl), ucl = cs_col("UCL", ch$ucl),
violation = cs_col("Signal", ch$violation, "boolean"), rule = cs_col("Rules", ch$rule, "categorical"),
chart = cs_col("Chart", ch$chart, "categorical"))))
tables <- list(list(
key = "points", title = "Points and limits",
columns = list(list(key = "label", label = "Subgroup", type = "string"), list(key = "n", label = "n", type = "number"),
list(key = "stat", label = "Statistic", type = "number"), list(key = "center", label = "Center", type = "number"),
list(key = "lcl", label = "LCL", type = "number"), list(key = "ucl", label = "UCL", type = "number"),
list(key = "rules", label = "Rules broken", type = "string")),
rows = data.frame(label = labels, n = as.numeric(res$sizes), stat = stat, center = center, lcl = lcl, ucl = ucl,
rules = vapply(viol_rules, function(r) paste(r, collapse = ", "), ""), stringsAsFactors = FALSE)
))
# %% Result
sizes_arr <- as.numeric(res$sizes)
values <- list(
chart = chart, chart_label = label, n_subgroups = m, mean_size = mean(sizes_arr),
center = center[1], sigma = res$sigma,
stat = I(stat), lcl = I(lcl), ucl = I(ucl),
violations = I(violations), violation_rules = I(rule_text[lengths(viol_rules) > 0]), n_signals = length(violations),
sec_stat = if (!is.null(sec)) I(sec$stat) else NULL, sec_center = if (!is.null(sec)) sec$center[1] else NULL,
sec_lcl = if (!is.null(sec)) I(sec$lcl) else NULL, sec_ucl = if (!is.null(sec)) I(sec$ucl) else NULL,
sec_violations = I(sec_viol),
cusum_pos = if (chart == "cusum") I(res$pos) else NULL,
cusum_neg = if (chart == "cusum") I(res$neg) else NULL
)
rule_names <- c(basic = "points beyond the limits", western_electric = "Western Electric rules", nelson = "Nelson rules")
summary <- sprintf("%s chart of %s: %d %s; ", label, value_col, m, if (mean(sizes_arr) != 1) "subgroups" else "points")
summary <- paste0(summary, if (length(violations)) sprintf("%d signal%s (%s).", length(violations), if (length(violations) != 1) "s" else "",
if (chart %in% c("cusum", "ewma")) "beyond the limits" else rule_names[[rules]]) else "no signals.")
notes <- sprintf("σ estimated from %s.", if (phase1 > 0 && phase1 < m) sprintf("the first %d subgroups (phase I)", phase1) else "all subgroups")
if (chart %in% c("xbar_r", "xbar_s", "imr"))
notes <- c(notes, c(xbar_r = "σ = mean(R / d2) (qcc UWAVE-R).", xbar_s = "σ = mean(S / c4) (qcc UWAVE-SD).", imr = "σ = mean moving range / 1.128.")[[chart]])
cs_publish(list(
method = "spc.control_chart",
methodVersion = "1.0.1",
title = sprintf("%s control chart", label),
engine = cs_engine_info("codesplashr", "nanoparquet"),
n = length(x),
summary = summary,
statistics = list(list(key = "center", label = "Center", value = center[1]), list(key = "sigma", label = "σ", value = res$sigma),
list(key = "n_subgroups", label = "Subgroups", value = m, format = "integer"),
list(key = "n_signals", label = "Signals", value = length(violations), format = "integer")),
tables = tables,
charts = charts,
warnings = warnings,
notes = as.list(notes),
values = values
))
Laney p′ chart of Nonconforming: 54 subgroups; 19 signals (Western Electric rules).
- Center
- 0.1778
- σ
- 0.5275
- Subgroups
- 54
- Signals
- 19
To keep this page light, the plots are shown once, under the R tab.
Points and limits
| Subgroup | n | Statistic | Center | LCL | UCL | Rules broken |
|---|---|---|---|---|---|---|
| 1 | 50 | 0.2400 | 0.1778 | 0 | 0.4016 | |
| 2 | 50 | 0.3000 | 0.1778 | 0 | 0.4016 | |
| 3 | 50 | 0.1600 | 0.1778 | 0 | 0.4016 | |
| 4 | 50 | 0.2000 | 0.1778 | 0 | 0.4016 | |
| 5 | 50 | 0.0800 | 0.1778 | 0 | 0.4016 | |
| 6 | 50 | 0.1400 | 0.1778 | 0 | 0.4016 | |
| 7 | 50 | 0.3200 | 0.1778 | 0 | 0.4016 | |
| 8 | 50 | 0.1800 | 0.1778 | 0 | 0.4016 | |
| 9 | 50 | 0.2800 | 0.1778 | 0 | 0.4016 | |
| 10 | 50 | 0.2000 | 0.1778 | 0 | 0.4016 | |
| 11 | 50 | 0.1000 | 0.1778 | 0 | 0.4016 | |
| 12 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 | |
| 13 | 50 | 0.3400 | 0.1778 | 0 | 0.4016 | |
| 14 | 50 | 0.2400 | 0.1778 | 0 | 0.4016 | |
| 15 | 50 | 0.4400 | 0.1778 | 0 | 0.4016 | 1, 2 |
| 16 | 50 | 0.1600 | 0.1778 | 0 | 0.4016 | |
| 17 | 50 | 0.2000 | 0.1778 | 0 | 0.4016 | |
| 18 | 50 | 0.1000 | 0.1778 | 0 | 0.4016 | |
| 19 | 50 | 0.2600 | 0.1778 | 0 | 0.4016 | |
| 20 | 50 | 0.2200 | 0.1778 | 0 | 0.4016 | |
| 21 | 50 | 0.4000 | 0.1778 | 0 | 0.4016 | |
| 22 | 50 | 0.3600 | 0.1778 | 0 | 0.4016 | 2 |
| 23 | 50 | 0.4800 | 0.1778 | 0 | 0.4016 | 1, 2, 3 |
| 24 | 50 | 0.3000 | 0.1778 | 0 | 0.4016 | 3 |
| 25 | 50 | 0.1800 | 0.1778 | 0 | 0.4016 | |
| 26 | 50 | 0.2400 | 0.1778 | 0 | 0.4016 | 4 |
| 27 | 50 | 0.1400 | 0.1778 | 0 | 0.4016 | |
| 28 | 50 | 0.2600 | 0.1778 | 0 | 0.4016 | |
| 29 | 50 | 0.1800 | 0.1778 | 0 | 0.4016 | |
| 30 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 | |
| 31 | 50 | 0.1800 | 0.1778 | 0 | 0.4016 | |
| 32 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 | |
| 33 | 50 | 0.2400 | 0.1778 | 0 | 0.4016 | |
| 34 | 50 | 0.1000 | 0.1778 | 0 | 0.4016 | |
| 35 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 | |
| 36 | 50 | 0.0800 | 0.1778 | 0 | 0.4016 | |
| 37 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 | |
| 38 | 50 | 0.0600 | 0.1778 | 0 | 0.4016 | |
| 39 | 50 | 0.1400 | 0.1778 | 0 | 0.4016 | |
| 40 | 50 | 0.1200 | 0.1778 | 0 | 0.4016 |
First 40 of 54 rows.
- σ estimated from all subgroups.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Control chart (X̄–R, X̄–S, I–MR, p, np, c, u, Laney p′, CUSUM, EWMA)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method spc.control_chart 1.0.1 · spec cb7f22d1e83eb02a
# Dataset: q1_orangejuice (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.quality import spc # twin of codesplashr::cs_spc_* (qcc 2.7 conventions)
# Classic equivalent in R: qcc::qcc(data, type = "xbar" | "R" | "S" | "xbar.one" | "p" | "np" | "c" | "u"),
# qcc::cusum(), qcc::ewma(). Python has no maintained SPC library with the same constants.
# %% Data
df = pd.read_parquet("tests/parity/fixtures/q1_orangejuice.parquet", columns=["Nonconforming", "Size"])
n_input = len(df)
df = df.dropna(subset=["Nonconforming", "Size"]) # listwise deletion over the analysis columns
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["Nonconforming", "Size"]) # infinite values stop the run (registry/errors.yaml)
# %% Subgroups
chart = "laney_p"
value_col = "Nonconforming"
subgroup_col = None
size_col = "Size"
phase1 = int(0)
nsigmas = 3
rules = "western_electric"
x = df[value_col].to_numpy(dtype=float)
def _label(v):
return str(int(v)) if isinstance(v, float) and v.is_integer() else str(v)
labels_raw = [_label(v) for v in df[subgroup_col]] if subgroup_col else None
attribute = chart in ("p", "np", "c", "u", "laney_p")
if attribute:
sizes = df[size_col].to_numpy(dtype=float) if size_col else None
if labels_raw is not None:
order = list(dict.fromkeys(labels_raw))
d = np.asarray([x[[i for i, l in enumerate(labels_raw) if l == lv]].sum() for lv in order])
n_ = np.asarray([sizes[[i for i, l in enumerate(labels_raw) if l == lv]].sum() for lv in order]) if sizes is not None else None
labels = order
else:
d, n_, labels = x, sizes, [str(i + 1) for i in range(len(x))]
res = spc.attributes_chart(d, n_, chart, phase1, nsigmas)
else:
groups, labels = spc.subgroups(x, labels_raw, None)
if len(groups) < 2: # control limits need two subgroups (registry/errors.yaml)
if subgroup_col:
cs.cs_error("CS_E_TOO_FEW_GROUPS", column=subgroup_col, min=2, k=len(groups))
cs.check_rows(len(x), 2, "A control chart")
if chart == "cusum":
res = spc.cusum(groups, phase1, None, 5, 1)
elif chart == "ewma":
res = spc.ewma(groups, phase1, None, 0.2, nsigmas)
else:
if chart == "imr" and any(len(g) != 1 for g in groups):
raise ValueError("I–MR charts use one value per row; choose X̄–R or X̄–S for subgroups.")
res = spc.variables_chart(groups, chart, phase1, nsigmas)
m = len(labels)
# %% Signals
if chart == "cusum":
stat = res["stat"]
center = np.full(m, res["center"])
viol_rules = [[1] if (res["pos"][i] > res["h"] or res["neg"][i] < -res["h"]) else [] for i in range(m)]
lcl = np.full(m, -res["h"])
ucl = np.full(m, res["h"])
elif chart == "ewma":
stat = res["ewma"]
center, lcl, ucl = res["center"], res["lcl"], res["ucl"]
viol_rules = [[1] if (stat[i] > ucl[i] or stat[i] < lcl[i]) else [] for i in range(m)]
else:
stat, center, lcl, ucl = res["stat"], res["center"], res["lcl"], res["ucl"]
viol_rules = spc.run_rules(stat, center, lcl, ucl, res["sd"], rules)
violations = [i + 1 for i in range(m) if viol_rules[i]]
sec = res.get("secondary")
sec_viol = []
if sec:
s_ok = np.isfinite(sec["stat"])
sec_viol = [i + 1 for i in range(m) if s_ok[i] and (sec["stat"][i] > sec["ucl"][i] or sec["stat"][i] < sec["lcl"][i])]
# %% Tables and chart
label = spc.CHART_LABEL[chart]
idx = list(range(1, m + 1))
panel = {"xbar_r": "X̄", "xbar_s": "X̄", "imr": "Individuals", "p": "p", "np": "np", "c": "c", "u": "u", "laney_p": "p′", "cusum": "CUSUM", "ewma": "EWMA"}[chart]
rows_idx, rows_val, rows_c, rows_l, rows_u, rows_v, rows_r, rows_f = [], [], [], [], [], [], [], []
if chart == "cusum":
for name, series in (("C⁺", res["pos"]), ("C⁻", res["neg"])):
rows_idx += idx; rows_val += list(map(float, series)); rows_c += [0.0] * m; rows_l += list(map(float, lcl)); rows_u += list(map(float, ucl))
rows_v += [bool(viol_rules[i]) for i in range(m)]; rows_r += [",".join(map(str, viol_rules[i])) for i in range(m)]; rows_f += [name] * m
else:
rows_idx += idx; rows_val += list(map(float, stat)); rows_c += list(map(float, center)); rows_l += list(map(float, lcl)); rows_u += list(map(float, ucl))
rows_v += [bool(viol_rules[i]) for i in range(m)]; rows_r += [",".join(map(str, viol_rules[i])) for i in range(m)]; rows_f += [panel] * m
if sec:
rows_idx += idx; rows_val += list(map(float, sec["stat"])); rows_c += list(map(float, sec["center"])); rows_l += list(map(float, sec["lcl"]))
rows_u += list(map(float, sec["ucl"])); rows_v += [i + 1 in sec_viol for i in range(m)]; rows_r += ["1" if i + 1 in sec_viol else "" for i in range(m)]
rows_f += [sec["label"]] * m
charts = [cs.chart(
"control", "control-chart", f"{label} chart of {value_col}",
{"index": cs.col("Subgroup" if not attribute or labels_raw else "Sample", rows_idx),
"value": cs.col(value_col if chart in ("imr",) else "Statistic", rows_val),
"center": cs.col("Center", rows_c), "lcl": cs.col("LCL", rows_l), "ucl": cs.col("UCL", rows_u),
"violation": cs.col("Signal", rows_v, "boolean"), "rule": cs.col("Rules", rows_r, "categorical"),
"chart": cs.col("Chart", rows_f, "categorical")},
)]
table_rows = []
for i in range(m):
table_rows.append({"label": labels[i], "n": float(res["sizes"][i]) if res.get("sizes") is not None else None, "stat": float(stat[i]),
"center": float(center[i]), "lcl": float(lcl[i]), "ucl": float(ucl[i]), "rules": ", ".join(map(str, viol_rules[i]))})
tables = [{
"key": "points", "title": "Points and limits",
"columns": [{"key": "label", "label": "Subgroup", "type": "string"}, {"key": "n", "label": "n", "type": "number"},
{"key": "stat", "label": "Statistic", "type": "number"}, {"key": "center", "label": "Center", "type": "number"},
{"key": "lcl", "label": "LCL", "type": "number"}, {"key": "ucl", "label": "UCL", "type": "number"},
{"key": "rules", "label": "Rules broken", "type": "string"}],
"rows": table_rows,
}]
# %% Result
sizes_arr = np.asarray(res["sizes"], dtype=float)
values = {
"chart": chart, "chart_label": label, "n_subgroups": m, "mean_size": float(np.mean(sizes_arr)),
"center": float(center[0]), "sigma": float(res["sigma"]),
"stat": list(map(float, stat)), "lcl": list(map(float, lcl)), "ucl": list(map(float, ucl)),
"violations": violations, "violation_rules": [",".join(map(str, r)) for r in viol_rules if r], "n_signals": len(violations),
"sec_stat": list(map(float, sec["stat"])) if sec else None, "sec_center": float(sec["center"][0]) if sec else None,
"sec_lcl": list(map(float, sec["lcl"])) if sec else None, "sec_ucl": list(map(float, sec["ucl"])) if sec else None,
"sec_violations": sec_viol,
"cusum_pos": list(map(float, res["pos"])) if chart == "cusum" else None,
"cusum_neg": list(map(float, res["neg"])) if chart == "cusum" else None,
}
rule_names = {"basic": "points beyond the limits", "western_electric": "Western Electric rules", "nelson": "Nelson rules"}
summary = f"{label} chart of {value_col}: {m} {'subgroups' if np.mean(sizes_arr) != 1 else 'points'}; "
summary += f"{len(violations)} signal{'s' if len(violations) != 1 else ''} ({rule_names[rules] if chart not in ('cusum', 'ewma') else 'beyond the limits'})." if violations else "no signals."
notes = [f"σ estimated from {'the first ' + str(phase1) + ' subgroups (phase I)' if 0 < phase1 < m else 'all subgroups'}."]
if chart in ("xbar_r", "xbar_s", "imr"):
notes.append({"xbar_r": "σ = mean(R / d2) (qcc UWAVE-R).", "xbar_s": "σ = mean(S / c4) (qcc UWAVE-SD).", "imr": "σ = mean moving range / 1.128."}[chart])
cs.publish({
"title": f"{label} control chart",
"engine": cs.engine_info("numpy", "pandas"),
"n": len(x),
"summary": summary,
"statistics": [
{"key": "center", "label": "Center", "value": float(center[0])},
{"key": "sigma", "label": "σ", "value": float(res["sigma"])},
{"key": "n_subgroups", "label": "Subgroups", "value": m, "format": "integer"},
{"key": "n_signals", "label": "Signals", "value": len(violations), "format": "integer"},
],
"tables": tables,
"charts": charts,
"warnings": warnings,
"notes": notes,
"values": values,
})
Using all the samples, the center is 0.178, and the card's scaled sigma is 0.528. The plain binomial value is , so the limits are about times wider.
19 points still signal. Wider limits absorb routine variation, and they don't hide a real, sustained change.
Counting defects
In the appCircuit boards are inspected for flaws, in a fixed inspection unit. A c chart assumes Poisson counts, so its standard deviation is the square root of its mean.
c chart of Defects: 46 points; 5 signals (Western Electric rules).
- Center
- 19.174
- σ
- 4.379
- Subgroups
- 46
- Signals
- 5
Plots
View data
| Center | Chart | Sample | LCL | Rules | UCL | Statistic | Signal |
|---|---|---|---|---|---|---|---|
| 19.174 | c | 1 | 6.038 | 32.310 | 21 | false | |
| 19.174 | c | 2 | 6.038 | 32.310 | 24 | false | |
| 19.174 | c | 3 | 6.038 | 32.310 | 16 | false | |
| 19.174 | c | 4 | 6.038 | 32.310 | 12 | false | |
| 19.174 | c | 5 | 6.038 | 32.310 | 15 | false | |
| 19.174 | c | 6 | 6.038 | 1 | 32.310 | 5 | true |
| 19.174 | c | 7 | 6.038 | 32.310 | 28 | false | |
| 19.174 | c | 8 | 6.038 | 32.310 | 20 | false | |
| 19.174 | c | 9 | 6.038 | 2 | 32.310 | 31 | true |
| 19.174 | c | 10 | 6.038 | 32.310 | 25 | false | |
| 19.174 | c | 11 | 6.038 | 32.310 | 20 | false | |
| 19.174 | c | 12 | 6.038 | 32.310 | 24 | false | |
| 19.174 | c | 13 | 6.038 | 32.310 | 16 | false | |
| 19.174 | c | 14 | 6.038 | 32.310 | 19 | false | |
| 19.174 | c | 15 | 6.038 | 32.310 | 10 | false | |
| 19.174 | c | 16 | 6.038 | 32.310 | 17 | false | |
| 19.174 | c | 17 | 6.038 | 32.310 | 13 | false | |
| 19.174 | c | 18 | 6.038 | 32.310 | 22 | false | |
| 19.174 | c | 19 | 6.038 | 32.310 | 18 | false | |
| 19.174 | c | 20 | 6.038 | 1 | 32.310 | 39 | true |
| 19.174 | c | 21 | 6.038 | 2 | 32.310 | 30 | true |
| 19.174 | c | 22 | 6.038 | 32.310 | 24 | false | |
| 19.174 | c | 23 | 6.038 | 32.310 | 16 | false | |
| 19.174 | c | 24 | 6.038 | 32.310 | 19 | false | |
| 19.174 | c | 25 | 6.038 | 32.310 | 17 | false | |
| 19.174 | c | 26 | 6.038 | 32.310 | 15 | false | |
| 19.174 | c | 27 | 6.038 | 32.310 | 16 | false | |
| 19.174 | c | 28 | 6.038 | 32.310 | 18 | false | |
| 19.174 | c | 29 | 6.038 | 32.310 | 12 | false | |
| 19.174 | c | 30 | 6.038 | 4 | 32.310 | 15 | true |
| 19.174 | c | 31 | 6.038 | 32.310 | 24 | false | |
| 19.174 | c | 32 | 6.038 | 32.310 | 21 | false | |
| 19.174 | c | 33 | 6.038 | 32.310 | 28 | false | |
| 19.174 | c | 34 | 6.038 | 32.310 | 20 | false | |
| 19.174 | c | 35 | 6.038 | 32.310 | 25 | false | |
| 19.174 | c | 36 | 6.038 | 32.310 | 19 | false | |
| 19.174 | c | 37 | 6.038 | 32.310 | 18 | false | |
| 19.174 | c | 38 | 6.038 | 32.310 | 21 | false | |
| 19.174 | c | 39 | 6.038 | 32.310 | 16 | false | |
| 19.174 | c | 40 | 6.038 | 32.310 | 22 | false | |
| 19.174 | c | 41 | 6.038 | 32.310 | 19 | false | |
| 19.174 | c | 42 | 6.038 | 32.310 | 12 | false | |
| 19.174 | c | 43 | 6.038 | 32.310 | 14 | false | |
| 19.174 | c | 44 | 6.038 | 32.310 | 9 | false | |
| 19.174 | c | 45 | 6.038 | 32.310 | 16 | false | |
| 19.174 | c | 46 | 6.038 | 32.310 | 21 | false |
Points and limits
| Subgroup | n | Statistic | Center | LCL | UCL | Rules broken |
|---|---|---|---|---|---|---|
| 1 | 1 | 21 | 19.174 | 6.038 | 32.310 | |
| 2 | 1 | 24 | 19.174 | 6.038 | 32.310 | |
| 3 | 1 | 16 | 19.174 | 6.038 | 32.310 | |
| 4 | 1 | 12 | 19.174 | 6.038 | 32.310 | |
| 5 | 1 | 15 | 19.174 | 6.038 | 32.310 | |
| 6 | 1 | 5 | 19.174 | 6.038 | 32.310 | 1 |
| 7 | 1 | 28 | 19.174 | 6.038 | 32.310 | |
| 8 | 1 | 20 | 19.174 | 6.038 | 32.310 | |
| 9 | 1 | 31 | 19.174 | 6.038 | 32.310 | 2 |
| 10 | 1 | 25 | 19.174 | 6.038 | 32.310 | |
| 11 | 1 | 20 | 19.174 | 6.038 | 32.310 | |
| 12 | 1 | 24 | 19.174 | 6.038 | 32.310 | |
| 13 | 1 | 16 | 19.174 | 6.038 | 32.310 | |
| 14 | 1 | 19 | 19.174 | 6.038 | 32.310 | |
| 15 | 1 | 10 | 19.174 | 6.038 | 32.310 | |
| 16 | 1 | 17 | 19.174 | 6.038 | 32.310 | |
| 17 | 1 | 13 | 19.174 | 6.038 | 32.310 | |
| 18 | 1 | 22 | 19.174 | 6.038 | 32.310 | |
| 19 | 1 | 18 | 19.174 | 6.038 | 32.310 | |
| 20 | 1 | 39 | 19.174 | 6.038 | 32.310 | 1 |
| 21 | 1 | 30 | 19.174 | 6.038 | 32.310 | 2 |
| 22 | 1 | 24 | 19.174 | 6.038 | 32.310 | |
| 23 | 1 | 16 | 19.174 | 6.038 | 32.310 | |
| 24 | 1 | 19 | 19.174 | 6.038 | 32.310 | |
| 25 | 1 | 17 | 19.174 | 6.038 | 32.310 | |
| 26 | 1 | 15 | 19.174 | 6.038 | 32.310 | |
| 27 | 1 | 16 | 19.174 | 6.038 | 32.310 | |
| 28 | 1 | 18 | 19.174 | 6.038 | 32.310 | |
| 29 | 1 | 12 | 19.174 | 6.038 | 32.310 | |
| 30 | 1 | 15 | 19.174 | 6.038 | 32.310 | 4 |
| 31 | 1 | 24 | 19.174 | 6.038 | 32.310 | |
| 32 | 1 | 21 | 19.174 | 6.038 | 32.310 | |
| 33 | 1 | 28 | 19.174 | 6.038 | 32.310 | |
| 34 | 1 | 20 | 19.174 | 6.038 | 32.310 | |
| 35 | 1 | 25 | 19.174 | 6.038 | 32.310 | |
| 36 | 1 | 19 | 19.174 | 6.038 | 32.310 | |
| 37 | 1 | 18 | 19.174 | 6.038 | 32.310 | |
| 38 | 1 | 21 | 19.174 | 6.038 | 32.310 | |
| 39 | 1 | 16 | 19.174 | 6.038 | 32.310 | |
| 40 | 1 | 22 | 19.174 | 6.038 | 32.310 |
First 40 of 46 rows.
- σ estimated from all subgroups.
The exact R script · R R version 4.5.1 (2025-06-13)
# Control chart (X̄–R, X̄–S, I–MR, p, np, c, u, Laney p′, CUSUM, EWMA)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method spc.control_chart 1.0.1 · spec f13918f944567f74
# Dataset: q1_circuit (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_spc_*(): twin of codesplash.quality.spc (qcc 2.7 conventions)
# Classic equivalent: qcc::qcc(qcc.groups(x, sample), type = "xbar"), qcc(type = "R" | "S" | "xbar.one" |
# "p" | "np" | "c" | "u"), qcc::cusum(), qcc::ewma() — same constants and sigma estimators.
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/q1_circuit.parquet", col_select = c("Defects")))
n_input <- nrow(df)
df <- df[stats::complete.cases(df[c("Defects")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("Defects")) # infinite values stop the run (registry/errors.yaml)
# %% Subgroups
chart <- "c"
value_col <- "Defects"
subgroup_col <- NULL
size_col <- NULL
phase1 <- as.integer(0)
nsigmas <- 3
rules <- "western_electric"
x <- as.numeric(df[[value_col]])
labels_raw <- if (!is.null(subgroup_col)) as.character(df[[subgroup_col]]) else NULL
attribute <- chart %in% c("p", "np", "c", "u", "laney_p")
if (attribute) {
sizes <- if (!is.null(size_col)) as.numeric(df[[size_col]]) else NULL
if (!is.null(labels_raw)) {
lv <- unique(labels_raw)
d <- vapply(lv, function(l) sum(x[labels_raw == l]), 0)
n_ <- if (!is.null(sizes)) vapply(lv, function(l) sum(sizes[labels_raw == l]), 0) else NULL
labels <- lv
} else {
d <- x; n_ <- sizes; labels <- as.character(seq_along(x))
}
res <- cs_spc_attributes(unname(d), if (is.null(n_)) NULL else unname(n_), chart, phase1, nsigmas)
} else {
sg <- cs_spc_subgroups(x, labels_raw, NULL)
groups <- sg$groups; labels <- sg$labels
if (length(groups) < 2) { # control limits need two subgroups (registry/errors.yaml)
if (!is.null(subgroup_col)) cs_error("CS_E_TOO_FEW_GROUPS", column = subgroup_col, min = 2, k = length(groups))
cs_check_rows(length(x), 2, "A control chart")
}
if (chart == "cusum") {
res <- cs_spc_cusum(groups, phase1, NULL, 5, 1)
} else if (chart == "ewma") {
res <- cs_spc_ewma(groups, phase1, NULL, 0.2, nsigmas)
} else {
if (chart == "imr" && any(lengths(groups) != 1)) stop("I–MR charts use one value per row; choose X̄–R or X̄–S for subgroups.", call. = FALSE)
res <- cs_spc_variables(groups, chart, phase1, nsigmas)
}
}
m <- length(labels)
# %% Signals
if (chart == "cusum") {
stat <- res$stat
center <- rep(res$center, m)
viol_rules <- lapply(seq_len(m), function(i) if (res$pos[i] > res$h || res$neg[i] < -res$h) 1L else integer())
lcl <- rep(-res$h, m); ucl <- rep(res$h, m)
} else if (chart == "ewma") {
stat <- res$ewma
center <- res$center; lcl <- res$lcl; ucl <- res$ucl
viol_rules <- lapply(seq_len(m), function(i) if (stat[i] > ucl[i] || stat[i] < lcl[i]) 1L else integer())
} else {
stat <- res$stat; center <- res$center; lcl <- res$lcl; ucl <- res$ucl
viol_rules <- cs_spc_rules(stat, center, lcl, ucl, res$sd, rules)
}
violations <- which(lengths(viol_rules) > 0)
sec <- res$secondary
sec_viol <- integer()
if (!is.null(sec)) sec_viol <- which(is.finite(sec$stat) & (sec$stat > sec$ucl | sec$stat < sec$lcl))
# %% Tables and chart
chart_labels <- c(xbar_r = "X̄–R", xbar_s = "X̄–S", imr = "I–MR", p = "p", np = "np", c = "c", u = "u",
laney_p = "Laney p′", cusum = "CUSUM", ewma = "EWMA")
label <- chart_labels[[chart]]
panel <- c(xbar_r = "X̄", xbar_s = "X̄", imr = "Individuals", p = "p", np = "np", c = "c", u = "u", laney_p = "p′",
cusum = "CUSUM", ewma = "EWMA")[[chart]]
rule_text <- vapply(viol_rules, function(r) paste(r, collapse = ","), "")
idx <- seq_len(m)
if (chart == "cusum") {
ch <- data.frame(index = c(idx, idx), value = c(res$pos, res$neg), center = 0, lcl = c(lcl, lcl), ucl = c(ucl, ucl),
violation = c(lengths(viol_rules) > 0, lengths(viol_rules) > 0), rule = c(rule_text, rule_text),
chart = rep(c("C⁺", "C⁻"), each = m), stringsAsFactors = FALSE)
} else {
ch <- data.frame(index = idx, value = stat, center = center, lcl = lcl, ucl = ucl, violation = lengths(viol_rules) > 0,
rule = rule_text, chart = panel, stringsAsFactors = FALSE)
if (!is.null(sec)) ch <- rbind(ch, data.frame(index = idx, value = sec$stat, center = sec$center, lcl = sec$lcl, ucl = sec$ucl,
violation = idx %in% sec_viol, rule = ifelse(idx %in% sec_viol, "1", ""),
chart = sec$label, stringsAsFactors = FALSE))
}
charts <- list(cs_chart(
"control", "control-chart", sprintf("%s chart of %s", label, value_col),
list(index = cs_col(if (!attribute || !is.null(labels_raw)) "Subgroup" else "Sample", ch$index),
value = cs_col(if (chart == "imr") value_col else "Statistic", ch$value),
center = cs_col("Center", ch$center), lcl = cs_col("LCL", ch$lcl), ucl = cs_col("UCL", ch$ucl),
violation = cs_col("Signal", ch$violation, "boolean"), rule = cs_col("Rules", ch$rule, "categorical"),
chart = cs_col("Chart", ch$chart, "categorical"))))
tables <- list(list(
key = "points", title = "Points and limits",
columns = list(list(key = "label", label = "Subgroup", type = "string"), list(key = "n", label = "n", type = "number"),
list(key = "stat", label = "Statistic", type = "number"), list(key = "center", label = "Center", type = "number"),
list(key = "lcl", label = "LCL", type = "number"), list(key = "ucl", label = "UCL", type = "number"),
list(key = "rules", label = "Rules broken", type = "string")),
rows = data.frame(label = labels, n = as.numeric(res$sizes), stat = stat, center = center, lcl = lcl, ucl = ucl,
rules = vapply(viol_rules, function(r) paste(r, collapse = ", "), ""), stringsAsFactors = FALSE)
))
# %% Result
sizes_arr <- as.numeric(res$sizes)
values <- list(
chart = chart, chart_label = label, n_subgroups = m, mean_size = mean(sizes_arr),
center = center[1], sigma = res$sigma,
stat = I(stat), lcl = I(lcl), ucl = I(ucl),
violations = I(violations), violation_rules = I(rule_text[lengths(viol_rules) > 0]), n_signals = length(violations),
sec_stat = if (!is.null(sec)) I(sec$stat) else NULL, sec_center = if (!is.null(sec)) sec$center[1] else NULL,
sec_lcl = if (!is.null(sec)) I(sec$lcl) else NULL, sec_ucl = if (!is.null(sec)) I(sec$ucl) else NULL,
sec_violations = I(sec_viol),
cusum_pos = if (chart == "cusum") I(res$pos) else NULL,
cusum_neg = if (chart == "cusum") I(res$neg) else NULL
)
rule_names <- c(basic = "points beyond the limits", western_electric = "Western Electric rules", nelson = "Nelson rules")
summary <- sprintf("%s chart of %s: %d %s; ", label, value_col, m, if (mean(sizes_arr) != 1) "subgroups" else "points")
summary <- paste0(summary, if (length(violations)) sprintf("%d signal%s (%s).", length(violations), if (length(violations) != 1) "s" else "",
if (chart %in% c("cusum", "ewma")) "beyond the limits" else rule_names[[rules]]) else "no signals.")
notes <- sprintf("σ estimated from %s.", if (phase1 > 0 && phase1 < m) sprintf("the first %d subgroups (phase I)", phase1) else "all subgroups")
if (chart %in% c("xbar_r", "xbar_s", "imr"))
notes <- c(notes, c(xbar_r = "σ = mean(R / d2) (qcc UWAVE-R).", xbar_s = "σ = mean(S / c4) (qcc UWAVE-SD).", imr = "σ = mean moving range / 1.128.")[[chart]])
cs_publish(list(
method = "spc.control_chart",
methodVersion = "1.0.1",
title = sprintf("%s control chart", label),
engine = cs_engine_info("codesplashr", "nanoparquet"),
n = length(x),
summary = summary,
statistics = list(list(key = "center", label = "Center", value = center[1]), list(key = "sigma", label = "σ", value = res$sigma),
list(key = "n_subgroups", label = "Subgroups", value = m, format = "integer"),
list(key = "n_signals", label = "Signals", value = length(violations), format = "integer")),
tables = tables,
charts = charts,
warnings = warnings,
notes = as.list(notes),
values = values
))
c chart of Defects: 46 points; 5 signals (Western Electric rules).
- Center
- 19.174
- σ
- 4.379
- Subgroups
- 46
- Signals
- 5
To keep this page light, the plots are shown once, under the R tab.
Points and limits
| Subgroup | n | Statistic | Center | LCL | UCL | Rules broken |
|---|---|---|---|---|---|---|
| 1 | 1 | 21 | 19.174 | 6.038 | 32.310 | |
| 2 | 1 | 24 | 19.174 | 6.038 | 32.310 | |
| 3 | 1 | 16 | 19.174 | 6.038 | 32.310 | |
| 4 | 1 | 12 | 19.174 | 6.038 | 32.310 | |
| 5 | 1 | 15 | 19.174 | 6.038 | 32.310 | |
| 6 | 1 | 5 | 19.174 | 6.038 | 32.310 | 1 |
| 7 | 1 | 28 | 19.174 | 6.038 | 32.310 | |
| 8 | 1 | 20 | 19.174 | 6.038 | 32.310 | |
| 9 | 1 | 31 | 19.174 | 6.038 | 32.310 | 2 |
| 10 | 1 | 25 | 19.174 | 6.038 | 32.310 | |
| 11 | 1 | 20 | 19.174 | 6.038 | 32.310 | |
| 12 | 1 | 24 | 19.174 | 6.038 | 32.310 | |
| 13 | 1 | 16 | 19.174 | 6.038 | 32.310 | |
| 14 | 1 | 19 | 19.174 | 6.038 | 32.310 | |
| 15 | 1 | 10 | 19.174 | 6.038 | 32.310 | |
| 16 | 1 | 17 | 19.174 | 6.038 | 32.310 | |
| 17 | 1 | 13 | 19.174 | 6.038 | 32.310 | |
| 18 | 1 | 22 | 19.174 | 6.038 | 32.310 | |
| 19 | 1 | 18 | 19.174 | 6.038 | 32.310 | |
| 20 | 1 | 39 | 19.174 | 6.038 | 32.310 | 1 |
| 21 | 1 | 30 | 19.174 | 6.038 | 32.310 | 2 |
| 22 | 1 | 24 | 19.174 | 6.038 | 32.310 | |
| 23 | 1 | 16 | 19.174 | 6.038 | 32.310 | |
| 24 | 1 | 19 | 19.174 | 6.038 | 32.310 | |
| 25 | 1 | 17 | 19.174 | 6.038 | 32.310 | |
| 26 | 1 | 15 | 19.174 | 6.038 | 32.310 | |
| 27 | 1 | 16 | 19.174 | 6.038 | 32.310 | |
| 28 | 1 | 18 | 19.174 | 6.038 | 32.310 | |
| 29 | 1 | 12 | 19.174 | 6.038 | 32.310 | |
| 30 | 1 | 15 | 19.174 | 6.038 | 32.310 | 4 |
| 31 | 1 | 24 | 19.174 | 6.038 | 32.310 | |
| 32 | 1 | 21 | 19.174 | 6.038 | 32.310 | |
| 33 | 1 | 28 | 19.174 | 6.038 | 32.310 | |
| 34 | 1 | 20 | 19.174 | 6.038 | 32.310 | |
| 35 | 1 | 25 | 19.174 | 6.038 | 32.310 | |
| 36 | 1 | 19 | 19.174 | 6.038 | 32.310 | |
| 37 | 1 | 18 | 19.174 | 6.038 | 32.310 | |
| 38 | 1 | 21 | 19.174 | 6.038 | 32.310 | |
| 39 | 1 | 16 | 19.174 | 6.038 | 32.310 | |
| 40 | 1 | 22 | 19.174 | 6.038 | 32.310 |
First 40 of 46 rows.
- σ estimated from all subgroups.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Control chart (X̄–R, X̄–S, I–MR, p, np, c, u, Laney p′, CUSUM, EWMA)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method spc.control_chart 1.0.1 · spec f13918f944567f74
# Dataset: q1_circuit (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.quality import spc # twin of codesplashr::cs_spc_* (qcc 2.7 conventions)
# Classic equivalent in R: qcc::qcc(data, type = "xbar" | "R" | "S" | "xbar.one" | "p" | "np" | "c" | "u"),
# qcc::cusum(), qcc::ewma(). Python has no maintained SPC library with the same constants.
# %% Data
df = pd.read_parquet("tests/parity/fixtures/q1_circuit.parquet", columns=["Defects"])
n_input = len(df)
df = df.dropna(subset=["Defects"]) # listwise deletion over the analysis columns
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["Defects"]) # infinite values stop the run (registry/errors.yaml)
# %% Subgroups
chart = "c"
value_col = "Defects"
subgroup_col = None
size_col = None
phase1 = int(0)
nsigmas = 3
rules = "western_electric"
x = df[value_col].to_numpy(dtype=float)
def _label(v):
return str(int(v)) if isinstance(v, float) and v.is_integer() else str(v)
labels_raw = [_label(v) for v in df[subgroup_col]] if subgroup_col else None
attribute = chart in ("p", "np", "c", "u", "laney_p")
if attribute:
sizes = df[size_col].to_numpy(dtype=float) if size_col else None
if labels_raw is not None:
order = list(dict.fromkeys(labels_raw))
d = np.asarray([x[[i for i, l in enumerate(labels_raw) if l == lv]].sum() for lv in order])
n_ = np.asarray([sizes[[i for i, l in enumerate(labels_raw) if l == lv]].sum() for lv in order]) if sizes is not None else None
labels = order
else:
d, n_, labels = x, sizes, [str(i + 1) for i in range(len(x))]
res = spc.attributes_chart(d, n_, chart, phase1, nsigmas)
else:
groups, labels = spc.subgroups(x, labels_raw, None)
if len(groups) < 2: # control limits need two subgroups (registry/errors.yaml)
if subgroup_col:
cs.cs_error("CS_E_TOO_FEW_GROUPS", column=subgroup_col, min=2, k=len(groups))
cs.check_rows(len(x), 2, "A control chart")
if chart == "cusum":
res = spc.cusum(groups, phase1, None, 5, 1)
elif chart == "ewma":
res = spc.ewma(groups, phase1, None, 0.2, nsigmas)
else:
if chart == "imr" and any(len(g) != 1 for g in groups):
raise ValueError("I–MR charts use one value per row; choose X̄–R or X̄–S for subgroups.")
res = spc.variables_chart(groups, chart, phase1, nsigmas)
m = len(labels)
# %% Signals
if chart == "cusum":
stat = res["stat"]
center = np.full(m, res["center"])
viol_rules = [[1] if (res["pos"][i] > res["h"] or res["neg"][i] < -res["h"]) else [] for i in range(m)]
lcl = np.full(m, -res["h"])
ucl = np.full(m, res["h"])
elif chart == "ewma":
stat = res["ewma"]
center, lcl, ucl = res["center"], res["lcl"], res["ucl"]
viol_rules = [[1] if (stat[i] > ucl[i] or stat[i] < lcl[i]) else [] for i in range(m)]
else:
stat, center, lcl, ucl = res["stat"], res["center"], res["lcl"], res["ucl"]
viol_rules = spc.run_rules(stat, center, lcl, ucl, res["sd"], rules)
violations = [i + 1 for i in range(m) if viol_rules[i]]
sec = res.get("secondary")
sec_viol = []
if sec:
s_ok = np.isfinite(sec["stat"])
sec_viol = [i + 1 for i in range(m) if s_ok[i] and (sec["stat"][i] > sec["ucl"][i] or sec["stat"][i] < sec["lcl"][i])]
# %% Tables and chart
label = spc.CHART_LABEL[chart]
idx = list(range(1, m + 1))
panel = {"xbar_r": "X̄", "xbar_s": "X̄", "imr": "Individuals", "p": "p", "np": "np", "c": "c", "u": "u", "laney_p": "p′", "cusum": "CUSUM", "ewma": "EWMA"}[chart]
rows_idx, rows_val, rows_c, rows_l, rows_u, rows_v, rows_r, rows_f = [], [], [], [], [], [], [], []
if chart == "cusum":
for name, series in (("C⁺", res["pos"]), ("C⁻", res["neg"])):
rows_idx += idx; rows_val += list(map(float, series)); rows_c += [0.0] * m; rows_l += list(map(float, lcl)); rows_u += list(map(float, ucl))
rows_v += [bool(viol_rules[i]) for i in range(m)]; rows_r += [",".join(map(str, viol_rules[i])) for i in range(m)]; rows_f += [name] * m
else:
rows_idx += idx; rows_val += list(map(float, stat)); rows_c += list(map(float, center)); rows_l += list(map(float, lcl)); rows_u += list(map(float, ucl))
rows_v += [bool(viol_rules[i]) for i in range(m)]; rows_r += [",".join(map(str, viol_rules[i])) for i in range(m)]; rows_f += [panel] * m
if sec:
rows_idx += idx; rows_val += list(map(float, sec["stat"])); rows_c += list(map(float, sec["center"])); rows_l += list(map(float, sec["lcl"]))
rows_u += list(map(float, sec["ucl"])); rows_v += [i + 1 in sec_viol for i in range(m)]; rows_r += ["1" if i + 1 in sec_viol else "" for i in range(m)]
rows_f += [sec["label"]] * m
charts = [cs.chart(
"control", "control-chart", f"{label} chart of {value_col}",
{"index": cs.col("Subgroup" if not attribute or labels_raw else "Sample", rows_idx),
"value": cs.col(value_col if chart in ("imr",) else "Statistic", rows_val),
"center": cs.col("Center", rows_c), "lcl": cs.col("LCL", rows_l), "ucl": cs.col("UCL", rows_u),
"violation": cs.col("Signal", rows_v, "boolean"), "rule": cs.col("Rules", rows_r, "categorical"),
"chart": cs.col("Chart", rows_f, "categorical")},
)]
table_rows = []
for i in range(m):
table_rows.append({"label": labels[i], "n": float(res["sizes"][i]) if res.get("sizes") is not None else None, "stat": float(stat[i]),
"center": float(center[i]), "lcl": float(lcl[i]), "ucl": float(ucl[i]), "rules": ", ".join(map(str, viol_rules[i]))})
tables = [{
"key": "points", "title": "Points and limits",
"columns": [{"key": "label", "label": "Subgroup", "type": "string"}, {"key": "n", "label": "n", "type": "number"},
{"key": "stat", "label": "Statistic", "type": "number"}, {"key": "center", "label": "Center", "type": "number"},
{"key": "lcl", "label": "LCL", "type": "number"}, {"key": "ucl", "label": "UCL", "type": "number"},
{"key": "rules", "label": "Rules broken", "type": "string"}],
"rows": table_rows,
}]
# %% Result
sizes_arr = np.asarray(res["sizes"], dtype=float)
values = {
"chart": chart, "chart_label": label, "n_subgroups": m, "mean_size": float(np.mean(sizes_arr)),
"center": float(center[0]), "sigma": float(res["sigma"]),
"stat": list(map(float, stat)), "lcl": list(map(float, lcl)), "ucl": list(map(float, ucl)),
"violations": violations, "violation_rules": [",".join(map(str, r)) for r in viol_rules if r], "n_signals": len(violations),
"sec_stat": list(map(float, sec["stat"])) if sec else None, "sec_center": float(sec["center"][0]) if sec else None,
"sec_lcl": list(map(float, sec["lcl"])) if sec else None, "sec_ucl": list(map(float, sec["ucl"])) if sec else None,
"sec_violations": sec_viol,
"cusum_pos": list(map(float, res["pos"])) if chart == "cusum" else None,
"cusum_neg": list(map(float, res["neg"])) if chart == "cusum" else None,
}
rule_names = {"basic": "points beyond the limits", "western_electric": "Western Electric rules", "nelson": "Nelson rules"}
summary = f"{label} chart of {value_col}: {m} {'subgroups' if np.mean(sizes_arr) != 1 else 'points'}; "
summary += f"{len(violations)} signal{'s' if len(violations) != 1 else ''} ({rule_names[rules] if chart not in ('cusum', 'ewma') else 'beyond the limits'})." if violations else "no signals."
notes = [f"σ estimated from {'the first ' + str(phase1) + ' subgroups (phase I)' if 0 < phase1 < m else 'all subgroups'}."]
if chart in ("xbar_r", "xbar_s", "imr"):
notes.append({"xbar_r": "σ = mean(R / d2) (qcc UWAVE-R).", "xbar_s": "σ = mean(S / c4) (qcc UWAVE-SD).", "imr": "σ = mean moving range / 1.128."}[chart])
cs.publish({
"title": f"{label} control chart",
"engine": cs.engine_info("numpy", "pandas"),
"n": len(x),
"summary": summary,
"statistics": [
{"key": "center", "label": "Center", "value": float(center[0])},
{"key": "sigma", "label": "σ", "value": float(res["sigma"])},
{"key": "n_subgroups", "label": "Subgroups", "value": m, "format": "integer"},
{"key": "n_signals", "label": "Signals", "value": len(violations), "format": "integer"},
],
"tables": tables,
"charts": charts,
"warnings": warnings,
"notes": notes,
"values": values,
})
The center is 19.2, and sigma is 4.38.
Check yourself
What is the upper limit, at three sigma? Give two decimals.
Show the answer
Answer: 32.31
19.174 + 3 × √19.174 = 19.174 + 13.136 = 32.31.
Check your understanding
CheckpointCheck yourself
A p chart with 5,000 items per day flags most days, yet nothing seems to have changed. What should you try?
Show the answer
Answer: b
Large samples make binomial limits very tight. Laney p′ tests against the variation the process really shows.
Check yourself
A u chart uses limits at 2.5 sigma instead of 3. What should you expect?
Show the answer
Answer: a
Tighter limits trade one error for the other. On the circuit data, such a chart flags more points than the c chart did, though sizes also vary there, so the counts aren't directly comparable.
Try it
In the appIn the Quality studio, choose Process monitoring, and on the Control chart step pick the chart type. p, np, c, u and Laney p′ are all in the list, and the Sample size role takes your sizes.
Recap
Recap- Defective items use p or np. Defects on an item use c or u.
- Use p or u when sample sizes vary.
- An improvement is a signal too, so set new limits afterwards.
- With large samples, check for overdispersion and consider Laney p′.
Concepts in this lesson