Skip to content

Conversation

@ericpsimon
Copy link
Contributor

Summary

Implements a new anomaly detection strategy pattern for the Term data validation library, providing flexible rate-of-change based anomaly detection with asymmetric thresholds and robust edge case handling.

Closes TER-167

What does this PR do?

  • Introduces AnomalyDetectionStrategy trait as an alternative to the existing detector pattern
  • Implements RelativeRateOfChangeStrategy with configurable asymmetric thresholds
  • Adds comprehensive input validation and floating-point edge case handling
  • Provides 10 comprehensive tests covering all edge cases
  • Includes working example demonstrating usage patterns

Type of change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Key Features

  • Asymmetric Thresholds: Different limits for increases vs decreases (e.g., allow 20% growth but only 5% decline)
  • Robust Validation: Rejects NaN, infinity, and negative rate thresholds at construction
  • Epsilon-based Comparisons: Handles floating-point precision issues correctly
  • Zero Baseline Handling: Special handling for near-zero values to prevent division errors
  • Configurable History Requirements: Set minimum history points needed for detection
  • Rich Result Details: Provides confidence scores, expected ranges, and detailed explanations

Checklist

  • Tests pass locally (10 tests added, all passing)
  • Code formatted with cargo fmt
  • No clippy warnings (cargo clippy -- -D warnings)
  • Documentation updated with rustdoc comments
  • Example added (examples/src/anomaly_detection_strategy.rs)
  • Input validation with comprehensive error messages
  • Edge cases handled (NaN, infinity, near-zero, negative values)

Testing

# Run strategy tests
cargo test -p term-guard --lib analyzers::anomaly::strategy::tests

# Run example
cargo run --example anomaly_detection_strategy

Performance Considerations

  • O(1) algorithm examining only the last historical point
  • Minimal allocations in hot path
  • Pre-allocated HashMap capacity where possible
  • Zero-copy patterns for historical data

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

…idation

- Add AnomalyDetectionStrategy trait for flexible anomaly detection
- Implement RelativeRateOfChangeStrategy with asymmetric thresholds
- Add comprehensive input validation (NaN, infinity, negative checks)
- Use epsilon-based floating point comparisons for robustness
- Support configurable min history requirements
- Add 10 comprehensive tests including edge cases
- Create example demonstrating usage patterns

Fixes TER-167: Implements the RelativeRateOfChangeStrategy that detects
anomalies based on percentage changes between consecutive metric values.

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
@ericpsimon ericpsimon merged commit 86b50db into main Sep 9, 2025
5 checks passed
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.

1 participant