deequ 2.0.1 anomaly的代码简单分析
创始人
2024-02-07 23:43:51
0

anomaly 总体说明

anomaly是基于两次check的结果,因此必须使用repository 进行比较,比较方法被称为strategy,有如下几种:

  • OnlineNormalStrategy 直接override def detect ,根据平均值和标准方差进行比较 mean and standard deviation.
  • SimpleThresholdStrategy : 值必须在指定范围内
  • BatchNormalStrategy :正太分布检测,所有数据在 mean lowerDeviationFactor * stdDev 和 mean + upperDeviationFactor * stdDev之间
  • AbsoluteChangeStrategy 两次变化的绝对数量,使用super trait的diff:减法
  • RelativeRateOfChangeStrategy ,两次变化的比例,diff函数中使用除法

repository

repository 目前只有文件及内存两种,文件其实就是json格式的结果保存(带timestamp 及tag:即key)

核心代码

生成Check,check包括3部分:

 /*** Add a check using Anomaly Detection methods. The Anomaly Detection Strategy only checks* if the new value is an Anomaly.** @param anomalyDetectionStrategy 策略,可以见上一节 The anomaly detection strategy* @param analyzer 和普通check一样的Analyzer,比如size,containUrl等The analyzer for the metric to run anomaly detection on* @param anomalyCheckConfig Some configuration settings for the Check 配置,主要是指定描述、指定可以比较的上一次结果的生成日期访问、tag等等,可以忽略不用,example里面也没有用*/def addAnomalyCheck[S <: State[S]](anomalyDetectionStrategy: AnomalyDetectionStrategy,analyzer: Analyzer[S, Metric[Double]],anomalyCheckConfig: Option[AnomalyCheckConfig] = None): this.type = {val anomalyCheckConfigOrDefault = anomalyCheckConfig.getOrElse {val checkDescription = s"Anomaly check for ${analyzer.toString}"AnomalyCheckConfig(CheckLevel.Warning, checkDescription)}checks :+= VerificationRunBuilderHelper.getAnomalyCheck(metricsRepository.get,anomalyDetectionStrategy, analyzer, anomalyCheckConfigOrDefault)this}

checkConfig 类

/*** Configuration for an anomaly check** @param level         Assertion level of the check group. If any of the constraints fail this*                      level is used for the status of the check.* @param description   The name describes the check block. Generally will be used to show in*                      the logs.* @param withTagValues Can contain a Map with tag names and the corresponding values to filter*                      for the Anomaly Detection* @param afterDate     The minimum dateTime of previous AnalysisResults to use for the*                      Anomaly Detection* @param beforeDate    The maximum dateTime of previous AnalysisResults to use for the*                      Anomaly Detection* @return*/
case class AnomalyCheckConfig(level: CheckLevel.Value,description: String,withTagValues: Map[String, String] = Map.empty,afterDate: Option[Long] = None,beforeDate: Option[Long] = None)

默认Constraint

 def anomalyConstraint[S <: State[S]](analyzer: Analyzer[S, Metric[Double]],anomalyAssertion: Double => Boolean,hint: Option[String] = None): Constraint = {val constraint = AnalysisBasedConstraint[S, Double, Double](analyzer, anomalyAssertion,hint = hint)new NamedConstraint(constraint, s"AnomalyConstraint($analyzer)")}

实际比较在detect 或者diff 函数内,不同strategy 类代码稍有不同

// 默认detect调用diff进行比较
override def detect(dataSeries: Vector[Double],searchInterval: (Int, Int))
: Seq[(Int, Anomaly)] = {val (start, end) = searchIntervalrequire(start <= end,"The start of the interval cannot be larger than the end.")val startPoint = Seq(start - order, 0).maxval data = diff(DenseVector(dataSeries.slice(startPoint, end): _*), order).datadata.zipWithIndex.filter { case (value, _) =>(value < maxRateDecrease.getOrElse(Double.MinValue)|| value > maxRateIncrease.getOrElse(Double.MaxValue))}.map { case (change, index) =>(index + startPoint + order, Anomaly(Option(dataSeries(index + startPoint + order)), 1.0,Some(s"[AbsoluteChangeStrategy]: Change of $change is not in bounds [" +s"${maxRateDecrease.getOrElse(Double.MinValue)}, " +s"${maxRateIncrease.getOrElse(Double.MaxValue)}]. Order=$order")))}
}/**
RelativeRateOfChangeStrategy 的diff ,除法* Calculates the rate of change with respect to the specified order.* If the order is set to 1, the resulting value for a point at index i* is equal to dataSeries (i) / dataSeries(i - 1).* Note that this difference cannot be calculated for the first [[order]] elements in the vector.* The resulting vector is therefore smaller by [[order]] elements.** @param dataSeries The values contained in a DenseVector[Double]* @param order      The order of the derivative.* @return A vector with the resulting rates of change for all values*         except the first [[order]] elements.*/
override def diff(dataSeries: DenseVector[Double], order: Int): DenseVector[Double] = {require(order > 0, "Order of diff cannot be zero or negative")if (dataSeries.length == 0) {dataSeries} else {val valuesRight = dataSeries.slice(order, dataSeries.length)val valuesLeft = dataSeries.slice(0, dataSeries.length - order)valuesRight / valuesLeft}
}// AbsoluteChangeStrategy 使用super trait的diff:减法
def diff(dataSeries: DenseVector[Double], order: Int): DenseVector[Double] = {require(order >= 0, "Order of diff cannot be negative")if (order == 0 || dataSeries.length == 0) {dataSeries} else {val valuesRight = dataSeries.slice(1, dataSeries.length)val valuesLeft = dataSeries.slice(0, dataSeries.length - 1)diff(valuesRight - valuesLeft, order - 1)}
}

