Skip to content

Commit 62550a5

Browse files
committed
Fix precision issue
1 parent 1ee03c0 commit 62550a5

File tree

1 file changed

+23
-23
lines changed
  • x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue

1 file changed

+23
-23
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue/ConfidenceInterval.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,19 @@ static void process(DoubleBlock.Builder builder, int position, DoubleBlock bestE
148148
assert bestEstimateBlock.getValueCount(position) == 1 : "bestEstimate: expected 1 element, got " + bestEstimateBlock.getValueCount(position);
149149
assert bucketCountBlock.getValueCount(position) == 1 : "bucketCount: expected 1 element, got " + bucketCountBlock.getValueCount(position);
150150
assert emptyBucketValueBlock.getValueCount(position) == 1 : "emptyBucketValue: expected 1 element, got " + emptyBucketValueBlock.getValueCount(position);
151-
double bestEstimate = bestEstimateBlock.getDouble(bestEstimateBlock.getFirstValueIndex(position));
151+
Number bestEstimate = bestEstimateBlock.getDouble(bestEstimateBlock.getFirstValueIndex(position));
152152
int bucketCount = bucketCountBlock.getInt(bucketCountBlock.getFirstValueIndex(position));
153153
double emptyBucketValue = emptyBucketValueBlock.getDouble(emptyBucketValueBlock.getFirstValueIndex(position));
154154

155-
double[] estimates = new double[estimatesBlock.getValueCount(position)];
155+
Number[] estimates = new Number[estimatesBlock.getValueCount(position)];
156156
for (int i = 0; i < estimatesBlock.getValueCount(position); i++) {
157157
estimates[i] = estimatesBlock.getDouble(estimatesBlock.getFirstValueIndex(position) + i);
158158
}
159159

160-
double[] confidenceInterval = computeConfidenceInterval(bestEstimate, estimates, bucketCount, emptyBucketValue);
160+
Number[] confidenceInterval = computeConfidenceInterval(bestEstimate, estimates, bucketCount, emptyBucketValue);
161161
builder.beginPositionEntry();
162-
for (double v : confidenceInterval) {
163-
builder.appendDouble(v);
162+
for (Number v : confidenceInterval) {
163+
builder.appendDouble(v.doubleValue());
164164
}
165165
builder.endPositionEntry();
166166

@@ -172,19 +172,19 @@ static void process(IntBlock.Builder builder, int position, IntBlock bestEstimat
172172
assert bestEstimateBlock.getValueCount(position) == 1 : "bestEstimate: expected 1 element, got " + bestEstimateBlock.getValueCount(position);
173173
assert bucketCountBlock.getValueCount(position) == 1 : "bucketCount: expected 1 element, got " + bucketCountBlock.getValueCount(position);
174174
assert emptyBucketValueBlock.getValueCount(position) == 1 : "emptyBucketValue: expected 1 element, got " + emptyBucketValueBlock.getValueCount(position);
175-
double bestEstimate = bestEstimateBlock.getInt(bestEstimateBlock.getFirstValueIndex(position));
175+
Number bestEstimate = bestEstimateBlock.getInt(bestEstimateBlock.getFirstValueIndex(position));
176176
int bucketCount = bucketCountBlock.getInt(bucketCountBlock.getFirstValueIndex(position));
177177
double emptyBucketValue = emptyBucketValueBlock.getDouble(emptyBucketValueBlock.getFirstValueIndex(position));
178178

179-
double[] estimates = new double[estimatesBlock.getValueCount(position)];
179+
Number[] estimates = new Number[estimatesBlock.getValueCount(position)];
180180
for (int i = 0; i < estimatesBlock.getValueCount(position); i++) {
181181
estimates[i] = estimatesBlock.getInt(estimatesBlock.getFirstValueIndex(position) + i);
182182
}
183183

184-
double[] confidenceInterval = computeConfidenceInterval(bestEstimate, estimates, bucketCount, emptyBucketValue);
184+
Number[] confidenceInterval = computeConfidenceInterval(bestEstimate, estimates, bucketCount, emptyBucketValue);
185185
builder.beginPositionEntry();
186-
for (double v : confidenceInterval) {
187-
builder.appendInt((int) v);
186+
for (Number v : confidenceInterval) {
187+
builder.appendInt(v.intValue());
188188
}
189189
builder.endPositionEntry();
190190
}
@@ -194,32 +194,32 @@ static void process(LongBlock.Builder builder, int position, LongBlock bestEstim
194194
assert bestEstimateBlock.getValueCount(position) == 1 : "bestEstimate: expected 1 element, got " + bestEstimateBlock.getValueCount(position);
195195
assert bucketCountBlock.getValueCount(position) == 1 : "bucketCount: expected 1 element, got " + bucketCountBlock.getValueCount(position);
196196
assert emptyBucketValueBlock.getValueCount(position) == 1 : "emptyBucketValue: expected 1 element, got " + emptyBucketValueBlock.getValueCount(position);
197-
double bestEstimate = bestEstimateBlock.getLong(bestEstimateBlock.getFirstValueIndex(position));
197+
Number bestEstimate = bestEstimateBlock.getLong(bestEstimateBlock.getFirstValueIndex(position));
198198
int bucketCount = bucketCountBlock.getInt(bucketCountBlock.getFirstValueIndex(position));
199199
double emptyBucketValue = emptyBucketValueBlock.getDouble(emptyBucketValueBlock.getFirstValueIndex(position));
200200

201-
double[] estimates = new double[estimatesBlock.getValueCount(position)];
201+
Number[] estimates = new Number[estimatesBlock.getValueCount(position)];
202202
for (int i = 0; i < estimatesBlock.getValueCount(position); i++) {
203203
estimates[i] = estimatesBlock.getLong(estimatesBlock.getFirstValueIndex(position) + i);
204204
}
205205

206-
double[] confidenceInterval = computeConfidenceInterval(bestEstimate, estimates, bucketCount, emptyBucketValue);
206+
Number[] confidenceInterval = computeConfidenceInterval(bestEstimate, estimates, bucketCount, emptyBucketValue);
207207
builder.beginPositionEntry();
208-
for (double v : confidenceInterval) {
209-
builder.appendLong((long) v);
208+
for (Number v : confidenceInterval) {
209+
builder.appendLong(v.longValue());
210210
}
211211
builder.endPositionEntry();
212212
}
213213

214-
private static double[] computeConfidenceInterval(double bestEstimate, double[] estimates, int bucketCount, double emptyBucketValue) {
214+
private static Number[] computeConfidenceInterval(Number bestEstimate, Number[] estimates, int bucketCount, double emptyBucketValue) {
215215
System.out.println("@@@ computeConfidenceInterval: bestEstimate = " + bestEstimate + ", estimates = " + Arrays.toString(estimates) + ", bucketCount = " + bucketCount + ", emptyBucketValue = " + emptyBucketValue);
216216
Mean estimatesMean = new Mean();
217217
StandardDeviation estimatesStdDev = new StandardDeviation(false);
218218
Skewness estimatesSkew = new Skewness();
219-
for (double estimate : estimates) {
220-
estimatesMean.increment(estimate);
221-
estimatesStdDev.increment(estimate);
222-
estimatesSkew.increment(estimate);
219+
for (Number estimate : estimates) {
220+
estimatesMean.increment(estimate.doubleValue());
221+
estimatesStdDev.increment(estimate.doubleValue());
222+
estimatesSkew.increment(estimate.doubleValue());
223223
}
224224
if (Double.isNaN(emptyBucketValue) == false) {
225225
for (int i = 0; i < bucketCount - estimates.length; i++) {
@@ -233,21 +233,21 @@ private static double[] computeConfidenceInterval(double bestEstimate, double[]
233233
double sm = estimatesStdDev.getResult();
234234

235235
if (sm == 0.0) {
236-
return new double[] { bestEstimate, bestEstimate, bestEstimate };
236+
return new Number[] { bestEstimate, bestEstimate, bestEstimate };
237237
}
238238

239239
double a = estimatesSkew.getResult() / 6;
240240

241241
NormalDistribution norm = new NormalDistribution(0, 1);
242242

243-
double z0 = (bestEstimate - mm) / sm;
243+
double z0 = (bestEstimate.doubleValue() - mm) / sm;
244244
double dz = norm.inverseCumulativeProbability((1 + 0.95) / 2); // for 95% confidence interval
245245
double zl = z0 - dz;
246246
double zu = z0 + dz;
247247

248248
sm /= Math.sqrt(estimatesMean.getN());
249249

250-
return new double[] {
250+
return new Number[] {
251251
mm + sm * (z0 + zl / (1 - Math.min(0.8, a * zl))),
252252
bestEstimate,
253253
mm + sm * (z0 + zu / (1 - Math.min(0.8, a * zu))),

0 commit comments

Comments
 (0)