Skip to content

feat: add HampelFilter, robust outlier replacement for streams - #7598

Open
alxkm wants to merge 1 commit into
TheAlgorithms:masterfrom
alxkm:feat/hampel-filter
Open

feat: add HampelFilter, robust outlier replacement for streams#7598
alxkm wants to merge 1 commit into
TheAlgorithms:masterfrom
alxkm:feat/hampel-filter

Conversation

@alxkm

@alxkm alxkm commented Sep 8, 2026

Copy link
Copy Markdown
Member

Adds the Hampel filter (Hampel identifier), which replaces the samples of a signal that look like outliers and leaves every other sample untouched. MedianFilter comes with it: the Hampel filter keeps one as its sliding window and reads the sorted contents through its package-private copySortedWindow, so the two cannot be separated.

For each sample the filter computes two robust statistics over the window ending at that sample -- the median, and the median absolute deviation MAD = median(|x_i - median|). The MAD is rescaled by 1.4826, the factor that makes it match the standard deviation of normally distributed data, and the sample is flagged when

|x - median| > threshold * 1.4826 * MAD

Flagged samples are replaced by the window median, the rest pass through unchanged. Both statistics are robust, so a spike cannot inflate the very yardstick it is measured against -- which is what happens with a mean and a standard deviation instead. The window is causal and includes the sample being judged, so there is no delay, and the raw sample rather than its replacement enters the window, which keeps the statistics honest.

A threshold of 3 is the usual starting point, but the false positive rate depends just as much on the window size, because the MAD of a few samples is itself a noisy estimate of the spread. On clean Gaussian noise at threshold 3 the filter flags roughly 7% of samples with a window of 5, 3% with 11 and 0.5% with 101, approaching the 0.3% of a perfect sigma. The Javadoc says so, and MedianFilter covers the plain variant that rewrites every sample.

Each sample costs O(w) time and nothing is allocated after construction.

HampelFilterTest covers 25 cases and MedianFilterTest 23. Among them: the MAD is checked against a brute force computation over several window sizes, an isolated spike is flagged and replaced by the local median, a clean ramp is left alone because the MAD grows with the slope, a zero MAD makes the identifier maximally strict, and detectOutliers reports flags without altering the signal.

Checklist

  • 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

Co-authored-by: Oleksandr Klymenko <19151554+alxkm@users.noreply.github.com>
Signed-off-by: alxkm <19151554+alxkm@users.noreply.github.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.91%. Comparing base (8abfcf7) to head (66ba354).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7598      +/-   ##
============================================
+ Coverage     80.83%   80.91%   +0.08%     
- Complexity     7624     7680      +56     
============================================
  Files           820      822       +2     
  Lines         24379    24490     +111     
  Branches       4783     4800      +17     
============================================
+ Hits          19706    19817     +111     
  Misses         3907     3907              
  Partials        766      766              

☔ 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.

@alxkm
alxkm marked this pull request as ready for review September 8, 2026 17:56
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