|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
1 | 8 | package org.elasticsearch.xpack.esql.expression.function.scalar.approximate; |
2 | 9 |
|
3 | | -public class ConfidenceIntervalTests { |
| 10 | +import com.carrotsearch.randomizedtesting.annotations.Name; |
| 11 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 12 | + |
| 13 | +import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 14 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 15 | +import org.elasticsearch.xpack.esql.core.type.DataType; |
| 16 | +import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase; |
| 17 | +import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.function.Supplier; |
| 22 | +import java.util.stream.IntStream; |
| 23 | + |
| 24 | +import static java.lang.Double.NaN; |
| 25 | +import static org.hamcrest.Matchers.both; |
| 26 | +import static org.hamcrest.Matchers.closeTo; |
| 27 | +import static org.hamcrest.Matchers.contains; |
| 28 | +import static org.hamcrest.Matchers.greaterThan; |
| 29 | +import static org.hamcrest.Matchers.lessThan; |
| 30 | +import static org.hamcrest.Matchers.nullValue; |
| 31 | + |
| 32 | +public class ConfidenceIntervalTests extends AbstractScalarFunctionTestCase { |
| 33 | + |
| 34 | + @ParametersFactory |
| 35 | + public static Iterable<Object[]> parameters() { |
| 36 | + List<TestCaseSupplier> suppliers = new ArrayList<>(); |
| 37 | + suppliers.add(randomBuckets()); |
| 38 | + suppliers.add(allBucketsFilled()); |
| 39 | + suppliers.add(nanBuckets_ignoreNan()); |
| 40 | + suppliers.add(nanBuckets_zeroNan()); |
| 41 | + return parameterSuppliersFromTypedDataWithDefaultChecks(false, suppliers); |
| 42 | + } |
| 43 | + |
| 44 | + public ConfidenceIntervalTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) { |
| 45 | + this.testCase = testCaseSupplier.get(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected Expression build(Source source, List<Expression> args) { |
| 50 | + return new ConfidenceInterval(source, args.get(0), args.get(1), args.get(2), args.get(3), args.get(4)); |
| 51 | + } |
| 52 | + |
| 53 | + private static TestCaseSupplier randomBuckets() { |
| 54 | + return new TestCaseSupplier( |
| 55 | + "randomBuckets", |
| 56 | + List.of(DataType.DOUBLE, DataType.DOUBLE, DataType.INTEGER, DataType.INTEGER, DataType.DOUBLE), |
| 57 | + () -> { |
| 58 | + int trialCount = randomIntBetween(1, 10); |
| 59 | + int bucketCount = randomIntBetween(3, 10); |
| 60 | + double confidenceLevel = randomDoubleBetween(0.8, 0.95, true); |
| 61 | + double bestEstimate = bucketCount / 2.0; |
| 62 | + List<Double> estimates = IntStream.range(0, trialCount * bucketCount) |
| 63 | + .mapToDouble(i -> randomDoubleBetween((i % bucketCount), (i % bucketCount) + 1, true)) |
| 64 | + .boxed() |
| 65 | + .toList(); |
| 66 | + return new TestCaseSupplier.TestCase( |
| 67 | + List.of( |
| 68 | + new TestCaseSupplier.TypedData(bestEstimate, DataType.DOUBLE, "bestEstimate"), |
| 69 | + new TestCaseSupplier.TypedData(estimates, DataType.DOUBLE, "estimates"), |
| 70 | + new TestCaseSupplier.TypedData(trialCount, DataType.INTEGER, "trialCount"), |
| 71 | + new TestCaseSupplier.TypedData(bucketCount, DataType.INTEGER, "bucketCount"), |
| 72 | + new TestCaseSupplier.TypedData(confidenceLevel, DataType.DOUBLE, "confidenceLevel") |
| 73 | + ), |
| 74 | + "ConfidenceIntervalEvaluator[bestEstimateBlock=Attribute[channel=0], estimatesBlock=Attribute[channel=1], trialCountBlock=" |
| 75 | + + "Attribute[channel=2], bucketCountBlock=Attribute[channel=3], confidenceLevelBlock=Attribute[channel=4]]", |
| 76 | + DataType.DOUBLE, |
| 77 | + contains( |
| 78 | + both(greaterThan(0.0)).and(lessThan(bestEstimate)), |
| 79 | + both(greaterThan(bestEstimate)).and(lessThan((double) bucketCount)) |
| 80 | + ) |
| 81 | + ); |
| 82 | + } |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + private static TestCaseSupplier allBucketsFilled() { |
| 87 | + return new TestCaseSupplier( |
| 88 | + "allBucketsFilled", |
| 89 | + List.of(DataType.DOUBLE, DataType.DOUBLE, DataType.INTEGER, DataType.INTEGER, DataType.DOUBLE), |
| 90 | + () -> new TestCaseSupplier.TestCase( |
| 91 | + List.of( |
| 92 | + new TestCaseSupplier.TypedData(2.0, DataType.DOUBLE, "bestEstimate"), |
| 93 | + new TestCaseSupplier.TypedData( |
| 94 | + List.of(2.15, 1.73, 2.1, 2.49, 2.41, 2.06, 2.29, 1.97, 1.54, 1.97, 2.41, 1.75, 1.55, 2.33, 1.64), |
| 95 | + DataType.DOUBLE, |
| 96 | + "estimates" |
| 97 | + ), |
| 98 | + new TestCaseSupplier.TypedData(3, DataType.INTEGER, "trialCount"), |
| 99 | + new TestCaseSupplier.TypedData(5, DataType.INTEGER, "bucketCount"), |
| 100 | + new TestCaseSupplier.TypedData(0.8, DataType.DOUBLE, "confidence_level") |
| 101 | + ), |
| 102 | + "ConfidenceIntervalEvaluator[bestEstimateBlock=Attribute[channel=0], estimatesBlock=Attribute[channel=1], trialCountBlock=" |
| 103 | + + "Attribute[channel=2], bucketCountBlock=Attribute[channel=3], confidenceLevelBlock=Attribute[channel=4]]", |
| 104 | + DataType.DOUBLE, |
| 105 | + contains(closeTo(1.8293144967855208, 1e-9), closeTo(2.164428203663303, 1e-9)) |
| 106 | + ) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + private static TestCaseSupplier nanBuckets_ignoreNan() { |
| 111 | + return new TestCaseSupplier( |
| 112 | + "nanBuckets_ignoreNan", |
| 113 | + List.of(DataType.DOUBLE, DataType.DOUBLE, DataType.INTEGER, DataType.INTEGER, DataType.DOUBLE), |
| 114 | + () -> new TestCaseSupplier.TestCase( |
| 115 | + List.of( |
| 116 | + new TestCaseSupplier.TypedData(2.0, DataType.DOUBLE, "bestEstimate"), |
| 117 | + new TestCaseSupplier.TypedData( |
| 118 | + List.of(2.15, NaN, NaN, 2.49, 2.41, NaN, 2.29, NaN, 1.54, 1.97, 2.41, NaN, 1.55, NaN, 1.64), |
| 119 | + DataType.DOUBLE, |
| 120 | + "estimates" |
| 121 | + ), |
| 122 | + new TestCaseSupplier.TypedData(3, DataType.INTEGER, "trialCount"), |
| 123 | + new TestCaseSupplier.TypedData(5, DataType.INTEGER, "bucketCount"), |
| 124 | + new TestCaseSupplier.TypedData(0.8, DataType.DOUBLE, "confidence_level") |
| 125 | + ), |
| 126 | + "ConfidenceIntervalEvaluator[bestEstimateBlock=Attribute[channel=0], estimatesBlock=Attribute[channel=1], trialCountBlock=" |
| 127 | + + "Attribute[channel=2], bucketCountBlock=Attribute[channel=3], confidenceLevelBlock=Attribute[channel=4]]", |
| 128 | + DataType.DOUBLE, |
| 129 | + contains(closeTo(1.8443260740876288, 1e-9), closeTo(2.164997868635109, 1e-9)) |
| 130 | + ) |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + private static TestCaseSupplier nanBuckets_zeroNan() { |
| 135 | + return new TestCaseSupplier( |
| 136 | + "nanBuckets_zeroNan", |
| 137 | + List.of(DataType.DOUBLE, DataType.DOUBLE, DataType.INTEGER, DataType.INTEGER, DataType.DOUBLE), |
| 138 | + () -> new TestCaseSupplier.TestCase( |
| 139 | + List.of( |
| 140 | + new TestCaseSupplier.TypedData(1.0, DataType.DOUBLE, "bestEstimate"), |
| 141 | + new TestCaseSupplier.TypedData( |
| 142 | + List.of(2.15, NaN, NaN, 2.49, 2.41, NaN, 2.29, NaN, 1.54, 1.97, 2.41, NaN, 1.55, NaN, 1.64), |
| 143 | + DataType.DOUBLE, |
| 144 | + "estimates" |
| 145 | + ), |
| 146 | + new TestCaseSupplier.TypedData(3, DataType.INTEGER, "trialCount"), |
| 147 | + new TestCaseSupplier.TypedData(5, DataType.INTEGER, "bucketCount"), |
| 148 | + new TestCaseSupplier.TypedData(0.8, DataType.DOUBLE, "confidence_level") |
| 149 | + ), |
| 150 | + "ConfidenceIntervalEvaluator[bestEstimateBlock=Attribute[channel=0], estimatesBlock=Attribute[channel=1], trialCountBlock=" |
| 151 | + + "Attribute[channel=2], bucketCountBlock=Attribute[channel=3], confidenceLevelBlock=Attribute[channel=4]]", |
| 152 | + DataType.DOUBLE, |
| 153 | + contains(closeTo(0.4041519539094244, 1e-9), closeTo(1.6023321533418913, 1e-9)) |
| 154 | + ) |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + private static TestCaseSupplier inconsistentData() { |
| 159 | + return new TestCaseSupplier( |
| 160 | + "nanBuckets_zeroNan", |
| 161 | + List.of(DataType.DOUBLE, DataType.DOUBLE, DataType.INTEGER, DataType.INTEGER, DataType.DOUBLE), |
| 162 | + () -> new TestCaseSupplier.TestCase( |
| 163 | + List.of( |
| 164 | + new TestCaseSupplier.TypedData(123.456, DataType.DOUBLE, "bestEstimate"), |
| 165 | + new TestCaseSupplier.TypedData( |
| 166 | + List.of(2.15, NaN, NaN, 2.49, 2.41, NaN, 2.29, NaN, 1.54, 1.97, 2.41, NaN, 1.55, NaN, 1.64), |
| 167 | + DataType.DOUBLE, |
| 168 | + "estimates" |
| 169 | + ), |
| 170 | + new TestCaseSupplier.TypedData(3, DataType.INTEGER, "trialCount"), |
| 171 | + new TestCaseSupplier.TypedData(5, DataType.INTEGER, "bucketCount"), |
| 172 | + new TestCaseSupplier.TypedData(0.8, DataType.DOUBLE, "confidence_level") |
| 173 | + ), |
| 174 | + "ConfidenceIntervalEvaluator[bestEstimateBlock=Attribute[channel=0], estimatesBlock=Attribute[channel=1], trialCountBlock=" |
| 175 | + + "Attribute[channel=2], bucketCountBlock=Attribute[channel=3], confidenceLevelBlock=Attribute[channel=4]]", |
| 176 | + DataType.DOUBLE, |
| 177 | + nullValue() |
| 178 | + ) |
| 179 | + ); |
| 180 | + } |
4 | 181 | } |
0 commit comments