Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sensitivity accept floats for better precision #12

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/** @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,29 +78,29 @@
* @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;
$this->latest = $latest;
$this->sensitivity = $sensitivity;

$this->high = ceil($this->mean + ($sensitivity * $this->std_dev));

Check warning on line 90 in src/AnomalyDetectionResult.php

View workflow job for this annotation

GitHub Actions / Mutatation testing with PHP 8.3

Escaped Mutant for Mutator "RoundingFamily": --- Original +++ New @@ @@ $this->mean = $mean; $this->latest = $latest; $this->sensitivity = $sensitivity; - $this->high = ceil($this->mean + $sensitivity * $this->std_dev); + $this->high = round($this->mean + $sensitivity * $this->std_dev); $this->low = floor($this->mean - $sensitivity * $this->std_dev); if ($this->latest >= $this->low && $this->latest <= $this->high) { return;
$this->low = floor($this->mean - ($sensitivity * $this->std_dev));

Check warning on line 91 in src/AnomalyDetectionResult.php

View workflow job for this annotation

GitHub Actions / Mutatation testing with PHP 8.3

Escaped Mutant for Mutator "RoundingFamily": --- Original +++ New @@ @@ $this->latest = $latest; $this->sensitivity = $sensitivity; $this->high = ceil($this->mean + $sensitivity * $this->std_dev); - $this->low = floor($this->mean - $sensitivity * $this->std_dev); + $this->low = round($this->mean - $sensitivity * $this->std_dev); if ($this->latest >= $this->low && $this->latest <= $this->high) { return; }

if ($this->latest >= $this->low && $this->latest <= $this->high) {

Check warning on line 93 in src/AnomalyDetectionResult.php

View workflow job for this annotation

GitHub Actions / Mutatation testing with PHP 8.3

Escaped Mutant for Mutator "GreaterThanOrEqualTo": --- Original +++ New @@ @@ $this->sensitivity = $sensitivity; $this->high = ceil($this->mean + $sensitivity * $this->std_dev); $this->low = floor($this->mean - $sensitivity * $this->std_dev); - if ($this->latest >= $this->low && $this->latest <= $this->high) { + if ($this->latest > $this->low && $this->latest <= $this->high) { return; } if ($this->std_dev > 0.0) {
return;
}

if ($this->std_dev > 0.0) {

Check warning on line 97 in src/AnomalyDetectionResult.php

View workflow job for this annotation

GitHub Actions / Mutatation testing with PHP 8.3

Escaped Mutant for Mutator "GreaterThan": --- Original +++ New @@ @@ if ($this->latest >= $this->low && $this->latest <= $this->high) { return; } - if ($this->std_dev > 0.0) { + if ($this->std_dev >= 0.0) { $this->hops = abs($this->mean - $this->latest) / $this->std_dev; } if ($this->latest < $this->low) {
$this->hops = abs($this->mean - $this->latest) / $this->std_dev;

Check warning on line 98 in src/AnomalyDetectionResult.php

View workflow job for this annotation

GitHub Actions / Mutatation testing with PHP 8.3

Escaped Mutant for Mutator "Division": --- Original +++ New @@ @@ return; } if ($this->std_dev > 0.0) { - $this->hops = abs($this->mean - $this->latest) / $this->std_dev; + $this->hops = abs($this->mean - $this->latest) * $this->std_dev; } if ($this->latest < $this->low) { $this->direction = self::DIRECTION_DOWN;
}

if ($this->latest < $this->low) {

Check warning on line 101 in src/AnomalyDetectionResult.php

View workflow job for this annotation

GitHub Actions / Mutatation testing with PHP 8.3

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ if ($this->std_dev > 0.0) { $this->hops = abs($this->mean - $this->latest) / $this->std_dev; } - if ($this->latest < $this->low) { + if ($this->latest <= $this->low) { $this->direction = self::DIRECTION_DOWN; } elseif ($this->latest > $this->high) { $this->direction = self::DIRECTION_UP;
$this->direction = self::DIRECTION_DOWN;
} elseif ($this->latest > $this->high) {

Check warning on line 103 in src/AnomalyDetectionResult.php

View workflow job for this annotation

GitHub Actions / Mutatation testing with PHP 8.3

Escaped Mutant for Mutator "GreaterThan": --- Original +++ New @@ @@ } if ($this->latest < $this->low) { $this->direction = self::DIRECTION_DOWN; - } elseif ($this->latest > $this->high) { + } elseif ($this->latest >= $this->high) { $this->direction = self::DIRECTION_UP; } }
$this->direction = self::DIRECTION_UP;
}
}
Expand All @@ -118,7 +118,7 @@
*
* @param int $precision the number of decimal digits to round to
*/
public function toArray(int $precision = 2): array

Check warning on line 121 in src/AnomalyDetectionResult.php

View workflow job for this annotation

GitHub Actions / Mutatation testing with PHP 8.3

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ * * @param int $precision the number of decimal digits to round to */ - public function toArray(int $precision = 2) : array + public function toArray(int $precision = 1) : array { return array_map(fn($val) => is_float($val) ? round($val, $precision) : $val, get_object_vars($this)); }
{
return array_map(
fn ($val) => is_float($val) ? round($val, $precision) : $val,
Expand All @@ -145,7 +145,7 @@
/**
* 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
Loading