ewma
Computes an EWMA (Exponentially Weighted Moving Average) control chart for the given observations.
The EWMA chart, due to Roberts (1959), is designed to detect small, sustained shifts in the process mean more quickly than a Shewhart chart such as xBarRChart or xBarSChart. Instead of reacting to each single observation, it tracks an exponentially weighted moving average that gives recent observations higher weight while retaining some memory of the past, so that a drift of roughly 0.5σ–1σ is detected within a few observations. EWMA is typically competitive with cusum for detecting small shifts and more robust for non-normal data.
The smoothed statistic is computed recursively:
Zₜ = λ·xₜ + (1 − λ)·Zₜ₋₁, Z₀ = μ₀with time-varying control limits:
σ_{Zₜ} = σ · √(λ/(2 − λ) · (1 − (1 − λ)^(2t)))
UCLₜ / LCLₜ = μ₀ ± L · σ_{Zₜ}The smoothing parameter lambda controls the memory of the chart — small values (λ ≈ 0.05–0.25) give high sensitivity to small shifts but slow response to large shifts, while larger values (λ ≈ 0.40) behave more like a Shewhart chart. Common choices are λ = 0.2 with L ≈ 2.7–3.0, which gives an in-control average run length comparable to a 3σ Shewhart chart while reacting much faster to small shifts.
NaN values in the data propagate through the computation (IEEE 754 semantics). An observation of NaN yields a NaN smoothed statistic from that point forward and never triggers an out-of-control signal (NaN comparisons are always false).
Example:
val observations = doubleArrayOf(25.0, 24.5, 25.2, 26.1, 25.8, 27.0, 26.5, 28.0)
val result = ewma(observations, target = 25.0, sigma = 1.0, lambda = 0.2, controlLimitWidth = 3.0)
result.smoothedValues // exponentially weighted moving averages Zₜ
result.ucl // upper control limits at each point
result.lcl // lower control limits at each point
result.outOfControl // indices of points outside control limitsReferences: Roberts (1959), "Control Chart Tests Based on Geometric Moving Averages"; Montgomery, "Introduction to Statistical Quality Control" (7th ed.), §9.2.
Return
an EwmaResult with the full Zₜ series, per-observation control limits, and the indices of any out-of-control points.
Parameters
the sequence of individual measurements to monitor. Must contain at least 1 element.
the reference mean μ₀ to monitor against, also used as the initial value Z₀ of the smoothed statistic.
the in-control process standard deviation σ. Must be strictly positive.
the smoothing parameter λ ∈ (0, 1] controlling the weight of new observations. Typical values are 0.05–0.40; λ = 0.2 is a common default.
the control limit width L, in units of σ_{Zₜ}. Must be strictly positive. Typical values are 2.7–3.0.
See also
Throws
if observations is empty.
if sigma is non-positive, lambda is outside (0, 1], or controlLimitWidth is non-positive.
Computes an EWMA control chart for an Iterable of observations.
Convenience overload that collects observations into a DoubleArray and delegates to the primary ewma function. See ewma for the full description of the algorithm, parameter guidance, and references.
Example:
val observations: List<Double> = listOf(25.0, 24.5, 25.2, 26.1, 25.8, 27.0, 26.5, 28.0)
val result = ewma(observations, target = 25.0, sigma = 1.0, lambda = 0.2, controlLimitWidth = 3.0)
result.outOfControl // indices of points outside control limitsReturn
an EwmaResult with the full Zₜ series, control limits, and out-of-control indices.
Parameters
the sequence of individual measurements to monitor. Must contain at least 1 element.
the reference mean μ₀, also used as the initial value Z₀.
the in-control process standard deviation σ. Must be strictly positive.
the smoothing parameter λ ∈ (0, 1].
the control limit width L. Must be strictly positive.
See also
Throws
if observations is empty.
if sigma is non-positive, lambda is outside (0, 1], or controlLimitWidth is non-positive.
Computes an EWMA control chart for a Sequence of observations.
Convenience overload that collects observations into a DoubleArray and delegates to the primary ewma function. See ewma for the full description of the algorithm, parameter guidance, and references.
Example:
val observations: Sequence<Double> = sequenceOf(25.0, 24.5, 25.2, 26.1, 25.8, 27.0, 26.5, 28.0)
val result = ewma(observations, target = 25.0, sigma = 1.0, lambda = 0.2, controlLimitWidth = 3.0)
result.outOfControl // indices of points outside control limitsReturn
an EwmaResult with the full Zₜ series, control limits, and out-of-control indices.
Parameters
the sequence of individual measurements to monitor. Must contain at least 1 element.
the reference mean μ₀, also used as the initial value Z₀.
the in-control process standard deviation σ. Must be strictly positive.
the smoothing parameter λ ∈ (0, 1].
the control limit width L. Must be strictly positive.
See also
Throws
if observations is empty.
if sigma is non-positive, lambda is outside (0, 1], or controlLimitWidth is non-positive.