-
Notifications
You must be signed in to change notification settings - Fork 1
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
Expose the number of observations #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,9 @@ | |
*/ | ||
public const DIRECTION_DOWN = 'down'; | ||
|
||
/** @var int The number of observations */ | ||
private int $count; | ||
|
||
/** @var float The standard deviation */ | ||
private float $std_dev; | ||
|
||
|
@@ -75,32 +78,34 @@ | |
/** | ||
* Create a new anomaly detection result instance. | ||
* | ||
* @param int $count The number of observations | ||
* @param float $std_dev The standard deviation | ||
* @param float $mean The mean value | ||
* @param float $latest The latest value | ||
* @param float|int $sensitivity The sensitivity (see `SlidingWindowCounter::detectAnomaly()`) | ||
*/ | ||
public function __construct(float $std_dev, float $mean, float $latest, float|int $sensitivity) | ||
public function __construct(int $count, float $std_dev, float $mean, float $latest, float|int $sensitivity) | ||
{ | ||
$this->count = $count; | ||
$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 95 in src/AnomalyDetectionResult.php
|
||
$this->low = floor($this->mean - ($sensitivity * $this->std_dev)); | ||
Check warning on line 96 in src/AnomalyDetectionResult.php
|
||
|
||
if ($this->latest >= $this->low && $this->latest <= $this->high) { | ||
Check warning on line 98 in src/AnomalyDetectionResult.php
|
||
return; | ||
} | ||
|
||
if ($this->std_dev > 0.0) { | ||
Check warning on line 102 in src/AnomalyDetectionResult.php
|
||
$this->hops = abs($this->mean - $this->latest) / $this->std_dev; | ||
Check warning on line 103 in src/AnomalyDetectionResult.php
|
||
} | ||
|
||
if ($this->latest < $this->low) { | ||
Check warning on line 106 in src/AnomalyDetectionResult.php
|
||
$this->direction = self::DIRECTION_DOWN; | ||
} elseif ($this->latest > $this->high) { | ||
Check warning on line 108 in src/AnomalyDetectionResult.php
|
||
$this->direction = self::DIRECTION_UP; | ||
} | ||
} | ||
|
@@ -118,7 +123,7 @@ | |
* | ||
* @param int $precision the number of decimal digits to round to | ||
*/ | ||
public function toArray(int $precision = 2): array | ||
Check warning on line 126 in src/AnomalyDetectionResult.php
|
||
{ | ||
return array_map( | ||
fn ($val) => is_float($val) ? round($val, $precision) : $val, | ||
|
@@ -126,6 +131,14 @@ | |
); | ||
} | ||
|
||
/** | ||
* The number of observations | ||
*/ | ||
public function getCount(): int | ||
{ | ||
return $this->count; | ||
} | ||
|
||
/** | ||
* The standard deviation. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would be able to see how many observations there are to make a better decision.