Apache Commons logo Apache Commons Statistics

CPD Results

The following document contains the results of PMD's CPD 7.17.0.

Duplications

File Line
org/apache/commons/statistics/descriptive/IntStatistics.java 616
org/apache/commons/statistics/descriptive/LongStatistics.java 594
stat = Statistics.getResultAsIntOrNull(min);
            break;
        case PRODUCT:
            stat = Statistics.getResultAsDoubleOrNull(product);
            break;
        case SKEWNESS:
            stat = getSkewness();
            break;
        case STANDARD_DEVIATION:
            stat = getStandardDeviation();
            break;
        case SUM:
            stat = Statistics.getResultAsBigIntegerOrNull(sum);
            break;
        case SUM_OF_LOGS:
            stat = Statistics.getResultAsDoubleOrNull(sumOfLogs);
            break;
        case SUM_OF_SQUARES:
            stat = Statistics.getResultAsBigIntegerOrNull(sumOfSquares);
            break;
        case VARIANCE:
            stat = getVariance();
            break;
        }
        if (stat != null) {
            return stat;
        }
        throw new IllegalArgumentException(UNSUPPORTED_STATISTIC + statistic);
    }

    /**
     * Gets the geometric mean.
     *
     * @return a geometric mean supplier (or null if unsupported)
     */
    private StatisticResult getGeometricMean() {
        if (sumOfLogs != null) {
            // Return a function that has access to the count and sumOfLogs
            return () -> GeometricMean.computeGeometricMean(count, sumOfLogs);
        }
        return null;
    }

    /**
     * Gets the kurtosis.
     *
     * @return a kurtosis supplier (or null if unsupported)
     */
    private StatisticResult getKurtosis() {
        if (moment instanceof SumOfFourthDeviations) {
            return new Kurtosis((SumOfFourthDeviations) moment)
                .setBiased(config.isBiased())::getAsDouble;
        }
        return null;
    }

    /**
     * Gets the mean.
     *
     * @return a mean supplier (or null if unsupported)
     */
    private StatisticResult getMean() {
        if (sum != null) {
            // Return a function that has access to the count and sum
            final Int128 s = sum.getSum();
            return () -> IntMean.computeMean(s, count);
File Line
org/apache/commons/statistics/descriptive/DoubleStatistics.java 711
org/apache/commons/statistics/descriptive/IntStatistics.java 751
org/apache/commons/statistics/descriptive/LongStatistics.java 729
public DoubleStatistics combine(DoubleStatistics other) {
        // Check compatibility
        Statistics.checkCombineCompatible(min, other.min);
        Statistics.checkCombineCompatible(max, other.max);
        Statistics.checkCombineCompatible(sum, other.sum);
        Statistics.checkCombineCompatible(product, other.product);
        Statistics.checkCombineCompatible(sumOfSquares, other.sumOfSquares);
        Statistics.checkCombineCompatible(sumOfLogs, other.sumOfLogs);
        Statistics.checkCombineAssignable(moment, other.moment);
        // Combine
        count += other.count;
        Statistics.combine(min, other.min);
        Statistics.combine(max, other.max);
        Statistics.combine(sum, other.sum);
        Statistics.combine(product, other.product);
        Statistics.combine(sumOfSquares, other.sumOfSquares);
        Statistics.combine(sumOfLogs, other.sumOfLogs);
        Statistics.combineMoment(moment, other.moment);
        return this;
    }

    /**
     * Sets the statistics configuration.
     *
     * <p>These options only control the final computation of statistics. The configuration
     * will not affect compatibility between instances during a
     * {@link #combine(DoubleStatistics) combine} operation.
     *
     * <p>Note: These options will affect any future computation of statistics. Supplier functions
     * that have been previously created will not be updated with the new configuration.
     *
     * @param v Value.
     * @return {@code this} instance
     * @throws NullPointerException if the value is null
     * @see #getResult(Statistic)
     */
    public DoubleStatistics setConfiguration(StatisticsConfiguration v) {
File Line
org/apache/commons/statistics/descriptive/Median.java 185
org/apache/commons/statistics/descriptive/Median.java 267
final double t = x[start];
                    x[start] = x[start + 1];
                    x[start + 1] = t;
                }
                return Interpolation.mean(x[start], x[start + 1]);
            case 1:
                return x[start];
            default:
                return Double.NaN;
            }
        }
        // Median index (including the offset)
        final int m = (start + end) >>> 1;
        // Odd
        if ((n & 0x1) == 1) {
            Selection.select(x, start, end, m);
            return x[m];
        }
        // Even: require (m-1, m)
        Selection.select(x, start, end, new int[] {m - 1, m});
        return Interpolation.mean(x[m - 1], x[m]);
    }
