View Javadoc
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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.distribution.continuous;
24  
25  import org.hipparchus.exception.LocalizedCoreFormats;
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.hipparchus.util.MathUtils;
28  
29  /**
30   * Implementation of the uniform real distribution.
31   *
32   * @see <a href="http://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">
33   * Uniform distribution (continuous), at Wikipedia</a>
34   */
35  public class UniformRealDistribution extends AbstractRealDistribution {
36      /** Serializable version identifier. */
37      private static final long serialVersionUID = 20120109L;
38      /** Lower bound of this distribution (inclusive). */
39      private final double lower;
40      /** Upper bound of this distribution (exclusive). */
41      private final double upper;
42  
43      /**
44       * Create a standard uniform real distribution with lower bound (inclusive)
45       * equal to zero and upper bound (exclusive) equal to one.
46       */
47      public UniformRealDistribution() {
48          this(0, 1);
49      }
50  
51      /**
52       * Create a uniform real distribution using the given lower and upper
53       * bounds.
54       *
55       * @param lower Lower bound of this distribution (inclusive).
56       * @param upper Upper bound of this distribution (exclusive).
57       * @throws MathIllegalArgumentException if {@code lower >= upper}.
58       */
59      public UniformRealDistribution(double lower, double upper)
60          throws MathIllegalArgumentException {
61          super();
62          if (lower >= upper) {
63              throw new MathIllegalArgumentException(
64                              LocalizedCoreFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
65                              lower, upper, false);
66          }
67  
68          this.lower = lower;
69          this.upper = upper;
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public double density(double x) {
75          if (x < lower || x > upper) {
76              return 0.0;
77          }
78          return 1 / (upper - lower);
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public double cumulativeProbability(double x)  {
84          if (x <= lower) {
85              return 0;
86          }
87          if (x >= upper) {
88              return 1;
89          }
90          return (x - lower) / (upper - lower);
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public double inverseCumulativeProbability(final double p)
96          throws MathIllegalArgumentException {
97          MathUtils.checkRangeInclusive(p, 0, 1);
98          return p * (upper - lower) + lower;
99      }
100 
101     /**
102      * {@inheritDoc}
103      *
104      * For lower bound {@code lower} and upper bound {@code upper}, the mean is
105      * {@code 0.5 * (lower + upper)}.
106      */
107     @Override
108     public double getNumericalMean() {
109         return 0.5 * (lower + upper);
110     }
111 
112     /**
113      * {@inheritDoc}
114      *
115      * For lower bound {@code lower} and upper bound {@code upper}, the
116      * variance is {@code (upper - lower)^2 / 12}.
117      */
118     @Override
119     public double getNumericalVariance() {
120         double ul = upper - lower;
121         return ul * ul / 12;
122     }
123 
124     /**
125      * {@inheritDoc}
126      *
127      * The lower bound of the support is equal to the lower bound parameter
128      * of the distribution.
129      *
130      * @return lower bound of the support
131      */
132     @Override
133     public double getSupportLowerBound() {
134         return lower;
135     }
136 
137     /**
138      * {@inheritDoc}
139      *
140      * The upper bound of the support is equal to the upper bound parameter
141      * of the distribution.
142      *
143      * @return upper bound of the support
144      */
145     @Override
146     public double getSupportUpperBound() {
147         return upper;
148     }
149 
150     /**
151      * {@inheritDoc}
152      *
153      * The support of this distribution is connected.
154      *
155      * @return {@code true}
156      */
157     @Override
158     public boolean isSupportConnected() {
159         return true;
160     }
161 
162 }