source: src/main/java/agents/anac/y2019/harddealer/math3/distribution/ParetoDistribution.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 10.1 KB
Line 
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
18package agents.anac.y2019.harddealer.math3.distribution;
19
20import agents.anac.y2019.harddealer.math3.exception.NotStrictlyPositiveException;
21import agents.anac.y2019.harddealer.math3.exception.NumberIsTooLargeException;
22import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
23import agents.anac.y2019.harddealer.math3.random.RandomGenerator;
24import agents.anac.y2019.harddealer.math3.random.Well19937c;
25import agents.anac.y2019.harddealer.math3.util.FastMath;
26
27/**
28 * Implementation of the Pareto distribution.
29 *
30 * <p>
31 * <strong>Parameters:</strong>
32 * The probability distribution function of {@code X} is given by (for {@code x >= k}):
33 * <pre>
34 * α * k^α / x^(α + 1)
35 * </pre>
36 * <p>
37 * <ul>
38 * <li>{@code k} is the <em>scale</em> parameter: this is the minimum possible value of {@code X},</li>
39 * <li>{@code α} is the <em>shape</em> parameter: this is the Pareto index</li>
40 * </ul>
41 *
42 * @see <a href="http://en.wikipedia.org/wiki/Pareto_distribution">
43 * Pareto distribution (Wikipedia)</a>
44 * @see <a href="http://mathworld.wolfram.com/ParetoDistribution.html">
45 * Pareto distribution (MathWorld)</a>
46 *
47 * @since 3.3
48 */
49public class ParetoDistribution extends AbstractRealDistribution {
50
51 /** Default inverse cumulative probability accuracy. */
52 public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
53
54 /** Serializable version identifier. */
55 private static final long serialVersionUID = 20130424;
56
57 /** The scale parameter of this distribution. */
58 private final double scale;
59
60 /** The shape parameter of this distribution. */
61 private final double shape;
62
63 /** Inverse cumulative probability accuracy. */
64 private final double solverAbsoluteAccuracy;
65
66 /**
67 * Create a Pareto distribution with a scale of {@code 1} and a shape of {@code 1}.
68 */
69 public ParetoDistribution() {
70 this(1, 1);
71 }
72
73 /**
74 * Create a Pareto distribution using the specified scale and shape.
75 * <p>
76 * <b>Note:</b> this constructor will implicitly create an instance of
77 * {@link Well19937c} as random generator to be used for sampling only (see
78 * {@link #sample()} and {@link #sample(int)}). In case no sampling is
79 * needed for the created distribution, it is advised to pass {@code null}
80 * as random generator via the appropriate constructors to avoid the
81 * additional initialisation overhead.
82 *
83 * @param scale the scale parameter of this distribution
84 * @param shape the shape parameter of this distribution
85 * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}.
86 */
87 public ParetoDistribution(double scale, double shape)
88 throws NotStrictlyPositiveException {
89 this(scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
90 }
91
92 /**
93 * Create a Pareto distribution using the specified scale, shape and
94 * inverse cumulative distribution accuracy.
95 * <p>
96 * <b>Note:</b> this constructor will implicitly create an instance of
97 * {@link Well19937c} as random generator to be used for sampling only (see
98 * {@link #sample()} and {@link #sample(int)}). In case no sampling is
99 * needed for the created distribution, it is advised to pass {@code null}
100 * as random generator via the appropriate constructors to avoid the
101 * additional initialisation overhead.
102 *
103 * @param scale the scale parameter of this distribution
104 * @param shape the shape parameter of this distribution
105 * @param inverseCumAccuracy Inverse cumulative probability accuracy.
106 * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}.
107 */
108 public ParetoDistribution(double scale, double shape, double inverseCumAccuracy)
109 throws NotStrictlyPositiveException {
110 this(new Well19937c(), scale, shape, inverseCumAccuracy);
111 }
112
113 /**
114 * Creates a Pareto distribution.
115 *
116 * @param rng Random number generator.
117 * @param scale Scale parameter of this distribution.
118 * @param shape Shape parameter of this distribution.
119 * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}.
120 */
121 public ParetoDistribution(RandomGenerator rng, double scale, double shape)
122 throws NotStrictlyPositiveException {
123 this(rng, scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
124 }
125
126 /**
127 * Creates a Pareto distribution.
128 *
129 * @param rng Random number generator.
130 * @param scale Scale parameter of this distribution.
131 * @param shape Shape parameter of this distribution.
132 * @param inverseCumAccuracy Inverse cumulative probability accuracy.
133 * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}.
134 */
135 public ParetoDistribution(RandomGenerator rng,
136 double scale,
137 double shape,
138 double inverseCumAccuracy)
139 throws NotStrictlyPositiveException {
140 super(rng);
141
142 if (scale <= 0) {
143 throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, scale);
144 }
145
146 if (shape <= 0) {
147 throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, shape);
148 }
149
150 this.scale = scale;
151 this.shape = shape;
152 this.solverAbsoluteAccuracy = inverseCumAccuracy;
153 }
154
155 /**
156 * Returns the scale parameter of this distribution.
157 *
158 * @return the scale parameter
159 */
160 public double getScale() {
161 return scale;
162 }
163
164 /**
165 * Returns the shape parameter of this distribution.
166 *
167 * @return the shape parameter
168 */
169 public double getShape() {
170 return shape;
171 }
172
173 /**
174 * {@inheritDoc}
175 * <p>
176 * For scale {@code k}, and shape {@code α} of this distribution, the PDF
177 * is given by
178 * <ul>
179 * <li>{@code 0} if {@code x < k},</li>
180 * <li>{@code α * k^α / x^(α + 1)} otherwise.</li>
181 * </ul>
182 */
183 public double density(double x) {
184 if (x < scale) {
185 return 0;
186 }
187 return FastMath.pow(scale, shape) / FastMath.pow(x, shape + 1) * shape;
188 }
189
190 /** {@inheritDoc}
191 *
192 * See documentation of {@link #density(double)} for computation details.
193 */
194 @Override
195 public double logDensity(double x) {
196 if (x < scale) {
197 return Double.NEGATIVE_INFINITY;
198 }
199 return FastMath.log(scale) * shape - FastMath.log(x) * (shape + 1) + FastMath.log(shape);
200 }
201
202 /**
203 * {@inheritDoc}
204 * <p>
205 * For scale {@code k}, and shape {@code α} of this distribution, the CDF is given by
206 * <ul>
207 * <li>{@code 0} if {@code x < k},</li>
208 * <li>{@code 1 - (k / x)^α} otherwise.</li>
209 * </ul>
210 */
211 public double cumulativeProbability(double x) {
212 if (x <= scale) {
213 return 0;
214 }
215 return 1 - FastMath.pow(scale / x, shape);
216 }
217
218 /**
219 * {@inheritDoc}
220 *
221 * @deprecated See {@link RealDistribution#cumulativeProbability(double,double)}
222 */
223 @Override
224 @Deprecated
225 public double cumulativeProbability(double x0, double x1)
226 throws NumberIsTooLargeException {
227 return probability(x0, x1);
228 }
229
230 /** {@inheritDoc} */
231 @Override
232 protected double getSolverAbsoluteAccuracy() {
233 return solverAbsoluteAccuracy;
234 }
235
236 /**
237 * {@inheritDoc}
238 * <p>
239 * For scale {@code k} and shape {@code α}, the mean is given by
240 * <ul>
241 * <li>{@code ∞} if {@code α <= 1},</li>
242 * <li>{@code α * k / (α - 1)} otherwise.</li>
243 * </ul>
244 */
245 public double getNumericalMean() {
246 if (shape <= 1) {
247 return Double.POSITIVE_INFINITY;
248 }
249 return shape * scale / (shape - 1);
250 }
251
252 /**
253 * {@inheritDoc}
254 * <p>
255 * For scale {@code k} and shape {@code α}, the variance is given by
256 * <ul>
257 * <li>{@code ∞} if {@code 1 < α <= 2},</li>
258 * <li>{@code k^2 * α / ((α - 1)^2 * (α - 2))} otherwise.</li>
259 * </ul>
260 */
261 public double getNumericalVariance() {
262 if (shape <= 2) {
263 return Double.POSITIVE_INFINITY;
264 }
265 double s = shape - 1;
266 return scale * scale * shape / (s * s) / (shape - 2);
267 }
268
269 /**
270 * {@inheritDoc}
271 * <p>
272 * The lower bound of the support is equal to the scale parameter {@code k}.
273 *
274 * @return lower bound of the support
275 */
276 public double getSupportLowerBound() {
277 return scale;
278 }
279
280 /**
281 * {@inheritDoc}
282 * <p>
283 * The upper bound of the support is always positive infinity no matter the parameters.
284 *
285 * @return upper bound of the support (always {@code Double.POSITIVE_INFINITY})
286 */
287 public double getSupportUpperBound() {
288 return Double.POSITIVE_INFINITY;
289 }
290
291 /** {@inheritDoc} */
292 public boolean isSupportLowerBoundInclusive() {
293 return true;
294 }
295
296 /** {@inheritDoc} */
297 public boolean isSupportUpperBoundInclusive() {
298 return false;
299 }
300
301 /**
302 * {@inheritDoc}
303 * <p>
304 * The support of this distribution is connected.
305 *
306 * @return {@code true}
307 */
308 public boolean isSupportConnected() {
309 return true;
310 }
311
312 /** {@inheritDoc} */
313 @Override
314 public double sample() {
315 final double n = random.nextDouble();
316 return scale / FastMath.pow(n, 1 / shape);
317 }
318}
Note: See TracBrowser for help on using the repository browser.