Skip to content

Commit

Permalink
Make sensitivity accept floats for better precision
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Oct 9, 2024
1 parent e93507c commit c59b7de
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/AnomalyDetectionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -145,7 +145,7 @@ public function getMean(): float
/**
* The sensitivity.
*/
public function getSensitivity(): int
public function getSensitivity(): float|int
{
return $this->sensitivity;
}
Expand Down
4 changes: 2 additions & 2 deletions src/SlidingWindowCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions tests/AnomalyDetectionResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit c59b7de

Please sign in to comment.