Skip to content

Commit

Permalink
[plugins-detector] add lower/upper sensitivity to meanVariance (#1269)
Browse files Browse the repository at this point in the history
* [plugins-detector] add lower/upper sensitivity to meanVariance

* add metric min/max value to mean-variance detector
  • Loading branch information
cyrilou242 authored Dec 13, 2023
1 parent ed018e2 commit 8486789
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static ai.startree.thirdeye.spi.Constants.COL_TIME;
import static ai.startree.thirdeye.spi.Constants.COL_UPPER_BOUND;
import static ai.startree.thirdeye.spi.Constants.COL_VALUE;
import static ai.startree.thirdeye.spi.util.SpiUtils.optional;
import static ai.startree.thirdeye.spi.util.TimeUtils.isoPeriod;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
Expand Down Expand Up @@ -60,11 +61,15 @@ public class MeanVarianceRuleDetector implements AnomalyDetector<MeanVarianceRul
);

private Pattern pattern;
private double sensitivity;
private double lowerSensitivity;
private double upperSensitivity;
private int lookback;
private MeanVarianceRuleDetectorSpec spec;
private Period seasonality = Period.ZERO; // PT0S: special period for no seasonality

private double metricMaximumValue;
private double metricMinimumValue;

/**
* Mapping of sensitivity to sigma on range of 0.5 - 1.5
* 10 corresponds to sigma of 0.5
Expand All @@ -89,8 +94,18 @@ protected static int computeSteps(final String periodString,
@Override
public void init(final MeanVarianceRuleDetectorSpec spec) {
this.spec = spec;
pattern = spec.getPattern();
sensitivity = spec.getSensitivity();
this.pattern = spec.getPattern();
if (spec.getLowerSensitivity() != null || spec.getUpperSensitivity() != null) {
checkArgument(spec.getLowerSensitivity() != null, "lowerSensitivity is null. lowerSensitivity must be set when upperSensitivity is set.");
checkArgument(spec.getUpperSensitivity() != null, "upperSensitivity is null. upperSensitivity must be set when lowerSensitivity is set.");
this.lowerSensitivity = spec.getLowerSensitivity();
this.upperSensitivity = spec.getUpperSensitivity();
} else {
this.lowerSensitivity = spec.getSensitivity();
this.upperSensitivity = spec.getSensitivity();
}
this.metricMinimumValue = optional(spec.getMetricMinimumValue()).orElse(Double.NEGATIVE_INFINITY);
this.metricMaximumValue = optional(spec.getMetricMaximumValue()).orElse(Double.POSITIVE_INFINITY);

if (spec.getLookbackPeriod() != null) {
checkArgument(spec.getMonitoringGranularity() != null,
Expand Down Expand Up @@ -203,10 +218,11 @@ private DataFrame computeBaseline(final DataFrame inputDF, final ReadableInterva
std = 0.0;
}
//calculate baseline, error , upper and lower bound for prediction window.
baselineArray[k] = mean;
final double error = sigma(sensitivity) * std;
upperBoundArray[k] = baselineArray[k] + error;
lowerBoundArray[k] = baselineArray[k] - error;
baselineArray[k] = bounded(mean);
final double upperError = sigma(upperSensitivity) * std;
final double lowerError = sigma(lowerSensitivity) * std;
upperBoundArray[k] = bounded(baselineArray[k] + upperError);
lowerBoundArray[k] = bounded(baselineArray[k] - lowerError);
}
//Construct the dataframe.
resultDF
Expand Down Expand Up @@ -261,4 +277,8 @@ private DataFrame getLookbackDf(final DataFrame inputDF, final long endTimeMilli
+ indexStart);
return inputDF.slice(indexStart, indexEnd);
}

private double bounded(final double val) {
return Math.min(metricMaximumValue, Math.max(val, metricMinimumValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public class MeanVarianceRuleDetectorSpec extends AbstractSpec {
/**Biggest period in ISO-8601 format. Possible values are P7D and P1D. Used to take into account seasonality when computing mean-variance.*/
private String seasonalityPeriod = null;

// TODO CYRIL add tests for this feature
private Double upperSensitivity;
private Double lowerSensitivity;

private Double metricMaximumValue;
private Double metricMinimumValue;

public int getLookback() {
return lookback;
}
Expand Down Expand Up @@ -74,4 +81,40 @@ public MeanVarianceRuleDetectorSpec setSeasonalityPeriod(final String seasonalit
this.seasonalityPeriod = seasonalityPeriod;
return this;
}

public Double getUpperSensitivity() {
return upperSensitivity;
}

public MeanVarianceRuleDetectorSpec setUpperSensitivity(final Double upperSensitivity) {
this.upperSensitivity = upperSensitivity;
return this;
}

public Double getLowerSensitivity() {
return lowerSensitivity;
}

public MeanVarianceRuleDetectorSpec setLowerSensitivity(final Double lowerSensitivity) {
this.lowerSensitivity = lowerSensitivity;
return this;
}

public Double getMetricMaximumValue() {
return metricMaximumValue;
}

public MeanVarianceRuleDetectorSpec setMetricMaximumValue(final Double metricMaximumValue) {
this.metricMaximumValue = metricMaximumValue;
return this;
}

public Double getMetricMinimumValue() {
return metricMinimumValue;
}

public MeanVarianceRuleDetectorSpec setMetricMinimumValue(final Double metricMinimumValue) {
this.metricMinimumValue = metricMinimumValue;
return this;
}
}

0 comments on commit 8486789

Please sign in to comment.