LaplaceDistribution.java

  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.math3.distribution;

  18. import org.apache.commons.math3.exception.NotStrictlyPositiveException;
  19. import org.apache.commons.math3.exception.OutOfRangeException;
  20. import org.apache.commons.math3.exception.util.LocalizedFormats;
  21. import org.apache.commons.math3.random.RandomGenerator;
  22. import org.apache.commons.math3.random.Well19937c;
  23. import org.apache.commons.math3.util.FastMath;

  24. /**
  25.  * This class implements the Laplace distribution.
  26.  *
  27.  * @see <a href="http://en.wikipedia.org/wiki/Laplace_distribution">Laplace distribution (Wikipedia)</a>
  28.  *
  29.  * @since 3.4
  30.  */
  31. public class LaplaceDistribution extends AbstractRealDistribution {

  32.     /** Serializable version identifier. */
  33.     private static final long serialVersionUID = 20141003;

  34.     /** The location parameter. */
  35.     private final double mu;
  36.     /** The scale parameter. */
  37.     private final double beta;

  38.     /**
  39.      * Build a new instance.
  40.      * <p>
  41.      * <b>Note:</b> this constructor will implicitly create an instance of
  42.      * {@link Well19937c} as random generator to be used for sampling only (see
  43.      * {@link #sample()} and {@link #sample(int)}). In case no sampling is
  44.      * needed for the created distribution, it is advised to pass {@code null}
  45.      * as random generator via the appropriate constructors to avoid the
  46.      * additional initialisation overhead.
  47.      *
  48.      * @param mu location parameter
  49.      * @param beta scale parameter (must be positive)
  50.      * @throws NotStrictlyPositiveException if {@code beta <= 0}
  51.      */
  52.     public LaplaceDistribution(double mu, double beta) {
  53.         this(new Well19937c(), mu, beta);
  54.     }

  55.     /**
  56.      * Build a new instance.
  57.      *
  58.      * @param rng Random number generator
  59.      * @param mu location parameter
  60.      * @param beta scale parameter (must be positive)
  61.      * @throws NotStrictlyPositiveException if {@code beta <= 0}
  62.      */
  63.     public LaplaceDistribution(RandomGenerator rng, double mu, double beta) {
  64.         super(rng);

  65.         if (beta <= 0.0) {
  66.             throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_SCALE, beta);
  67.         }

  68.         this.mu = mu;
  69.         this.beta = beta;
  70.     }

  71.     /**
  72.      * Access the location parameter, {@code mu}.
  73.      *
  74.      * @return the location parameter.
  75.      */
  76.     public double getLocation() {
  77.         return mu;
  78.     }

  79.     /**
  80.      * Access the scale parameter, {@code beta}.
  81.      *
  82.      * @return the scale parameter.
  83.      */
  84.     public double getScale() {
  85.         return beta;
  86.     }

  87.     /** {@inheritDoc} */
  88.     public double density(double x) {
  89.         return FastMath.exp(-FastMath.abs(x - mu) / beta) / (2.0 * beta);
  90.     }

  91.     /** {@inheritDoc} */
  92.     public double cumulativeProbability(double x) {
  93.         if (x <= mu) {
  94.             return FastMath.exp((x - mu) / beta) / 2.0;
  95.         } else {
  96.             return 1.0 - FastMath.exp((mu - x) / beta) / 2.0;
  97.         }
  98.     }

  99.     @Override
  100.     public double inverseCumulativeProbability(double p) throws OutOfRangeException {
  101.         if (p < 0.0 || p > 1.0) {
  102.             throw new OutOfRangeException(p, 0.0, 1.0);
  103.         } else if (p == 0) {
  104.             return Double.NEGATIVE_INFINITY;
  105.         } else if (p == 1) {
  106.             return Double.POSITIVE_INFINITY;
  107.         }
  108.         double x = (p > 0.5) ? -Math.log(2.0 - 2.0 * p) : Math.log(2.0 * p);
  109.         return mu + beta * x;
  110.     }

  111.     /** {@inheritDoc} */
  112.     public double getNumericalMean() {
  113.         return mu;
  114.     }

  115.     /** {@inheritDoc} */
  116.     public double getNumericalVariance() {
  117.         return 2.0 * beta * beta;
  118.     }

  119.     /** {@inheritDoc} */
  120.     public double getSupportLowerBound() {
  121.         return Double.NEGATIVE_INFINITY;
  122.     }

  123.     /** {@inheritDoc} */
  124.     public double getSupportUpperBound() {
  125.         return Double.POSITIVE_INFINITY;
  126.     }

  127.     /** {@inheritDoc} */
  128.     public boolean isSupportLowerBoundInclusive() {
  129.         return false;
  130.     }

  131.     /** {@inheritDoc} */
  132.     public boolean isSupportUpperBoundInclusive() {
  133.         return false;
  134.     }

  135.     /** {@inheritDoc} */
  136.     public boolean isSupportConnected() {
  137.         return true;
  138.     }

  139. }