forked from numenta/NAB
-
Notifications
You must be signed in to change notification settings - Fork 3
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
New detector: naive WIP #4
Open
breznak
wants to merge
6
commits into
master
Choose a base branch
from
new_detector_naive_community
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
69db56e
NAB: add new naive detector
breznak 9c0fc4f
WIP implement new naive detector
breznak a4297c1
Review: anomaly score fn for Naive detector
breznak ee7e580
Naive: debugging out of bounds scores
breznak b6f383b
Merge branch 'master' into new_detector_naive_community
breznak 8a9c25d
Merge branch 'master_community' into new_detector_naive_community
breznak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# ---------------------------------------------------------------------- | ||
# Copyright (C) 2015, Numenta, Inc. Unless you have an agreement | ||
# with Numenta, Inc., for a separate license for this software code, the | ||
# following terms and conditions apply: | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero Public License version 3 as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
# See the GNU Affero Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero Public License | ||
# along with this program. If not, see http://www.gnu.org/licenses. | ||
# | ||
# http://numenta.org/licenses/ | ||
# ---------------------------------------------------------------------- | ||
|
||
import math #exp | ||
|
||
from nab.detectors.base import AnomalyDetector | ||
|
||
EPSILON = 0.00000001 | ||
|
||
class NaiveDetector(AnomalyDetector): | ||
""" | ||
This is implementation of the "naive forecast", aka "random walk forecasting", | ||
which is a baseline algorithm for time-series forecasting. | ||
It predicts the last seen value. So `Prediction(t+1) = Input(t)`. | ||
|
||
Hyperparameter to optimize is @param coef in `initialize`. | ||
""" | ||
|
||
|
||
def initialize(self, coef=10.0): | ||
""" | ||
@param `coef` for the activation function that scales anomaly score to [0, 1.0] | ||
The function is: `anomalyScore = 1-exp(-coef*x)`, where | ||
`x=abs(current - predicted)/predicted`. | ||
""" | ||
super().initialize() | ||
self.predicted = 0.0 #previous value, last seen | ||
self.coef = coef | ||
|
||
|
||
def handleRecord(self, inputData): | ||
"""The predicted value is simply the last seen value, | ||
Anomaly score is computed as a function of current,predicted. | ||
|
||
See @ref `initialize` param `coef`. | ||
""" | ||
current = float(inputData["value"]) | ||
inputData['predicted'] = self.predicted | ||
try: | ||
anomalyScore = self.anomalyFn_(current, self.predicted) | ||
except: | ||
#on any math error (overflow,...), we mark this as anomaly. tough love | ||
anomalyScore = 1.0 | ||
|
||
ret = [anomalyScore, self.predicted] | ||
self.predicted=current | ||
return (ret) | ||
|
||
|
||
def getAdditionalHeaders(self): | ||
return ['predicted'] | ||
|
||
|
||
def anomalyFn_(self, current, predicted): | ||
""" | ||
compute anomaly score from 2 scalars | ||
""" | ||
if predicted == 0.0: | ||
predicted = EPSILON #avoid division by zero | ||
|
||
# the computation | ||
x = abs(current - predicted)/predicted | ||
score = 1-math.exp(-self.coef * x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a better way to get anomaly score from predictions? |
||
|
||
# bound to anomaly range (should not happen, but some are over) | ||
# if(score > 1): | ||
# score = 1.0 | ||
# elif(score < 0): | ||
# score = 0.0 | ||
|
||
assert(score >= 0 and score <= 1), print("ERR: score: "+str(score)+ "\t curr: "+str(current)+"\t pred: "+str(predicted)) | ||
return score | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Naive, random-walk is a simple predictor