Skip to content

Commit 3746a7e

Browse files
authored
Merge pull request #985 from htm-community/fix-anomaly-likelihood
Fix the anomaly likelihood class.
2 parents c581af3 + e4ffeb2 commit 3746a7e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

py/htm/algorithms/anomaly_likelihood.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ def __init__(self, period = 1000):
4545
self.mean = 1.0
4646
self.var = 0.0003
4747
self.std = math.sqrt(self.var)
48+
self.prev = 0.0
49+
self.n_records = 0
4850

49-
def compute(self, anomaly):
51+
def compute(self, anomaly) -> float:
5052
""" Returns the probability that an anomaly has occurred.
5153
5254
Calculates the log-likelihood of the probability that the given anomaly
@@ -55,7 +57,11 @@ def compute(self, anomaly):
5557
"""
5658
likelihood = self.get_likelihood(anomaly)
5759
self.add_record(anomaly)
58-
return self.get_log_likelihood(likelihood)
60+
combined = 1 - (1 - likelihood) * (1 - self.prev)
61+
self.prev = likelihood
62+
if self.n_records < self.period:
63+
return 0.0
64+
return self.get_log_likelihood(combined)
5965

6066
def add_record(self, anomaly):
6167
"""
@@ -74,6 +80,7 @@ def add_record(self, anomaly):
7480
# Catch all for super low variance to handle numerical precision issues
7581
self.var = max(self.var, 0.0003)
7682
self.std = math.sqrt(self.var)
83+
self.n_records += 1
7784

7885
def get_likelihood(self, anomaly):
7986
"""

0 commit comments

Comments
 (0)