shunping commented on code in PR #34232: URL: https://github.com/apache/beam/pull/34232#discussion_r1987795493
########## sdks/python/apache_beam/ml/anomaly/detectors/robust_zscore.py: ########## @@ -0,0 +1,119 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import logging +import math +from typing import Optional + +import apache_beam as beam +from apache_beam.ml.anomaly.base import AnomalyDetector +from apache_beam.ml.anomaly.specifiable import specifiable +from apache_beam.ml.anomaly.thresholds import FixedThreshold +from apache_beam.ml.anomaly.univariate.base import EPSILON +from apache_beam.ml.anomaly.univariate.mad import MadTracker + + +# pylint: disable=line-too-long +@specifiable +class RobustZScore(AnomalyDetector): + """Robust Z-Score anomaly detector. + + This class implements an detection algorithm based on Robust Z-Score (also + known as Modified Z-Score), which is a robust alternative to the traditional + Z-score [#]_. It uses the median and Median Absolute Deviation (MAD) to + compute a score that is less sensitive to outliers. + + The score is calculated as: `|0.6745 * (value - median) / MAD|` Review Comment: Agree. It is a commonly used constant that has theoretical meaning: https://en.wikipedia.org/wiki/Median_absolute_deviation#Derivation. Also, instead of letting users set a different scale factor here, they can achieve the same effect by using a different threshold function (e.g. `FixedThreshold(4)`). In this way, we can keep consistency between traditional Z-score and its robust version. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
