Permalink
Browse files

Added save/load to anomaly detector for user.

  • Loading branch information...
1 parent 13b550e commit 69c2fa016875fd15bca10c98bb6cde9ea672f02e @gburtini committed Feb 22, 2012
Showing with 28 additions and 0 deletions.
  1. +28 −0 lib/parametric/anomaly_detection.php
@@ -21,6 +21,7 @@ class LL_OnlineAnomalyDetection extends LL_AnomalyDetection {
private $m2s;
private $counts;
+ protected $toSave = array("sums", "m2s", "counts");
/*
* addObservation($x)
* - pass in an array $x representing all the dimensions relevant to your observation.
@@ -48,6 +49,7 @@ public function addObservation($x) {
}
}
+
protected function computeParameters($xs) {
if(!is_array($xs[0])) {
$xs = $this->arrayNonArray($xs);
@@ -130,6 +132,32 @@ public function isAnomaly($data_point, $p=0.01)
return ($probability < $p);
}
+
+ protected $toSave = array("mean", "variance");
+ public function save() {
+ $saveString = "";
+ foreach($this->toSave as $save)
+ {
+ $saveString .= implode(",", $this->$save) . "|";
+ }
+ $s_learned = intval($this->learned);
+
+ return $saveString . $s_learned;
+ }
+
+ public function load($saveString) {
+ $saveArray = explode("|", $saveString);
+ if(count($saveArray) != count($this->toSave)-1)
+ return false;
+
+ foreach($this->toSave as $key=>$load)
+ {
+ $this->$load = explode(",", $saveArray[$key]);
+ }
+
+ $this->learned = (bool) end($saveArray);
+ }
+
protected function computeProbability($x) {
if(!is_array($x))
$x = array($x);

0 comments on commit 69c2fa0

Please sign in to comment.