001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.statistics.distribution;
018
019/**
020 * Implementation of the Laplace distribution.
021 *
022 * <p>The probability density function of \( X \) is:
023 *
024 * <p>\[ f(x; \mu, b) = \frac{1}{2b} \exp \left( -\frac{|x-\mu|}{b} \right) \]
025 *
026 * <p>for \( \mu \) the location,
027 * \( b &gt; 0 \) the scale, and
028 * \( x \in (-\infty, \infty) \).
029 *
030 * @see <a href="https://en.wikipedia.org/wiki/Laplace_distribution">Laplace distribution (Wikipedia)</a>
031 * @see <a href="https://mathworld.wolfram.com/LaplaceDistribution.html">Laplace distribution (MathWorld)</a>
032 */
033public final class LaplaceDistribution extends AbstractContinuousDistribution {
034    /** The location parameter. */
035    private final double mu;
036    /** The scale parameter. */
037    private final double beta;
038    /** log(2 * beta). */
039    private final double log2beta;
040
041    /**
042     * @param mu Location parameter.
043     * @param beta Scale parameter (must be positive).
044     */
045    private LaplaceDistribution(double mu,
046                                double beta) {
047        this.mu = mu;
048        this.beta = beta;
049        log2beta = Math.log(2.0 * beta);
050    }
051
052    /**
053     * Creates a Laplace distribution.
054     *
055     * @param mu Location parameter.
056     * @param beta Scale parameter (must be positive).
057     * @return the distribution
058     * @throws IllegalArgumentException if {@code beta <= 0}
059     */
060    public static LaplaceDistribution of(double mu,
061                                         double beta) {
062        if (beta <= 0) {
063            throw new DistributionException(DistributionException.NOT_STRICTLY_POSITIVE, beta);
064        }
065        return new LaplaceDistribution(mu, beta);
066    }
067
068    /**
069     * Gets the location parameter of this distribution.
070     *
071     * @return the location parameter.
072     */
073    public double getLocation() {
074        return mu;
075    }
076
077    /**
078     * Gets the scale parameter of this distribution.
079     *
080     * @return the scale parameter.
081     */
082    public double getScale() {
083        return beta;
084    }
085
086    /** {@inheritDoc} */
087    @Override
088    public double density(double x) {
089        return Math.exp(-Math.abs(x - mu) / beta) / (2.0 * beta);
090    }
091
092    /** {@inheritDoc} */
093    @Override
094    public double logDensity(double x) {
095        return -Math.abs(x - mu) / beta - log2beta;
096    }
097
098    /** {@inheritDoc} */
099    @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}