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.OutOfRangeException;
|
---|
22 | import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
|
---|
23 | import agents.anac.y2019.harddealer.math3.random.RandomGenerator;
|
---|
24 | import agents.anac.y2019.harddealer.math3.random.Well19937c;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Implementation of the uniform real distribution.
|
---|
28 | *
|
---|
29 | * @see <a href="http://en.wikipedia.org/wiki/Uniform_distribution_(continuous)"
|
---|
30 | * >Uniform distribution (continuous), at Wikipedia</a>
|
---|
31 | *
|
---|
32 | * @since 3.0
|
---|
33 | */
|
---|
34 | public class UniformRealDistribution extends AbstractRealDistribution {
|
---|
35 | /** Default inverse cumulative probability accuracy.
|
---|
36 | * @deprecated as of 3.2 not used anymore, will be removed in 4.0
|
---|
37 | */
|
---|
38 | @Deprecated
|
---|
39 | public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
|
---|
40 | /** Serializable version identifier. */
|
---|
41 | private static final long serialVersionUID = 20120109L;
|
---|
42 | /** Lower bound of this distribution (inclusive). */
|
---|
43 | private final double lower;
|
---|
44 | /** Upper bound of this distribution (exclusive). */
|
---|
45 | private final double upper;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Create a standard uniform real distribution with lower bound (inclusive)
|
---|
49 | * equal to zero and upper bound (exclusive) equal to one.
|
---|
50 | * <p>
|
---|
51 | * <b>Note:</b> this constructor will implicitly create an instance of
|
---|
52 | * {@link Well19937c} as random generator to be used for sampling only (see
|
---|
53 | * {@link #sample()} and {@link #sample(int)}). In case no sampling is
|
---|
54 | * needed for the created distribution, it is advised to pass {@code null}
|
---|
55 | * as random generator via the appropriate constructors to avoid the
|
---|
56 | * additional initialisation overhead.
|
---|
57 | */
|
---|
58 | public UniformRealDistribution() {
|
---|
59 | this(0, 1);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Create a uniform real distribution using the given lower and upper
|
---|
64 | * bounds.
|
---|
65 | * <p>
|
---|
66 | * <b>Note:</b> this constructor will implicitly create an instance of
|
---|
67 | * {@link Well19937c} as random generator to be used for sampling only (see
|
---|
68 | * {@link #sample()} and {@link #sample(int)}). In case no sampling is
|
---|
69 | * needed for the created distribution, it is advised to pass {@code null}
|
---|
70 | * as random generator via the appropriate constructors to avoid the
|
---|
71 | * additional initialisation overhead.
|
---|
72 | *
|
---|
73 | * @param lower Lower bound of this distribution (inclusive).
|
---|
74 | * @param upper Upper bound of this distribution (exclusive).
|
---|
75 | * @throws NumberIsTooLargeException if {@code lower >= upper}.
|
---|
76 | */
|
---|
77 | public UniformRealDistribution(double lower, double upper)
|
---|
78 | throws NumberIsTooLargeException {
|
---|
79 | this(new Well19937c(), lower, upper);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Create a uniform distribution.
|
---|
84 | *
|
---|
85 | * @param lower Lower bound of this distribution (inclusive).
|
---|
86 | * @param upper Upper bound of this distribution (exclusive).
|
---|
87 | * @param inverseCumAccuracy Inverse cumulative probability accuracy.
|
---|
88 | * @throws NumberIsTooLargeException if {@code lower >= upper}.
|
---|
89 | * @deprecated as of 3.2, inverse CDF is now calculated analytically, use
|
---|
90 | * {@link #UniformRealDistribution(double, double)} instead.
|
---|
91 | */
|
---|
92 | @Deprecated
|
---|
93 | public UniformRealDistribution(double lower, double upper, double inverseCumAccuracy)
|
---|
94 | throws NumberIsTooLargeException {
|
---|
95 | this(new Well19937c(), lower, upper);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Creates a uniform distribution.
|
---|
100 | *
|
---|
101 | * @param rng Random number generator.
|
---|
102 | * @param lower Lower bound of this distribution (inclusive).
|
---|
103 | * @param upper Upper bound of this distribution (exclusive).
|
---|
104 | * @param inverseCumAccuracy Inverse cumulative probability accuracy.
|
---|
105 | * @throws NumberIsTooLargeException if {@code lower >= upper}.
|
---|
106 | * @since 3.1
|
---|
107 | * @deprecated as of 3.2, inverse CDF is now calculated analytically, use
|
---|
108 | * {@link #UniformRealDistribution(RandomGenerator, double, double)}
|
---|
109 | * instead.
|
---|
110 | */
|
---|
111 | @Deprecated
|
---|
112 | public UniformRealDistribution(RandomGenerator rng,
|
---|
113 | double lower,
|
---|
114 | double upper,
|
---|
115 | double inverseCumAccuracy){
|
---|
116 | this(rng, lower, upper);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Creates a uniform distribution.
|
---|
121 | *
|
---|
122 | * @param rng Random number generator.
|
---|
123 | * @param lower Lower bound of this distribution (inclusive).
|
---|
124 | * @param upper Upper bound of this distribution (exclusive).
|
---|
125 | * @throws NumberIsTooLargeException if {@code lower >= upper}.
|
---|
126 | * @since 3.1
|
---|
127 | */
|
---|
128 | public UniformRealDistribution(RandomGenerator rng,
|
---|
129 | double lower,
|
---|
130 | double upper)
|
---|
131 | throws NumberIsTooLargeException {
|
---|
132 | super(rng);
|
---|
133 | if (lower >= upper) {
|
---|
134 | throw new NumberIsTooLargeException(
|
---|
135 | LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
|
---|
136 | lower, upper, false);
|
---|
137 | }
|
---|
138 |
|
---|
139 | this.lower = lower;
|
---|
140 | this.upper = upper;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /** {@inheritDoc} */
|
---|
144 | public double density(double x) {
|
---|
145 | if (x < lower || x > upper) {
|
---|
146 | return 0.0;
|
---|
147 | }
|
---|
148 | return 1 / (upper - lower);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** {@inheritDoc} */
|
---|
152 | public double cumulativeProbability(double x) {
|
---|
153 | if (x <= lower) {
|
---|
154 | return 0;
|
---|
155 | }
|
---|
156 | if (x >= upper) {
|
---|
157 | return 1;
|
---|
158 | }
|
---|
159 | return (x - lower) / (upper - lower);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** {@inheritDoc} */
|
---|
163 | @Override
|
---|
164 | public double inverseCumulativeProbability(final double p)
|
---|
165 | throws OutOfRangeException {
|
---|
166 | if (p < 0.0 || p > 1.0) {
|
---|
167 | throw new OutOfRangeException(p, 0, 1);
|
---|
168 | }
|
---|
169 | return p * (upper - lower) + lower;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * {@inheritDoc}
|
---|
174 | *
|
---|
175 | * For lower bound {@code lower} and upper bound {@code upper}, the mean is
|
---|
176 | * {@code 0.5 * (lower + upper)}.
|
---|
177 | */
|
---|
178 | public double getNumericalMean() {
|
---|
179 | return 0.5 * (lower + upper);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * {@inheritDoc}
|
---|
184 | *
|
---|
185 | * For lower bound {@code lower} and upper bound {@code upper}, the
|
---|
186 | * variance is {@code (upper - lower)^2 / 12}.
|
---|
187 | */
|
---|
188 | public double getNumericalVariance() {
|
---|
189 | double ul = upper - lower;
|
---|
190 | return ul * ul / 12;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * {@inheritDoc}
|
---|
195 | *
|
---|
196 | * The lower bound of the support is equal to the lower bound parameter
|
---|
197 | * of the distribution.
|
---|
198 | *
|
---|
199 | * @return lower bound of the support
|
---|
200 | */
|
---|
201 | public double getSupportLowerBound() {
|
---|
202 | return lower;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * {@inheritDoc}
|
---|
207 | *
|
---|
208 | * The upper bound of the support is equal to the upper bound parameter
|
---|
209 | * of the distribution.
|
---|
210 | *
|
---|
211 | * @return upper bound of the support
|
---|
212 | */
|
---|
213 | public double getSupportUpperBound() {
|
---|
214 | return upper;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** {@inheritDoc} */
|
---|
218 | public boolean isSupportLowerBoundInclusive() {
|
---|
219 | return true;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /** {@inheritDoc} */
|
---|
223 | public boolean isSupportUpperBoundInclusive() {
|
---|
224 | return true;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * {@inheritDoc}
|
---|
229 | *
|
---|
230 | * The support of this distribution is connected.
|
---|
231 | *
|
---|
232 | * @return {@code true}
|
---|
233 | */
|
---|
234 | public boolean isSupportConnected() {
|
---|
235 | return true;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /** {@inheritDoc} */
|
---|
239 | @Override
|
---|
240 | public double sample() {
|
---|
241 | final double u = random.nextDouble();
|
---|
242 | return u * upper + (1 - u) * lower;
|
---|
243 | }
|
---|
244 | }
|
---|