cusum

fun cusum(observations: DoubleArray, target: Double, k: Double, h: Double): CusumResult(source)

Computes a two-sided tabular CUSUM control chart for the given observations.

The cumulative sum (CUSUM) chart is designed to detect small, sustained shifts in the process mean more quickly than a Shewhart chart such as xBarRChart or xBarSChart. Where a Shewhart chart reacts only to each single observation, CUSUM accumulates deviations from the reference mean over time, so even a drift of roughly 0.5σ–1σ is detected within a few observations.

This implementation uses the standard tabular two-sided formulation:

C⁺ᵢ = max(0, C⁺ᵢ₋₁ + (xᵢ − μ₀ − K))
C⁻ᵢ = max(0, C⁻ᵢ₋₁ + (μ₀ − K − xᵢ))

with C⁺₀ = C⁻₀ = 0. An out-of-control signal is raised at the first index where C⁺ᵢ > H or C⁻ᵢ > H.

The allowance k (often denoted K) is typically chosen as half the magnitude of the shift (in σ units) that one wants to detect — a common default is K ≈ 0.5σ, which targets a 1σ shift. The decision interval h (often denoted H) is typically set to 4σ or 5σ, giving 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).

Example:

// Process with target 10.0, drifting upward after observation 4.
val observations = doubleArrayOf(10.0, 10.1, 9.9, 10.0, 10.2, 10.5, 10.7, 10.9, 11.1)
val result = cusum(observations, target = 10.0, k = 0.25, h = 4.0)
result.sPlus // non-negative running upper CUSUM values
result.sMinus // non-negative running lower CUSUM values
result.alarmIndex // zero-based index where C⁺ > H first, or -1 if never

References: Page (1954), "Continuous Inspection Schemes"; Montgomery, "Introduction to Statistical Quality Control" (7th ed.), §9.1.1.

Return

a CusumResult with the full C⁺ and C⁻ series and the index of the first alarm.

Parameters

observations

the sequence of individual measurements to monitor. Must contain at least 1 element.

target

the reference mean μ₀ to monitor against. Any finite value is allowed.

k

the allowance (slack) value K, half the magnitude of the shift to detect. Must be non-negative. Typically chosen as ≈ 0.5σ of the in-control process.

h

the decision interval H above which the CUSUM signals an out-of-control condition. Must be strictly positive. Typically chosen as 4σ or 5σ.

See also

Throws

if k is negative or h is non-positive.


fun cusum(observations: Iterable<Double>, target: Double, k: Double, h: Double): CusumResult(source)

Computes a two-sided tabular CUSUM control chart for an Iterable of observations.

Convenience overload that collects observations into a DoubleArray and delegates to the primary cusum function. See cusum for the full description of the algorithm, parameter guidance, and references.

Example:

val observations: List<Double> = listOf(10.0, 10.1, 9.9, 10.0, 10.2, 10.5, 10.7, 10.9, 11.1)
val result = cusum(observations, target = 10.0, k = 0.25, h = 4.0)
result.alarmIndex // zero-based index of first alarm, or -1 if never

Return

a CusumResult with the full C⁺ and C⁻ series and the index of the first alarm.

Parameters

observations

the sequence of individual measurements to monitor. Must contain at least 1 element.

target

the reference mean μ₀ to monitor against. Any finite value is allowed.

k

the allowance (slack) value K. Must be non-negative. Typically ≈ 0.5σ.

h

the decision interval H. Must be strictly positive. Typically 4σ or 5σ.

See also

Throws

if k is negative or h is non-positive.


fun cusum(observations: Sequence<Double>, target: Double, k: Double, h: Double): CusumResult(source)

Computes a two-sided tabular CUSUM control chart for a Sequence of observations.

Convenience overload that collects observations into a DoubleArray and delegates to the primary cusum function. See cusum for the full description of the algorithm, parameter guidance, and references.

Example:

val observations: Sequence<Double> = sequenceOf(10.0, 10.1, 9.9, 10.0, 10.2, 10.5, 10.7, 10.9, 11.1)
val result = cusum(observations, target = 10.0, k = 0.25, h = 4.0)
result.alarmIndex // zero-based index of first alarm, or -1 if never

Return

a CusumResult with the full C⁺ and C⁻ series and the index of the first alarm.

Parameters

observations

the sequence of individual measurements to monitor. Must contain at least 1 element.

target

the reference mean μ₀ to monitor against. Any finite value is allowed.

k

the allowance (slack) value K. Must be non-negative. Typically ≈ 0.5σ.

h

the decision interval H. Must be strictly positive. Typically 4σ or 5σ.

See also

Throws

if k is negative or h is non-positive.