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.discrete;
24
25 import org.hipparchus.exception.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27
28 /**
29 * Implementation of the uniform integer distribution.
30 *
31 * @see <a href="http://en.wikipedia.org/wiki/Uniform_distribution_(discrete)">
32 * Uniform distribution (discrete), at Wikipedia</a>
33 */
34 public class UniformIntegerDistribution extends AbstractIntegerDistribution {
35 /** Serializable version identifier. */
36 private static final long serialVersionUID = 20120109L;
37 /** Lower bound (inclusive) of this distribution. */
38 private final int lower;
39 /** Upper bound (inclusive) of this distribution. */
40 private final int upper;
41
42 /**
43 * Creates a new uniform integer distribution using the given lower and
44 * upper bounds (both inclusive).
45 *
46 * @param lower Lower bound (inclusive) of this distribution.
47 * @param upper Upper bound (inclusive) of this distribution.
48 * @throws MathIllegalArgumentException if {@code lower >= upper}.
49 */
50 public UniformIntegerDistribution(int lower, int upper)
51 throws MathIllegalArgumentException {
52 if (lower > upper) {
53 throw new MathIllegalArgumentException(
54 LocalizedCoreFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
55 lower, upper, true);
56 }
57 this.lower = lower;
58 this.upper = upper;
59 }
60
61 /** {@inheritDoc} */
62 @Override
63 public double probability(int x) {
64 if (x < lower || x > upper) {
65 return 0;
66 }
67 return 1.0 / (upper - lower + 1);
68 }
69
70 /** {@inheritDoc} */
71 @Override
72 public double cumulativeProbability(int x) {
73 if (x < lower) {
74 return 0;
75 }
76 if (x > upper) {
77 return 1;
78 }
79 return (x - lower + 1.0) / (upper - lower + 1.0);
80 }
81
82 /**
83 * {@inheritDoc}
84 *
85 * For lower bound {@code lower} and upper bound {@code upper}, the mean is
86 * {@code 0.5 * (lower + upper)}.
87 */
88 @Override
89 public double getNumericalMean() {
90 return 0.5 * (lower + upper);
91 }
92
93 /**
94 * {@inheritDoc}
95 *
96 * For lower bound {@code lower} and upper bound {@code upper}, and
97 * {@code n = upper - lower + 1}, the variance is {@code (n^2 - 1) / 12}.
98 */
99 @Override
100 public double getNumericalVariance() {
101 double n = upper - lower + 1;
102 return (n * n - 1) / 12.0;
103 }
104
105 /**
106 * {@inheritDoc}
107 *
108 * The lower bound of the support is equal to the lower bound parameter
109 * of the distribution.
110 *
111 * @return lower bound of the support
112 */
113 @Override
114 public int getSupportLowerBound() {
115 return lower;
116 }
117
118 /**
119 * {@inheritDoc}
120 *
121 * The upper bound of the support is equal to the upper bound parameter
122 * of the distribution.
123 *
124 * @return upper bound of the support
125 */
126 @Override
127 public int getSupportUpperBound() {
128 return upper;
129 }
130
131 /**
132 * {@inheritDoc}
133 *
134 * The support of this distribution is connected.
135 *
136 * @return {@code true}
137 */
138 @Override
139 public boolean isSupportConnected() {
140 return true;
141 }
142 }