UniformRealDistribution.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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.distribution.continuous;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.util.MathUtils;

  25. /**
  26.  * Implementation of the uniform real distribution.
  27.  *
  28.  * @see <a href="http://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">
  29.  * Uniform distribution (continuous), at Wikipedia</a>
  30.  */
  31. public class UniformRealDistribution extends AbstractRealDistribution {
  32.     /** Serializable version identifier. */
  33.     private static final long serialVersionUID = 20120109L;
  34.     /** Lower bound of this distribution (inclusive). */
  35.     private final double lower;
  36.     /** Upper bound of this distribution (exclusive). */
  37.     private final double upper;

  38.     /**
  39.      * Create a standard uniform real distribution with lower bound (inclusive)
  40.      * equal to zero and upper bound (exclusive) equal to one.
  41.      */
  42.     public UniformRealDistribution() {
  43.         this(0, 1);
  44.     }

  45.     /**
  46.      * Create a uniform real distribution using the given lower and upper
  47.      * bounds.
  48.      *
  49.      * @param lower Lower bound of this distribution (inclusive).
  50.      * @param upper Upper bound of this distribution (exclusive).
  51.      * @throws MathIllegalArgumentException if {@code lower >= upper}.
  52.      */
  53.     public UniformRealDistribution(double lower, double upper)
  54.         throws MathIllegalArgumentException {
  55.         super();
  56.         if (lower >= upper) {
  57.             throw new MathIllegalArgumentException(
  58.                             LocalizedCoreFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
  59.                             lower, upper, false);
  60.         }

  61.         this.lower = lower;
  62.         this.upper = upper;
  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     public double density(double x) {
  67.         if (x < lower || x > upper) {
  68.             return 0.0;
  69.         }
  70.         return 1 / (upper - lower);
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     public double cumulativeProbability(double x)  {
  75.         if (x <= lower) {
  76.             return 0;
  77.         }
  78.         if (x >= upper) {
  79.             return 1;
  80.         }
  81.         return (x - lower) / (upper - lower);
  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     public double inverseCumulativeProbability(final double p)
  86.         throws MathIllegalArgumentException {
  87.         MathUtils.checkRangeInclusive(p, 0, 1);
  88.         return p * (upper - lower) + lower;
  89.     }

  90.     /**
  91.      * {@inheritDoc}
  92.      *
  93.      * For lower bound {@code lower} and upper bound {@code upper}, the mean is
  94.      * {@code 0.5 * (lower + upper)}.
  95.      */
  96.     @Override
  97.     public double getNumericalMean() {
  98.         return 0.5 * (lower + upper);
  99.     }

  100.     /**
  101.      * {@inheritDoc}
  102.      *
  103.      * For lower bound {@code lower} and upper bound {@code upper}, the
  104.      * variance is {@code (upper - lower)^2 / 12}.
  105.      */
  106.     @Override
  107.     public double getNumericalVariance() {
  108.         double ul = upper - lower;
  109.         return ul * ul / 12;
  110.     }

  111.     /**
  112.      * {@inheritDoc}
  113.      *
  114.      * The lower bound of the support is equal to the lower bound parameter
  115.      * of the distribution.
  116.      *
  117.      * @return lower bound of the support
  118.      */
  119.     @Override
  120.     public double getSupportLowerBound() {
  121.         return lower;
  122.     }

  123.     /**
  124.      * {@inheritDoc}
  125.      *
  126.      * The upper bound of the support is equal to the upper bound parameter
  127.      * of the distribution.
  128.      *
  129.      * @return upper bound of the support
  130.      */
  131.     @Override
  132.     public double getSupportUpperBound() {
  133.         return upper;
  134.     }

  135.     /**
  136.      * {@inheritDoc}
  137.      *
  138.      * The support of this distribution is connected.
  139.      *
  140.      * @return {@code true}
  141.      */
  142.     @Override
  143.     public boolean isSupportConnected() {
  144.         return true;
  145.     }

  146. }