Skip to content

Commit 2c8d087

Browse files
committed
Move p-value computation to ChangeDetector
1 parent c2e6f2c commit 2c8d087

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/changepoint/ChangeDetector.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public class ChangeDetector {
4444
this.values = bucketValues.getValues();
4545
}
4646

47-
ChangeType detect(double pValueThreshold) {
47+
ChangeType detect(double minBucketsPValue) {
48+
// This was obtained by simulating the test power for a fixed size effect as a
49+
// function of the bucket value count.
50+
double pValueThreshold = minBucketsPValue * Math.exp(-0.04 * (values.length - 2 * (ChangePointAggregator.MINIMUM_BUCKETS + 1)));
4851
return testForChange(pValueThreshold).changeType(bucketValues, slope(values));
4952
}
5053

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/changepoint/ChangePointAggregator.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ public class ChangePointAggregator extends SiblingPipelineAggregator {
3131
static final double P_VALUE_THRESHOLD = 0.01;
3232
static final int MINIMUM_BUCKETS = 10;
3333

34-
private static double changePValueThreshold(int nValues) {
35-
// This was obtained by simulating the test power for a fixed size effect as a
36-
// function of the bucket value count.
37-
return P_VALUE_THRESHOLD * Math.exp(-0.04 * (double) (nValues - 2 * (MINIMUM_BUCKETS + 1)));
38-
}
39-
4034
public ChangePointAggregator(String name, String bucketsPath, Map<String, Object> metadata) {
4135
super(name, new String[] { bucketsPath }, metadata);
4236
}
@@ -82,24 +76,22 @@ static ChangeType getChangeType(MlAggsHelper.DoubleBucketValues bucketValues) {
8276
);
8377
}
8478

85-
ChangeType spikeOrDip = testForSpikeOrDip(bucketValues, P_VALUE_THRESHOLD);
86-
ChangeType change = new ChangeDetector(bucketValues).detect(changePValueThreshold(bucketValues.getValues().length));
87-
logger.trace("change p-value: [{}]", change.pValue());
88-
if (spikeOrDip.pValue() < change.pValue()) {
89-
change = spikeOrDip;
90-
}
91-
return change;
92-
}
93-
94-
static ChangeType testForSpikeOrDip(MlAggsHelper.DoubleBucketValues bucketValues, double pValueThreshold) {
79+
ChangeType spikeOrDip;
9580
try {
9681
SpikeAndDipDetector detect = new SpikeAndDipDetector(bucketValues);
97-
ChangeType result = detect.detect(pValueThreshold);
98-
logger.trace("spike or dip p-value: [{}]", result.pValue());
99-
return result;
82+
spikeOrDip = detect.detect(P_VALUE_THRESHOLD);
83+
logger.trace("spike or dip p-value: [{}]", spikeOrDip.pValue());
10084
} catch (NotStrictlyPositiveException nspe) {
10185
logger.debug("failure testing for dips and spikes", nspe);
102-
return new Indeterminable("failure testing for dips and spikes");
86+
spikeOrDip = new Indeterminable("failure testing for dips and spikes");
87+
}
88+
89+
ChangeType change = new ChangeDetector(bucketValues).detect(P_VALUE_THRESHOLD);
90+
logger.trace("change p-value: [{}]", change.pValue());
91+
92+
if (spikeOrDip.pValue() < change.pValue()) {
93+
change = spikeOrDip;
10394
}
95+
return change;
10496
}
10597
}

0 commit comments

Comments
 (0)