-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Implement MSLE (MeanSquaredLogarithmicError) evaluation metric for regression analysis #58684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
przemekwitek
merged 3 commits into
elastic:master
from
przemekwitek:evaluation_loss_functions
Jun 30, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
...icsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetric.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch 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. | ||
| */ | ||
| package org.elasticsearch.client.ml.dataframe.evaluation.regression; | ||
|
|
||
| import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; | ||
| import org.elasticsearch.common.Nullable; | ||
| import org.elasticsearch.common.ParseField; | ||
| import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
| import org.elasticsearch.common.xcontent.ToXContent; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
|
|
||
| import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
| import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; | ||
|
|
||
| /** | ||
| * Calculates the mean squared error between two known numerical fields. | ||
| * | ||
| * equation: msle = 1/n * Σ(log(y + offset) - log(y´ + offset))^2 | ||
| * where offset is used to make sure the argument to log function is always positive | ||
| */ | ||
| public class MeanSquaredLogarithmicErrorMetric implements EvaluationMetric { | ||
|
|
||
| public static final String NAME = "mean_squared_logarithmic_error"; | ||
|
|
||
| public static final ParseField OFFSET = new ParseField("offset"); | ||
|
|
||
| private static final ConstructingObjectParser<MeanSquaredLogarithmicErrorMetric, Void> PARSER = | ||
| new ConstructingObjectParser<>(NAME, true, args -> new MeanSquaredLogarithmicErrorMetric((Double) args[0])); | ||
|
|
||
| static { | ||
| PARSER.declareDouble(optionalConstructorArg(), OFFSET); | ||
| } | ||
|
|
||
| public static MeanSquaredLogarithmicErrorMetric fromXContent(XContentParser parser) { | ||
| return PARSER.apply(parser, null); | ||
| } | ||
|
|
||
| private final Double offset; | ||
|
|
||
| public MeanSquaredLogarithmicErrorMetric(@Nullable Double offset) { | ||
| this.offset = offset; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
| builder.startObject(); | ||
| if (offset != null) { | ||
| builder.field(OFFSET.getPreferredName(), offset); | ||
| } | ||
| builder.endObject(); | ||
| return builder; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| MeanSquaredLogarithmicErrorMetric that = (MeanSquaredLogarithmicErrorMetric) o; | ||
| return Objects.equals(this.offset, that.offset); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(offset); | ||
| } | ||
|
|
||
| public static class Result implements EvaluationMetric.Result { | ||
|
|
||
| public static final ParseField ERROR = new ParseField("error"); | ||
| private final double error; | ||
|
|
||
| public static Result fromXContent(XContentParser parser) { | ||
| return PARSER.apply(parser, null); | ||
| } | ||
|
|
||
| private static final ConstructingObjectParser<Result, Void> PARSER = | ||
| new ConstructingObjectParser<>("mean_squared_error_result", true, args -> new Result((double) args[0])); | ||
|
|
||
| static { | ||
| PARSER.declareDouble(constructorArg(), ERROR); | ||
| } | ||
|
|
||
| public Result(double error) { | ||
| this.error = error; | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
| builder.startObject(); | ||
| builder.field(ERROR.getPreferredName(), error); | ||
| builder.endObject(); | ||
| return builder; | ||
| } | ||
|
|
||
| public double getError() { | ||
| return error; | ||
| } | ||
|
|
||
| @Override | ||
| public String getMetricName() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Result that = (Result) o; | ||
| return Objects.equals(that.error, this.error); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(error); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ient/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetricResultTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch 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. | ||
| */ | ||
| package org.elasticsearch.client.ml.dataframe.evaluation.regression; | ||
|
|
||
| import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; | ||
| import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.test.AbstractXContentTestCase; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class MeanSquaredLogarithmicErrorMetricResultTests extends AbstractXContentTestCase<MeanSquaredLogarithmicErrorMetric.Result> { | ||
|
|
||
| public static MeanSquaredLogarithmicErrorMetric.Result randomResult() { | ||
| return new MeanSquaredLogarithmicErrorMetric.Result(randomDouble()); | ||
| } | ||
|
|
||
| @Override | ||
| protected MeanSquaredLogarithmicErrorMetric.Result createTestInstance() { | ||
| return randomResult(); | ||
| } | ||
|
|
||
| @Override | ||
| protected MeanSquaredLogarithmicErrorMetric.Result doParseInstance(XContentParser parser) throws IOException { | ||
| return MeanSquaredLogarithmicErrorMetric.Result.fromXContent(parser); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean supportsUnknownFields() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected NamedXContentRegistry xContentRegistry() { | ||
| return new NamedXContentRegistry(new MlEvaluationNamedXContentProvider().getNamedXContentParsers()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.