|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.ml.dataframe.evaluation.regression; |
| 20 | + |
| 21 | +import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; |
| 22 | +import org.elasticsearch.common.ParseField; |
| 23 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 24 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 26 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Objects; |
| 30 | + |
| 31 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; |
| 32 | + |
| 33 | +/** |
| 34 | + * Calculates R-Squared between two known numerical fields. |
| 35 | + * |
| 36 | + * equation: mse = 1 - SSres/SStot |
| 37 | + * such that, |
| 38 | + * SSres = Σ(y - y´)^2 |
| 39 | + * SStot = Σ(y - y_mean)^2 |
| 40 | + */ |
| 41 | +public class RSquaredMetric implements EvaluationMetric { |
| 42 | + |
| 43 | + public static final String NAME = "r_squared"; |
| 44 | + |
| 45 | + private static final ObjectParser<RSquaredMetric, Void> PARSER = |
| 46 | + new ObjectParser<>("r_squared", true, RSquaredMetric::new); |
| 47 | + |
| 48 | + public static RSquaredMetric fromXContent(XContentParser parser) { |
| 49 | + return PARSER.apply(parser, null); |
| 50 | + } |
| 51 | + |
| 52 | + public RSquaredMetric() { |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 58 | + builder.startObject(); |
| 59 | + builder.endObject(); |
| 60 | + return builder; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean equals(Object o) { |
| 65 | + if (this == o) return true; |
| 66 | + if (o == null || getClass() != o.getClass()) return false; |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public int hashCode() { |
| 72 | + // create static hash code from name as there are currently no unique fields per class instance |
| 73 | + return Objects.hashCode(NAME); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public String getName() { |
| 78 | + return NAME; |
| 79 | + } |
| 80 | + |
| 81 | + public static class Result implements EvaluationMetric.Result { |
| 82 | + |
| 83 | + public static final ParseField VALUE = new ParseField("value"); |
| 84 | + private final double value; |
| 85 | + |
| 86 | + public static Result fromXContent(XContentParser parser) { |
| 87 | + return PARSER.apply(parser, null); |
| 88 | + } |
| 89 | + |
| 90 | + private static final ConstructingObjectParser<Result, Void> PARSER = |
| 91 | + new ConstructingObjectParser<>("r_squared_result", true, args -> new Result((double) args[0])); |
| 92 | + |
| 93 | + static { |
| 94 | + PARSER.declareDouble(constructorArg(), VALUE); |
| 95 | + } |
| 96 | + |
| 97 | + public Result(double value) { |
| 98 | + this.value = value; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 103 | + builder.startObject(); |
| 104 | + builder.field(VALUE.getPreferredName(), value); |
| 105 | + builder.endObject(); |
| 106 | + return builder; |
| 107 | + } |
| 108 | + |
| 109 | + public double getValue() { |
| 110 | + return value; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String getMetricName() { |
| 115 | + return NAME; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean equals(Object o) { |
| 120 | + if (this == o) return true; |
| 121 | + if (o == null || getClass() != o.getClass()) return false; |
| 122 | + Result that = (Result) o; |
| 123 | + return Objects.equals(that.value, this.value); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public int hashCode() { |
| 128 | + return Objects.hash(value); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments