|
| 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 | + |
| 20 | +package org.elasticsearch.search.aggregations.metrics; |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * Used to calculate sums using the Kahan summation algorithm. |
| 25 | + * |
| 26 | + * <p>The Kahan summation algorithm (also known as compensated summation) reduces the numerical errors that |
| 27 | + * occur when adding a sequence of finite precision floating point numbers. Numerical errors arise due to |
| 28 | + * truncation and rounding. These errors can lead to numerical instability. |
| 29 | + * |
| 30 | + * @see <a href="http://en.wikipedia.org/wiki/Kahan_summation_algorithm">Kahan Summation Algorithm</a> |
| 31 | + */ |
| 32 | +public class CompensatedSum { |
| 33 | + |
| 34 | + private static final double NO_CORRECTION = 0.0; |
| 35 | + |
| 36 | + private double value; |
| 37 | + private double delta; |
| 38 | + |
| 39 | + /** |
| 40 | + * Used to calculate sums using the Kahan summation algorithm. |
| 41 | + * |
| 42 | + * @param value the sum |
| 43 | + * @param delta correction term |
| 44 | + */ |
| 45 | + public CompensatedSum(double value, double delta) { |
| 46 | + this.value = value; |
| 47 | + this.delta = delta; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * The value of the sum. |
| 52 | + */ |
| 53 | + public double value() { |
| 54 | + return value; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * The correction term. |
| 59 | + */ |
| 60 | + public double delta() { |
| 61 | + return delta; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Increments the Kahan sum by adding a value without a correction term. |
| 66 | + */ |
| 67 | + public CompensatedSum add(double value) { |
| 68 | + return add(value, NO_CORRECTION); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Increments the Kahan sum by adding two sums, and updating the correction term for reducing numeric errors. |
| 73 | + */ |
| 74 | + public CompensatedSum add(double value, double delta) { |
| 75 | + // If the value is Inf or NaN, just add it to the running tally to "convert" to |
| 76 | + // Inf/NaN. This keeps the behavior bwc from before kahan summing |
| 77 | + if (Double.isFinite(value) == false) { |
| 78 | + this.value = value + this.value; |
| 79 | + } |
| 80 | + |
| 81 | + if (Double.isFinite(this.value)) { |
| 82 | + double correctedSum = value + (this.delta + delta); |
| 83 | + double updatedValue = this.value + correctedSum; |
| 84 | + this.delta = correctedSum - (updatedValue - this.value); |
| 85 | + this.value = updatedValue; |
| 86 | + } |
| 87 | + |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | +} |
| 93 | + |
0 commit comments