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 |
|
---|
18 | package agents.anac.y2019.harddealer.math3.distribution;
|
---|
19 |
|
---|
20 | import agents.anac.y2019.harddealer.math3.exception.NumberIsTooLargeException;
|
---|
21 | import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
|
---|
22 | import agents.anac.y2019.harddealer.math3.random.RandomGenerator;
|
---|
23 | import agents.anac.y2019.harddealer.math3.random.Well19937c;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Implementation of the uniform integer distribution.
|
---|
27 | *
|
---|
28 | * @see <a href="http://en.wikipedia.org/wiki/Uniform_distribution_(discrete)"
|
---|
29 | * >Uniform distribution (discrete), at Wikipedia</a>
|
---|
30 | *
|
---|
31 | * @since 3.0
|
---|
32 | */
|
---|
33 | public class UniformIntegerDistribution extends AbstractIntegerDistribution {
|
---|
34 | /** Serializable version identifier. */
|
---|
35 | private static final long serialVersionUID = 20120109L;
|
---|
36 | /** Lower bound (inclusive) of this distribution. */
|
---|
37 | private final int lower;
|
---|
38 | /** Upper bound (inclusive) of this distribution. */
|
---|
39 | private final int upper;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Creates a new uniform integer distribution using the given lower and
|
---|
43 | * upper bounds (both inclusive).
|
---|
44 | * <p>
|
---|
45 | * <b>Note:</b> this constructor will implicitly create an instance of
|
---|
46 | * {@link Well19937c} as random generator to be used for sampling only (see
|
---|
47 | * {@link #sample()} and {@link #sample(int)}). In case no sampling is
|
---|
48 | * needed for the created distribution, it is advised to pass {@code null}
|
---|
49 | * as random generator via the appropriate constructors to avoid the
|
---|
50 | * additional initialisation overhead.
|
---|
51 | *
|
---|
52 | * @param lower Lower bound (inclusive) of this distribution.
|
---|
53 | * @param upper Upper bound (inclusive) of this distribution.
|
---|
54 | * @throws NumberIsTooLargeException if {@code lower >= upper}.
|
---|
55 | */
|
---|
56 | public UniformIntegerDistribution(int lower, int upper)
|
---|
57 | throws NumberIsTooLargeException {
|
---|
58 | this(new Well19937c(), lower, upper);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Creates a new uniform integer distribution using the given lower and
|
---|
63 | * upper bounds (both inclusive).
|
---|
64 | *
|
---|
65 | * @param rng Random number generator.
|
---|
66 | * @param lower Lower bound (inclusive) of this distribution.
|
---|
67 | * @param upper Upper bound (inclusive) of this distribution.
|
---|
68 | * @throws NumberIsTooLargeException if {@code lower > upper}.
|
---|
69 | * @since 3.1
|
---|
70 | */
|
---|
71 | public UniformIntegerDistribution(RandomGenerator rng,
|
---|
72 | int lower,
|
---|
73 | int upper)
|
---|
74 | throws NumberIsTooLargeException {
|
---|
75 | super(rng);
|
---|
76 |
|
---|
77 | if (lower > upper) {
|
---|
78 | throw new NumberIsTooLargeException(
|
---|
79 | LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
|
---|
80 | lower, upper, true);
|
---|
81 | }
|
---|
82 | this.lower = lower;
|
---|
83 | this.upper = upper;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** {@inheritDoc} */
|
---|
87 | public double probability(int x) {
|
---|
88 | if (x < lower || x > upper) {
|
---|
89 | return 0;
|
---|
90 | }
|
---|
91 | return 1.0 / (upper - lower + 1);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** {@inheritDoc} */
|
---|
95 | public double cumulativeProbability(int x) {
|
---|
96 | if (x < lower) {
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 | if (x > upper) {
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 | return (x - lower + 1.0) / (upper - lower + 1.0);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * {@inheritDoc}
|
---|
107 | *
|
---|
108 | * For lower bound {@code lower} and upper bound {@code upper}, the mean is
|
---|
109 | * {@code 0.5 * (lower + upper)}.
|
---|
110 | */
|
---|
111 | public double getNumericalMean() {
|
---|
112 | return 0.5 * (lower + upper);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * {@inheritDoc}
|
---|
117 | *
|
---|
118 | * For lower bound {@code lower} and upper bound {@code upper}, and
|
---|
119 | * {@code n = upper - lower + 1}, the variance is {@code (n^2 - 1) / 12}.
|
---|
120 | */
|
---|
121 | public double getNumericalVariance() {
|
---|
122 | double n = upper - lower + 1;
|
---|
123 | return (n * n - 1) / 12.0;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * {@inheritDoc}
|
---|
128 | *
|
---|
129 | * The lower bound of the support is equal to the lower bound parameter
|
---|
130 | * of the distribution.
|
---|
131 | *
|
---|
132 | * @return lower bound of the support
|
---|
133 | */
|
---|
134 | public int getSupportLowerBound() {
|
---|
135 | return lower;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * {@inheritDoc}
|
---|
140 | *
|
---|
141 | * The upper bound of the support is equal to the upper bound parameter
|
---|
142 | * of the distribution.
|
---|
143 | *
|
---|
144 | * @return upper bound of the support
|
---|
145 | */
|
---|
146 | public int 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 | public boolean isSupportConnected() {
|
---|
158 | return true;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** {@inheritDoc} */
|
---|
162 | @Override
|
---|
163 | public int sample() {
|
---|
164 | final int max = (upper - lower) + 1;
|
---|
165 | if (max <= 0) {
|
---|
166 | // The range is too wide to fit in a positive int (larger
|
---|
167 | // than 2^31); as it covers more than half the integer range,
|
---|
168 | // we use a simple rejection method.
|
---|
169 | while (true) {
|
---|
170 | final int r = random.nextInt();
|
---|
171 | if (r >= lower &&
|
---|
172 | r <= upper) {
|
---|
173 | return r;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | } else {
|
---|
177 | // We can shift the range and directly generate a positive int.
|
---|
178 | return lower + random.nextInt(max);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|