UniformIntegerDistribution.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.discrete;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;

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

  37.     /**
  38.      * Creates a new uniform integer distribution using the given lower and
  39.      * upper bounds (both inclusive).
  40.      *
  41.      * @param lower Lower bound (inclusive) of this distribution.
  42.      * @param upper Upper bound (inclusive) of this distribution.
  43.      * @throws MathIllegalArgumentException if {@code lower >= upper}.
  44.      */
  45.     public UniformIntegerDistribution(int lower, int upper)
  46.         throws MathIllegalArgumentException {
  47.         if (lower > upper) {
  48.             throw new MathIllegalArgumentException(
  49.                             LocalizedCoreFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
  50.                             lower, upper, true);
  51.         }
  52.         this.lower = lower;
  53.         this.upper = upper;
  54.     }

  55.     /** {@inheritDoc} */
  56.     @Override
  57.     public double probability(int x) {
  58.         if (x < lower || x > upper) {
  59.             return 0;
  60.         }
  61.         return 1.0 / (upper - lower + 1);
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     public double cumulativeProbability(int x) {
  66.         if (x < lower) {
  67.             return 0;
  68.         }
  69.         if (x > upper) {
  70.             return 1;
  71.         }
  72.         return (x - lower + 1.0) / (upper - lower + 1.0);
  73.     }

  74.     /**
  75.      * {@inheritDoc}
  76.      *
  77.      * For lower bound {@code lower} and upper bound {@code upper}, the mean is
  78.      * {@code 0.5 * (lower + upper)}.
  79.      */
  80.     @Override
  81.     public double getNumericalMean() {
  82.         return 0.5 * (lower + upper);
  83.     }

  84.     /**
  85.      * {@inheritDoc}
  86.      *
  87.      * For lower bound {@code lower} and upper bound {@code upper}, and
  88.      * {@code n = upper - lower + 1}, the variance is {@code (n^2 - 1) / 12}.
  89.      */
  90.     @Override
  91.     public double getNumericalVariance() {
  92.         double n = upper - lower + 1;
  93.         return (n * n - 1) / 12.0;
  94.     }

  95.     /**
  96.      * {@inheritDoc}
  97.      *
  98.      * The lower bound of the support is equal to the lower bound parameter
  99.      * of the distribution.
  100.      *
  101.      * @return lower bound of the support
  102.      */
  103.     @Override
  104.     public int getSupportLowerBound() {
  105.         return lower;
  106.     }

  107.     /**
  108.      * {@inheritDoc}
  109.      *
  110.      * The upper bound of the support is equal to the upper bound parameter
  111.      * of the distribution.
  112.      *
  113.      * @return upper bound of the support
  114.      */
  115.     @Override
  116.     public int getSupportUpperBound() {
  117.         return upper;
  118.     }

  119.     /**
  120.      * {@inheritDoc}
  121.      *
  122.      * The support of this distribution is connected.
  123.      *
  124.      * @return {@code true}
  125.      */
  126.     @Override
  127.     public boolean isSupportConnected() {
  128.         return true;
  129.     }
  130. }