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 | package agents.anac.y2019.harddealer.math3.distribution;
|
---|
18 |
|
---|
19 | import agents.anac.y2019.harddealer.math3.exception.OutOfRangeException;
|
---|
20 | import agents.anac.y2019.harddealer.math3.random.RandomGenerator;
|
---|
21 | import agents.anac.y2019.harddealer.math3.random.Well19937c;
|
---|
22 | import agents.anac.y2019.harddealer.math3.special.Erf;
|
---|
23 | import agents.anac.y2019.harddealer.math3.util.FastMath;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * This class implements the <a href="http://en.wikipedia.org/wiki/L%C3%A9vy_distribution">
|
---|
27 | * Lévy distribution</a>.
|
---|
28 | *
|
---|
29 | * @since 3.2
|
---|
30 | */
|
---|
31 | public class LevyDistribution extends AbstractRealDistribution {
|
---|
32 |
|
---|
33 | /** Serializable UID. */
|
---|
34 | private static final long serialVersionUID = 20130314L;
|
---|
35 |
|
---|
36 | /** Location parameter. */
|
---|
37 | private final double mu;
|
---|
38 |
|
---|
39 | /** Scale parameter. */
|
---|
40 | private final double c; // Setting this to 1 returns a cumProb of 1.0
|
---|
41 |
|
---|
42 | /** Half of c (for calculations). */
|
---|
43 | private final double halfC;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Build a new instance.
|
---|
47 | * <p>
|
---|
48 | * <b>Note:</b> this constructor will implicitly create an instance of
|
---|
49 | * {@link Well19937c} as random generator to be used for sampling only (see
|
---|
50 | * {@link #sample()} and {@link #sample(int)}). In case no sampling is
|
---|
51 | * needed for the created distribution, it is advised to pass {@code null}
|
---|
52 | * as random generator via the appropriate constructors to avoid the
|
---|
53 | * additional initialisation overhead.
|
---|
54 | *
|
---|
55 | * @param mu location parameter
|
---|
56 | * @param c scale parameter
|
---|
57 | * @since 3.4
|
---|
58 | */
|
---|
59 | public LevyDistribution(final double mu, final double c) {
|
---|
60 | this(new Well19937c(), mu, c);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Creates a LevyDistribution.
|
---|
65 | * @param rng random generator to be used for sampling
|
---|
66 | * @param mu location
|
---|
67 | * @param c scale parameter
|
---|
68 | */
|
---|
69 | public LevyDistribution(final RandomGenerator rng, final double mu, final double c) {
|
---|
70 | super(rng);
|
---|
71 | this.mu = mu;
|
---|
72 | this.c = c;
|
---|
73 | this.halfC = 0.5 * c;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** {@inheritDoc}
|
---|
77 | * <p>
|
---|
78 | * From Wikipedia: The probability density function of the Lévy distribution
|
---|
79 | * over the domain is
|
---|
80 | * </p>
|
---|
81 | * <pre>
|
---|
82 | * f(x; μ, c) = √(c / 2π) * e<sup>-c / 2 (x - μ)</sup> / (x - μ)<sup>3/2</sup>
|
---|
83 | * </pre>
|
---|
84 | * <p>
|
---|
85 | * For this distribution, {@code X}, this method returns {@code P(X < x)}.
|
---|
86 | * If {@code x} is less than location parameter μ, {@code Double.NaN} is
|
---|
87 | * returned, as in these cases the distribution is not defined.
|
---|
88 | * </p>
|
---|
89 | */
|
---|
90 | public double density(final double x) {
|
---|
91 | if (x < mu) {
|
---|
92 | return Double.NaN;
|
---|
93 | }
|
---|
94 |
|
---|
95 | final double delta = x - mu;
|
---|
96 | final double f = halfC / delta;
|
---|
97 | return FastMath.sqrt(f / FastMath.PI) * FastMath.exp(-f) /delta;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** {@inheritDoc}
|
---|
101 | *
|
---|
102 | * See documentation of {@link #density(double)} for computation details.
|
---|
103 | */
|
---|
104 | @Override
|
---|
105 | public double logDensity(double x) {
|
---|
106 | if (x < mu) {
|
---|
107 | return Double.NaN;
|
---|
108 | }
|
---|
109 |
|
---|
110 | final double delta = x - mu;
|
---|
111 | final double f = halfC / delta;
|
---|
112 | return 0.5 * FastMath.log(f / FastMath.PI) - f - FastMath.log(delta);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /** {@inheritDoc}
|
---|
116 | * <p>
|
---|
117 | * From Wikipedia: the cumulative distribution function is
|
---|
118 | * </p>
|
---|
119 | * <pre>
|
---|
120 | * f(x; u, c) = erfc (√ (c / 2 (x - u )))
|
---|
121 | * </pre>
|
---|
122 | */
|
---|
123 | public double cumulativeProbability(final double x) {
|
---|
124 | if (x < mu) {
|
---|
125 | return Double.NaN;
|
---|
126 | }
|
---|
127 | return Erf.erfc(FastMath.sqrt(halfC / (x - mu)));
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** {@inheritDoc} */
|
---|
131 | @Override
|
---|
132 | public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
|
---|
133 | if (p < 0.0 || p > 1.0) {
|
---|
134 | throw new OutOfRangeException(p, 0, 1);
|
---|
135 | }
|
---|
136 | final double t = Erf.erfcInv(p);
|
---|
137 | return mu + halfC / (t * t);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** Get the scale parameter of the distribution.
|
---|
141 | * @return scale parameter of the distribution
|
---|
142 | */
|
---|
143 | public double getScale() {
|
---|
144 | return c;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /** Get the location parameter of the distribution.
|
---|
148 | * @return location parameter of the distribution
|
---|
149 | */
|
---|
150 | public double getLocation() {
|
---|
151 | return mu;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** {@inheritDoc} */
|
---|
155 | public double getNumericalMean() {
|
---|
156 | return Double.POSITIVE_INFINITY;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /** {@inheritDoc} */
|
---|
160 | public double getNumericalVariance() {
|
---|
161 | return Double.POSITIVE_INFINITY;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** {@inheritDoc} */
|
---|
165 | public double getSupportLowerBound() {
|
---|
166 | return mu;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /** {@inheritDoc} */
|
---|
170 | public double getSupportUpperBound() {
|
---|
171 | return Double.POSITIVE_INFINITY;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** {@inheritDoc} */
|
---|
175 | public boolean isSupportLowerBoundInclusive() {
|
---|
176 | // there is a division by x-mu in the computation, so density
|
---|
177 | // is not finite at lower bound, bound must be excluded
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** {@inheritDoc} */
|
---|
182 | public boolean isSupportUpperBoundInclusive() {
|
---|
183 | // upper bound is infinite, so it must be excluded
|
---|
184 | return false;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** {@inheritDoc} */
|
---|
188 | public boolean isSupportConnected() {
|
---|
189 | return true;
|
---|
190 | }
|
---|
191 |
|
---|
192 | }
|
---|