You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2021/08/02 00:39:02 UTC

[commons-math] 03/03: Implement "ConstantContinuousDistribution" as a local workaround.

This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit 3df6d879e701b442fabf709c8143e6ca8f8f9547
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Mon Aug 2 02:31:37 2021 +0200

    Implement "ConstantContinuousDistribution" as a local workaround.
    
    Corresponding code to be removed from "Commons Statistics" (no other known use case).
---
 .../legacy/distribution/EmpiricalDistribution.java | 83 +++++++++++++++++++++-
 1 file changed, 82 insertions(+), 1 deletion(-)

diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistribution.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistribution.java
index 9e473e8..0c72c0a 100644
--- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistribution.java
+++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistribution.java
@@ -23,8 +23,8 @@ import java.util.function.Function;
 
 import org.apache.commons.statistics.distribution.NormalDistribution;
 import org.apache.commons.statistics.distribution.ContinuousDistribution;
-import org.apache.commons.statistics.distribution.ConstantContinuousDistribution;
 import org.apache.commons.numbers.core.Precision;
+import org.apache.commons.rng.UniformRandomProvider;
 import org.apache.commons.math4.legacy.exception.OutOfRangeException;
 import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
 import org.apache.commons.math4.legacy.stat.descriptive.StatisticalSummary;
@@ -563,4 +563,85 @@ public final class EmpiricalDistribution extends AbstractRealDistribution
             }
         };
     }
+
+    /**
+     * Constant distribution.
+     */
+    private static class ConstantContinuousDistribution implements ContinuousDistribution {
+        /** Constant value of the distribution. */
+        private final double value;
+
+        /**
+         * Create a constant real distribution with the given value.
+         *
+         * @param value Value of this distribution.
+         */
+        ConstantContinuousDistribution(double value) {
+            this.value = value;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public double density(double x) {
+            return x == value ? 1 : 0;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public double cumulativeProbability(double x)  {
+            return x < value ? 0 : 1;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public double inverseCumulativeProbability(final double p) {
+            if (p < 0 ||
+                p > 1) {
+                // Should never happen.
+                throw new IllegalArgumentException("Internal error");
+            }
+            return value;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public double getMean() {
+            return value;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public double getVariance() {
+            return 0;
+        }
+
+        /**{@inheritDoc} */
+        @Override
+        public double getSupportLowerBound() {
+            return value;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public double getSupportUpperBound() {
+            return value;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public boolean isSupportConnected() {
+            return true;
+        }
+
+        /**
+         * {@inheritDoc}
+         *
+         * @param rng Not used: distribution contains a single value.
+         * @return the value of the distribution.
+         */
+        @Override
+        public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
+            return this::getSupportLowerBound;
+        }
+    }
 }