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 org.apache.commons.statistics.distribution;
18
19 /**
20 * Implementation of the Laplace distribution.
21 *
22 * <p>The probability density function of \( X \) is:
23 *
24 * <p>\[ f(x; \mu, b) = \frac{1}{2b} \exp \left( -\frac{|x-\mu|}{b} \right) \]
25 *
26 * <p>for \( \mu \) the location,
27 * \( b > 0 \) the scale, and
28 * \( x \in (-\infty, \infty) \).
29 *
30 * @see <a href="https://en.wikipedia.org/wiki/Laplace_distribution">Laplace distribution (Wikipedia)</a>
31 * @see <a href="https://mathworld.wolfram.com/LaplaceDistribution.html">Laplace distribution (MathWorld)</a>
32 */
33 public final class LaplaceDistribution extends AbstractContinuousDistribution {
34 /** The location parameter. */
35 private final double mu;
36 /** The scale parameter. */
37 private final double beta;
38 /** log(2 * beta). */
39 private final double log2beta;
40
41 /**
42 * @param mu Location parameter.
43 * @param beta Scale parameter (must be positive).
44 */
45 private LaplaceDistribution(double mu,
46 double beta) {
47 this.mu = mu;
48 this.beta = beta;
49 log2beta = Math.log(2.0 * beta);
50 }
51
52 /**
53 * Creates a Laplace distribution.
54 *
55 * @param mu Location parameter.
56 * @param beta Scale parameter (must be positive).
57 * @return the distribution
58 * @throws IllegalArgumentException if {@code beta <= 0}
59 */
60 public static LaplaceDistribution of(double mu,
61 double beta) {
62 if (beta <= 0) {
63 throw new DistributionException(DistributionException.NOT_STRICTLY_POSITIVE, beta);
64 }
65 return new LaplaceDistribution(mu, beta);
66 }
67
68 /**
69 * Gets the location parameter of this distribution.
70 *
71 * @return the location parameter.
72 */
73 public double getLocation() {
74 return mu;
75 }
76
77 /**
78 * Gets the scale parameter of this distribution.
79 *
80 * @return the scale parameter.
81 */
82 public double getScale() {
83 return beta;
84 }
85
86 /** {@inheritDoc} */
87 @Override
88 public double density(double x) {
89 return Math.exp(-Math.abs(x - mu) / beta) / (2.0 * beta);
90 }
91
92 /** {@inheritDoc} */
93 @Override
94 public double logDensity(double x) {
95 return -Math.abs(x - mu) / beta - log2beta;
96 }
97
98 /** {@inheritDoc} */
99 @Override
100 public double cumulativeProbability(double x) {
101 if (x <= mu) {
102 return 0.5 * Math.exp((x - mu) / beta);
103 }
104 return 1.0 - 0.5 * Math.exp((mu - x) / beta);
105 }
106
107 /** {@inheritDoc} */
108 @Override
109 public double survivalProbability(double x) {
110 if (x <= mu) {
111 return 1.0 - 0.5 * Math.exp((x - mu) / beta);
112 }
113 return 0.5 * Math.exp((mu - x) / beta);
114 }
115
116 /** {@inheritDoc} */
117 @Override
118 public double inverseCumulativeProbability(double p) {
119 ArgumentUtils.checkProbability(p);
120 if (p == 0) {
121 return Double.NEGATIVE_INFINITY;
122 } else if (p == 1) {
123 return Double.POSITIVE_INFINITY;
124 }
125 final double x = (p > 0.5) ? -Math.log(2.0 * (1.0 - p)) : Math.log(2.0 * p);
126 return mu + beta * x;
127 }
128
129 /** {@inheritDoc} */
130 @Override
131 public double inverseSurvivalProbability(double p) {
132 ArgumentUtils.checkProbability(p);
133 if (p == 1) {
134 return Double.NEGATIVE_INFINITY;
135 } else if (p == 0) {
136 return Double.POSITIVE_INFINITY;
137 }
138 // By symmetry: x = -icdf(p); then transform back by the scale and location
139 final double x = (p > 0.5) ? Math.log(2.0 * (1.0 - p)) : -Math.log(2.0 * p);
140 return mu + beta * x;
141 }
142
143 /**
144 * {@inheritDoc}
145 *
146 * <p>The mean is equal to the {@linkplain #getLocation() location}.
147 */
148 @Override
149 public double getMean() {
150 return getLocation();
151 }
152
153 /**
154 * {@inheritDoc}
155 *
156 * <p>For scale parameter \( b \), the variance is \( 2 b^2 \).
157 */
158 @Override
159 public double getVariance() {
160 return 2.0 * beta * beta;
161 }
162
163 /**
164 * {@inheritDoc}
165 *
166 * <p>The lower bound of the support is always negative infinity.
167 *
168 * @return {@linkplain Double#NEGATIVE_INFINITY negative infinity}.
169 */
170 @Override
171 public double getSupportLowerBound() {
172 return Double.NEGATIVE_INFINITY;
173 }
174
175 /**
176 * {@inheritDoc}
177 *
178 * <p>The upper bound of the support is always positive infinity.
179 *
180 * @return {@linkplain Double#POSITIVE_INFINITY positive infinity}.
181 */
182 @Override
183 public double getSupportUpperBound() {
184 return Double.POSITIVE_INFINITY;
185 }
186
187 /** {@inheritDoc} */
188 @Override
189 double getMedian() {
190 // Overridden for the probability(double, double) method.
191 // This is intentionally not a public method.
192 return mu;
193 }
194 }