File Line
org/apache/commons/statistics/descriptive/IntStatistics.java 447
org/apache/commons/statistics/descriptive/LongStatistics.java 447
public void accept(int value) {
        count++;
        consumer.accept(value);
    }

    /**
     * Return the count of values recorded.
     *
     * @return the count of values
     */
    public long getCount() {
        return count;
    }

    /**
     * Check if the specified {@code statistic} is supported.
     *
     * <p>Note: This method will not return {@code false} if the argument is {@code null}.
     *
     * @param statistic Statistic.
     * @return {@code true} if supported
     * @throws NullPointerException if the {@code statistic} is {@code null}
     * @see #getResult(Statistic)
     */
    public boolean isSupported(Statistic statistic) {
        // Check for the appropriate underlying implementation
        // Exhaustive switch statement
        switch (statistic) {
        case GEOMETRIC_MEAN:
        case SUM_OF_LOGS:
            return sumOfLogs != null;
        case KURTOSIS:
            return moment instanceof SumOfFourthDeviations;
        case MAX:
            return max != null;
        case MIN:
            return min != null;
        case PRODUCT:
            return product != null;
        case SKEWNESS:
            return moment instanceof SumOfCubedDeviations;
        case STANDARD_DEVIATION:
        case VARIANCE:
            return sum != null && sumOfSquares != null;
        case MEAN:
        case SUM:
            return sum != null;
        case SUM_OF_SQUARES:
            return sumOfSquares != null;
        }
        // Unreachable code
        throw new IllegalArgumentException(UNSUPPORTED_STATISTIC + statistic);
    }

    /**
     * Gets the value of the specified {@code statistic} as a {@code double}.
     *
     * @param statistic Statistic.
     * @return the value
     * @throws IllegalArgumentException if the {@code statistic} is not supported
     * @see #isSupported(Statistic)
     * @see #getResult(Statistic)
     */
    public double getAsDouble(Statistic statistic) {
        return getResult(statistic).getAsDouble();
    }

    /**
     * Gets the value of the specified {@code statistic} as an {@code int}.
     *
     * <p>Use this method to access the {@code int} result for exact integer statistics,
     * for example {@link Statistic#MIN}.
     *
     * <p>Note: This method may throw an {@link ArithmeticException} if the result
     * overflows an {@code int}.
     *
     * @param statistic Statistic.
     * @return the value
     * @throws IllegalArgumentException if the {@code statistic} is not supported
     * @throws ArithmeticException if the {@code result} overflows an {@code int} or is not
     * finite
     * @see #isSupported(Statistic)
     * @see #getResult(Statistic)
     */
    public int getAsInt(Statistic statistic) {
File Line
org/apache/commons/statistics/descriptive/IntStatistics.java 231
org/apache/commons/statistics/descriptive/LongStatistics.java 231
return new IntStatistics(
                to - from,
                create(min, values, from, to),
                create(max, values, from, to),
                create(moment, values, from, to),
                create(sum, values, from, to),
                create(product, values, from, to),
                create(sumOfSquares, values, from, to),
                create(sumOfLogs, values, from, to),
                config);
        }

        /**
         * Creates the object from the {@code values}.
         *
         * @param <S> value type
         * @param <T> object type
         * @param constructor Constructor.
         * @param values Values
         * @param from Inclusive start of the range.
         * @param to Exclusive end of the range.
         * @return the instance
         */
        private static <S, T> T create(RangeFunction<S, T> constructor, S values, int from, int to) {
            if (constructor != null) {
                return constructor.apply(values, from, to);
            }
            return null;
        }
    }
