From f432ecb5dafb29aae931256f2abf62f62cf56a90 Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Wed, 9 Oct 2024 14:27:08 +0900 Subject: [PATCH] Make sensitivity accept floats for better precision (#12) --- src/AnomalyDetectionResult.php | 10 +++++----- src/SlidingWindowCounter.php | 4 ++-- tests/AnomalyDetectionResultTest.php | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/AnomalyDetectionResult.php b/src/AnomalyDetectionResult.php index c64a8d8..ea3752b 100644 --- a/src/AnomalyDetectionResult.php +++ b/src/AnomalyDetectionResult.php @@ -54,8 +54,8 @@ class AnomalyDetectionResult /** @var float The mean value */ private float $mean; - /** @var int The sensitivity */ - private int $sensitivity; + /** @var float|int The sensitivity */ + private float|int $sensitivity; /** @var float The low bound */ private float $low; @@ -78,9 +78,9 @@ class AnomalyDetectionResult * @param float $std_dev The standard deviation * @param float $mean The mean value * @param float $latest The latest value - * @param int $sensitivity The sensitivity (see `SlidingWindowCounter::detectAnomaly()`) + * @param float|int $sensitivity The sensitivity (see `SlidingWindowCounter::detectAnomaly()`) */ - public function __construct(float $std_dev, float $mean, float $latest, int $sensitivity) + public function __construct(float $std_dev, float $mean, float $latest, float|int $sensitivity) { $this->std_dev = $std_dev; $this->mean = $mean; @@ -145,7 +145,7 @@ public function getMean(): float /** * The sensitivity. */ - public function getSensitivity(): int + public function getSensitivity(): float|int { return $this->sensitivity; } diff --git a/src/SlidingWindowCounter.php b/src/SlidingWindowCounter.php index 93165c2..b79744d 100644 --- a/src/SlidingWindowCounter.php +++ b/src/SlidingWindowCounter.php @@ -241,10 +241,10 @@ public function getLatestValue(string $bucket_key): float * Tests the standard deviation against the acceptable range and returns the alert message. * * @param string $bucket_key The bucket key - * @param int $sensitivity The sensitivity: 3 = low (99.7%), 2 = standard (95%) 1 = high (68% deviation triggers alert) + * @param float|int $sensitivity The sensitivity: 3 = low (99.7%), 2 = standard (95%) 1 = high (68% deviation triggers alert) * @param null|int $start_time The optional start time; leaving out the start time will omit the leading null values */ - public function detectAnomaly(string $bucket_key, int $sensitivity = 2, ?int $start_time = null): AnomalyDetectionResult + public function detectAnomaly(string $bucket_key, float|int $sensitivity = 2, ?int $start_time = null): AnomalyDetectionResult { $variance = $this->getHistoricVariance($bucket_key, $start_time); diff --git a/tests/AnomalyDetectionResultTest.php b/tests/AnomalyDetectionResultTest.php index c9a3cef..54cc384 100644 --- a/tests/AnomalyDetectionResultTest.php +++ b/tests/AnomalyDetectionResultTest.php @@ -57,11 +57,11 @@ public function testHappyPath(): void */ public function testAllGetters(): void { - $result = new AnomalyDetectionResult(1.0, 10.0, 11.0, 1); + $result = new AnomalyDetectionResult(1.0, 10.0, 11.0, 0.9); $this->assertSame(1.0, $result->getStandardDeviation()); $this->assertSame(10.0, $result->getMean()); - $this->assertSame(1, $result->getSensitivity()); + $this->assertSame(0.9, $result->getSensitivity()); $this->assertSame(9.0, $result->getLow()); $this->assertSame(11.0, $result->getHigh()); $this->assertSame(11.0, $result->getLatest());