Skip to content

Commit

Permalink
Update pad.py
Browse files Browse the repository at this point in the history
Refine comments within PAD.
  • Loading branch information
hoanganhngo610 authored Mar 18, 2024
1 parent ebd98d8 commit c3ea315
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions river/anomaly/pad.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def learn_one(self, x: dict | None, y: base.typing.Target | float):
self.predictive_model, time_series.base.Forecaster
) and isinstance(y, float):
# When there's no data point as dict of features, the target will be passed
# to the forecaster as exogenous variable.
# to the forecaster as an exogenous variable.
if not x:
self.predictive_model.learn_one(y=y)
else:
Expand All @@ -144,25 +144,25 @@ def score_one(self, x: dict, y: base.typing.Target):
else:
y_pred = self.predictive_model.predict_one(x)

# Calculate the errors necessary for thresholding
# Calculate the squared error
squared_error = (y_pred - y) ** 2

# Based on the errors and hyperparameters, calculate threshold
# Calculate the threshold
threshold = self.dynamic_mae.get() + (
self.n_std * math.sqrt(self.dynamic_se_variance.get())
)

# When warmup hyper-parameter is used, the anomaly score is only returned once the warmup period has passed.
# When the warmup period has not passed, the default value of the anomaly score is 0.
# When the warmup period has not passed, the default value of the anomaly score is 0.0
if self.iter < self.warmup_period:
return 0.0

# Updating metrics only when not in warmup
# Update MAE and SEV when the warm-up parameter has passed.
self.dynamic_mae.update(squared_error)
self.dynamic_se_variance.update(squared_error)

# Every error above threshold is scored with 100% or 1.0
# Everything below is distributed linearly from 0.0 - 0.999...
# An error above the threshold will result in a score of 1.0.
# Else, the score will be linearly distributed within the interval (0.0, 1.0)
if squared_error >= threshold:
return 1.0
else:
Expand Down

0 comments on commit c3ea315

Please sign in to comment.