File Line
org/apache/commons/statistics/descriptive/IntStandardDeviation.java 152
org/apache/commons/statistics/descriptive/IntVariance.java 154
final IntStandardDeviation stat = new IntStandardDeviation();
            for (int i = from; i < to; i++) {
                stat.accept(values[i]);
            }
            return stat;
        }

        // Arrays can be processed using specialised counts knowing the maximum limit
        // for an array is 2^31 values.
        long s = 0;
        final UInt96 ss = UInt96.create();
        // Process pairs as we know two maximum value int^2 will not overflow
        // an unsigned long.
        final int end = from + (length & ~0x1);
        for (int i = from; i < end; i += 2) {
            final long x = values[i];
            final long y = values[i + 1];
            s += x + y;
            ss.addPositive(x * x + y * y);
        }
        if (end < to) {
            final long x = values[end];
            s += x;
            ss.addPositive(x * x);
        }

        // Convert
        return new IntStandardDeviation(UInt128.of(ss), Int128.of(s), length);
File Line
org/apache/commons/statistics/descriptive/Quantile.java 360
org/apache/commons/statistics/descriptive/Quantile.java 549
}

        // Collect interpolation positions. We use the output q as storage.
        final int[] indices = computeIndices(n, p, q, start);

        // Partition
        Selection.select(x, start, end, indices);

        // Compute
        for (int k = 0; k < p.length; k++) {
            // ip in [0, n); i in [start, end)
            final int ip = (int) q[k];
            final int i = start + ip;
            if (q[k] > ip) {
                q[k] = Interpolation.interpolate(x[i], x[i + 1], q[k] - ip);
            } else {
                q[k] = x[i];
            }
        }
        return q;
    }

    /**
     * Evaluate the {@code p}-th quantile of the values.
     *
     * <p>Note: This method may partially sort the input values if not configured to
     * {@link #withCopy(boolean) copy} the input data.
     *
     * <p><strong>Performance</strong>
     *
     * <p>It is not recommended to use this method for repeat calls for different quantiles
     * within the same values. The {@link #evaluate(int[], double...)} method should be used
     * which provides better performance.
     *
     * @param values Values.
     * @param p Probability for the quantile to compute.
     * @return the quantile
     * @throws IllegalArgumentException if the probability {@code p} is not in the range {@code [0, 1]}
     * @see #evaluate(int[], double...)
     */
    public double evaluate(int[] values, double p) {
File Line
org/apache/commons/statistics/descriptive/DoubleStatistics.java 402
org/apache/commons/statistics/descriptive/IntStatistics.java 372
org/apache/commons/statistics/descriptive/LongStatistics.java 372
public static DoubleStatistics ofRange(Set<Statistic> statistics, double[] values, int from, int to) {
        if (statistics.isEmpty()) {
            throw new IllegalArgumentException(NO_CONFIGURED_STATISTICS);
        }
        final Builder b = new Builder();
        statistics.forEach(b::add);
        return b.build(values, from, to);
    }

    /**
     * Returns a new builder configured to create instances to compute the specified
     * {@code statistics}.
     *
     * <p>Use this method to create an instance populated with an array of {@code double[]}
     * data using the {@link Builder#build(double...)} method:
     *
     * <pre>
     * double[] data = ...
     * DoubleStatistics stats = DoubleStatistics.builder(
     *     Statistic.MIN, Statistic.MAX, Statistic.VARIANCE)
     *     .build(data);
     * </pre>
     *
     * <p>The builder can be used to create multiple instances of {@link DoubleStatistics}
     * to be used in parallel, or on separate arrays of {@code double[]} data. These may
     * be {@link #combine(DoubleStatistics) combined}. For example:
     *
     * <pre>
     * double[][] data = ...
     * DoubleStatistics.Builder builder = DoubleStatistics.builder(
     *     Statistic.MIN, Statistic.MAX, Statistic.VARIANCE);
     * DoubleStatistics stats = Arrays.stream(data)
     *     .parallel()
     *     .map(builder::build)
     *     .reduce(DoubleStatistics::combine)
     *     .get();
     * </pre>
     *
     * <p>The builder can be used to create a {@link java.util.stream.Collector} for repeat
     * use on multiple data:
     *
     * <pre>{@code
     * DoubleStatistics.Builder builder = DoubleStatistics.builder(
     *     Statistic.MIN, Statistic.MAX, Statistic.VARIANCE);
     * Collector<double[], DoubleStatistics, DoubleStatistics> collector =
     *     Collector.of(builder::build,
     *                  (s, d) -> s.combine(builder.build(d)),
     *                  DoubleStatistics::combine);
     *
     * // Repeated
     * double[][] data = ...
     * DoubleStatistics stats = Arrays.stream(data).collect(collector);
     * }</pre>
     *
     * @param statistics Statistics to compute.
     * @return the builder
     * @throws IllegalArgumentException if there are no {@code statistics} to compute.
     */
    public static Builder builder(Statistic... statistics) {
        if (statistics.length == 0) {
            throw new IllegalArgumentException(NO_CONFIGURED_STATISTICS);
        }
        final Builder b = new Builder();
        for (final Statistic s : statistics) {
            b.add(s);
        }
        return b;
    }

    /**
     * Updates the state of the statistics to reflect the addition of {@code value}.
     *
     * @param value Value.
     */
    @Override
    public void accept(double value) {
File Line
org/apache/commons/statistics/descriptive/SumOfFourthDeviations.java 242
org/apache/commons/statistics/descriptive/SumOfFourthDeviations.java 294
final IntVariance variance = IntVariance.createFromRange(values, from, to);
        final double xbar = variance.computeMean();
        final double ss = variance.computeSumOfSquaredDeviations();
        // Unlike the double[] case, overflow/NaN is not possible:
        // (max value)^4 times max array length ~ (2^31)^4 * 2^31 ~ 2^155.
        // Compute sum of cubed and fourth deviations together.
        double sc = 0;
        double sq = 0;
        for (int i = from; i < to; i++) {
            final double x = values[i] - xbar;
            final double x2 = x * x;
            sc += x2 * x;
            sq += x2 * x2;
        }
        // Edge case to avoid floating-point error for zero
        if (to - from <= LENGTH_TWO) {
            sc = 0;
        }
        return new SumOfFourthDeviations(sq, sc, ss, xbar, to - from);
    }
File Line
org/apache/commons/statistics/descriptive/Quantile.java 282
org/apache/commons/statistics/descriptive/Quantile.java 461
}

        final double pos = estimationType.index(p, n);
        final int ip = (int) pos;
        final int i = start + ip;

        // Partition and compute
        if (pos > ip) {
            Selection.select(x, start, end, new int[] {i, i + 1});
            return Interpolation.interpolate(x[i], x[i + 1], pos - ip);
        }
        Selection.select(x, start, end, i);
        return x[i];
    }

    /**
     * Evaluate the {@code p}-th quantiles of the values.
     *
     * <p>Note: This method may partially sort the input values if not configured to
     * {@link #withCopy(boolean) copy} the input data.
     *
     * @param values Values.
     * @param p Probabilities for the quantiles to compute.
     * @return the quantiles
     * @throws IllegalArgumentException if any probability {@code p} is not in the range {@code [0, 1]};
     * no probabilities are specified; or if the values contain NaN and the configuration is {@link NaNPolicy#ERROR}
     * @see #with(NaNPolicy)
     */
    public double[] evaluate(double[] values, double... p) {
File Line
org/apache/commons/statistics/descriptive/IntStatistics.java 681
org/apache/commons/statistics/descriptive/LongStatistics.java 659
return () -> IntMean.computeMean(s, count);
        }
        return null;
    }

    /**
     * Gets the skewness.
     *
     * @return a skewness supplier (or null if unsupported)
     */
    private StatisticResult getSkewness() {
        if (moment instanceof SumOfCubedDeviations) {
            return new Skewness((SumOfCubedDeviations) moment)
                .setBiased(config.isBiased())::getAsDouble;
        }
        return null;
    }

    /**
     * Gets the standard deviation.
     *
     * @return a standard deviation supplier (or null if unsupported)
     */
    private StatisticResult getStandardDeviation() {
        return getVarianceOrStd(true);
    }

    /**
     * Gets the variance.
     *
     * @return a variance supplier (or null if unsupported)
     */
    private StatisticResult getVariance() {
        return getVarianceOrStd(false);
    }

    /**
     * Gets the variance or standard deviation.
     *
     * @param std Flag to control if the statistic is the standard deviation.
     * @return a variance/standard deviation supplier (or null if unsupported)
     */
    private StatisticResult getVarianceOrStd(boolean std) {
        if (sum != null && sumOfSquares != null) {
            // Return a function that has access to the count, sum and sum of squares
            final Int128 s = sum.getSum();
            final UInt128 ss = sumOfSquares.getSumOfSquares();