Skip to content

feat: add CusumDetector, cumulative sum change detection for streams - #7593

Open
alxkm wants to merge 1 commit into
TheAlgorithms:masterfrom
alxkm:feat/cusum-detector
Open

feat: add CusumDetector, cumulative sum change detection for streams#7593
alxkm wants to merge 1 commit into
TheAlgorithms:masterfrom
alxkm:feat/cusum-detector

Conversation

@alxkm

@alxkm alxkm commented Sep 7, 2026

Copy link
Copy Markdown
Member

CUSUM change detection

Adds CUSUM (cumulative sum) change detection. The repository has no change or
drift detection yet.

A threshold on raw samples only catches changes that are large compared with the
noise. CUSUM accumulates evidence instead: each sample is normalised to
z = (x - target) / sigma and pushed into two one-sided sums.

upper <- max(0, upper + z - k)
lower <- max(0, lower - z - k)

The allowance k is a toll paid on every step. On target, the toll exceeds the
average evidence, so both sums stay pinned at zero and the detector stays quiet
however long it runs. Once the mean shifts by more than k standard deviations,
the matching sum drifts up until it crosses the threshold h. A half-sigma
shift is invisible in a single sample but clear after twenty. O(1) time and
state per sample.

ShiftSignal is included because accept and scan return it.

CusumDetectorTest covers 16 cases. The two that matter most: a stream on
target raises no alarm over 100,000 samples, and in-control Gaussian noise gives
at most 2 false alarms in 2,000 samples — while a half-sigma drift is still
caught within 60 samples.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

codecov-commenter commented Sep 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.30508% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 80.79%. Comparing base (14c8db8) to head (715602f).

Files with missing lines Patch % Lines
...ava/com/thealgorithms/streaming/CusumDetector.java 98.30% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7593      +/-   ##
============================================
+ Coverage     80.74%   80.79%   +0.04%     
- Complexity     7570     7598      +28     
============================================
  Files           818      819       +1     
  Lines         24257    24316      +59     
  Branches       4772     4780       +8     
============================================
+ Hits          19587    19645      +58     
- Misses         3906     3907       +1     
  Partials        764      764              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Co-authored-by: Oleksandr Klymenko <19151554+alxkm@users.noreply.github.com>
Signed-off-by: alxkm <19151554+alxkm@users.noreply.github.com>
@alxkm
alxkm force-pushed the feat/cusum-detector branch from 3e1eb5f to 715602f Compare September 7, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants