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