You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2023/10/13 18:10:40 UTC
[commons-statistics] 01/02: STATISTICS-71: Add Product statistic
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-statistics.git
commit c3785b1765e50bf46671b6bcd73e0b64689865c4
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Fri Oct 13 19:09:14 2023 +0100
STATISTICS-71: Add Product statistic
---
.../commons/statistics/descriptive/Product.java | 110 +++++++++++++++++++++
.../statistics/descriptive/ProductTest.java | 69 +++++++++++++
2 files changed, 179 insertions(+)
diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/Product.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/Product.java
new file mode 100644
index 0000000..86cfbff
--- /dev/null
+++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/Product.java
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.statistics.descriptive;
+
+/**
+ * Returns the product of the available values.
+ *
+ * <ul>
+ * <li>The result is one if no values are observed.
+ * <li>The result is {@code NaN} if any of the values is {@code NaN}.
+ * </ul>
+ *
+ * <p>This class is designed to work with (though does not require)
+ * {@linkplain java.util.stream streams}.
+ *
+ * <p><strong>This instance is not thread safe.</strong>
+ * If multiple threads access an instance of this class concurrently,
+ * and at least one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or
+ * {@link DoubleStatisticAccumulator#combine(DoubleStatistic) combine} method, it must be synchronized externally.
+ *
+ * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept}
+ * and {@link DoubleStatisticAccumulator#combine(DoubleStatistic) combine}
+ * as {@code accumulator} and {@code combiner} functions of
+ * {@link java.util.stream.Collector Collector} on a parallel stream,
+ * because the parallel instance of {@link java.util.stream.Stream#collect Stream.collect()}
+ * provides the necessary partitioning, isolation, and merging of results for
+ * safe and efficient parallel execution.
+ *
+ * @since 1.1
+ */
+public final class Product implements DoubleStatistic, DoubleStatisticAccumulator<Product> {
+
+ /** Product of all values. */
+ private double productValue = 1;
+
+ /**
+ * Create an instance.
+ */
+ private Product() {
+ // No-op
+ }
+
+ /**
+ * Creates an instance.
+ *
+ * <p>The initial result is one.
+ *
+ * @return {@code Product} instance.
+ */
+ public static Product create() {
+ return new Product();
+ }
+
+ /**
+ * Returns an instance populated using the input {@code values}.
+ *
+ * <p>The result is {@code NaN} if any of the values is {@code NaN}
+ * or the product at any point is a {@code NaN}.
+ *
+ * <p>When the input is an empty array, the result is one.
+ *
+ * @param values Values.
+ * @return {@code Product} instance.
+ */
+ public static Product of(double... values) {
+ return Statistics.add(new Product(), values);
+ }
+
+ /**
+ * Updates the state of the statistic to reflect the addition of {@code value}.
+ *
+ * @param value Value.
+ */
+ @Override
+ public void accept(double value) {
+ this.productValue *= value;
+ }
+
+ /**
+ * Gets the product of all input values.
+ *
+ * <p>When no values have been added, the result is one.
+ *
+ * @return product of all values.
+ */
+ @Override
+ public double getAsDouble() {
+ return productValue;
+ }
+
+ @Override
+ public Product combine(Product other) {
+ productValue *= other.productValue;
+ return this;
+ }
+}
diff --git a/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/ProductTest.java b/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/ProductTest.java
new file mode 100644
index 0000000..5be7320
--- /dev/null
+++ b/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/ProductTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.statistics.descriptive;
+
+import java.util.Arrays;
+import java.util.stream.Stream;
+import org.apache.commons.statistics.distribution.DoubleTolerance;
+import org.apache.commons.statistics.distribution.DoubleTolerances;
+
+/**
+ * Test for {@link Product}.
+ */
+final class ProductTest extends BaseDoubleStatisticTest<Product> {
+
+ @Override
+ protected Product create() {
+ return Product.create();
+ }
+
+ @Override
+ protected Product create(double... values) {
+ return Product.of(values);
+ }
+
+ @Override
+ protected double getEmptyValue() {
+ return 1;
+ }
+
+ @Override
+ protected double getExpectedValue(double[] values) {
+ // The product is not high precision
+ return Arrays.stream(values)
+ .reduce(1, (a, b) -> a * b);
+ }
+
+ @Override
+ protected double getExpectedNonFiniteValue(double[] values) {
+ return getExpectedValue(values);
+ }
+
+ @Override
+ protected DoubleTolerance getTolerance() {
+ return DoubleTolerances.ulps(15);
+ }
+
+ @Override
+ protected Stream<StatisticTestData> streamTestData() {
+ final Stream.Builder<StatisticTestData> builder = Stream.builder();
+ // Python Numpy v1.25.1: numpy.product
+ builder.accept(addReference(24.0, DoubleTolerances.ulps(1), 1, 2, 3, 4));
+ builder.accept(addReference(3081078000.0, DoubleTolerances.ulps(1), 5, 9, 13, 14, 10, 12, 11, 15, 19));
+ return builder.build();
+ }
+}