detect_cusum#
- pyCGM2.Signal.detector.detect_cusum(x: ndarray, threshold: int = 1, drift: int = 0, ending: bool = False, show: bool = True, ax: Axes | None = None)#
Cumulative sum algorithm (CUSUM) to detect abrupt changes in data.
- Parameters:
x (np.ndarray) – Data in which to detect changes.
threshold (int, optional) – Amplitude threshold for the change in the data. Default is 1.
drift (int, optional) – Drift term that prevents any change in the absence of change. Default is 0.
ending (bool, optional) – If True, estimates when the change ends. If False, does not. Default is False.
show (bool, optional) – If True, plots data in matplotlib figure. If False, does not. Default is True.
ax (plt.Axes, optional) – A matplotlib Axes instance for the plot. Default is None.
- Returns:
tuple – A tuple containing: - ta (np.ndarray): Alarm time (index of when the change was detected). - tai (np.ndarray): Index of when the change started. - taf (np.ndarray): Index of when the change ended (if ending is True). - amp (np.ndarray): Amplitude of changes (if ending is True).
Notes
Tuning of the CUSUM algorithm according to Gustafsson (2000): Start with a very large threshold. Choose drift to half the expected change, then adjust so that g = 0 more than 50% of the time. Set the threshold for the desired false alarm rate or detection delay. Decrease drift for faster detection, increase for fewer false alarms.
By default, repeated sequential changes are not deleted. Set ending to True to delete them.
See the referenced IPython Notebook for more information.
References
Gustafsson (2000) Adaptive Filtering and Change Detection.
[CUSUM Notebook](demotu/detecta)
Examples
>>> x = np.random.randn(300)/5 >>> x[100:200] += np.arange(0, 4, 4/100) >>> ta, tai, taf, amp = detect_cusum(x, 2, .02, True, True) >>> x = np.random.randn(300) >>> x[100:200] += 6 >>> detect_cusum(x, 4, 1.5, True, True) >>> x = 2*np.sin(2*np.pi*np.arange(0, 3, .01)) >>> ta, tai, taf, amp = detect_cusum(x, 1, .05, True, True)
- Version history:
‘1.0.5’: Part of the detecta module - [Detecta PyPI](https://pypi.org/project/detecta/)