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:
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