source: src/main/java/agents/anac/y2019/harddealer/math3/distribution/GumbelDistribution.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: 5.2 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 */
17package agents.anac.y2019.harddealer.math3.distribution;
18
19import agents.anac.y2019.harddealer.math3.exception.NotStrictlyPositiveException;
20import agents.anac.y2019.harddealer.math3.exception.OutOfRangeException;
21import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
22import agents.anac.y2019.harddealer.math3.random.RandomGenerator;
23import agents.anac.y2019.harddealer.math3.random.Well19937c;
24import agents.anac.y2019.harddealer.math3.util.FastMath;
25import agents.anac.y2019.harddealer.math3.util.MathUtils;
26
27/**
28 * This class implements the Gumbel distribution.
29 *
30 * @see <a href="http://en.wikipedia.org/wiki/Gumbel_distribution">Gumbel Distribution (Wikipedia)</a>
31 * @see <a href="http://mathworld.wolfram.com/GumbelDistribution.html">Gumbel Distribution (Mathworld)</a>
32 *
33 * @since 3.4
34 */
35public class GumbelDistribution extends AbstractRealDistribution {
36
37 /** Serializable version identifier. */
38 private static final long serialVersionUID = 20141003;
39
40 /**
41 * Approximation of Euler's constant
42 * see http://mathworld.wolfram.com/Euler-MascheroniConstantApproximations.html
43 */
44 private static final double EULER = FastMath.PI / (2 * FastMath.E);
45
46 /** The location parameter. */
47 private final double mu;
48 /** The scale parameter. */
49 private final double beta;
50
51 /**
52 * Build a new instance.
53 * <p>
54 * <b>Note:</b> this constructor will implicitly create an instance of
55 * {@link Well19937c} as random generator to be used for sampling only (see
56 * {@link #sample()} and {@link #sample(int)}). In case no sampling is
57 * needed for the created distribution, it is advised to pass {@code null}
58 * as random generator via the appropriate constructors to avoid the
59 * additional initialisation overhead.
60 *
61 * @param mu location parameter
62 * @param beta scale parameter (must be positive)
63 * @throws NotStrictlyPositiveException if {@code beta <= 0}
64 */
65 public GumbelDistribution(double mu, double beta) {
66 this(new Well19937c(), mu, beta);
67 }
68
69 /**
70 * Build a new instance.
71 *
72 * @param rng Random number generator
73 * @param mu location parameter
74 * @param beta scale parameter (must be positive)
75 * @throws NotStrictlyPositiveException if {@code beta <= 0}
76 */
77 public GumbelDistribution(RandomGenerator rng, double mu, double beta) {
78 super(rng);
79
80 if (beta <= 0) {
81 throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, beta);
82 }
83
84 this.beta = beta;
85 this.mu = mu;
86 }
87
88 /**
89 * Access the location parameter, {@code mu}.
90 *
91 * @return the location parameter.
92 */
93 public double getLocation() {
94 return mu;
95 }
96
97 /**
98 * Access the scale parameter, {@code beta}.
99 *
100 * @return the scale parameter.
101 */
102 public double getScale() {
103 return beta;
104 }
105
106 /** {@inheritDoc} */
107 public double density(double x) {
108 final double z = (x - mu) / beta;
109 final double t = FastMath.exp(-z);
110 return FastMath.exp(-z - t) / beta;
111 }
112
113 /** {@inheritDoc} */
114 public double cumulativeProbability(double x) {
115 final double z = (x - mu) / beta;
116 return FastMath.exp(-FastMath.exp(-z));
117 }
118
119 /** {@inheritDoc} */
120 @Override
121 public double inverseCumulativeProbability(double p) throws OutOfRangeException {
122 if (p < 0.0 || p > 1.0) {
123 throw new OutOfRangeException(p, 0.0, 1.0);
124 } else if (p == 0) {
125 return Double.NEGATIVE_INFINITY;
126 } else if (p == 1) {
127 return Double.POSITIVE_INFINITY;
128 }
129 return mu - FastMath.log(-FastMath.log(p)) * beta;
130 }
131
132 /** {@inheritDoc} */
133 public double getNumericalMean() {
134 return mu + EULER * beta;
135 }
136
137 /** {@inheritDoc} */
138 public double getNumericalVariance() {
139 return (MathUtils.PI_SQUARED) / 6.0 * (beta * beta);
140 }
141
142 /** {@inheritDoc} */
143 public double getSupportLowerBound() {
144 return Double.NEGATIVE_INFINITY;
145 }
146
147 /** {@inheritDoc} */
148 public double getSupportUpperBound() {
149 return Double.POSITIVE_INFINITY;
150 }
151
152 /** {@inheritDoc} */
153 public boolean isSupportLowerBoundInclusive() {
154 return false;
155 }
156
157 /** {@inheritDoc} */
158 public boolean isSupportUpperBoundInclusive() {
159 return false;
160 }
161
162 /** {@inheritDoc} */
163 public boolean isSupportConnected() {
164 return true;
165 }
166
167}
Note: See TracBrowser for help on using the repository browser.