Detect Shifts with CUSUM

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 20. Everything before it counts as cleared.

CUSUM (cumulative sum control charts) detects small, persistent shifts in a process mean much faster than checking each point against a threshold. Both one-sided statistics start at 0:

St+=max⁡(0,  St−1++xt−μ0−k),St−=max⁡(0,  St−1−+μ0−xt−k)S^+_t = \max(0,\; S^+_{t-1} + x_t - \mu_0 - k), \qquad S^-_t = \max(0,\; S^-_{t-1} + \mu_0 - x_t - k)

An alarm fires when either exceeds h. Here k (the slack, typically half the shift you want to detect) and h (typically 4–5 standard deviations) are in the data's units.

Implement:

  • cusum(x, mu0, k, h): return (alarm_index, direction) for the first alarm ("up" or "down"), or (None, None)
  • cusum_paths(x, mu0, k): the arrays of S⁺ and S⁻
  • detection_delay(x, change_at, mu0, k, h): the alarm index minus change_at, or None if there's no alarm at or after the change
#monitoring#changepoint-detection#quality-control2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Detect Shifts with CUSUM · TechGradify