Compute a time-ordered exponentially weighted trace with startup limits, then separate process signals from specification failures.
EWMA control charts: detect slow shifts against a frozen baseline
Freeze a trustworthy reference process
An exponentially weighted moving average, or EWMA, updates a running statistic from each new observation. Before monitoring, estimate or choose a target mean and process spread from an in-control reference period. Record the unit, subgroup definition, sampling interval, instrument version and baseline dates. A baseline contaminated by a slow fault will normalize that fault away. Control limits describe variation expected under the reference process; they are not engineering specification limits for an individual product. P charts handle binomial proportions with varying denominators, whereas this worked EWMA assumes comparable numeric observations with one historical spread estimate. A process can remain in statistical control while producing values outside a customer tolerance.
Weight recent values without erasing history
At time t, the statistic is lambda times the new reading plus one minus lambda times the previous EWMA. Lambda must be above zero and no greater than one. A smaller value smooths more and can highlight a sustained small shift, but it also delays a sudden jump and creates dependence between plotted points. Show raw readings or a linked table beside the smooth trace; smoothing alone can hide an isolated dangerous measurement. Preserving extrema matters even when the monitoring statistic does not alert. Compare each EWMA point with limits designed for that same time index. Startup limits widen toward steady-state limits when the statistic begins at the fixed target; using the asymptotic limit immediately changes early false-alarm behavior.
Keep time steps honest
A recurrence is defined over ordered observations, not an arbitrary display order. If two shifts are missing, do not quietly join the previous EWMA to the next reading as though no time passed. Decide whether the index counts samples or calendar periods, document any reset after maintenance, and mark instrument downtime. Time-series gaps need a visible policy. The code below treats every input reading as the next consecutive sample and rejects nonfinite values; it does not infer time gaps. If the monitoring interval or subgroup size changes, a single baseline spread may no longer apply. Freeze lambda, target, spread and limit multiplier before evaluating a future run, then version them whenever they change.
Investigate signals without chasing noise
An EWMA point beyond its control limit is a signal to investigate the process and measurement system, not proof of a particular cause. Repeated searching and changing limits after seeing a signal inflates false discoveries. Estimate false-alarm behavior and detection delay under a realistic process model before choosing lambda and limit width; independence and stable variance assumptions matter. CUSUM charts offer a related running-evidence design with a reference shift and reset rule. A published chart should include target, lambda, multiplier, baseline window, each raw reading, the EWMA value, both time-specific limits and the investigation outcome. The export manifest protects that state when charts are shared outside the monitoring system.
Worked contract
from math import isfinite, sqrt
def ewma_trace(readings: list[tuple[str, float]], target: float,
sigma: float, weight: float = 0.25, multiplier: float = 3.0):
if not isfinite(target) or not isfinite(sigma) or sigma <= 0:
raise ValueError("target and positive baseline spread are required")
if not isfinite(weight) or not 0 < weight <= 1:
raise ValueError("weight must be in (0, 1]")
if not isfinite(multiplier) or multiplier <= 0:
raise ValueError("limit multiplier must be positive")
if len({sample_id for sample_id, _ in readings}) != len(readings) or any(
not sample_id or not isfinite(value) for sample_id, value in readings):
raise ValueError("sample IDs and readings must be valid")
current = target
ledger = []
for index, (sample_id, value) in enumerate(readings, start=1):
current = weight * value + (1 - weight) * current
startup_spread = sigma * sqrt(weight / (2 - weight) *
(1 - (1 - weight) ** (2 * index)))
lower = target - multiplier * startup_spread
upper = target + multiplier * startup_spread
ledger.append((sample_id, value, current, lower, upper,
current < lower or current > upper))
return ledger
probe_readings = [("shift-47", 52.0), ("shift-59", 53.0),
("shift-83", 54.0), ("shift-97", 56.0)]
trace = ewma_trace(probe_readings, target=50.0, sigma=2.0)
assert trace[0][2:5] == (50.5, 48.5, 51.5)
assert abs(trace[2][2] - 51.84375) < 1e-12
assert [row[-1] for row in trace] == [False, False, False, True]Performance and operating cost
For N ordered readings, the recurrence and time-specific limits take O(N) time and O(N) output space; streaming only the latest statistic and signal uses O(1) extra space. The four-reading check proves the startup-width calculation and a later alert under its chosen reference values. Those values are illustrative, not calibrated for a target false-alarm rate. The function assumes equally spaced independent baseline observations and does not solve missing periods, serial correlation or a drifting reference process.
Common Mistakes
- Treating control limits as customer specification limits.
- Estimating the baseline from the same visibly drifting period being monitored.
- Changing lambda or limits after viewing alerts without versioning the rule.
- Hiding raw spikes or missing periods beneath a smooth EWMA line.
Validation drill
Monitor 47 consecutive production shifts. Freeze a clean baseline first, compare startup with steady-state limits, flag gaps and resets, and explain each alert with the raw reading and investigation record.
Read next
- CUSUM charts: accumulate small departures from a stable target
- P charts: account for varying denominators before flagging a rate
- Time-series charts: distinguish missing days, zeros and provisional periods
- Chart exports: carry the data, filters and render contract together
