Skip to content

Commit

Permalink
[ML] Add multimodal distribution field processing for anomaly score e…
Browse files Browse the repository at this point in the history
…xplanation (#92978)

The companion PR to elastic/ml-cpp#2440 adds processing of multimodal_distribution field in the anomaly score explanation. I added a changelog entry in the ml-cpp PR hence I mark this PR as a non-issue.
  • Loading branch information
valeriy42 committed Jan 17, 2023
1 parent 06552d0 commit c24712b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/reference/ml/anomaly-detection/apis/get-record.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ reduced. If the bucket contains fewer samples than expected, the score is reduce
`lower_confidence_bound`::::
(Optional, double) Lower bound of the 95% confidence interval.
`multimodal_distribution`::::
(Optional, boolean) Indicates whether the bucket values' probability distribution has
several modes. When there are multiple modes, the typical value may not be the most
likely.
`multi_bucket_impact`::::
(Optional, integer) Impact of the deviation between actual and typical values in the
past 12 buckets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package org.elasticsearch.xpack.core.ml.job.results;

import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
Expand All @@ -31,6 +32,7 @@ public class AnomalyScoreExplanation implements ToXContentObject, Writeable {
public static final ParseField UPPER_CONFIDENCE_BOUND = new ParseField("upper_confidence_bound");
public static final ParseField HIGH_VARIANCE_PENALTY = new ParseField("high_variance_penalty");
public static final ParseField INCOMPLETE_BUCKET_PENALTY = new ParseField("incomplete_bucket_penalty");
public static final ParseField MULTIMODAL_DISTRIBUTION = new ParseField("multimodal_distribution");

public static final ObjectParser<AnomalyScoreExplanation, Void> STRICT_PARSER = createParser(false);
public static final ObjectParser<AnomalyScoreExplanation, Void> LENIENT_PARSER = createParser(true);
Expand All @@ -51,6 +53,7 @@ private static ObjectParser<AnomalyScoreExplanation, Void> createParser(Boolean
parser.declareDouble(AnomalyScoreExplanation::setUpperConfidenceBound, UPPER_CONFIDENCE_BOUND);
parser.declareBoolean(AnomalyScoreExplanation::setHighVariancePenalty, HIGH_VARIANCE_PENALTY);
parser.declareBoolean(AnomalyScoreExplanation::setIncompleteBucketPenalty, INCOMPLETE_BUCKET_PENALTY);
parser.declareBoolean(AnomalyScoreExplanation::setMultimodalDistribution, MULTIMODAL_DISTRIBUTION);
return parser;
}

Expand All @@ -64,6 +67,7 @@ private static ObjectParser<AnomalyScoreExplanation, Void> createParser(Boolean
private Double upperConfidenceBound;
private Boolean highVariancePenalty;
private Boolean incompleteBucketPenalty;
private Boolean multimodalDistribution;

AnomalyScoreExplanation() {}

Expand All @@ -78,6 +82,9 @@ public AnomalyScoreExplanation(StreamInput in) throws IOException {
this.upperConfidenceBound = in.readOptionalDouble();
this.highVariancePenalty = in.readOptionalBoolean();
this.incompleteBucketPenalty = in.readOptionalBoolean();
if (in.getVersion().onOrAfter(Version.V_8_7_0)) {
this.multimodalDistribution = in.readOptionalBoolean();
}
}

@Override
Expand All @@ -92,6 +99,9 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalDouble(upperConfidenceBound);
out.writeOptionalBoolean(highVariancePenalty);
out.writeOptionalBoolean(incompleteBucketPenalty);
if (out.getVersion().onOrAfter(Version.V_8_7_0)) {
out.writeOptionalBoolean(multimodalDistribution);
}
}

@Override
Expand Down Expand Up @@ -127,6 +137,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (incompleteBucketPenalty != null) {
builder.field(INCOMPLETE_BUCKET_PENALTY.getPreferredName(), incompleteBucketPenalty);
}
if (multimodalDistribution != null) {
builder.field(MULTIMODAL_DISTRIBUTION.getPreferredName(), multimodalDistribution);
}
builder.endObject();
return builder;
}
Expand All @@ -143,7 +156,8 @@ public int hashCode() {
typicalValue,
upperConfidenceBound,
highVariancePenalty,
incompleteBucketPenalty
incompleteBucketPenalty,
multimodalDistribution
);
}

Expand All @@ -166,7 +180,8 @@ public boolean equals(Object other) {
&& Objects.equals(this.typicalValue, that.typicalValue)
&& Objects.equals(this.upperConfidenceBound, that.upperConfidenceBound)
&& Objects.equals(this.highVariancePenalty, that.highVariancePenalty)
&& Objects.equals(this.incompleteBucketPenalty, that.incompleteBucketPenalty);
&& Objects.equals(this.incompleteBucketPenalty, that.incompleteBucketPenalty)
&& Objects.equals(this.multimodalDistribution, that.multimodalDistribution);
}

public String getAnomalyType() {
Expand Down Expand Up @@ -249,4 +264,12 @@ public void setIncompleteBucketPenalty(Boolean incompleteBucketPenalty) {
this.incompleteBucketPenalty = incompleteBucketPenalty;
}

public Boolean isMultimodalDistribution() {
return multimodalDistribution;
}

public void setMultimodalDistribution(Boolean multimodalDistribution) {
this.multimodalDistribution = multimodalDistribution;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public final class ReservedFieldNames {
AnomalyScoreExplanation.UPPER_CONFIDENCE_BOUND.getPreferredName(),
AnomalyScoreExplanation.HIGH_VARIANCE_PENALTY.getPreferredName(),
AnomalyScoreExplanation.INCOMPLETE_BUCKET_PENALTY.getPreferredName(),
AnomalyScoreExplanation.MULTIMODAL_DISTRIBUTION.getPreferredName(),

GeoResults.TYPICAL_POINT.getPreferredName(),
GeoResults.ACTUAL_POINT.getPreferredName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@
},
"incomplete_bucket_penalty": {
"type": "boolean"
},
"multimodal_distribution": {
"type": "boolean"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public AnomalyRecord createTestInstance(String jobId) {
anomalyScoreExplanation.setUpperConfidenceBound(randomDouble());
anomalyScoreExplanation.setHighVariancePenalty(randomBoolean());
anomalyScoreExplanation.setIncompleteBucketPenalty(randomBoolean());
anomalyScoreExplanation.setMultimodalDistribution(randomBoolean());
}

return anomalyRecord;
Expand Down

0 comments on commit c24712b

Please sign in to comment.