SPC Kit
Very much a work-in-progress, but here’s the basic idea: perform statistical process control calculations in SQL. Why?
- The database is closest to the data and will be the fastest place to manipulate it.
- SQL is a lingua franca that any language and framework can interoperate with easily.
But by all that’s holy take note of the LICENSE, in which I disclaim all warranties. If you use this for something
involving real consequences, that’s on you.
What the heck is statistical process control?
The short version is:
- A process shows two kinds of variability
- Common or ordinary variability, which can be seen all the time and is statistically predictable.
- Special or assignable variability, which is out of the ordinary.
- You can use some simple rules to detect the special/assignable events, so you can investigate what is going wrong.
- You can use some simple rules to compare common variability to your target performance, so you can figure out whether
improvement is necessary (and afterwards whether you’ve managed to improve things).
Because statistical process control is based on simple data and simple rules, it doesn’t require a PhD to apply
successfully. Folks were doing this stuff by hand in the 50s without fuss. Turning it into SQL makes it even easier to
apply in a modern context.
See References and Further Reading for some more detailed reading.
What it can do
- Report out-of-control samples on variables using:
- x̄R (aka XbarR) limits. These detect out-of-control sample averages, based on the variability of ranges of samples.
(See: Montgomery §6.2.1, Eqn 6.4)
- R̄ (aka Rbar) limits. These detect out-of-control sample ranges. (See: Montgomery §6.2.1, Eqn 6.5)
- x̄s (aka XbarS) limits. These detect out-of-control sample averages, based on the variability of the standard
deviation of samples. (See: Montgomery §6.3, Eqn 6.28)
- s̄ (aka Sbar) limits. These detect out-of-control sample standard deviations. (See: Montgomery §6.3, Eqns 6.25 &
6.27)
- Limits for individual measurements (aka XmR). These are applied to samples with a single measurement and track
measurement-to-measurement changes in means (X) and moving ranges (mR). Sensitive to departure from normality.
(See: Montgomery §6.4, Eqn 6.33; Wheeler & Chambers §3.6)
- Limits for Exponentially-Weighted Moving Averages (EWMA). These track shifts in the mean. Useful adjunct to the
usual Shewhart charts. (See: Montgomery §9.2, Eqns 9.25 & 9.26)
- Report out-of-control samples on attributes using:
- p limits, available in both conformant (aka yield chart) and non-conformant (aka fallout chart) flavors. (See:
Montgomery §7.2, Eqn 7.8)
- np limits, available in both conformant and non-conformant flavors. (See: Montgomery §7.2.1, Eqn 7.13)
- c limits. (See: Montgomery §7.3.1, Eqn 7.17)
Sample sizes are assumed to be equal throughout a window.
What it cannot do
Everything else. No variable sample sizes. No sensitizing rules. No u charts. No Cusum. No Hotelling T². Etc.
Alternatives
SQL not your style? Not a problem.
Here are some alternative packages I found with some light searching. Most of them include inbuilt plotting capability,
unlike SPC Kit. I have chosen examples where there are tests and some activity in the past few years (not always fair,
it is possible to “finish” an SPC package if you don’t bother with exotic charts). I have not tried out these packages,
so caveat emptor.
Installation
The SQL dialect used is unapologetically PostgreSQL, so you need that running first.
Then apply the sql/postgresql files in alphanumeric order. They are prefixed with numbers for your convenience.
You can optionally add sample data from the data directory. I mostly used these to check my calculations and rule
queries.
Usage
A lot of the details of what’s what and how it works lives in PostgreSQL comments. However, to help you to get started,
here is a short walkthrough of adding data and retrieving rule results. We will use data taken from Montgomery (see
References and Further Reading).
Establish systems and instruments.
Data is collected about Observed Systems using Instruments. For example, an observed system might be a process for
manufacturing screws. Instruments in this example would include screw length, screw head diameter and so on. Instruments
need not be physical measurement devices. Any kind of timeseries that can be observed from an Observed System can be an
Instrument.
For our first example we will use Tables 6.1 and 6.2 from Montgomery. Our Observed System will be the photolithography
process in a semiconductor factory:
insert into spc_data.observed_systems(id, name) overriding system value
values (1, 'Photolithography Process from Montgomery');
Montgomery’s example is to measure the flow width of the resist in microns. “Flow width of the resist” refers to the
spreading out of special photoresistant chemicals on the mask that is being made for the semiconductor. If they are too
narrow or too wide, the resulting circuit may be faulty.
Let’s add the instrument:
insert into spc_data.instruments(id, observed_system_id, name) overriding system value
values (1, 1, 'Flow Resist Width (Tables 6.1 and 6.2)');
Windows
Windows are spans of Samples that belong to an Instrument.
Montgomery gives two tables of data (6.1 and 6.2). Table 6.1 is intended for establishing the process control limits of
the current process; 6.2 is for when the process is operating under control. These are distinct uses for data. Most
importantly, the limits established with the first set of samples (traditionally 20 samples is considered the minimum
acceptable number) is then used in subsequent samples to detect out-of-control conditions.
Therefore, SPC-kit allows you to group together Samples into Windows, which express the purpose for which the Samples
are to be used. Let’s add two windows for the Tables 6.1 (limit establishment) and 6.2 (control):
insert into spc_data.windows(id, instrument_id, type, description) overriding system value
values (1, 1, 'limit_establishment', 'Table 6.1');
insert into spc_data.windows(id, instrument_id, type, description) overriding system value
values (2, 1, 'control', 'Table 6.2');
Each control window belongs to one limit establishment window. This relationship does not rely on time ranges, but is
explicitly recorded in spc_data.window_relationships. Let us connect our two windows together:
insert into spc_data.window_relationships (limit_establishment_window_id, control_window_id) values (1, 2);
Note that you may link a limit-establishment window to itself. This is useful for cases (like XmR) where the distinction
between limit establishment and control is unimportant. For completeness we will do so for the window established based
on Table 6.1:
insert into spc_data.window_relationships (limit_establishment_window_id, control_window_id) values (1, 1);
Samples and Measurements
Each Window contains Samples, which in turn have one or more Measurements. Let us add some data for the two tables,
starting with establishing the samples within each window:
-- @formatter:off
insert into spc_data.samples (id, window_id, include_in_limit_calculations) overriding system value
-- Table 6.1
values (1, 1, true), (2, 1, true), (3, 1, true), (4, 1, true), (5, 1, true),
(6, 1, true), (7, 1, true), (8, 1, true), (9, 1, true), (10, 1, true),
(11, 1, true), (12, 1, true), (13, 1, true), (14, 1, true), (15, 1, true),
(16, 1, true), (17, 1, true), (18, 1, true), (19, 1, true), (20, 1, true),
(21, 1, true), (22, 1, true), (23, 1, true), (24, 1, true), (25, 1, true),
-- Table 6.2
(26, 2, true), (27, 2, true), (28, 2, true), (29, 2, true), (30, 2, true),
(31, 2, true), (32, 2, true), (33, 2, true), (34, 2, true), (35, 2, true),
(36, 2, true), (37, 2, true), (38, 2, true), (39, 2, true), (40, 2, true),
(41, 2, true), (42, 2, true), (43, 2, true), (44, 2, true), (45, 2, true);
-- @formatter:on
Now we add data. Five measurements are taken per sample, yielding 125 measurements for Table 6.1 and another 100 for
Table 6.2, for a total of 225 measurements:
-- @formatter:off
insert into spc_data.measurements (id, sample_id, performed_at, measured_value) overriding system value
values (1, 1, '2023-01-01 00:00:00.000000 +00:00', 1.3235), (2, 1, '2023-01-01 00:00:01.000000 +00:00', 1.4128), (3, 1, '2023-01-01 00:00:02.000000 +00:00', 1.6744), (4, 1, '2023-01-01 00:00:03.000000 +00:00', 1.4573),
(5, 1, '2023-01-01 00:00:04.000000 +00:00', 1.6914), (6, 2, '2023-01-01 00:01:00.000000 +00:00', 1.4314), (7, 2, '2023-01-01 00:01:01.000000 +00:00', 1.3592), (8, 2, '2023-01-01 00:01:02.000000 +00:00', 1.6075),
(9, 2, '2023-01-01 00:01:03.000000 +00:00', 1.4666), (10, 2, '2023-01-01 00:01:04.000000 +00:00', 1.6109), (11, 3, '2023-01-01 00:02:00.000000 +00:00', 1.4284), (12, 3, '2023-01-01 00:02:01.000000 +00:00', 1.4871),
(13, 3, '2023-01-01 00:02:02.000000 +00:00', 1.4932), (14, 3, '2023-01-01 00:02:03.000000 +00:00', 1.4324), (15, 3, '2023-01-01 00:02:04.000000 +00:00', 1.5674), (16, 4, '2023-01-01 00:03:00.000000 +00:00', 1.5028),
(17, 4, '2023-01-01 00:03:01.000000 +00:00', 1.6352), (18, 4, '2023-01-01 00:03:02.000000 +00:00', 1.3841), (19, 4, '2023-01-01 00:03:03.000000 +00:00', 1.2831), (20, 4, '2023-01-01 00:03:04.000000 +00:00', 1.5507),
(21, 5, '2023-01-01 00:04:00.000000 +00:00', 1.5604), (22, 5, '2023-01-01 00:04:01.000000 +00:00', 1.2735), (23, 5, '2023-01-01 00:04:02.000000 +00:00', 1.5265), (24, 5, '2023-01-01 00:04:03.000000 +00:00', 1.4363),
(25, 5, '2023-01-01 00:04:04.000000 +00:00', 1.6441), (26, 6, '2023-01-01 00:05:00.000000 +00:00', 1.5955), (27, 6, '2023-01-01 00:05:01.000000 +00:00', 1.5451), (28, 6, '2023-01-01 00:05:02.000000 +00:00', 1.3574),
(29, 6, '2023-01-01 00:05:03.000000 +00:00', 1.3281), (30, 6, '2023-01-01 00:05:04.000000 +00:00', 1.4198), (31, 7, '2023-01-01 00:06:00.000000 +00:00', 1.6274), (32, 7, '2023-01-01 00:06:01.000000 +00:00', 1.5064),
(33, 7, '2023-01-01 00:06:02.000000 +00:00', 1.8366), (34, 7, '2023-01-01 00:06:03.000000 +00:00', 1.4177), (35, 7, '2023-01-01 00:06:04.000000 +00:00', 1.5144), (36, 8, '2023-01-01 00:07:00.000000 +00:00', 1.419 ),
(37, 8, '2023-01-01 00:07:01.000000 +00:00', 1.4303), (38, 8, '2023-01-01 00:07:02.000000 +00:00', 1.6637), (39, 8, '2023-01-01 00:07:03.000000 +00:00', 1.6067), (40, 8, '2023-01-01 00:07:04.000000 +00:00', 1.5519),
(41, 9, '2023-01-01 00:08:00.000000 +00:00', 1.3884), (42, 9, '2023-01-01 00:08:01.000000 +00:00', 1.7277), (43, 9, '2023-01-01 00:08:02.000000 +00:00', 1.5355), (44, 9, '2023-01-01 00:08:03.000000 +00:00', 1.5176),
(45, 9, '2023-01-01 00:08:04.000000 +00:00', 1.3688), (46, 10, '2023-01-01 00:09:00.000000 +00:00', 1.4039), (47, 10, '2023-01-01 00:09:01.000000 +00:00', 1.6697), (48, 10, '2023-01-01 00:09:02.000000 +00:00', 1.5089),
(49, 10, '2023-01-01 00:09:03.000000 +00:00', 1.4627), (50, 10, '2023-01-01 00:09:04.000000 +00:00', 1.522 ), (51, 11, '2023-01-01 00:10:00.000000 +00:00', 1.4158), (52, 11, '2023-01-01 00:10:01.000000 +00:00', 1.7667),
(53, 11, '2023-01-01 00:10:02.000000 +00:00', 1.4278), (54, 11, '2023-01-01 00:10:03.000000 +00:00', 1.5928), (55, 11, '2023-01-01 00:10:04.000000 +00:00', 1.4181), (56, 12, '2023-01-01 00:11:00.000000 +00:00', 1.5821),
(57, 12, '2023-01-01 00:11:01.000000 +00:00', 1.3355), (58, 12, '2023-01-01 00:11:02.000000 +00:00', 1.5777), (59, 12, '2023-01-01 00:11:03.000000 +00:00', 1.3908), (60, 12, '2023-01-01 00:11:04.000000 +00:00', 1.7559),
(61, 13, '2023-01-01 00:12:00.000000 +00:00', 1.2856), (62, 13, '2023-01-01 00:12:01.000000 +00:00', 1.4106), (63, 13, '2023-01-01 00:12:02.000000 +00:00', 1.4447), (64, 13, '2023-01-01 00:12:03.000000 +00:00', 1.6398),
(65, 13, '2023-01-01 00:12:04.000000 +00:00', 1.1928), (66, 14, '2023-01-01 00:13:00.000000 +00:00', 1.4951), (67, 14, '2023-01-01 00:13:01.000000 +00:00', 1.4036), (68, 14, '2023-01-01 00:13:02.000000 +00:00', 1.5893),
(69, 14, '2023-01-01 00:13:03.000000 +00:00', 1.6458), (70, 14, '2023-01-01 00:13:04.000000 +00:00', 1.4969), (71, 15, '2023-01-01 00:14:00.000000 +00:00', 1.3589), (72, 15, '2023-01-01 00:14:01.000000 +00:00', 1.2863),
(73, 15, '2023-01-01 00:14:02.000000 +00:00', 1.5996), (74, 15, '2023-01-01 00:14:03.000000 +00:00', 1.2497), (75, 15, '2023-01-01 00:14:04.000000 +00:00', 1.5471), (76, 16, '2023-01-01 00:15:00.000000 +00:00', 1.5747),
(77, 16, '2023-01-01 00:15:01.000000 +00:00', 1.5301), (78, 16, '2023-01-01 00:15:02.000000 +00:00', 1.5171), (79, 16, '2023-01-01 00:15:03.000000 +00:00', 1.1839), (80, 16, '2023-01-01 00:15:04.000000 +00:00', 1.8662),
(81, 17, '2023-01-01 00:16:00.000000 +00:00', 1.368 ), (82, 17, '2023-01-01 00:16:01.000000 +00:00', 1.7269), (83, 17, '2023-01-01 00:16:02.000000 +00:00', 1.3957), (84, 17, '2023-01-0
提示:代码过长已自动截断
Reading back rule results
Data inserted into spc_data is processed through spc_intermediate and then assembled into per-measurement Rules.
Each row in a Rule view tells you whether a Sample was within control limits, or whether it exceeded control limits.
Let’s look at Table 6.2 and see if we can find out-of-control Samples:
select id_sample as "Sample ID",
data_controlled_value as "Sample Average",
data_upper_limit as "Upper Limit",
rule_in_control as "In Control?",
rule_out_of_control_upper as "Out of Control Upper?"
from spc_reports.x_bar_r_rules
where id_control_window = 2
and not rule_in_control
order by id_sample;
Giving:
| Sample ID |
Sample Average |
Upper Limit |
In Control? |
Out of Control Upper? |
| 43 |
1.69696 |
1.693224336 |
false |
true |
| 45 |
1.77 |
1.693224336 |
false |
true |
We can see that samples 43 and 45 are unusually high: they are out of control. This means we need to perform an
investigation to establish what has occurred to cause the unusual sample average.
You may have noticed the prefixes for each column. They follow a consistent pattern across different views and
functions: id_ refers to an ID from another table, data_ represents some value as of that sample and rule_ is
whether a particular rule has been matched or not.
You Read The License, Right?
SPC Kit is Copyright © 2024 Jacques Chester.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
References and Further Reading
Listed in suggested order of priority.
- Stjernlöf, C. “Statistical Process Control: A Practitioner’s Guide”,
Entropic Thoughts.
- Chin, C. “Becoming Data Driven, From First Principles”,
Commoncog.
- Montgomery, Douglas C. Introduction to Statistical Quality Control, 8th EMEA Ed.
- Wheeler, Donald J and Chambers, David S. Understanding Statistical Process Control, 3rd Ed.
评论