OnlineNormalStrategy 直接override def detect ,根据平均值和标准方差进行比较 mean and standard deviation.

SimpleThresholdStrategy : 值必须在指定范围内

BatchNormalStrategy :正太分布检测,所有数据在 mean - lowerDeviationFactor * stdDev 和 mean + upperDeviationFactor * stdDev之间

相关内容

热门资讯

诺安积极回报混合A净值下跌3.... 诺安积极回报灵活配置混合型证券投资基金(简称:诺安积极回报混合A,代码001706)公布5月15日最...
易方达先锋成长混合C净值下跌3... 易方达先锋成长混合型证券投资基金(简称:易方达先锋成长混合C,代码011892)公布5月15日最新净...
永赢数字经济智选混合发起A净值... 永赢数字经济智选混合型发起式证券投资基金(简称:永赢数字经济智选混合发起A,代码018122)公布5...
中银证券优选行业龙头混合C净值... 中银证券优选行业龙头混合型证券投资基金(简称:中银证券优选行业龙头混合C,代码009641)公布5月...
恒越成长精选混合C净值下跌3.... 恒越成长精选混合型证券投资基金(简称:恒越成长精选混合C,代码010623)公布5月15日最新净值,...
新民发现解放战争时期烈士墓碑 转自:沈阳日报  近日,新民市兴隆堡镇西高力村村民在农田里发现一烈士墓碑,烈士名字叫李义,家乡为宾县...
国泰黄金ETF联接C净值下跌3... 国泰黄金交易型开放式证券投资基金联接基金(简称:国泰黄金ETF联接C,代码004253)公布5月15...
先锋聚优A净值下跌3.42% 先锋聚优灵活配置混合型证券投资基金(简称:先锋聚优A,代码004726)公布5月15日最新净值,下跌...
财通匠心优选一年持有期混合C净... 财通匠心优选一年持有期混合型证券投资基金(简称:财通匠心优选一年持有期混合C,代码014916)公布...
“工字号”体检车进企业送健康 转自:沈阳日报  5月15日一大早,一辆崭新的大巴车就停在顺丰快递沈阳分拨中心,大巴车的车身上印着“...
易方达产业机遇混合C净值下跌3... 易方达产业机遇混合型证券投资基金(简称:易方达产业机遇混合C,代码021180)公布5月15日最新净...
华夏中证云计算与大数据主题ET... 华夏中证云计算与大数据主题交易型开放式指数证券投资基金发起式联接基金(简称:华夏中证云计算与大数据主...
东兴数字经济混合发起A净值下跌... 东兴数字经济混合型发起式证券投资基金(简称:东兴数字经济混合发起A,代码020440)公布5月15日...
国泰优势行业混合A净值下跌3.... 国泰优势行业混合型证券投资基金(简称:国泰优势行业混合A,代码005819)公布5月15日最新净值,...
南方中证全指计算机ETF净值下... 南方中证全指计算机交易型开放式指数证券投资基金(简称:南方中证全指计算机ETF,代码159586)公...
合煦智远金融科技指数(LOF)... 合煦智远国证香蜜湖金融科技指数证券投资基金(LOF)(简称:合煦智远金融科技指数(LOF)C,代码1...
信澳业绩驱动混合A净值下跌3.... 信澳业绩驱动混合型证券投资基金(简称:信澳业绩驱动混合A,代码016370)公布5月15日最新净值,...
财通景气甄选一年持有期混合C净... 财通景气甄选一年持有期混合型证券投资基金(简称:财通景气甄选一年持有期混合C,代码017491)公布...
中航机遇领航混合发起C净值下跌... 中航机遇领航混合型发起式证券投资基金(简称:中航机遇领航混合发起C,代码018957)公布5月15日...
相信中国就是相信明天   近日,国家主席习近平复信中国丹麦商会负责人,勉励中国丹麦商会及会员企业为增进中丹、中欧友好和深化...