UniformContinuousDistribution.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.statistics.distribution;

  18. import org.apache.commons.rng.UniformRandomProvider;
  19. import org.apache.commons.rng.sampling.distribution.ContinuousUniformSampler;

  20. /**
  21.  * Implementation of the uniform distribution.
  22.  *
  23.  * <p>The probability density function of \( X \) is:
  24.  *
  25.  * <p>\[ f(x; a, b) = \frac{1}{b-a} \]
  26.  *
  27.  * <p>for \( -\infty \lt a \lt b \lt \infty \) and
  28.  * \( x \in [a, b] \).
  29.  *
  30.  * @see <a href="https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">
  31.  * Uniform distribution (Wikipedia)</a>
  32.  * @see <a href="https://mathworld.wolfram.com/UniformDistribution.html">
  33.  * Uniform distribution (MathWorld)</a>
  34.  */
  35. public final class UniformContinuousDistribution extends AbstractContinuousDistribution {
  36.     /** Lower bound of this distribution (inclusive). */
  37.     private final double lower;
  38.     /** Upper bound of this distribution (exclusive). */
  39.     private final double upper;
  40.     /** Range between the upper and lower bound of this distribution (cached for computations). */
  41.     private final double upperMinusLower;
  42.     /** Cache of the density. */
  43.     private final double pdf;
  44.     /** Cache of the log density. */
  45.     private final double logPdf;

  46.     /**
  47.      * @param lower Lower bound of this distribution (inclusive).
  48.      * @param upper Upper bound of this distribution (inclusive).
  49.      */
  50.     private UniformContinuousDistribution(double lower,
  51.                                           double upper) {
  52.         this.lower = lower;
  53.         this.upper = upper;
  54.         upperMinusLower = upper - lower;
  55.         pdf = 1.0 / upperMinusLower;
  56.         logPdf = -Math.log(upperMinusLower);
  57.     }

  58.     /**
  59.      * Creates a uniform continuous distribution.
  60.      *
  61.      * @param lower Lower bound of this distribution (inclusive).
  62.      * @param upper Upper bound of this distribution (inclusive).
  63.      * @return the distribution
  64.      * @throws IllegalArgumentException if {@code lower >= upper} or the range between the bounds
  65.      * is not finite
  66.      */
  67.     public static UniformContinuousDistribution of(double lower,
  68.                                                    double upper) {
  69.         if (lower >= upper) {
  70.             throw new DistributionException(DistributionException.INVALID_RANGE_LOW_GTE_HIGH,
  71.                                             lower, upper);
  72.         }
  73.         if (!Double.isFinite(upper - lower)) {
  74.             throw new DistributionException("Range %s is not finite", upper - lower);
  75.         }
  76.         return new UniformContinuousDistribution(lower, upper);
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public double density(double x) {
  81.         if (x < lower ||
  82.             x > upper) {
  83.             return 0;
  84.         }
  85.         return pdf;
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public double probability(double x0,
  90.                               double x1) {
  91.         if (x0 > x1) {
  92.             throw new DistributionException(DistributionException.INVALID_RANGE_LOW_GT_HIGH, x0, x1);
  93.         }
  94.         if (x0 >= upper || x1 <= lower) {
  95.             // (x0, x1] does not overlap [lower, upper]
  96.             return 0;
  97.         }

  98.         // x0 < upper
  99.         // x1 >= lower

  100.         // Find the range between x0 and x1 that is within [lower, upper].
  101.         final double l = Math.max(lower, x0);
  102.         final double u = Math.min(upper, x1);

  103.         return (u - l) / upperMinusLower;
  104.     }

  105.     /** {@inheritDoc} */
  106.     @Override
  107.     public double logDensity(double x) {
  108.         if (x < lower ||
  109.             x > upper) {
  110.             return Double.NEGATIVE_INFINITY;
  111.         }
  112.         return logPdf;
  113.     }

  114.     /** {@inheritDoc} */
  115.     @Override
  116.     public double cumulativeProbability(double x)  {
  117.         if (x <= lower) {
  118.             return 0;
  119.         }
  120.         if (x >= upper) {
  121.             return 1;
  122.         }
  123.         return (x - lower) / upperMinusLower;
  124.     }

  125.     /** {@inheritDoc} */
  126.     @Override
  127.     public double survivalProbability(double x) {
  128.         if (x <= lower) {
  129.             return 1;
  130.         }
  131.         if (x >= upper) {
  132.             return 0;
  133.         }
  134.         return (upper - x) / upperMinusLower;
  135.     }

  136.     /** {@inheritDoc} */
  137.     @Override
  138.     public double inverseCumulativeProbability(double p) {
  139.         ArgumentUtils.checkProbability(p);
  140.         // Avoid floating-point error for lower + p * (upper - lower) when p == 1.
  141.         return p == 1 ? upper : p * upperMinusLower + lower;
  142.     }

  143.     /** {@inheritDoc} */
  144.     @Override
  145.     public double inverseSurvivalProbability(double p) {
  146.         ArgumentUtils.checkProbability(p);
  147.         // Avoid floating-point error for upper - p * (upper - lower) when p == 1.
  148.         return p == 1 ? lower : upper - p * upperMinusLower;
  149.     }

  150.     /**
  151.      * {@inheritDoc}
  152.      *
  153.      * <p>For lower bound \( a \) and upper bound \( b \), the mean is \( \frac{1}{2} (a + b) \).
  154.      */
  155.     @Override
  156.     public double getMean() {
  157.         // Avoid overflow
  158.         return 0.5 * lower + 0.5 * upper;
  159.     }

  160.     /**
  161.      * {@inheritDoc}
  162.      *
  163.      * <p>For lower bound \( a \) and upper bound \( b \), the variance is \( \frac{1}{12} (b - a)^2 \).
  164.      */
  165.     @Override
  166.     public double getVariance() {
  167.         return upperMinusLower * upperMinusLower / 12;
  168.     }

  169.     /**
  170.      * {@inheritDoc}
  171.      *
  172.      * <p>The lower bound of the support is equal to the lower bound parameter
  173.      * of the distribution.
  174.      */
  175.     @Override
  176.     public double getSupportLowerBound() {
  177.         return lower;
  178.     }

  179.     /**
  180.      * {@inheritDoc}
  181.      *
  182.      * <p>The upper bound of the support is equal to the upper bound parameter
  183.      * of the distribution.
  184.      */
  185.     @Override
  186.     public double getSupportUpperBound() {
  187.         return upper;
  188.     }

  189.     /** {@inheritDoc} */
  190.     @Override
  191.     public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
  192.         // Uniform distribution sampler.
  193.         return ContinuousUniformSampler.of(rng, lower, upper)::sample;
  194.     }
  195. }